Sun, 01 Apr 2018 09:51:01 +0200
adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * Bounded string implementation.
30 *
31 * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings.
32 * The main difference to C strings is, that <code>sstr_t</code> does <b>not
33 * need to be <code>NULL</code>-terminated</b>. Instead the length is stored
34 * within the structure.
35 *
36 * When using <code>sstr_t</code>, developers must be full aware of what type
37 * of string (<code>NULL</code>-terminated) or not) they are using, when
38 * accessing the <code>char* ptr</code> directly.
39 *
40 * The UCX string module provides some common string functions, known from
41 * standard libc, working with <code>sstr_t</code>.
42 *
43 * @file string.h
44 * @author Mike Becker
45 * @author Olaf Wintermann
46 */
48 #ifndef UCX_STRING_H
49 #define UCX_STRING_H
51 #include "ucx.h"
52 #include "allocator.h"
53 #include <stddef.h>
55 /** Shortcut for a <code>sstr_t struct</code> literal. */
56 #define ST(s) { (char*)s, sizeof(s)-1 }
58 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
59 #define S(s) sstrn((char*)s, sizeof(s)-1)
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 /**
65 * The UCX string structure.
66 */
67 typedef struct {
68 /** A reference to the string (<b>not necessarily <code>NULL</code>
69 * -terminated</b>) */
70 char *ptr;
71 /** The length of the string */
72 size_t length;
73 } sstr_t;
75 typedef struct {
76 const char *ptr;
77 size_t length;
78 } scstr_t;
79 #ifdef __cplusplus
80 }
81 #endif
84 #ifdef __cplusplus
85 inline scstr_t s2scstr(sstr_t s) {
86 scstr_t c;
87 c.ptr = s.ptr;
88 c.length = s.ptr;
89 return c;
90 }
91 inline scstr_t s2scstr(scstr_t c) {
92 return c;
93 }
94 #define SCSTR s2scstr
95 #else
97 scstr_t ucx_sc2sc(scstr_t c);
98 scstr_t ucx_ss2sc(sstr_t str);
99 #if __STDC_VERSION__ >= 201112L
100 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
101 #elif defined(__GNUC__) || defined(__clang__)
102 #define SCSTR(str) __builtin_choose_expr( \
103 __builtin_types_compatible_p(typeof(str), sstr_t), \
104 ucx_ss2sc, \
105 ucx_sc2sc)(str)
106 #elif defined(__sun)
107 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
108 scstr_t ucx_tmp_var_c; \
109 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
110 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
111 ucx_tmp_var_c; })
112 #else
113 scstr_t ucx_ss2c_s();
114 #define SCSTR ucx_ss2c_s
115 #endif /* C11 feature test */
117 #endif /* C++ */
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
124 /**
125 * Creates a new sstr_t based on a C string.
126 *
127 * The length is implicitly inferred by using a call to <code>strlen()</code>.
128 *
129 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
130 * do want a copy, use sstrdup() on the return value of this function.
131 *
132 * @param cstring the C string to wrap
133 * @return a new sstr_t containing the C string
134 *
135 * @see sstrn()
136 */
137 sstr_t sstr(char *cstring);
139 /**
140 * Creates a new sstr_t of the specified length based on a C string.
141 *
142 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
143 * do want a copy, use sstrdup() on the return value of this function.
144 *
145 * @param cstring the C string to wrap
146 * @param length the length of the string
147 * @return a new sstr_t containing the C string
148 *
149 * @see sstr()
150 * @see S()
151 */
152 sstr_t sstrn(char *cstring, size_t length);
155 scstr_t scstr(const char *cstring);
156 scstr_t scstrn(const char *cstring, size_t length);
158 /**
159 * Returns the cumulated length of all specified strings.
160 *
161 * At least one string must be specified.
162 *
163 * <b>Attention:</b> if the count argument does not match the count of the
164 * specified strings, the behavior is undefined.
165 *
166 * @param count the total number of specified strings (so at least 1)
167 * @param string the first string
168 * @param ... all other strings
169 * @return the cumulated length of all strings
170 */
171 size_t sstrnlen(size_t count, sstr_t string, ...);
173 /**
174 * Concatenates two or more strings.
175 *
176 * The resulting string will be allocated by standard <code>malloc()</code>.
177 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
178 *
179 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
180 * terminated.
181 *
182 * @param count the total number of strings to concatenate
183 * @param s1 first string
184 * @param s2 second string
185 * @param ... all remaining strings
186 * @return the concatenated string
187 */
188 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...);
190 /**
191 * Concatenates two or more strings using a UcxAllocator.
192 *
193 * See sstrcat() for details.
194 *
195 * @param a the allocator to use
196 * @param count the total number of strings to concatenate
197 * @param s1 first string
198 * @param s2 second string
199 * @param ... all remaining strings
200 * @return the concatenated string
201 */
202 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...);
205 /**
206 * Returns a substring starting at the specified location.
207 *
208 * <b>Attention:</b> the new string references the same memory area as the
209 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
210 * Use sstrdup() to get a copy.
211 *
212 * @param string input string
213 * @param start start location of the substring
214 * @return a substring of <code>string</code> starting at <code>start</code>
215 *
216 * @see sstrsubsl()
217 * @see sstrchr()
218 */
219 sstr_t sstrsubs(sstr_t string, size_t start);
221 /**
222 * Returns a substring with a maximum length starting at the specified location.
223 *
224 * <b>Attention:</b> the new string references the same memory area as the
225 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
226 * Use sstrdup() to get a copy.
227 *
228 * @param string input string
229 * @param start start location of the substring
230 * @param length the maximum length of the substring
231 * @return a substring of <code>string</code> starting at <code>start</code>
232 * with a maximum length of <code>length</code>
233 *
234 * @see sstrsubs()
235 * @see sstrchr()
236 */
237 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
239 /**
240 * Returns a substring starting at the location of the first occurrence of the
241 * specified character.
242 *
243 * If the string does not contain the character, an empty string is returned.
244 *
245 * @param string the string where to locate the character
246 * @param chr the character to locate
247 * @return a substring starting at the first location of <code>chr</code>
248 *
249 * @see sstrsubs()
250 */
251 sstr_t sstrchr(sstr_t string, int chr);
253 /**
254 * Returns a substring starting at the location of the last occurrence of the
255 * specified character.
256 *
257 * If the string does not contain the character, an empty string is returned.
258 *
259 * @param string the string where to locate the character
260 * @param chr the character to locate
261 * @return a substring starting at the last location of <code>chr</code>
262 *
263 * @see sstrsubs()
264 */
265 sstr_t sstrrchr(sstr_t string, int chr);
268 const char* ucx_strstr(
269 const char *str,
270 size_t length,
271 const char *match,
272 size_t matchlen,
273 size_t *newlen);
275 /**
276 * Returns a substring starting at the location of the first occurrence of the
277 * specified string.
278 *
279 * If the string does not contain the other string, an empty string is returned.
280 *
281 * If <code>match</code> is an empty string, the complete <code>string</code> is
282 * returned.
283 *
284 * @param string the string to be scanned
285 * @param match string containing the sequence of characters to match
286 * @return a substring starting at the first occurrence of
287 * <code>match</code>, or an empty string, if the sequence is not
288 * present in <code>string</code>
289 */
290 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
291 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
293 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
294 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
296 /**
297 * Splits a string into parts by using a delimiter string.
298 *
299 * This function will return <code>NULL</code>, if one of the following happens:
300 * <ul>
301 * <li>the string length is zero</li>
302 * <li>the delimeter length is zero</li>
303 * <li>the string equals the delimeter</li>
304 * <li>memory allocation fails</li>
305 * </ul>
306 *
307 * The integer referenced by <code>count</code> is used as input and determines
308 * the maximum size of the resulting array, i.e. the maximum count of splits to
309 * perform + 1.
310 *
311 * The integer referenced by <code>count</code> is also used as output and is
312 * set to
313 * <ul>
314 * <li>-2, on memory allocation errors</li>
315 * <li>-1, if either the string or the delimiter is an empty string</li>
316 * <li>0, if the string equals the delimiter</li>
317 * <li>1, if the string does not contain the delimiter</li>
318 * <li>the count of array items, otherwise</li>
319 * </ul>
320 *
321 * If the string starts with the delimiter, the first item of the resulting
322 * array will be an empty string.
323 *
324 * If the string ends with the delimiter and the maximum list size is not
325 * exceeded, the last array item will be an empty string.
326 * In case the list size would be exceeded, the last array item will be the
327 * remaining string after the last split, <i>including</i> the terminating
328 * delimiter.
329 *
330 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
331 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
332 * an allocator to managed memory, to avoid this.
333 *
334 * @param string the string to split
335 * @param delim the delimiter string
336 * @param count IN: the maximum size of the resulting array (0 = no limit),
337 * OUT: the actual size of the array
338 * @return a sstr_t array containing the split strings or
339 * <code>NULL</code> on error
340 *
341 * @see sstrsplit_a()
342 */
343 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
345 #define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
347 /**
348 * Performing sstrsplit() using a UcxAllocator.
349 *
350 * <i>Read the description of sstrsplit() for details.</i>
351 *
352 * The memory for the sstr_t.ptr pointers of the array items and the memory for
353 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
354 * function.
355 *
356 * <b>Note:</b> the allocator is not used for memory that is freed within the
357 * same call of this function (locally scoped variables).
358 *
359 * @param allocator the UcxAllocator used for allocating memory
360 * @param string the string to split
361 * @param delim the delimiter string
362 * @param count IN: the maximum size of the resulting array (0 = no limit),
363 * OUT: the actual size of the array
364 * @return a sstr_t array containing the split strings or
365 * <code>NULL</code> on error
366 *
367 * @see sstrsplit()
368 */
369 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
370 ssize_t *count);
372 #define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
374 /**
375 * Compares two UCX strings with standard <code>memcmp()</code>.
376 *
377 * At first it compares the sstr_t.length attribute of the two strings. The
378 * <code>memcmp()</code> function is called, if and only if the lengths match.
379 *
380 * @param s1 the first string
381 * @param s2 the second string
382 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
383 * length of s1 is greater than the length of s2 or the result of
384 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
385 */
386 int ucx_str_cmp(scstr_t s1, scstr_t s2);
388 #define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2))
390 /**
391 * Compares two UCX strings ignoring the case.
392 *
393 * At first it compares the sstr_t.length attribute of the two strings. If and
394 * only if the lengths match, both strings are compared char by char ignoring
395 * the case.
396 *
397 * @param s1 the first string
398 * @param s2 the second string
399 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
400 * length of s1 is greater than the length of s2 or the difference between the
401 * first two differing characters otherwise (i.e. 0 if the strings match and
402 * no characters differ)
403 */
404 int ucx_str_casecmp(scstr_t s1, scstr_t s2);
406 #define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2))
408 /**
409 * Creates a duplicate of the specified string.
410 *
411 * The new sstr_t will contain a copy allocated by standard
412 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
413 * <code>free()</code>.
414 *
415 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
416 * terminated.
417 *
418 * @param string the string to duplicate
419 * @return a duplicate of the string
420 * @see sstrdup_a()
421 */
422 sstr_t scstrdup(scstr_t string);
424 #define sstrdup(s) scstrdup(SCSTR(s))
426 /**
427 * Creates a duplicate of the specified string using a UcxAllocator.
428 *
429 * The new sstr_t will contain a copy allocated by the allocators
430 * ucx_allocator_malloc function. So it is implementation depended, whether the
431 * returned sstr_t.ptr pointer must be passed to the allocators
432 * ucx_allocator_free function manually.
433 *
434 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
435 * terminated.
436 *
437 * @param allocator a valid instance of a UcxAllocator
438 * @param string the string to duplicate
439 * @return a duplicate of the string
440 * @see sstrdup()
441 */
442 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
444 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
447 size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
449 /**
450 * Omits leading and trailing spaces.
451 *
452 * This function returns a new sstr_t containing a trimmed version of the
453 * specified string.
454 *
455 * <b>Note:</b> the new sstr_t references the same memory, thus you
456 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
457 * <code>free()</code>. It is also highly recommended to avoid assignments like
458 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
459 * source string. Assignments of this type are only permitted, if the
460 * sstr_t.ptr of the source string does not need to be freed or if another
461 * reference to the source string exists.
462 *
463 * @param string the string that shall be trimmed
464 * @return a new sstr_t containing the trimmed string
465 */
466 sstr_t sstrtrim(sstr_t string);
468 scstr_t scstrtrim(scstr_t string);
470 /**
471 * Checks, if a string has a specific prefix.
472 * @param string the string to check
473 * @param prefix the prefix the string should have
474 * @return 1, if and only if the string has the specified prefix, 0 otherwise
475 */
476 int ucx_strprefix(scstr_t string, scstr_t prefix);
478 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
480 /**
481 * Checks, if a string has a specific suffix.
482 * @param string the string to check
483 * @param suffix the suffix the string should have
484 * @return 1, if and only if the string has the specified suffix, 0 otherwise
485 */
486 int ucx_strsuffix(scstr_t string, scstr_t suffix);
488 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
490 /**
491 * Returns a lower case version of a string.
492 *
493 * This function creates a duplicate of the input string, first. See the
494 * documentation of sstrdup() for the implications.
495 *
496 * @param string the input string
497 * @return the resulting lower case string
498 * @see sstrdup()
499 */
500 sstr_t ucx_strlower(scstr_t string);
502 #define sstrlower(string) ucx_strlower(SCSTR(string))
504 /**
505 * Returns a lower case version of a string.
506 *
507 * This function creates a duplicate of the input string, first. See the
508 * documentation of sstrdup_a() for the implications.
509 *
510 * @param allocator the allocator used for duplicating the string
511 * @param string the input string
512 * @return the resulting lower case string
513 * @see sstrdup_a()
514 */
515 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
517 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
519 /**
520 * Returns a upper case version of a string.
521 *
522 * This function creates a duplicate of the input string, first. See the
523 * documentation of sstrdup() for the implications.
524 *
525 * @param string the input string
526 * @return the resulting upper case string
527 * @see sstrdup()
528 */
529 sstr_t ucx_strupper(scstr_t string);
531 #define sstrupper(string) ucx_strupper(SCSTR(string))
533 /**
534 * Returns a upper case version of a string.
535 *
536 * This function creates a duplicate of the input string, first. See the
537 * documentation of sstrdup_a() for the implications.
538 *
539 * @param allocator the allocator used for duplicating the string
540 * @param string the input string
541 * @return the resulting upper case string
542 * @see sstrdup_a()
543 */
544 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
546 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
548 #ifdef __cplusplus
549 }
550 #endif
552 #endif /* UCX_STRING_H */