Sun, 13 May 2018 07:13:09 +0200
completes conversion to scstr
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;
80 #ifdef __cplusplus
81 }
82 #endif
85 #ifdef __cplusplus
86 inline scstr_t s2scstr(sstr_t s) {
87 scstr_t c;
88 c.ptr = s.ptr;
89 c.length = s.ptr;
90 return c;
91 }
92 inline scstr_t s2scstr(scstr_t c) {
93 return c;
94 }
95 #define SCSTR s2scstr
96 #else
98 scstr_t ucx_sc2sc(scstr_t c);
99 scstr_t ucx_ss2sc(sstr_t str);
100 #if __STDC_VERSION__ >= 201112L
101 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
102 #elif defined(__GNUC__) || defined(__clang__)
103 #define SCSTR(str) __builtin_choose_expr( \
104 __builtin_types_compatible_p(typeof(str), sstr_t), \
105 ucx_ss2sc, \
106 ucx_sc2sc)(str)
107 #elif defined(__sun)
108 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
109 scstr_t ucx_tmp_var_c; \
110 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
111 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
112 ucx_tmp_var_c; })
113 #else
114 scstr_t ucx_ss2c_s();
115 #define SCSTR ucx_ss2c_s
116 #endif /* C11 feature test */
118 #endif /* C++ */
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
125 /**
126 * Creates a new sstr_t based on a C string.
127 *
128 * The length is implicitly inferred by using a call to <code>strlen()</code>.
129 *
130 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
131 * do want a copy, use sstrdup() on the return value of this function.
132 *
133 * @param cstring the C string to wrap
134 * @return a new sstr_t containing the C string
135 *
136 * @see sstrn()
137 */
138 sstr_t sstr(char *cstring);
140 /**
141 * Creates a new sstr_t of the specified length based on a C string.
142 *
143 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
144 * do want a copy, use sstrdup() on the return value of this function.
145 *
146 * @param cstring the C string to wrap
147 * @param length the length of the string
148 * @return a new sstr_t containing the C string
149 *
150 * @see sstr()
151 * @see S()
152 */
153 sstr_t sstrn(char *cstring, size_t length);
156 scstr_t scstr(const char *cstring);
157 scstr_t scstrn(const char *cstring, size_t length);
159 /**
160 * Returns the cumulated length of all specified strings.
161 *
162 * At least one string must be specified.
163 *
164 * <b>Attention:</b> if the count argument does not match the count of the
165 * specified strings, the behavior is undefined.
166 *
167 * @param count the total number of specified strings (so at least 1)
168 * @param string the first string
169 * @param ... all other strings
170 * @return the cumulated length of all strings
171 */
172 size_t ucx_strnlen(size_t count, ...);
174 #define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
176 /**
177 * Concatenates two or more strings.
178 *
179 * The resulting string will be allocated by standard <code>malloc()</code>.
180 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
181 *
182 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
183 * terminated.
184 *
185 * @param count the total number of strings to concatenate
186 * @param s1 first string
187 * @param ... all remaining strings
188 * @return the concatenated string
189 */
190 sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
192 #define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
194 /**
195 * Concatenates two or more strings using a UcxAllocator.
196 *
197 * See sstrcat() for details.
198 *
199 * @param a the allocator to use
200 * @param count the total number of strings to concatenate
201 * @param s1 first string
202 * @param ... all remaining strings
203 * @return the concatenated string
204 */
205 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
207 #define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__)
209 /**
210 * Returns a substring starting at the specified location.
211 *
212 * <b>Attention:</b> the new string references the same memory area as the
213 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
214 * Use sstrdup() to get a copy.
215 *
216 * @param string input string
217 * @param start start location of the substring
218 * @return a substring of <code>string</code> starting at <code>start</code>
219 *
220 * @see sstrsubsl()
221 * @see sstrchr()
222 */
223 sstr_t sstrsubs(sstr_t string, size_t start);
225 /**
226 * Returns a substring with a maximum length starting at the specified location.
227 *
228 * <b>Attention:</b> the new string references the same memory area as the
229 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
230 * Use sstrdup() to get a copy.
231 *
232 * @param string input string
233 * @param start start location of the substring
234 * @param length the maximum length of the substring
235 * @return a substring of <code>string</code> starting at <code>start</code>
236 * with a maximum length of <code>length</code>
237 *
238 * @see sstrsubs()
239 * @see sstrchr()
240 */
241 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
243 scstr_t scstrsubs(scstr_t s, size_t start);
244 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
247 int ucx_strchr(const char *string, size_t length, int chr, size_t *pos);
248 int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos);
250 /**
251 * Returns a substring starting at the location of the first occurrence of the
252 * specified character.
253 *
254 * If the string does not contain the character, an empty string is returned.
255 *
256 * @param string the string where to locate the character
257 * @param chr the character to locate
258 * @return a substring starting at the first location of <code>chr</code>
259 *
260 * @see sstrsubs()
261 */
262 sstr_t sstrchr(sstr_t string, int chr);
264 /**
265 * Returns a substring starting at the location of the last occurrence of the
266 * specified character.
267 *
268 * If the string does not contain the character, an empty string is returned.
269 *
270 * @param string the string where to locate the character
271 * @param chr the character to locate
272 * @return a substring starting at the last location of <code>chr</code>
273 *
274 * @see sstrsubs()
275 */
276 sstr_t sstrrchr(sstr_t string, int chr);
279 scstr_t scstrchr(scstr_t string, int chr);
280 scstr_t scstrrchr(scstr_t string, int chr);
282 const char* ucx_strstr(
283 const char *str,
284 size_t length,
285 const char *match,
286 size_t matchlen,
287 size_t *newlen);
289 /**
290 * Returns a substring starting at the location of the first occurrence of the
291 * specified string.
292 *
293 * If the string does not contain the other string, an empty string is returned.
294 *
295 * If <code>match</code> is an empty string, the complete <code>string</code> is
296 * returned.
297 *
298 * @param string the string to be scanned
299 * @param match string containing the sequence of characters to match
300 * @return a substring starting at the first occurrence of
301 * <code>match</code>, or an empty string, if the sequence is not
302 * present in <code>string</code>
303 */
304 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
305 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
307 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
308 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
310 /**
311 * Splits a string into parts by using a delimiter string.
312 *
313 * This function will return <code>NULL</code>, if one of the following happens:
314 * <ul>
315 * <li>the string length is zero</li>
316 * <li>the delimeter length is zero</li>
317 * <li>the string equals the delimeter</li>
318 * <li>memory allocation fails</li>
319 * </ul>
320 *
321 * The integer referenced by <code>count</code> is used as input and determines
322 * the maximum size of the resulting array, i.e. the maximum count of splits to
323 * perform + 1.
324 *
325 * The integer referenced by <code>count</code> is also used as output and is
326 * set to
327 * <ul>
328 * <li>-2, on memory allocation errors</li>
329 * <li>-1, if either the string or the delimiter is an empty string</li>
330 * <li>0, if the string equals the delimiter</li>
331 * <li>1, if the string does not contain the delimiter</li>
332 * <li>the count of array items, otherwise</li>
333 * </ul>
334 *
335 * If the string starts with the delimiter, the first item of the resulting
336 * array will be an empty string.
337 *
338 * If the string ends with the delimiter and the maximum list size is not
339 * exceeded, the last array item will be an empty string.
340 * In case the list size would be exceeded, the last array item will be the
341 * remaining string after the last split, <i>including</i> the terminating
342 * delimiter.
343 *
344 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
345 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
346 * an allocator to managed memory, to avoid this.
347 *
348 * @param string the string to split
349 * @param delim the delimiter string
350 * @param count IN: the maximum size of the resulting array (0 = no limit),
351 * OUT: the actual size of the array
352 * @return a sstr_t array containing the split strings or
353 * <code>NULL</code> on error
354 *
355 * @see sstrsplit_a()
356 */
357 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
359 #define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
361 /**
362 * Performing sstrsplit() using a UcxAllocator.
363 *
364 * <i>Read the description of sstrsplit() for details.</i>
365 *
366 * The memory for the sstr_t.ptr pointers of the array items and the memory for
367 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
368 * function.
369 *
370 * <b>Note:</b> the allocator is not used for memory that is freed within the
371 * same call of this function (locally scoped variables).
372 *
373 * @param allocator the UcxAllocator used for allocating memory
374 * @param string the string to split
375 * @param delim the delimiter string
376 * @param count IN: the maximum size of the resulting array (0 = no limit),
377 * OUT: the actual size of the array
378 * @return a sstr_t array containing the split strings or
379 * <code>NULL</code> on error
380 *
381 * @see sstrsplit()
382 */
383 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
384 ssize_t *count);
386 #define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
388 /**
389 * Compares two UCX strings with standard <code>memcmp()</code>.
390 *
391 * At first it compares the sstr_t.length attribute of the two strings. The
392 * <code>memcmp()</code> function is called, if and only if the lengths match.
393 *
394 * @param s1 the first string
395 * @param s2 the second string
396 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
397 * length of s1 is greater than the length of s2 or the result of
398 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
399 */
400 int ucx_str_cmp(scstr_t s1, scstr_t s2);
402 #define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2))
404 /**
405 * Compares two UCX strings ignoring the case.
406 *
407 * At first it compares the sstr_t.length attribute of the two strings. If and
408 * only if the lengths match, both strings are compared char by char ignoring
409 * the case.
410 *
411 * @param s1 the first string
412 * @param s2 the second string
413 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
414 * length of s1 is greater than the length of s2 or the difference between the
415 * first two differing characters otherwise (i.e. 0 if the strings match and
416 * no characters differ)
417 */
418 int ucx_str_casecmp(scstr_t s1, scstr_t s2);
420 #define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2))
422 /**
423 * Creates a duplicate of the specified string.
424 *
425 * The new sstr_t will contain a copy allocated by standard
426 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
427 * <code>free()</code>.
428 *
429 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
430 * terminated.
431 *
432 * @param string the string to duplicate
433 * @return a duplicate of the string
434 * @see sstrdup_a()
435 */
436 sstr_t scstrdup(scstr_t string);
438 #define sstrdup(s) scstrdup(SCSTR(s))
440 /**
441 * Creates a duplicate of the specified string using a UcxAllocator.
442 *
443 * The new sstr_t will contain a copy allocated by the allocators
444 * ucx_allocator_malloc function. So it is implementation depended, whether the
445 * returned sstr_t.ptr pointer must be passed to the allocators
446 * ucx_allocator_free function manually.
447 *
448 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
449 * terminated.
450 *
451 * @param allocator a valid instance of a UcxAllocator
452 * @param string the string to duplicate
453 * @return a duplicate of the string
454 * @see sstrdup()
455 */
456 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
458 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
461 size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
463 /**
464 * Omits leading and trailing spaces.
465 *
466 * This function returns a new sstr_t containing a trimmed version of the
467 * specified string.
468 *
469 * <b>Note:</b> the new sstr_t references the same memory, thus you
470 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
471 * <code>free()</code>. It is also highly recommended to avoid assignments like
472 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
473 * source string. Assignments of this type are only permitted, if the
474 * sstr_t.ptr of the source string does not need to be freed or if another
475 * reference to the source string exists.
476 *
477 * @param string the string that shall be trimmed
478 * @return a new sstr_t containing the trimmed string
479 */
480 sstr_t sstrtrim(sstr_t string);
482 scstr_t scstrtrim(scstr_t string);
484 /**
485 * Checks, if a string has a specific prefix.
486 * @param string the string to check
487 * @param prefix the prefix the string should have
488 * @return 1, if and only if the string has the specified prefix, 0 otherwise
489 */
490 int ucx_strprefix(scstr_t string, scstr_t prefix);
492 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
494 /**
495 * Checks, if a string has a specific suffix.
496 * @param string the string to check
497 * @param suffix the suffix the string should have
498 * @return 1, if and only if the string has the specified suffix, 0 otherwise
499 */
500 int ucx_strsuffix(scstr_t string, scstr_t suffix);
502 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
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() for the implications.
509 *
510 * @param string the input string
511 * @return the resulting lower case string
512 * @see sstrdup()
513 */
514 sstr_t ucx_strlower(scstr_t string);
516 #define sstrlower(string) ucx_strlower(SCSTR(string))
518 /**
519 * Returns a lower case version of a string.
520 *
521 * This function creates a duplicate of the input string, first. See the
522 * documentation of sstrdup_a() for the implications.
523 *
524 * @param allocator the allocator used for duplicating the string
525 * @param string the input string
526 * @return the resulting lower case string
527 * @see sstrdup_a()
528 */
529 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
531 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, 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() for the implications.
538 *
539 * @param string the input string
540 * @return the resulting upper case string
541 * @see sstrdup()
542 */
543 sstr_t ucx_strupper(scstr_t string);
545 #define sstrupper(string) ucx_strupper(SCSTR(string))
547 /**
548 * Returns a upper case version of a string.
549 *
550 * This function creates a duplicate of the input string, first. See the
551 * documentation of sstrdup_a() for the implications.
552 *
553 * @param allocator the allocator used for duplicating the string
554 * @param string the input string
555 * @return the resulting upper case string
556 * @see sstrdup_a()
557 */
558 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
560 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
562 #ifdef __cplusplus
563 }
564 #endif
566 #endif /* UCX_STRING_H */