This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathfoundation-list.cpp
More file actions
285 lines (220 loc) · 6.49 KB
/
foundation-list.cpp
File metadata and controls
285 lines (220 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
bool MCListCreateMutable(char_t p_delimiter, MCListRef& r_list)
{
__MCList *self;
if (!__MCValueCreate(kMCValueTypeCodeList, self))
return false;
MCStringCreateWithNativeChars((const char_t *)&p_delimiter, 1, self -> delimiter);
r_list = self;
return true;
}
bool MCListCreateMutable(MCStringRef p_delimiter, MCListRef& r_list)
{
__MCList *self;
if (!__MCValueCreate(kMCValueTypeCodeList, self))
return false;
self -> delimiter = MCValueRetain(p_delimiter);
r_list = self;
return true;
}
MC_DLLEXPORT_DEF
bool MCListAppend(MCListRef self, MCValueRef p_value)
{
__MCAssertIsList(self);
MCAssert(nil != p_value);
bool t_first = self->buffer == nil;
if (t_first)
if (!MCStringCreateMutable(0, self -> buffer))
return false;
MCStringRef t_string = nil;
switch (MCValueGetTypeCode(p_value))
{
case kMCValueTypeCodeName:
t_string = MCNameGetString((MCNameRef)p_value);
break;
case kMCValueTypeCodeString:
t_string = (MCStringRef)p_value;
break;
case kMCValueTypeCodeBoolean:
t_string = kMCTrue == (MCBooleanRef)p_value ? kMCTrueString : kMCFalseString;
break;
case kMCValueTypeCodeList:
t_string = ((MCListRef)p_value)->buffer;
if (t_string == nil)
t_string = kMCEmptyString;
break;
default:
if (!MCStringFormat(t_string, "%@", p_value))
{
return false;
}
break;
}
if (!t_first && !MCStringAppend(self -> buffer, self -> delimiter))
return false;
return MCStringAppend(self -> buffer, t_string);
}
MC_DLLEXPORT_DEF
bool MCListCopy(MCListRef self, MCListRef& r_list)
{
__MCAssertIsList(self);
// If we are immutable, just bump the reference count
if (!(self -> flags & kMCListFlagIsMutable))
{
r_list = MCValueRetain(self);
return true;
}
// Create a new list
MCListRef t_new_list;
if (!MCListCreateMutable(self -> delimiter, t_new_list))
return false;
// Mark the new list as immutable
t_new_list -> flags &= ~kMCListFlagIsMutable;
// Create an immutable copy of the list contents
if (self -> buffer == nil)
{
t_new_list -> buffer = nil;
}
else if (!MCStringCopy(self -> buffer, t_new_list -> buffer))
{
MCValueRelease(t_new_list);
return false;
}
r_list = t_new_list;
return true;
}
MC_DLLEXPORT_DEF
bool MCListCopyAndRelease(MCListRef self, MCListRef& r_list)
{
__MCAssertIsList(self);
// If there are no other references, just clear the mutable flag
if (self -> references == 1)
{
self -> flags &= ~kMCListFlagIsMutable;
r_list = self;
return true;
}
// Otherwise perform a normal copy operation followed by a release
if (!MCListCopy(self, r_list))
return false;
MCValueRelease(self);
return true;
}
MC_DLLEXPORT_DEF
bool MCListCopyAsString(MCListRef self, MCStringRef& r_string)
{
__MCAssertIsList(self);
MCStringRef t_string;
if (self -> buffer != nil)
t_string = self -> buffer;
else
t_string = kMCEmptyString;
if (!MCStringCopy(t_string, r_string))
return false;
return true;
}
MC_DLLEXPORT_DEF
bool MCListCopyAsStringAndRelease(MCListRef self, MCStringRef& r_string)
{
if (!MCListCopyAsString(self, r_string))
return false;
MCValueRelease(self);
return true;
}
MC_DLLEXPORT_DEF
bool MCListAppendFormat(MCListRef self, const char *p_format, ...)
{
__MCAssertIsList(self);
MCAssert(nil != p_format);
bool t_success;
t_success = true;
MCStringRef t_string;
t_string = nil;
va_list t_args;
va_start(t_args, p_format);
t_success = MCStringFormatV(t_string, p_format, t_args);
va_end(t_args);
if (t_success)
t_success = MCListAppend(self, t_string);
if (t_string != nil)
MCValueRelease(t_string);
return t_success;
}
MC_DLLEXPORT_DEF
bool MCListAppendNativeChars(MCListRef self, const char_t *p_chars, uindex_t p_char_count)
{
__MCAssertIsList(self);
MCAssert(p_chars != nil);
bool t_first = self->buffer == nil;
if (t_first)
if (!MCStringCreateMutable(0, self -> buffer))
return false;
if (!t_first && !MCStringAppend(self -> buffer, self -> delimiter))
return false;
return MCStringAppendNativeChars(self -> buffer, p_chars, p_char_count);
}
MC_DLLEXPORT_DEF
bool MCListAppendSubstring(MCListRef self, MCStringRef p_string, MCRange p_range)
{
return MCListAppendFormat(self, "%*@", &p_range, p_string);
}
MC_DLLEXPORT_DEF
bool MCListIsEmpty(MCListRef self)
{
__MCAssertIsList(self);
return self -> buffer == nil;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF MCListRef kMCEmptyList;
bool __MCListInitialize(void)
{
if (!MCListCreateMutable('\n', kMCEmptyList))
return false;
return true;
}
void __MCListFinalize(void)
{
}
void __MCListDestroy(__MCList *self)
{
MCValueRelease(self -> delimiter);
MCValueRelease(self -> buffer);
}
hash_t __MCListHash(__MCList *self)
{
if (self -> buffer == nil)
return MCStringHash(kMCEmptyString, kMCStringOptionCompareExact);
return MCStringHash(self -> buffer, kMCStringOptionCompareExact);
}
bool __MCListIsEqualTo(__MCList *list, __MCList *other_list)
{
return MCStringIsEqualTo(list -> buffer != nil ? list -> buffer : kMCEmptyString, other_list -> buffer != nil ? other_list -> buffer : kMCEmptyString, kMCStringOptionCompareExact);
}
bool __MCListCopyDescription(__MCList *self, MCStringRef& r_string)
{
MCAutoStringRef t_self_string;
if (!MCListCopyAsString (self, &t_self_string))
return false;
return MCValueCopyDescription(*t_self_string, r_string);
}
bool __MCListImmutableCopy(__MCList *self, bool p_release, __MCList*& r_immutable_value)
{
if (!p_release)
return MCListCopy(self, r_immutable_value);
return MCListCopyAndRelease(self, r_immutable_value);
}
////////////////////////////////////////////////////////////////////////////////