autoc 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +4 -0
- data/README +3 -1
- data/doc/AutoC.html +23 -7
- data/doc/AutoC/Code.html +3 -3
- data/doc/AutoC/Collection.html +289 -76
- data/doc/AutoC/HashMap.html +44 -46
- data/doc/AutoC/HashSet.html +20 -17
- data/doc/AutoC/List.html +56 -92
- data/doc/AutoC/Module.html +2 -2
- data/doc/AutoC/Module/File.html +2 -2
- data/doc/AutoC/Module/Header.html +6 -4
- data/doc/AutoC/Module/Source.html +26 -26
- data/doc/AutoC/Priority.html +2 -2
- data/doc/AutoC/Queue.html +30 -92
- data/doc/AutoC/Reference.html +217 -61
- data/doc/AutoC/String.html +1393 -0
- data/doc/AutoC/Type.html +240 -128
- data/doc/AutoC/UserDefinedType.html +688 -47
- data/doc/AutoC/Vector.html +154 -62
- data/doc/_index.html +9 -2
- data/doc/class_list.html +1 -1
- data/doc/file.CHANGES.html +10 -2
- data/doc/file.README.html +5 -3
- data/doc/index.html +5 -3
- data/doc/method_list.html +235 -97
- data/doc/top-level-namespace.html +2 -2
- data/lib/autoc.rb +3 -1
- data/lib/autoc/code.rb +3 -2
- data/lib/autoc/collection.rb +36 -40
- data/lib/autoc/collection/hash_map.rb +10 -14
- data/lib/autoc/collection/hash_set.rb +12 -11
- data/lib/autoc/collection/list.rb +21 -11
- data/lib/autoc/collection/queue.rb +5 -8
- data/lib/autoc/collection/vector.rb +28 -12
- data/lib/autoc/string.rb +492 -0
- data/lib/autoc/type.rb +155 -66
- data/test/test.rb +157 -35
- data/test/test_char_string.rb +270 -0
- data/test/test_int_list.rb +35 -0
- data/test/test_int_vector.rb +34 -0
- data/test/test_value_hash_map.rb +162 -0
- data/test/test_value_hash_set.rb +173 -0
- data/test/test_value_list.rb +193 -0
- data/test/test_value_queue.rb +275 -0
- data/test/test_value_vector.rb +155 -0
- data/test/value.rb +80 -0
- metadata +15 -8
- data/test/test.c +0 -1041
- data/test/test.h +0 -41
- data/test/test_auto.c +0 -3407
- data/test/test_auto.h +0 -765
data/lib/autoc/string.rb
ADDED
@@ -0,0 +1,492 @@
|
|
1
|
+
require "autoc/code"
|
2
|
+
require "autoc/type"
|
3
|
+
|
4
|
+
|
5
|
+
module AutoC
|
6
|
+
|
7
|
+
|
8
|
+
=begin
|
9
|
+
|
10
|
+
String is wrapper around the standard null-terminated C string which has the capabilities of both a plain string and a string builder optimized for appending and incremental building.
|
11
|
+
|
12
|
+
It is ought to be on par with the raw C strings performance-wise (or even exceed it since the string size is tracked).
|
13
|
+
|
14
|
+
Unlike the plain C string, this String type has value type semantics but it can be turned into the reference type with {AutoC::Reference}.
|
15
|
+
|
16
|
+
The String's default character type, *_CharType_*, is *_char_* although this can be changed.
|
17
|
+
|
18
|
+
String generally obeys the Vector interface with respect to working with its characters and resembles the List interface when using its building capabilities.
|
19
|
+
|
20
|
+
== Generated C interface
|
21
|
+
|
22
|
+
=== String management
|
23
|
+
|
24
|
+
[cols="2*"]
|
25
|
+
|===
|
26
|
+
|*_void_* ~type~Copy(*_Type_* * +dst+, *_Type_* * +src+)
|
27
|
+
|
|
28
|
+
Create a new string +dst+ filled with a _copy_ the contents of +src+.
|
29
|
+
|
30
|
+
NOTE: Previous contents of +dst+ is overwritten.
|
31
|
+
|
32
|
+
|*_void_* ~type~CopyRange(*_Type_* * +dst+, *_Type_* * +src+, *_size_t_* first, *_size_t_* last)
|
33
|
+
|
|
34
|
+
Create a new string +dst+ filled with a part the contents of +src+ lying in the range +[first, last]+, that is including the character at position +first+ and including the character at position +last+.
|
35
|
+
|
36
|
+
NOTE: Previous contents of +dst+ is overwritten.
|
37
|
+
|
38
|
+
WARNING: +first+ *must not* exceed +last+ (that is, +first+ <= +last+) and both indices *must* be valid otherwise behavior is undefined. See ~type~Within().
|
39
|
+
|
40
|
+
|*_void_* ~type~Ctor(*_Type_* * +self+, *_const CharType *_* +chars+)
|
41
|
+
|
|
42
|
+
Create a new string +self+ with a _copy_ of the null-terminated C string +chars+.
|
43
|
+
|
44
|
+
NULL value of +chars+ is permitted; this case corresponds to an empty string "".
|
45
|
+
|
46
|
+
NOTE: Previous contents of +self+ is overwritten.
|
47
|
+
|
48
|
+
|*_void_* ~type~Dtor(*_Type_* * +self+)
|
49
|
+
|
|
50
|
+
Destroy string +self+.
|
51
|
+
|
52
|
+
|*_int_* ~type~Equal(*_Type_* * +lt+, *_Type_* * +rt+)
|
53
|
+
|
|
54
|
+
Return non-zero value if strings +lt+ and +rt+ are considered equal by contents and zero value otherwise.
|
55
|
+
|
56
|
+
|*_size_t_* ~type~Identify(*_Type_* * +self+)
|
57
|
+
|
|
58
|
+
Return hash code for string +self+.
|
59
|
+
|===
|
60
|
+
|
61
|
+
=== Basic operations
|
62
|
+
|
63
|
+
[cols=2*]
|
64
|
+
|===
|
65
|
+
|*_const CharType *_* ~type~Chars(*_Type_* * +self+)
|
66
|
+
|
|
67
|
+
Return a _read-only view_ of the string in a form of the standard C null-terminated string.
|
68
|
+
|
69
|
+
NOTE: the returned value need not to be freed.
|
70
|
+
|
71
|
+
WARNING: the returned value should be considered *volatile* and thus may be altered or invalidated by a subsequent call to any String method!
|
72
|
+
|
73
|
+
|*_int_* ~type~Empty(*_Type_* * +self+)
|
74
|
+
|
|
75
|
+
Return non-zero value if string +self+ has zero length and zero value otherwise.
|
76
|
+
|
77
|
+
|*_CharType_* ~type~Get(*_Type_* * +self+, *_size_t_* +index+)
|
78
|
+
|
|
79
|
+
Return a _copy_ of the character stored in +self+ at position +index+.
|
80
|
+
|
81
|
+
WARNING: +index+ *must* be a valid index otherwise the behavior is undefined. See ~type~Within().
|
82
|
+
|
83
|
+
|*_void_* ~type~Set(*_Type_* * +self+, *_size_t_* +index+, *_CharType_* +value+)
|
84
|
+
|
|
85
|
+
|
86
|
+
Store a _copy_ of the character +value+ in string +self+ at position +index+.
|
87
|
+
|
88
|
+
WARNING: +index+ *must* be a valid index otherwise the behavior is undefined. See ~type~Within().
|
89
|
+
|
90
|
+
|*_size_t_* ~type~Size(*_Type_* * +self+)
|
91
|
+
|
|
92
|
+
Return number of characters stored in string +self+.
|
93
|
+
|
94
|
+
Note that this does not include the null terminator.
|
95
|
+
|
96
|
+
|*_int_* ~type~Within(*_Type_* * +self+, *_size_t_* +index+)
|
97
|
+
|
|
98
|
+
Return non-zero value if +index+ is a valid character index and zero value otherwise.
|
99
|
+
A valid index lies between 0 and ~type~Size()-1 inclusively.
|
100
|
+
|===
|
101
|
+
|
102
|
+
=== String buffer operations
|
103
|
+
|
104
|
+
Functions which provide the string buffer functionality.
|
105
|
+
This allows the incremental building of strings without excessive storage copying/reallocation.
|
106
|
+
|
107
|
+
[cols=2*]
|
108
|
+
|===
|
109
|
+
|*_void_* ~type~PushChars(*_Type_* * +self+, *_const CharType*_* +chars+)
|
110
|
+
|
|
111
|
+
Append a _copy_ of the null-terminated C string +chars+ to string +self+.
|
112
|
+
|
113
|
+
|*_int_* ~type~PushChar(*_Type_* * +self+, *_CharType_* +value+)
|
114
|
+
|
|
115
|
+
Append a _copy_ of the character +value+ to string +self+.
|
116
|
+
|
117
|
+
Return non-zero value on success and zero value on conversion error.
|
118
|
+
|
119
|
+
NOTE: this convenience function applies generic formatting rules and is currently implemented as a macro; for more precise control over the formatting process use the ~type~PushFormat() function.
|
120
|
+
|
121
|
+
|*_int_* ~type~PushInt(*_Type_* * +self+, *_int_* +value+)
|
122
|
+
|
|
123
|
+
Append string representation of the integer +value+ to string +self+.
|
124
|
+
|
125
|
+
Return non-zero value on success and zero value on conversion error.
|
126
|
+
|
127
|
+
NOTE: this convenience function applies generic formatting rules and is currently implemented as a macro; for more precise control over the formatting process use the ~type~PushFormat() function.
|
128
|
+
|
129
|
+
|*_int_* ~type~PushFloat(*_Type_* * +self+, *_double_* +value+)
|
130
|
+
|
|
131
|
+
Append string representation of the floating-point +value+ to string +self+.
|
132
|
+
|
133
|
+
Return non-zero value on success and zero value on conversion error.
|
134
|
+
|
135
|
+
NOTE: this convenience function applies generic formatting rules and is currently implemented as a macro; for more precise control over the formatting process use the ~type~PushFormat() function.
|
136
|
+
|
137
|
+
|*_int_* ~type~PushPtr(*_Type_* * +self+, *_void*_* +value+)
|
138
|
+
|
|
139
|
+
Append string representation of the pointer +value+ to string +self+.
|
140
|
+
|
141
|
+
Return non-zero value on success and zero value on conversion error.
|
142
|
+
|
143
|
+
NOTE: this convenience function applies generic formatting rules and is currently implemented as a macro; for more precise control over the formatting process use the ~type~PushFormat() function.
|
144
|
+
|
145
|
+
|*_void_* ~type~PushString(*_Type_* * +self+, *_Type_* * +from+)
|
146
|
+
|
|
147
|
+
Append a _copy_ of the contents of string +from+ to string +self+.
|
148
|
+
|
149
|
+
|*_int_* ~type~PushFormat(*_Type_* * +self+, *_const char*_* format, ...);
|
150
|
+
|
|
151
|
+
Append the _?sprintf()_- formatted string to string +self+.
|
152
|
+
|
153
|
+
Return non-zero value on successful formatting and zero value if the call to _?sprintf()_ failed.
|
154
|
+
The latter usually happens due to the encoding error.
|
155
|
+
|
156
|
+
This function tries to use the _vsnprintf()_ standard C function if possible and falls back to *unsafe* _vsprintf()_ function which is ought to be present in every ANSI-compliant standard C library.
|
157
|
+
The former function is used on the platforms which are known to have it; the Autotools-compliant _HAVE_VSNPRINTF_ macro is also taken into consideration.
|
158
|
+
_Note that the choice is obviously made at compile-time._
|
159
|
+
|
160
|
+
If using the _vsnprintf()_ and the allocated buffer is not large enough this function continuously expands the buffer to eventually accommodate the resulting string.
|
161
|
+
On the contrary, when the *unsafe* _vsprintf()_ is used, the buffer overrun causes this function to *abort()* in order to possible data corruption not to slip away uncaught.
|
162
|
+
|
163
|
+
Current implementation operates on the heap-allocated buffer whose initial size is determined by the _AUTOC_BUFFER_SIZE_ macro.
|
164
|
+
If not explicitly set it defaults to 4096 bytes.
|
165
|
+
|===
|
166
|
+
|
167
|
+
=== Iteration over string's characters
|
168
|
+
|
169
|
+
[cols=2*]
|
170
|
+
|===
|
171
|
+
|*_void_* ~it~Ctor(*_IteratorType_* * +it+, *_Type_* * +self+)
|
172
|
+
|
|
173
|
+
Create a new forward iterator +it+ on string +self+.
|
174
|
+
|
175
|
+
NOTE: Previous contents of +it+ is overwritten.
|
176
|
+
|
177
|
+
|*_void_* ~it~CtorEx(*_IteratorType_* * +it+, *_Type_* * +self+, *_int_* +forward+)
|
178
|
+
|
|
179
|
+
Create a new iterator +it+ on string +self+.
|
180
|
+
Non-zero value of +forward+ specifies a forward iterator, zero value specifies a backward iterator.
|
181
|
+
|
182
|
+
NOTE: Previous contents of +it+ is overwritten.
|
183
|
+
|
184
|
+
|*_int_* ~it~Move(*_IteratorType_* * +it+)
|
185
|
+
|
|
186
|
+
Advance iterator position of +it+ *and* return non-zero value if new position is valid and zero value otherwise.
|
187
|
+
|
188
|
+
|*_CharType_* ~it~Get(*_IteratorType_* * +it+)
|
189
|
+
|
|
190
|
+
Return a _copy_ of the character pointed to by the iterator +it+.
|
191
|
+
|
192
|
+
WARNING: current position *must* be valid otherwise the behavior is undefined. See ~it~Move().
|
193
|
+
|===
|
194
|
+
|
195
|
+
=end
|
196
|
+
class String < Type
|
197
|
+
|
198
|
+
include Redirecting
|
199
|
+
|
200
|
+
def char_type; :char end
|
201
|
+
|
202
|
+
def char_type_ref; "#{char_type}*" end
|
203
|
+
|
204
|
+
attr_reader :it_ref
|
205
|
+
|
206
|
+
def initialize(type_name = :String, visibility = :public)
|
207
|
+
super
|
208
|
+
@it_ref = "#{it}*"
|
209
|
+
@list = List.new(list, {:type => char_type_ref, :dtor => free}, :private) # List takes ownership over the strings put into it hence the custom element destructor
|
210
|
+
initialize_redirectors
|
211
|
+
@ctor = define_redirector(:ctor, Function::Signature.new([type_ref^:self, "const #{char_type_ref}"^:chars]))
|
212
|
+
end
|
213
|
+
|
214
|
+
# No default constructor provided
|
215
|
+
def constructible?; false end
|
216
|
+
|
217
|
+
def write_intf_types(stream)
|
218
|
+
stream << %$
|
219
|
+
/***
|
220
|
+
**** #{type}<#{char_type}> (#{self.class})
|
221
|
+
***/
|
222
|
+
$ if public?
|
223
|
+
[@list].each {|obj| obj.write_intf_types(stream)} # TODO : this should be handled by the entity dependencies system
|
224
|
+
stream << %$
|
225
|
+
typedef struct #{type} #{type};
|
226
|
+
typedef struct #{it} #{it};
|
227
|
+
struct #{type} {
|
228
|
+
size_t size;
|
229
|
+
union data {
|
230
|
+
#{char_type_ref} string;
|
231
|
+
#{@list.type} list;
|
232
|
+
} data;
|
233
|
+
int is_list;
|
234
|
+
};
|
235
|
+
struct #{it} {
|
236
|
+
#{type_ref} string;
|
237
|
+
int index, forward;
|
238
|
+
};
|
239
|
+
$
|
240
|
+
end
|
241
|
+
|
242
|
+
def write_intf_decls(stream, declare, define)
|
243
|
+
super
|
244
|
+
write_redirectors(stream, declare, define)
|
245
|
+
stream << %$
|
246
|
+
#include <string.h>
|
247
|
+
#ifdef AUTOC_BUFFER_SIZE
|
248
|
+
#define #{_bufferSize} AUTOC_BUFFER_SIZE
|
249
|
+
#else
|
250
|
+
#define #{_bufferSize} 4096 /* Stay in sync with the documentation! */
|
251
|
+
#endif
|
252
|
+
#define #{join}(self) if(self->is_list) #{_join}(self);
|
253
|
+
#define #{split}(self) if(!self->is_list) #{_split}(self);
|
254
|
+
#{declare} void #{_join}(#{type_ref});
|
255
|
+
#{declare} void #{_split}(#{type_ref});
|
256
|
+
#{declare} #{ctor.declaration};
|
257
|
+
#{declare} #{dtor.declaration};
|
258
|
+
#{declare} #{copy.declaration};
|
259
|
+
#{declare} void #{copyRange}(#{type_ref}, #{type_ref}, size_t, size_t);
|
260
|
+
#{declare} #{equal.declaration};
|
261
|
+
#{declare} #{identify.declaration};
|
262
|
+
#define #{empty}(self) (#{size}(self) == 0)
|
263
|
+
#{define} size_t #{size}(#{type_ref} self) {
|
264
|
+
#{assert}(self);
|
265
|
+
/* #{join}(self); assuming the changes to the contents are reflected in the size */
|
266
|
+
#ifndef NDEBUG
|
267
|
+
/* Type invariants which must hold true */
|
268
|
+
if(!self->is_list) #{assert}(self->size == strlen(self->data.string));
|
269
|
+
/* TODO self->is_list case */
|
270
|
+
#endif
|
271
|
+
return self->size;
|
272
|
+
}
|
273
|
+
#{define} int #{within}(#{type_ref} self, size_t index) {
|
274
|
+
#{assert}(self);
|
275
|
+
/* Omitting excessive call to #{join}() */
|
276
|
+
return index < #{size}(self);
|
277
|
+
}
|
278
|
+
#{define} #{char_type} #{get}(#{type_ref} self, size_t index) {
|
279
|
+
#{assert}(self);
|
280
|
+
#{assert}(#{within}(self, index));
|
281
|
+
#{join}(self);
|
282
|
+
return self->data.string[index];
|
283
|
+
}
|
284
|
+
#{define} void #{set}(#{type_ref} self, size_t index, #{char_type} value) {
|
285
|
+
#{assert}(self);
|
286
|
+
#{assert}(#{within}(self, index));
|
287
|
+
#{join}(self);
|
288
|
+
self->data.string[index] = value;
|
289
|
+
}
|
290
|
+
#{define} const #{char_type_ref} #{chars}(#{type_ref} self) {
|
291
|
+
#{assert}(self);
|
292
|
+
#{join}(self);
|
293
|
+
return self->data.string;
|
294
|
+
}
|
295
|
+
#{declare} int #{pushFormat}(#{type_ref}, const char*, ...);
|
296
|
+
#{declare} void #{pushChars}(#{type_ref}, const #{char_type_ref});
|
297
|
+
#{declare} void #{pushString}(#{type_ref}, #{type_ref});
|
298
|
+
#define #{pushChar}(self, c) #{pushFormat}(self, "%c", (char)(c))
|
299
|
+
#define #{pushInt}(self, i) #{pushFormat}(self, "%d", (int)(i))
|
300
|
+
#define #{pushFloat}(self, f) #{pushFormat}(self, "%e", (double)(f))
|
301
|
+
#define #{pushPtr}(self, p) #{pushFormat}(self, "%p", (void*)(p))
|
302
|
+
#define #{itCtor}(self, type) #{itCtorEx}(self, type, 1)
|
303
|
+
#{define} void #{itCtorEx}(#{it_ref} self, #{type_ref} string, int forward) {
|
304
|
+
#{assert}(self);
|
305
|
+
#{assert}(string);
|
306
|
+
self->string = string;
|
307
|
+
self->forward = forward;
|
308
|
+
self->index = forward ? -1 : #{size}(string);
|
309
|
+
}
|
310
|
+
#{define} int #{itMove}(#{it_ref} self) {
|
311
|
+
#{assert}(self);
|
312
|
+
if(self->forward) ++self->index; else --self->index;
|
313
|
+
return #{within}(self->string, self->index);
|
314
|
+
}
|
315
|
+
#{define} #{char_type} #{itGet}(#{it_ref} self) {
|
316
|
+
#{assert}(self);
|
317
|
+
return #{get}(self->string, self->index);
|
318
|
+
}
|
319
|
+
$
|
320
|
+
end
|
321
|
+
|
322
|
+
def write_impls(stream, define)
|
323
|
+
super
|
324
|
+
[@list].each {|obj|
|
325
|
+
obj.write_intf_decls(stream, static, inline)
|
326
|
+
obj.write_impls(stream, static)
|
327
|
+
}
|
328
|
+
stream << %$
|
329
|
+
#include <stdio.h>
|
330
|
+
#include <stdarg.h>
|
331
|
+
#undef AUTOC_VSNPRINTF
|
332
|
+
#if defined(_MSC_VER)
|
333
|
+
#define AUTOC_VSNPRINTF _vsnprintf
|
334
|
+
#elif defined(__DMC__) || defined (__LCC__)
|
335
|
+
#define AUTOC_VSNPRINTF vsnprintf
|
336
|
+
#elif defined(HAVE_VSNPRINTF) || __STDC_VERSION__ >= 199901L /* Be Autotools-friendly, C99 must have snprintf() */
|
337
|
+
#define AUTOC_VSNPRINTF vsnprintf
|
338
|
+
#endif
|
339
|
+
#ifndef AUTOC_VSNPRINTF
|
340
|
+
/* #warning Using unsafe vsprintf() function */
|
341
|
+
#endif
|
342
|
+
#{define} void #{_join}(#{type_ref} self) {
|
343
|
+
#{@list.it} it;
|
344
|
+
#{char_type_ref} string;
|
345
|
+
size_t* size; /* size sizes cache to avoid excessive calls to strlen() */
|
346
|
+
size_t i, start = 0, total = 0;
|
347
|
+
#{assert}(self);
|
348
|
+
#{assert}(self->is_list);
|
349
|
+
if(!#{@list.empty}(&self->data.list)) {
|
350
|
+
size = (size_t*)malloc(#{@list.size}(&self->data.list)*sizeof(size_t)); #{assert}(size);
|
351
|
+
#{@list.itCtor}(&it, &self->data.list);
|
352
|
+
for(i = 0; #{@list.itMove}(&it); ++i) {
|
353
|
+
total += (size[i] = strlen(#{@list.itGet}(&it)));
|
354
|
+
}
|
355
|
+
string = (#{char_type_ref})#{malloc}((total + 1)*sizeof(#{char_type})); #{assert}(string);
|
356
|
+
#{@list.itCtor}(&it, &self->data.list);
|
357
|
+
/* List is a LIFO structure therefore merging should be performed from right to left */
|
358
|
+
i = 0; start = total;
|
359
|
+
while(#{@list.itMove}(&it)) {
|
360
|
+
start -= size[i];
|
361
|
+
memcpy(&string[start], #{@list.itGet}(&it), size[i]*sizeof(#{char_type}));
|
362
|
+
++i;
|
363
|
+
}
|
364
|
+
string[total] = '\\0';
|
365
|
+
#{free}(size);
|
366
|
+
} else {
|
367
|
+
string = (#{char_type_ref})#{calloc}(1, sizeof(#{char_type})); #{assert}(string);
|
368
|
+
}
|
369
|
+
#{@list.dtor}(&self->data.list);
|
370
|
+
self->size = total;
|
371
|
+
self->data.string = string;
|
372
|
+
self->is_list = 0;
|
373
|
+
}
|
374
|
+
#{define} void #{_split}(#{type_ref} self) {
|
375
|
+
#{@list.type} list;
|
376
|
+
#{assert}(self);
|
377
|
+
#{assert}(!self->is_list);
|
378
|
+
#{@list.ctor}(&list);
|
379
|
+
#{@list.push}(&list, self->data.string);
|
380
|
+
/* self->size = strlen(self->data.string); not needed since the size shouldn't have changed */
|
381
|
+
#{assert}(self->size == strlen(self->data.string));
|
382
|
+
self->data.list = list;
|
383
|
+
self->is_list = 1;
|
384
|
+
}
|
385
|
+
#{define} #{ctor.definition} {
|
386
|
+
#{assert}(self);
|
387
|
+
if(chars) {
|
388
|
+
size_t nbytes;
|
389
|
+
self->size = strlen(chars);
|
390
|
+
nbytes = (self->size + 1)*sizeof(#{char_type});
|
391
|
+
self->data.string = (#{char_type_ref})#{malloc}(nbytes); #{assert}(self->data.string);
|
392
|
+
memcpy(self->data.string, chars, nbytes);
|
393
|
+
self->is_list = 0;
|
394
|
+
} else {
|
395
|
+
/* NULL argument is permitted and corresponds to empty string */
|
396
|
+
self->size = 0;
|
397
|
+
#{@list.ctor}(&self->data.list);
|
398
|
+
self->is_list = 1;
|
399
|
+
}
|
400
|
+
}
|
401
|
+
#{define} #{dtor.definition} {
|
402
|
+
#{assert}(self);
|
403
|
+
if(self->is_list) #{@list.dtor}(&self->data.list); else #{free}(self->data.string);
|
404
|
+
}
|
405
|
+
#{define} #{copy.definition} {
|
406
|
+
#{assert}(src);
|
407
|
+
#{assert}(dst);
|
408
|
+
#{assert}(src != dst);
|
409
|
+
#{ctor}(dst, #{chars}(src));
|
410
|
+
}
|
411
|
+
#{define} void #{copyRange}(#{type_ref} dst, #{type_ref} src, size_t first, size_t last) {
|
412
|
+
size_t size;
|
413
|
+
#{char_type_ref} string;
|
414
|
+
#{assert}(src);
|
415
|
+
#{assert}(src != dst);
|
416
|
+
#{assert}(first <= last);
|
417
|
+
#{assert}(#{within}(src, first));
|
418
|
+
#{assert}(#{within}(src, last));
|
419
|
+
size = last - first + 1;
|
420
|
+
string = (#{char_type_ref})#{malloc}((size + 1)*sizeof(#{char_type})); #{assert}(string);
|
421
|
+
memcpy(string, &#{chars}(src)[first], size*sizeof(#{char_type}));
|
422
|
+
string[size] = '\\0';
|
423
|
+
#{ctor}(dst, string);
|
424
|
+
#{free}(string);
|
425
|
+
}
|
426
|
+
#{define} #{equal.definition} {
|
427
|
+
#{assert}(lt);
|
428
|
+
#{assert}(rt);
|
429
|
+
return strcmp(#{chars}(lt), #{chars}(rt)) == 0;
|
430
|
+
}
|
431
|
+
#{define} #{identify.definition} {
|
432
|
+
size_t index, result = 0;
|
433
|
+
#{assert}(self);
|
434
|
+
#{join}(self);
|
435
|
+
for(index = 0; index < #{size}(self); ++index) {
|
436
|
+
result ^= self->data.string[index];
|
437
|
+
result = AUTOC_RCYCLE(result);
|
438
|
+
}
|
439
|
+
return result;
|
440
|
+
}
|
441
|
+
#{define} int #{pushFormat}(#{type_ref} self, const char* format, ...) {
|
442
|
+
va_list args;
|
443
|
+
char* buffer;
|
444
|
+
int i, c, buffer_size = #{_bufferSize};
|
445
|
+
#{assert}(self);
|
446
|
+
#{assert}(format);
|
447
|
+
do {
|
448
|
+
buffer = (char*)#{malloc}(buffer_size*sizeof(char)); #{assert}(buffer);
|
449
|
+
va_start(args, format);
|
450
|
+
#ifdef AUTOC_VSNPRINTF
|
451
|
+
i = AUTOC_VSNPRINTF(buffer, buffer_size, format, args);
|
452
|
+
#else
|
453
|
+
i = vsprintf(buffer, format, args);
|
454
|
+
if(i >= buffer_size) #{abort}();
|
455
|
+
/* Since vsprintf() can not truncate its output this means the buffer overflow and
|
456
|
+
there is no guarantee that some useful data is not corrupted so its better
|
457
|
+
to crash right here than to let the corruption slip away uncaught */
|
458
|
+
#endif
|
459
|
+
c = (i > 0 && !(i < buffer_size));
|
460
|
+
if(i > 0 && !c) #{pushChars}(self, buffer);
|
461
|
+
va_end(args);
|
462
|
+
#{free}(buffer);
|
463
|
+
buffer_size *= 2;
|
464
|
+
} while(c);
|
465
|
+
return i >= 0;
|
466
|
+
}
|
467
|
+
#{define} void #{pushChars}(#{type_ref} self, const #{char_type_ref} chars) {
|
468
|
+
#{char_type_ref} string;
|
469
|
+
size_t size, nbytes;
|
470
|
+
#{assert}(self);
|
471
|
+
#{assert}(chars);
|
472
|
+
#{split}(self);
|
473
|
+
size = strlen(chars);
|
474
|
+
nbytes = (size + 1)*sizeof(#{char_type});
|
475
|
+
string = (#{char_type_ref})#{malloc}(nbytes); #{assert}(string);
|
476
|
+
memcpy(string, chars, nbytes);
|
477
|
+
#{@list.push}(&self->data.list, string);
|
478
|
+
self->size += size;
|
479
|
+
}
|
480
|
+
#{define} void #{pushString}(#{type_ref} self, #{type_ref} from) {
|
481
|
+
#{assert}(self);
|
482
|
+
#{assert}(from);
|
483
|
+
#{pushChars}(self, #{chars}(from));
|
484
|
+
}
|
485
|
+
#undef AUTOC_SNPRINTF
|
486
|
+
$
|
487
|
+
end
|
488
|
+
|
489
|
+
end # String
|
490
|
+
|
491
|
+
|
492
|
+
end # AutoC
|