-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
str.h
451 lines (388 loc) · 13.5 KB
/
str.h
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* @file str.h
* Dynamic text string.
*
* Simple dynamic string management. @ingroup base
*
* Uses @ref memzone or standard malloc for memory allocation, chosen during
* initialization of a string. The string instance itself is always allocated
* with malloc.
*
* AutoStr is a variant of ddstring_t that is automatically put up for garbage
* collection. You may create an AutoStr instance either by converting an
* existing string to one with AutoStr_FromStr() or by allocating a new
* instance with AutoStr_New(). You are @em not allowed to manually delete an
* AutoStr instance; it will be deleted automatically during the next garbage
* recycling.
*
* Using AutoStr instances is recommended when you have a dynamic string as a
* return value from a function, or when you have strings with a limited scope
* that need to be deleted after exiting the scope.
*
* @todo Make this opaque for better forward compatibility -- prevents initialization
* with static C strings, though (which is probably for the better anyway).
* @todo Derive from Qt::QString
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2008-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#ifndef LIBDENG_API_STRING_H
#define LIBDENG_API_STRING_H
#include "libdeng.h"
#include "types.h"
#include "reader.h"
#include "writer.h"
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Dynamic string instance. Use Str_New() to allocate one from the heap, or
* Str_Init() to initialize a string located on the stack.
*
* You can init global ddstring_t variables with static string constants,
* for example: @code ddstring_t mystr = { "Hello world." }; @endcode
*/
typedef struct ddstring_s {
/// String buffer.
char* str;
/// String length (no terminating nulls).
size_t length;
/// Allocated buffer size: includes the terminating null and possibly
/// some extra space.
size_t size;
// Memory management.
void (*memFree)(void*);
void* (*memAlloc)(size_t n);
void* (*memCalloc)(size_t n);
} ddstring_t;
/**
* The primary alias for the ddstring struct.
*/
typedef struct ddstring_s Str;
/**
* An alias for ddstring_t that is used with the convention of automatically
* trashing the string during construction so that it gets deleted during the
* next recycling. Thus it is useful for strings restricted to the local scope
* or for return values in cases when caller is not always required to take
* ownership.
*/
typedef ddstring_t AutoStr;
// Format checking for Str_Appendf in GCC2
#if defined(__GNUC__) && __GNUC__ >= 2
# define PRINTF_F(f,v) __attribute__ ((format (printf, f, v)))
#else
# define PRINTF_F(f,v)
#endif
/**
* Allocate a new uninitialized string. Use Str_Delete() to destroy
* the returned string. Memory for the string is allocated with de::Zone.
*
* @return New ddstring_t instance.
*
* @see Str_Delete
*/
DENG_PUBLIC Str* Str_New(void);
/**
* Allocate a new uninitialized string. Use Str_Delete() to destroy
* the returned string. Memory for the string is allocated with malloc.
*
* @return New ddstring_t instance.
*
* @see Str_Delete
*/
DENG_PUBLIC Str* Str_NewStd(void);
/**
* Constructs a new string by reading it from @a reader.
* Memory for the string is allocated with de::Zone.
*/
DENG_PUBLIC Str* Str_NewFromReader(Reader* reader);
/**
* Call this for uninitialized strings. Global variables are
* automatically cleared, so they don't need initialization.
*/
DENG_PUBLIC Str* Str_Init(Str* ds);
/**
* Call this for uninitialized strings. Makes the string use standard
* malloc for memory allocations.
*/
DENG_PUBLIC Str* Str_InitStd(Str* ds);
/**
* Initializes @a ds with a static const C string. No memory allocation
* model is selected; use this for strings that remain constant.
* If the string is never modified calling Str_Free() is not needed.
*/
DENG_PUBLIC Str* Str_InitStatic(Str* ds, const char* staticConstStr);
/**
* Empty an existing string. After this the string is in the same
* state as just after being initialized.
*/
DENG_PUBLIC void Str_Free(Str* ds);
/**
* Destroy a string allocated with Str_New(). In addition to freeing
* the contents of the string, it also unallocates the string instance
* that was created by Str_New().
*
* @param ds String to delete (that was returned by Str_New()).
*/
DENG_PUBLIC void Str_Delete(Str* ds);
/**
* Empties a string, but does not free its memory.
*/
DENG_PUBLIC Str* Str_Clear(Str* ds);
DENG_PUBLIC Str* Str_Reserve(Str* ds, int length);
/**
* Reserves memory for the string. There will be at least @a length bytes
* allocated for the string after this. If the string needs to be resized, its
* contents are @em not preserved.
*/
DENG_PUBLIC Str* Str_ReserveNotPreserving(Str* str, int length);
DENG_PUBLIC Str* Str_Set(Str* ds, const char* text);
DENG_PUBLIC Str* Str_Append(Str* ds, const char* appendText);
DENG_PUBLIC Str* Str_AppendChar(Str* ds, char ch);
/**
* Appends the contents of another string. Enough memory must already be
* reserved before calling this. Use in situations where good performance is
* critical.
*/
DENG_PUBLIC Str* Str_AppendWithoutAllocs(Str* str, const Str* append);
/**
* Appends a single character. Enough memory must already be reserved before
* calling this. Use in situations where good performance is critical.
*
* @param str String.
* @param ch Character to append. Cannot be 0.
*/
DENG_PUBLIC Str* Str_AppendCharWithoutAllocs(Str* str, char ch);
/**
* Append formated text.
*/
DENG_PUBLIC Str* Str_Appendf(Str* ds, const char* format, ...) PRINTF_F(2,3);
/**
* Appends a portion of a string.
*/
DENG_PUBLIC Str* Str_PartAppend(Str* dest, const char* src, int start, int count);
/**
* Prepend is not even a word, is it? It should be 'prefix'?
*/
DENG_PUBLIC Str* Str_Prepend(Str* ds, const char* prependText);
DENG_PUBLIC Str* Str_PrependChar(Str* ds, char ch);
/**
* Determines the length of the string in characters.
* This is safe for all strings. @see Str_Size()
*
* @param ds String instance.
*
* @return Length of the string as an integer.
*/
DENG_PUBLIC int Str_Length(const Str* ds);
/**
* Determines the length of the string in characters.
* This is safe for all strings. @see Str_Length()
*
* @param ds String instance.
*
* @return Length of the string.
*/
DENG_PUBLIC size_t Str_Size(const Str* ds);
DENG_PUBLIC boolean Str_IsEmpty(const Str* ds);
/**
* This is safe for all strings.
*/
DENG_PUBLIC char* Str_Text(const Str* ds);
/**
* Makes a copy of @a src and replaces the previous contents of @a dest with
* it. The copy will have least as much memory reserved in its internal buffer
* as the original.
*
* If @a src is a static string (i.e., no memory allocated for its buffer), new
* memory will be allocated for the copy.
*
* @param dest String where the copy is stored.
* @param src Original string to copy.
*
* @return The @a dest string with the copied content.
*/
DENG_PUBLIC Str* Str_Copy(Str* dest, const Str* src);
DENG_PUBLIC Str* Str_CopyOrClear(Str* dest, const Str* src);
/**
* Strip whitespace from beginning.
*
* @param ds String instance.
* @param count If not @c NULL the number of characters stripped is written here.
* @return Same as @a str for caller convenience.
*/
DENG_PUBLIC Str* Str_StripLeft2(Str* ds, int* count);
DENG_PUBLIC Str* Str_StripLeft(Str* ds);
/**
* Strip whitespace from end.
*
* @param ds String instance.
* @param count If not @c NULL the number of characters stripped is written here.
* @return Same as @a str for caller convenience.
*/
DENG_PUBLIC Str* Str_StripRight2(Str* ds, int* count);
DENG_PUBLIC Str* Str_StripRight(Str* ds);
/**
* Strip whitespace from beginning and end.
*
* @param ds String instance.
* @param count If not @c NULL the number of characters stripped is written here.
* @return Same as @a str for caller convenience.
*/
DENG_PUBLIC Str* Str_Strip2(Str* ds, int* count);
DENG_PUBLIC Str* Str_Strip(Str* ds);
/**
* Extract a line of text from the source.
*
* @param ds String instance where to put the extracted line.
* @param src Source string. Must be @c NULL terminated.
*/
DENG_PUBLIC const char* Str_GetLine(Str* ds, const char* src);
/**
* @defgroup copyDelimiterFlags Copy Delimiter Flags
*/
/*@{*/
#define CDF_OMIT_DELIMITER 0x1 // Do not copy delimiters into the dest path.
#define CDF_OMIT_WHITESPACE 0x2 // Do not copy whitespace into the dest path.
/*@}*/
/**
* Copies characters from @a src to @a dest until a @a delimiter character is encountered.
*
* @param dest Destination string.
* @param src Source string.
* @param delimiter Delimiter character.
* @param cdflags @ref copyDelimiterFlags
*
* @return Pointer to the character within @a src where copy stopped
* else @c NULL if the end was reached.
*/
DENG_PUBLIC const char* Str_CopyDelim2(Str* dest, const char* src, char delimiter, int cdflags);
DENG_PUBLIC const char* Str_CopyDelim(Str* dest, const char* src, char delimiter);
/**
* Case sensitive comparison.
*/
DENG_PUBLIC int Str_Compare(const Str* str, const char* text);
/**
* Non case sensitive comparison.
*/
DENG_PUBLIC int Str_CompareIgnoreCase(const Str* ds, const char* text);
/**
* Retrieves a character in the string.
*
* @param str String to get the character from.
* @param index Index of the character.
*
* @return The character at @c index, or 0 if the index is not in range.
*/
DENG_PUBLIC char Str_At(const Str* str, int index);
/**
* Retrieves a character in the string. Indices start from the end of the string.
*
* @param str String to get the character from.
* @param reverseIndex Index of the character, where 0 is the last character of the string.
*
* @return The character at @c index, or 0 if the index is not in range.
*/
DENG_PUBLIC char Str_RAt(const Str* str, int reverseIndex);
DENG_PUBLIC void Str_Truncate(Str* str, int position);
/**
* Percent-encode characters in string. Will encode the default set of
* characters for the unicode utf8 charset.
*
* @param str String instance.
* @return Same as @a str.
*/
DENG_PUBLIC Str* Str_PercentEncode(Str* str);
/**
* Percent-encode characters in string.
*
* @param str String instance.
* @param excludeChars List of characters that should NOT be encoded. @c 0 terminated.
* @param includeChars List of characters that will always be encoded (has precedence over
* @a excludeChars). @c 0 terminated.
* @return Same as @a str.
*/
DENG_PUBLIC Str* Str_PercentEncode2(Str* str, const char* excludeChars, const char* includeChars);
/**
* Decode the percent-encoded string. Will match codes for the unicode
* utf8 charset.
*
* @param str String instance.
* @return Same as @a str.
*/
DENG_PUBLIC Str* Str_PercentDecode(Str* str);
DENG_PUBLIC void Str_Write(const Str* str, Writer* writer);
DENG_PUBLIC void Str_Read(Str* str, Reader* reader);
/*
* AutoStr
*/
DENG_PUBLIC AutoStr* AutoStr_New(void);
DENG_PUBLIC AutoStr* AutoStr_NewStd(void);
/**
* Converts a ddstring to an AutoStr. After this you are not allowed
* to call Str_Delete() manually on the string; the garbage collector
* will destroy the string during the next recycling.
*
* No memory allocations are done in this function.
*
* @param str String instance.
*
* @return AutoStr instance. The returned instance is @a str after
* having been trashed.
*/
DENG_PUBLIC AutoStr* AutoStr_FromStr(Str* str);
/**
* Constructs an AutoStr instance (zone allocated) and initializes its
* contents with @a text.
*
* @param text Text for the new string.
*
* @return AutoStr instance.
*/
DENG_PUBLIC AutoStr* AutoStr_FromText(const char* text);
/**
* Converts an AutoStr to a normal ddstring. You must call Str_Delete()
* on the returned string manually to destroy it.
*
* No memory allocations are done in this function.
*
* @param as AutoStr instance.
*
* @return ddstring instance. The returned instance is @a as after
* having been taken out of the garbage.
*/
DENG_PUBLIC Str* Str_FromAutoStr(AutoStr* as);
/**
* Prints a formatted string into a fixed-size buffer. At most @c size
* characters will be written to the output buffer @c str. The output will
* always contain a terminating null character.
*
* @param str Output buffer.
* @param size Size of the output buffer.
* @param format Format of the output.
* @param ap Variable-size argument list.
*
* @return Number of characters written to the output buffer if lower than or
* equal to @c size, else @c -1.
*/
DENG_PUBLIC int dd_vsnprintf(char* str, size_t size, const char* format, va_list ap);
#ifdef __cplusplus
} // extern "C"
# include "str.hh" // C++ wrapper for ddstring_t
#endif
#endif /* LIBDENG_API_STRING_H */