Wed, 02 May 2018 21:45:52 +0200
adds more convenience macros for sstr
This commit also increases the UCX version number towards the next release.
- New product version: 1.1
- New library version: 2.0.1 - we are backwards, but not forward compatible.
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 /** Expands a sstr_t to printf arguments. */
62 #define SFMT(s) (int) (s).length, (s).ptr
64 /** Format specifier for a sstr_t. */
65 #define PRIsstr ".*s"
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
71 /**
72 * The UCX string structure.
73 */
74 typedef struct {
75 /** A reference to the string (<b>not necessarily <code>NULL</code>
76 * -terminated</b>) */
77 char *ptr;
78 /** The length of the string */
79 size_t length;
80 } sstr_t;
82 /**
83 * Creates a new sstr_t based on a C string.
84 *
85 * The length is implicitly inferred by using a call to <code>strlen()</code>.
86 *
87 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
88 * do want a copy, use sstrdup() on the return value of this function.
89 *
90 * @param cstring the C string to wrap
91 * @return a new sstr_t containing the C string
92 *
93 * @see sstrn()
94 */
95 sstr_t sstr(char *cstring);
97 /**
98 * Creates a new sstr_t of the specified length based on a C string.
99 *
100 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
101 * do want a copy, use sstrdup() on the return value of this function.
102 *
103 * @param cstring the C string to wrap
104 * @param length the length of the string
105 * @return a new sstr_t containing the C string
106 *
107 * @see sstr()
108 * @see S()
109 */
110 sstr_t sstrn(char *cstring, size_t length);
113 /**
114 * Returns the cumulated length of all specified strings.
115 *
116 * At least one string must be specified.
117 *
118 * <b>Attention:</b> if the count argument does not match the count of the
119 * specified strings, the behavior is undefined.
120 *
121 * @param count the total number of specified strings (so at least 1)
122 * @param string the first string
123 * @param ... all other strings
124 * @return the cumulated length of all strings
125 */
126 size_t sstrnlen(size_t count, sstr_t string, ...);
128 /**
129 * Concatenates two or more strings.
130 *
131 * The resulting string will be allocated by standard <code>malloc()</code>.
132 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
133 *
134 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
135 * terminated.
136 *
137 * @param count the total number of strings to concatenate
138 * @param s1 first string
139 * @param s2 second string
140 * @param ... all remaining strings
141 * @return the concatenated string
142 */
143 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...);
145 /**
146 * Concatenates two or more strings using a UcxAllocator.
147 *
148 * See sstrcat() for details.
149 *
150 * @param a the allocator to use
151 * @param count the total number of strings to concatenate
152 * @param s1 first string
153 * @param s2 second string
154 * @param ... all remaining strings
155 * @return the concatenated string
156 */
157 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...);
160 /**
161 * Returns a substring starting at the specified location.
162 *
163 * <b>Attention:</b> the new string references the same memory area as the
164 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
165 * Use sstrdup() to get a copy.
166 *
167 * @param string input string
168 * @param start start location of the substring
169 * @return a substring of <code>string</code> starting at <code>start</code>
170 *
171 * @see sstrsubsl()
172 * @see sstrchr()
173 */
174 sstr_t sstrsubs(sstr_t string, size_t start);
176 /**
177 * Returns a substring with a maximum length starting at the specified location.
178 *
179 * <b>Attention:</b> the new string references the same memory area as the
180 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
181 * Use sstrdup() to get a copy.
182 *
183 * @param string input string
184 * @param start start location of the substring
185 * @param length the maximum length of the substring
186 * @return a substring of <code>string</code> starting at <code>start</code>
187 * with a maximum length of <code>length</code>
188 *
189 * @see sstrsubs()
190 * @see sstrchr()
191 */
192 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
194 /**
195 * Returns a substring starting at the location of the first occurrence of the
196 * specified character.
197 *
198 * If the string does not contain the character, an empty string is returned.
199 *
200 * @param string the string where to locate the character
201 * @param chr the character to locate
202 * @return a substring starting at the first location of <code>chr</code>
203 *
204 * @see sstrsubs()
205 */
206 sstr_t sstrchr(sstr_t string, int chr);
208 /**
209 * Returns a substring starting at the location of the last occurrence of the
210 * specified character.
211 *
212 * If the string does not contain the character, an empty string is returned.
213 *
214 * @param string the string where to locate the character
215 * @param chr the character to locate
216 * @return a substring starting at the last location of <code>chr</code>
217 *
218 * @see sstrsubs()
219 */
220 sstr_t sstrrchr(sstr_t string, int chr);
222 /**
223 * Returns a substring starting at the location of the first occurrence of the
224 * specified string.
225 *
226 * If the string does not contain the other string, an empty string is returned.
227 *
228 * If <code>match</code> is an empty string, the complete <code>string</code> is
229 * returned.
230 *
231 * @param string the string to be scanned
232 * @param match string containing the sequence of characters to match
233 * @return a substring starting at the first occurrence of
234 * <code>match</code>, or an empty string, if the sequence is not
235 * present in <code>string</code>
236 */
237 sstr_t sstrstr(sstr_t string, sstr_t match);
239 /**
240 * Splits a string into parts by using a delimiter string.
241 *
242 * This function will return <code>NULL</code>, if one of the following happens:
243 * <ul>
244 * <li>the string length is zero</li>
245 * <li>the delimeter length is zero</li>
246 * <li>the string equals the delimeter</li>
247 * <li>memory allocation fails</li>
248 * </ul>
249 *
250 * The integer referenced by <code>count</code> is used as input and determines
251 * the maximum size of the resulting array, i.e. the maximum count of splits to
252 * perform + 1.
253 *
254 * The integer referenced by <code>count</code> is also used as output and is
255 * set to
256 * <ul>
257 * <li>-2, on memory allocation errors</li>
258 * <li>-1, if either the string or the delimiter is an empty string</li>
259 * <li>0, if the string equals the delimiter</li>
260 * <li>1, if the string does not contain the delimiter</li>
261 * <li>the count of array items, otherwise</li>
262 * </ul>
263 *
264 * If the string starts with the delimiter, the first item of the resulting
265 * array will be an empty string.
266 *
267 * If the string ends with the delimiter and the maximum list size is not
268 * exceeded, the last array item will be an empty string.
269 * In case the list size would be exceeded, the last array item will be the
270 * remaining string after the last split, <i>including</i> the terminating
271 * delimiter.
272 *
273 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
274 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
275 * an allocator to managed memory, to avoid this.
276 *
277 * @param string the string to split
278 * @param delim the delimiter string
279 * @param count IN: the maximum size of the resulting array (0 = no limit),
280 * OUT: the actual size of the array
281 * @return a sstr_t array containing the split strings or
282 * <code>NULL</code> on error
283 *
284 * @see sstrsplit_a()
285 */
286 sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count);
288 /**
289 * Performing sstrsplit() using a UcxAllocator.
290 *
291 * <i>Read the description of sstrsplit() for details.</i>
292 *
293 * The memory for the sstr_t.ptr pointers of the array items and the memory for
294 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
295 * function.
296 *
297 * <b>Note:</b> the allocator is not used for memory that is freed within the
298 * same call of this function (locally scoped variables).
299 *
300 * @param allocator the UcxAllocator used for allocating memory
301 * @param string the string to split
302 * @param delim the delimiter string
303 * @param count IN: the maximum size of the resulting array (0 = no limit),
304 * OUT: the actual size of the array
305 * @return a sstr_t array containing the split strings or
306 * <code>NULL</code> on error
307 *
308 * @see sstrsplit()
309 */
310 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
311 ssize_t *count);
313 /**
314 * Compares two UCX strings with standard <code>memcmp()</code>.
315 *
316 * At first it compares the sstr_t.length attribute of the two strings. The
317 * <code>memcmp()</code> function is called, if and only if the lengths match.
318 *
319 * @param s1 the first string
320 * @param s2 the second string
321 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
322 * length of s1 is greater than the length of s2 or the result of
323 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
324 */
325 int sstrcmp(sstr_t s1, sstr_t s2);
327 /**
328 * Compares two UCX strings ignoring the case.
329 *
330 * At first it compares the sstr_t.length attribute of the two strings. If and
331 * only if the lengths match, both strings are compared char by char ignoring
332 * the case.
333 *
334 * @param s1 the first string
335 * @param s2 the second string
336 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
337 * length of s1 is greater than the length of s2 or the difference between the
338 * first two differing characters otherwise (i.e. 0 if the strings match and
339 * no characters differ)
340 */
341 int sstrcasecmp(sstr_t s1, sstr_t s2);
343 /**
344 * Creates a duplicate of the specified string.
345 *
346 * The new sstr_t will contain a copy allocated by standard
347 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
348 * <code>free()</code>.
349 *
350 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
351 * terminated.
352 *
353 * @param string the string to duplicate
354 * @return a duplicate of the string
355 * @see sstrdup_a()
356 */
357 sstr_t sstrdup(sstr_t string);
359 /**
360 * Creates a duplicate of the specified string using a UcxAllocator.
361 *
362 * The new sstr_t will contain a copy allocated by the allocators
363 * ucx_allocator_malloc function. So it is implementation depended, whether the
364 * returned sstr_t.ptr pointer must be passed to the allocators
365 * ucx_allocator_free function manually.
366 *
367 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
368 * terminated.
369 *
370 * @param allocator a valid instance of a UcxAllocator
371 * @param string the string to duplicate
372 * @return a duplicate of the string
373 * @see sstrdup()
374 */
375 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
377 /**
378 * Omits leading and trailing spaces.
379 *
380 * This function returns a new sstr_t containing a trimmed version of the
381 * specified string.
382 *
383 * <b>Note:</b> the new sstr_t references the same memory, thus you
384 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
385 * <code>free()</code>. It is also highly recommended to avoid assignments like
386 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
387 * source string. Assignments of this type are only permitted, if the
388 * sstr_t.ptr of the source string does not need to be freed or if another
389 * reference to the source string exists.
390 *
391 * @param string the string that shall be trimmed
392 * @return a new sstr_t containing the trimmed string
393 */
394 sstr_t sstrtrim(sstr_t string);
396 /**
397 * Checks, if a string has a specific prefix.
398 * @param string the string to check
399 * @param prefix the prefix the string should have
400 * @return 1, if and only if the string has the specified prefix, 0 otherwise
401 */
402 int sstrprefix(sstr_t string, sstr_t prefix);
404 /**
405 * Checks, if a string has a specific suffix.
406 * @param string the string to check
407 * @param suffix the suffix the string should have
408 * @return 1, if and only if the string has the specified suffix, 0 otherwise
409 */
410 int sstrsuffix(sstr_t string, sstr_t suffix);
412 /**
413 * Returns a lower case version of a string.
414 *
415 * This function creates a duplicate of the input string, first. See the
416 * documentation of sstrdup() for the implications.
417 *
418 * @param string the input string
419 * @return the resulting lower case string
420 * @see sstrdup()
421 */
422 sstr_t sstrlower(sstr_t string);
424 /**
425 * Returns a lower case version of a string.
426 *
427 * This function creates a duplicate of the input string, first. See the
428 * documentation of sstrdup_a() for the implications.
429 *
430 * @param allocator the allocator used for duplicating the string
431 * @param string the input string
432 * @return the resulting lower case string
433 * @see sstrdup_a()
434 */
435 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
437 /**
438 * Returns a upper case version of a string.
439 *
440 * This function creates a duplicate of the input string, first. See the
441 * documentation of sstrdup() for the implications.
442 *
443 * @param string the input string
444 * @return the resulting upper case string
445 * @see sstrdup()
446 */
447 sstr_t sstrupper(sstr_t string);
449 /**
450 * Returns a upper case version of a string.
451 *
452 * This function creates a duplicate of the input string, first. See the
453 * documentation of sstrdup_a() for the implications.
454 *
455 * @param allocator the allocator used for duplicating the string
456 * @param string the input string
457 * @return the resulting upper case string
458 * @see sstrdup_a()
459 */
460 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
462 #ifdef __cplusplus
463 }
464 #endif
466 #endif /* UCX_STRING_H */