-
Notifications
You must be signed in to change notification settings - Fork 8
/
str.h
419 lines (377 loc) · 12.4 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
/*
Copyright (c) 2020, Marc Sune Clos
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CDADA_STRING_H__
#define __CDADA_STRING_H__
#include <stdbool.h>
#include <stdint.h>
#include <cdada/utils.h>
/**
* @file cdada/str.h
* @author Marc Sune<marcdevel (at) gmail.com>
*
* @brief String data structure
*
* Uses std::string as a backend
*/
/**
* cdada str structure
*/
typedef void cdada_str_t;
CDADA_BEGIN_DECLS
/**
* cdada str structure iterator
*
* @param str String ptr
* @param key Key (immutable)
* @param opaque A pointer to an opaque object tat will be passed to the callback
*/
typedef void (*cdada_str_it)(const cdada_str_t* str, const char it,
uint32_t pos,
void* opaque);
/**
* Create a string data structure (in heap)
*
* @returns Returns a cdada_string object or NULL, if some error is found
*/
cdada_str_t* cdada_str_create(const char* str);
/**
* Destroy a str structure
*
* @returns Return codes:
* CDADA_SUCCESS: string is destroyed
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_destroy(cdada_str_t* str);
/**
* Traverse string, char by char
*
* @param str String
* @param func Traverse function for this specific str
* @param opaque User data (opaque ptr)
*
* @returns Return codes:
* CDADA_SUCCESS: string was successfully traversed
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_traverse(const cdada_str_t* str, cdada_str_it func,
void* opaque);
/**
* Reverse traverse str, char by char
*
* @param str String
* @param func Traverse function for this specific str
* @param opaque User data (opaque ptr)
*
* @returns Return codes:
* CDADA_SUCCESS: string was successfully reverse traversed
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_rtraverse(const cdada_str_t* str, cdada_str_it func,
void* opaque);
//String properties
/**
* Is the str empty string ("")
*
* @returns Returns true if string is empty("") else (including invalid) false
*/
bool cdada_str_empty(const cdada_str_t* str);
/**
* Return the length, excluding '\0'
*
* @returns Returns length. If string is NULL or corrupted, returns 0
*/
uint32_t cdada_str_length(const cdada_str_t* str);
//Access
/**
* Get the C string
*
* @param str String pointer
*
* @returns Returns C string. If string is NULL or corrupted, returns NULL
*/
const char* cdada_str(const cdada_str_t* str);
/**
* Find the first positon of the substring
*
* @param str String pointer
* @param substr Substring to match
* @param pos Position where to substring was found
*
* @returns Return codes:
* CDADA_SUCCESS: substring was found
* CDADA_E_NOT_FOUND: substring not found
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, substr, pos is NULL
*/
int cdada_str_find_first(const cdada_str_t* str, const char* substr,
uint32_t* pos);
/**
* Find the last positon of the substring
*
* @param str String pointer
* @param substr Substring to match
* @param pos Position where to substring was found
*
* @returns Return codes:
* CDADA_SUCCESS: substring was found
* CDADA_E_NOT_FOUND: substring not found
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, substr or n are NULL
*/
int cdada_str_find_last(const cdada_str_t* str, const char* substr,
uint32_t* pos);
/**
* Get the number of times a substring is found in the str
*
* @param str String pointer
* @param substr Substring to match
* @param n Number of occurrences
*
* @returns Return codes:
* CDADA_SUCCESS: substring was found
* CDADA_E_NOT_FOUND: substring not found
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, substr or n are NULL
*/
int cdada_str_find_count(const cdada_str_t* str, const char* substr,
uint32_t* n);
/**
* @brief Find all occurrences of substring
*
* This function will return all the occurences of a substring in the str
*
* If the number of occurrances > pos_len, the function will return E_INCOMPLETE
*
* @param str String pointer
* @param substr Substring to match
* @param size Size of the array of occurences
* @param poss Array of positions
* @param cnt Number of find() occurrences
*
* @returns Return codes:
* CDADA_SUCCESS: substring was found
* CDADA_E_NOT_FOUND: substring not found
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, substr, size=0, poss or
* or cnt are NULL
*/
int cdada_str_find_all(const cdada_str_t* str, const char* substr,
const uint32_t size,
uint32_t* poss,
uint32_t* cnt);
/**
* Get the first char in the str
* @param str String pointer
* @param c char
*
* @returns Return codes:
* CDADA_SUCCESS: got first char
* CDADA_E_EMPTY: string is empty
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, c is NULL
*/
int cdada_str_first_c(const cdada_str_t* str, char* c);
/**
* Get the a char in the str
* @param str String pointer
* @param pos Position of the char
* @param c char
*
* @returns Return codes:
* CDADA_SUCCESS: got char
* CDADA_E_EMPTY: string is empty
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, c is NULL
*/
int cdada_str_get_c(const cdada_str_t* str, const uint32_t pos, char* c);
/**
* Get the last char in the str
* @param str String pointer
* @param c char
*
* @returns Return codes:
* CDADA_SUCCESS: got last char
* CDADA_E_EMPTY: string is empty
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, c is NULL
*/
int cdada_str_last_c(const cdada_str_t* str, char* c);
//Comparison
/**
* Compares the two (cdada) strings s1 and s2. Locale is not taken into account.
* It returns 0 if s1 and s2 are equal, a negative value if s1 is less than s2,
* 1 if s1 is greater than s2.
*
* @param s1 String pointer
* @param s2 String pointer
* @param n Compare at most n characters. Use 0 to behave as strcmp()
*/
int cdada_str_ncmp(const cdada_str_t* s1, const cdada_str_t* s2, uint32_t n);
/**
* Compares the two strings s1 (cdada) and s2 (C string). Locale is not taken
* into account. It returns 0 if s1 and s2 are equal, a negative value if s1 is
* less than s2, 1 if s1 is greater than s2.
*
* @param s1 cdada string pointer
* @param s2 C string pointer
* @param n Compare at most n characters. Use 0 to behave as strcmp()
*/
int cdada_str_ncmp_c(const cdada_str_t* s1, const char* s2, uint32_t n);
//Manipulation
/**
* Set or replace the contents of the string
*
* @returns Return codes:
* CDADA_SUCCESS: string reset
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_set(cdada_str_t* str, const char* new_str);
/**
* Clears the contents of the str ("")
*
* @returns Return codes:
* CDADA_SUCCESS: string is cleared
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
static inline int cdada_str_clear(cdada_str_t* str){
return cdada_str_set(str, "");
}
/**
* Append a substring at the end of the str
*
* @param str String pointer
* @param substr Substring to insert
*
* @returns Return codes:
* CDADA_SUCCESS: substr appended
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, substr is NULL
*/
int cdada_str_append(cdada_str_t* str, const char* substr);
/**
* Trim n characters from the end of the string
*
* @param str String pointer
* @param n Number of characters
*
* @returns Return codes:
* CDADA_SUCCESS: substr appended
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, n > length
*/
int cdada_str_trim(cdada_str_t* str, const uint32_t n);
/**
* Insert a substring in a specific position of the string
*
* @param str String pointer
* @param pos Position where to inser the substring
* @param substr Substring to insert
*
* @returns Return codes:
* CDADA_SUCCESS: substr inserted
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, pos > length
*/
int cdada_str_insert(cdada_str_t* str, uint32_t pos, const char* substr);
/**
* Erase a substring starting at position [pos] element in the str
*
* @param str String pointer
* @param pos Position where to insert the substring
* @param substr_len Length of the substr
*
* @returns Return codes:
* CDADA_SUCCESS: substr erased
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted, pos > length,
* (pos+substr_len) > length
*/
int cdada_str_erase(cdada_str_t* str, const uint32_t pos,
const uint32_t substr_len);
/**
* Convert string to all lower case
*
* @param str String pointer
*
* @returns Return codes:
* CDADA_SUCCESS: string transformed to lower
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_lower(cdada_str_t* str);
/**
* Convert string to all upper case
*
* @param str String pointer
*
* @returns Return codes:
* CDADA_SUCCESS: string transformed to upper
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_upper(cdada_str_t* str);
/**
* Replace all occurences of match with new_str
*
* @param str String pointer
* @param match Match substring
* @param new_str Substring to replace with
*
* @returns Return codes:
* CDADA_SUCCESS: string replaced
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_replace_all(cdada_str_t* str, const char* match,
const char* new_str);
/**
* Replace first occurence of match with new_str, starting at position pos
*
* @param str String pointer
* @param match Match substring
* @param new_str Substring to replace with
* @param pos Start position
*
* @returns Return codes:
* CDADA_SUCCESS: string replaced
* CDADA_E_MEM: out of memory
* CDADA_E_UNKNOWN: corrupted string or internal error (bug)
* CDADA_E_INVALID: string is NULL or corrupted
*/
int cdada_str_replace(cdada_str_t* str, const char* match, const char* new_str,
const uint32_t pos);
CDADA_END_DECLS
#endif //__CDADA_STRING_H__