1.1 --- a/src/ucx/string.h Sun Nov 03 15:35:29 2019 +0100 1.2 +++ b/src/ucx/string.h Sun Nov 03 16:22:46 2019 +0100 1.3 @@ -83,6 +83,7 @@ 1.4 #ifdef __cplusplus 1.5 extern "C" { 1.6 #endif 1.7 + 1.8 /** 1.9 * The UCX string structure. 1.10 */ 1.11 @@ -112,7 +113,7 @@ 1.12 1.13 #ifdef __cplusplus 1.14 /** 1.15 - * One of two type adjustment functions that return a scstr_t. 1.16 + * One of two type adjustment functions that return an scstr_t. 1.17 * 1.18 * Used <b>internally</b> to convert a UCX string to an immutable UCX string. 1.19 * 1.20 @@ -129,7 +130,7 @@ 1.21 } 1.22 1.23 /** 1.24 - * One of two type adjustment functions that return a scstr_t. 1.25 + * One of two type adjustment functions that return an scstr_t. 1.26 * 1.27 * Used <b>internally</b> to convert a UCX string to an immutable UCX string. 1.28 * This variant is used, when the string is already immutable and no operation 1.29 @@ -147,13 +148,13 @@ 1.30 /** 1.31 * Converts a UCX string to an immutable UCX string (scstr_t). 1.32 * @param str some UCX string 1.33 - * @return the an immutable version of the provided string 1.34 + * @return an immutable version of the provided string 1.35 */ 1.36 #define SCSTR(s) s2scstr(s) 1.37 #else 1.38 1.39 /** 1.40 - * One of two type adjustment functions that return a scstr_t. 1.41 + * One of two type adjustment functions that return an scstr_t. 1.42 * 1.43 * Used <b>internally</b> to convert a UCX string to an immutable UCX string. 1.44 * This variant is used, when the string is already immutable and no operation 1.45 @@ -167,7 +168,7 @@ 1.46 scstr_t ucx_sc2sc(scstr_t str); 1.47 1.48 /** 1.49 - * One of two type adjustment functions that return a scstr_t. 1.50 + * One of two type adjustment functions that return an scstr_t. 1.51 * 1.52 * Used <b>internally</b> to convert a UCX string to an immutable UCX string. 1.53 * 1.54 @@ -182,7 +183,7 @@ 1.55 /** 1.56 * Converts a UCX string to an immutable UCX string (scstr_t). 1.57 * @param str some UCX string 1.58 - * @return the an immutable version of the provided string 1.59 + * @return an immutable version of the provided string 1.60 */ 1.61 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str) 1.62 1.63 @@ -191,7 +192,7 @@ 1.64 /** 1.65 * Converts a UCX string to an immutable UCX string (scstr_t). 1.66 * @param str some UCX string 1.67 - * @return the an immutable version of the provided string 1.68 + * @return an immutable version of the provided string 1.69 */ 1.70 #define SCSTR(str) __builtin_choose_expr( \ 1.71 __builtin_types_compatible_p(typeof(str), sstr_t), \ 1.72 @@ -244,8 +245,8 @@ 1.73 * 1.74 * The length is implicitly inferred by using a call to <code>strlen()</code>. 1.75 * 1.76 - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 1.77 - * do want a copy, use sstrdup() on the return value of this function. 1.78 + * <b>Note:</b> the sstr_t will share the specified pointer to the C string. 1.79 + * If you do want a copy, use sstrdup() on the return value of this function. 1.80 * 1.81 * If you need to wrap a constant string, use scstr(). 1.82 * 1.83 @@ -259,8 +260,8 @@ 1.84 /** 1.85 * Creates a new sstr_t of the specified length based on a C string. 1.86 * 1.87 - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 1.88 - * do want a copy, use sstrdup() on the return value of this function. 1.89 + * <b>Note:</b> the sstr_t will share the specified pointer to the C string. 1.90 + * If you do want a copy, use sstrdup() on the return value of this function. 1.91 * 1.92 * If you need to wrap a constant string, use scstrn(). 1.93 * 1.94 @@ -278,8 +279,8 @@ 1.95 * 1.96 * The length is implicitly inferred by using a call to <code>strlen()</code>. 1.97 * 1.98 - * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you 1.99 - * do want a copy, use scstrdup() on the return value of this function. 1.100 + * <b>Note:</b> the scstr_t will share the specified pointer to the C string. 1.101 + * If you do want a copy, use scstrdup() on the return value of this function. 1.102 * 1.103 * @param cstring the C string to wrap 1.104 * @return a new scstr_t containing the C string 1.105 @@ -292,9 +293,8 @@ 1.106 /** 1.107 * Creates a new scstr_t of the specified length based on a constant C string. 1.108 * 1.109 - * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you 1.110 - * do want a copy, use scstrdup() on the return value of this function. 1.111 - * 1.112 + * <b>Note:</b> the scstr_t will share the specified pointer to the C string. 1.113 + * If you do want a copy, use scstrdup() on the return value of this function. * 1.114 * 1.115 * @param cstring the C string to wrap 1.116 * @param length the length of the string 1.117 @@ -305,21 +305,24 @@ 1.118 scstr_t scstrn(const char *cstring, size_t length); 1.119 1.120 /** 1.121 - * Returns the cumulated length of all specified strings. 1.122 + * Returns the accumulated length of all specified strings. 1.123 * 1.124 - * <b>Attention:</b> if the count argument does not match the count of the 1.125 + * <b>Attention:</b> if the count argument is larger than the count of the 1.126 * specified strings, the behavior is undefined. 1.127 * 1.128 - * @param count the total number of specified strings (so at least 1) 1.129 + * @param count the total number of specified strings 1.130 * @param ... all strings 1.131 - * @return the cumulated length of all strings 1.132 + * @return the accumulated length of all strings 1.133 */ 1.134 size_t scstrnlen(size_t count, ...); 1.135 1.136 /** 1.137 - * Alias for scstrnlen() which automatically converts the arguments. 1.138 + * Returns the accumulated length of all specified strings. 1.139 * 1.140 - * @param count the total number of specified strings (so at least 1) 1.141 + * <b>Attention:</b> if the count argument is larger than the count of the 1.142 + * specified strings, the behavior is undefined. 1.143 + * 1.144 + * @param count the total number of specified strings 1.145 * @param ... all strings 1.146 * @return the cumulated length of all strings 1.147 */ 1.148 @@ -342,7 +345,13 @@ 1.149 sstr_t scstrcat(size_t count, scstr_t s1, ...); 1.150 1.151 /** 1.152 - * Alias for scstrcat() which automatically converts the arguments. 1.153 + * Concatenates two or more strings. 1.154 + * 1.155 + * The resulting string will be allocated by standard <code>malloc()</code>. 1.156 + * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>. 1.157 + * 1.158 + * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 1.159 + * terminated. 1.160 * 1.161 * @param count the total number of strings to concatenate 1.162 * @param s1 first string 1.163 @@ -354,35 +363,47 @@ 1.164 /** 1.165 * Concatenates two or more strings using a UcxAllocator. 1.166 * 1.167 - * See scstrcat() for details. 1.168 + * The resulting string must be freed by the allocators <code>free()</code> 1.169 + * implementation. 1.170 + * 1.171 + * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 1.172 + * terminated. 1.173 * 1.174 - * @param a the allocator to use 1.175 + * @param alloc the allocator to use 1.176 * @param count the total number of strings to concatenate 1.177 * @param s1 first string 1.178 * @param ... all remaining strings 1.179 * @return the concatenated string 1.180 + * 1.181 + * @see scstrcat() 1.182 */ 1.183 -sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...); 1.184 +sstr_t scstrcat_a(UcxAllocator *alloc, size_t count, scstr_t s1, ...); 1.185 1.186 /** 1.187 - * Alias for scstrcat_a() which automatically converts the arguments. 1.188 + * Concatenates two or more strings using a UcxAllocator. 1.189 * 1.190 - * See sstrcat() for details. 1.191 + * The resulting string must be freed by the allocators <code>free()</code> 1.192 + * implementation. 1.193 + * 1.194 + * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 1.195 + * terminated. 1.196 * 1.197 - * @param a the allocator to use 1.198 + * @param alloc the allocator to use 1.199 * @param count the total number of strings to concatenate 1.200 * @param s1 first string 1.201 * @param ... all remaining strings 1.202 * @return the concatenated string 1.203 + * 1.204 + * @see sstrcat() 1.205 */ 1.206 -#define sstrcat_a(a, count, s1, ...) \ 1.207 - scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__) 1.208 +#define sstrcat_a(alloc, count, s1, ...) \ 1.209 + scstrcat_a(alloc, count, SCSTR(s1), __VA_ARGS__) 1.210 1.211 /** 1.212 * Returns a substring starting at the specified location. 1.213 * 1.214 * <b>Attention:</b> the new string references the same memory area as the 1.215 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 1.216 + * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated. 1.217 * Use sstrdup() to get a copy. 1.218 * 1.219 * @param string input string 1.220 @@ -395,10 +416,10 @@ 1.221 sstr_t sstrsubs(sstr_t string, size_t start); 1.222 1.223 /** 1.224 - * Returns a substring with a maximum length starting at the specified location. 1.225 + * Returns a substring with the given length starting at the specified location. 1.226 * 1.227 * <b>Attention:</b> the new string references the same memory area as the 1.228 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 1.229 + * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated. 1.230 * Use sstrdup() to get a copy. 1.231 * 1.232 * @param string input string 1.233 @@ -417,7 +438,7 @@ 1.234 * location. 1.235 * 1.236 * <b>Attention:</b> the new string references the same memory area as the 1.237 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 1.238 +* input string and is <b>NOT</b> required to be <code>NULL</code>-terminated. 1.239 * Use scstrdup() to get a copy. 1.240 * 1.241 * @param string input string 1.242 @@ -434,7 +455,7 @@ 1.243 * at the specified location. 1.244 * 1.245 * <b>Attention:</b> the new string references the same memory area as the 1.246 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 1.247 + * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated. 1.248 * Use scstrdup() to get a copy. 1.249 * 1.250 * @param string input string 1.251 @@ -522,7 +543,13 @@ 1.252 sstr_t scstrsstr(sstr_t string, scstr_t match); 1.253 1.254 /** 1.255 - * Alias for scstrsstr() which automatically converts the match string. 1.256 + * Returns a substring starting at the location of the first occurrence of the 1.257 + * specified string. 1.258 + * 1.259 + * If the string does not contain the other string, an empty string is returned. 1.260 + * 1.261 + * If <code>match</code> is an empty string, the complete <code>string</code> is 1.262 + * returned. 1.263 * 1.264 * @param string the string to be scanned 1.265 * @param match string containing the sequence of characters to match 1.266 @@ -550,7 +577,13 @@ 1.267 scstr_t scstrscstr(scstr_t string, scstr_t match); 1.268 1.269 /** 1.270 - * Alias for scstrscstr() which automatically converts the match string. 1.271 + * Returns an immutable substring starting at the location of the 1.272 + * first occurrence of the specified immutable string. 1.273 + * 1.274 + * If the string does not contain the other string, an empty string is returned. 1.275 + * 1.276 + * If <code>match</code> is an empty string, the complete <code>string</code> is 1.277 + * returned. 1.278 * 1.279 * @param string the string to be scanned 1.280 * @param match string containing the sequence of characters to match 1.281 @@ -595,6 +628,55 @@ 1.282 * delimiter. 1.283 * 1.284 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array 1.285 + * items must be manually passed to <code>free()</code>. Use scstrsplit_a() with 1.286 + * an allocator to managed memory, to avoid this. 1.287 + * 1.288 + * @param string the string to split 1.289 + * @param delim the delimiter string 1.290 + * @param count IN: the maximum size of the resulting array (0 = no limit), 1.291 + * OUT: the actual size of the array 1.292 + * @return a sstr_t array containing the split strings or 1.293 + * <code>NULL</code> on error 1.294 + * 1.295 + * @see scstrsplit_a() 1.296 + */ 1.297 +sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count); 1.298 + 1.299 +/** 1.300 + * Splits a string into parts by using a delimiter string. 1.301 + * 1.302 + * This function will return <code>NULL</code>, if one of the following happens: 1.303 + * <ul> 1.304 + * <li>the string length is zero</li> 1.305 + * <li>the delimeter length is zero</li> 1.306 + * <li>the string equals the delimeter</li> 1.307 + * <li>memory allocation fails</li> 1.308 + * </ul> 1.309 + * 1.310 + * The integer referenced by <code>count</code> is used as input and determines 1.311 + * the maximum size of the resulting array, i.e. the maximum count of splits to 1.312 + * perform + 1. 1.313 + * 1.314 + * The integer referenced by <code>count</code> is also used as output and is 1.315 + * set to 1.316 + * <ul> 1.317 + * <li>-2, on memory allocation errors</li> 1.318 + * <li>-1, if either the string or the delimiter is an empty string</li> 1.319 + * <li>0, if the string equals the delimiter</li> 1.320 + * <li>1, if the string does not contain the delimiter</li> 1.321 + * <li>the count of array items, otherwise</li> 1.322 + * </ul> 1.323 + * 1.324 + * If the string starts with the delimiter, the first item of the resulting 1.325 + * array will be an empty string. 1.326 + * 1.327 + * If the string ends with the delimiter and the maximum list size is not 1.328 + * exceeded, the last array item will be an empty string. 1.329 + * In case the list size would be exceeded, the last array item will be the 1.330 + * remaining string after the last split, <i>including</i> the terminating 1.331 + * delimiter. 1.332 + * 1.333 + * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array 1.334 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with 1.335 * an allocator to managed memory, to avoid this. 1.336 * 1.337 @@ -605,20 +687,6 @@ 1.338 * @return a sstr_t array containing the split strings or 1.339 * <code>NULL</code> on error 1.340 * 1.341 - * @see scstrsplit_a() 1.342 - */ 1.343 -sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count); 1.344 - 1.345 -/** 1.346 - * Alias for scstrsplit() which automatically converts the arguments. 1.347 - * 1.348 - * @param string the string to split 1.349 - * @param delim the delimiter string 1.350 - * @param count IN: the maximum size of the resulting array (0 = no limit), 1.351 - * OUT: the actual size of the array 1.352 - * @return a sstr_t array containing the split strings or 1.353 - * <code>NULL</code> on error 1.354 - * 1.355 * @see sstrsplit_a() 1.356 */ 1.357 #define sstrsplit(string, delim, count) \ 1.358 @@ -633,9 +701,6 @@ 1.359 * the sstr_t array itself are allocated by using the UcxAllocator.malloc() 1.360 * function. 1.361 * 1.362 - * <b>Note:</b> the allocator is not used for memory that is freed within the 1.363 - * same call of this function (locally scoped variables). 1.364 - * 1.365 * @param allocator the UcxAllocator used for allocating memory 1.366 * @param string the string to split 1.367 * @param delim the delimiter string 1.368 @@ -650,7 +715,13 @@ 1.369 ssize_t *count); 1.370 1.371 /** 1.372 - * Alias for scstrsplit_a() which automatically converts the arguments. 1.373 + * Performing sstrsplit() using a UcxAllocator. 1.374 + * 1.375 + * <i>Read the description of sstrsplit() for details.</i> 1.376 + * 1.377 + * The memory for the sstr_t.ptr pointers of the array items and the memory for 1.378 + * the sstr_t array itself are allocated by using the UcxAllocator.malloc() 1.379 + * function. 1.380 * 1.381 * @param allocator the UcxAllocator used for allocating memory 1.382 * @param string the string to split 1.383 @@ -680,7 +751,10 @@ 1.384 int scstrcmp(scstr_t s1, scstr_t s2); 1.385 1.386 /** 1.387 - * Alias for scstrcmp() which automatically converts its arguments. 1.388 + * Compares two UCX strings with standard <code>memcmp()</code>. 1.389 + * 1.390 + * At first it compares the sstr_t.length attribute of the two strings. The 1.391 + * <code>memcmp()</code> function is called, if and only if the lengths match. 1.392 * 1.393 * @param s1 the first string 1.394 * @param s2 the second string 1.395 @@ -706,7 +780,11 @@ 1.396 int scstrcasecmp(scstr_t s1, scstr_t s2); 1.397 1.398 /** 1.399 - * Alias for scstrcasecmp() which automatically converts the arguments. 1.400 + * Compares two UCX strings ignoring the case. 1.401 + * 1.402 + * At first it compares the sstr_t.length attribute of the two strings. If and 1.403 + * only if the lengths match, both strings are compared char by char ignoring 1.404 + * the case. 1.405 * 1.406 * @param s1 the first string 1.407 * @param s2 the second string 1.408 @@ -733,7 +811,14 @@ 1.409 sstr_t scstrdup(scstr_t string); 1.410 1.411 /** 1.412 - * Alias for scstrdup() which automatically converts the argument. 1.413 + * Creates a duplicate of the specified string. 1.414 + * 1.415 + * The new sstr_t will contain a copy allocated by standard 1.416 + * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to 1.417 + * <code>free()</code>. 1.418 + * 1.419 + * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 1.420 + * terminated, regardless of the argument. 1.421 * 1.422 * @param string the string to duplicate 1.423 * @return a duplicate of the string 1.424 @@ -760,7 +845,15 @@ 1.425 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string); 1.426 1.427 /** 1.428 - * Alias for scstrdup_a() which automatically converts the argument. 1.429 + * Creates a duplicate of the specified string using a UcxAllocator. 1.430 + * 1.431 + * The new sstr_t will contain a copy allocated by the allocators 1.432 + * UcxAllocator.malloc() function. So it is implementation depended, whether the 1.433 + * returned sstr_t.ptr pointer must be passed to the allocators 1.434 + * UcxAllocator.free() function manually. 1.435 + * 1.436 + * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 1.437 + * terminated, regardless of the argument. 1.438 * 1.439 * @param allocator a valid instance of a UcxAllocator 1.440 * @param string the string to duplicate 1.441 @@ -810,6 +903,7 @@ 1.442 1.443 /** 1.444 * Checks, if a string has a specific prefix. 1.445 + * 1.446 * @param string the string to check 1.447 * @param prefix the prefix the string should have 1.448 * @return 1, if and only if the string has the specified prefix, 0 otherwise 1.449 @@ -817,7 +911,7 @@ 1.450 int scstrprefix(scstr_t string, scstr_t prefix); 1.451 1.452 /** 1.453 - * Alias for scstrprefix() which automatically converts the arguments. 1.454 + * Checks, if a string has a specific prefix. 1.455 * 1.456 * @param string the string to check 1.457 * @param prefix the prefix the string should have 1.458 @@ -827,6 +921,7 @@ 1.459 1.460 /** 1.461 * Checks, if a string has a specific suffix. 1.462 + * 1.463 * @param string the string to check 1.464 * @param suffix the suffix the string should have 1.465 * @return 1, if and only if the string has the specified suffix, 0 otherwise 1.466 @@ -834,7 +929,7 @@ 1.467 int scstrsuffix(scstr_t string, scstr_t suffix); 1.468 1.469 /** 1.470 - * Alias for scstrsuffix() which automatically converts the arguments. 1.471 + * Checks, if a string has a specific suffix. 1.472 * 1.473 * @param string the string to check 1.474 * @param suffix the suffix the string should have 1.475 @@ -845,8 +940,8 @@ 1.476 /** 1.477 * Returns a lower case version of a string. 1.478 * 1.479 - * This function creates a duplicate of the input string, first. See the 1.480 - * documentation of scstrdup() for the implications. 1.481 + * This function creates a duplicate of the input string, first 1.482 + * (see scstrdup()). 1.483 * 1.484 * @param string the input string 1.485 * @return the resulting lower case string 1.486 @@ -855,7 +950,10 @@ 1.487 sstr_t scstrlower(scstr_t string); 1.488 1.489 /** 1.490 - * Alias for scstrlower() which automatically converts the argument. 1.491 + * Returns a lower case version of a string. 1.492 + * 1.493 + * This function creates a duplicate of the input string, first 1.494 + * (see sstrdup()). 1.495 * 1.496 * @param string the input string 1.497 * @return the resulting lower case string 1.498 @@ -865,8 +963,8 @@ 1.499 /** 1.500 * Returns a lower case version of a string. 1.501 * 1.502 - * This function creates a duplicate of the input string, first. See the 1.503 - * documentation of scstrdup_a() for the implications. 1.504 + * This function creates a duplicate of the input string, first 1.505 + * (see scstrdup_a()). 1.506 * 1.507 * @param allocator the allocator used for duplicating the string 1.508 * @param string the input string 1.509 @@ -877,7 +975,10 @@ 1.510 1.511 1.512 /** 1.513 - * Alias for scstrlower_a() which automatically converts the argument. 1.514 + * Returns a lower case version of a string. 1.515 + * 1.516 + * This function creates a duplicate of the input string, first 1.517 + * (see sstrdup_a()). 1.518 * 1.519 * @param allocator the allocator used for duplicating the string 1.520 * @param string the input string 1.521 @@ -888,8 +989,8 @@ 1.522 /** 1.523 * Returns a upper case version of a string. 1.524 * 1.525 - * This function creates a duplicate of the input string, first. See the 1.526 - * documentation of scstrdup() for the implications. 1.527 + * This function creates a duplicate of the input string, first 1.528 + * (see scstrdup()). 1.529 * 1.530 * @param string the input string 1.531 * @return the resulting upper case string 1.532 @@ -898,7 +999,10 @@ 1.533 sstr_t scstrupper(scstr_t string); 1.534 1.535 /** 1.536 - * Alias for scstrupper() which automatically converts the argument. 1.537 + * Returns a upper case version of a string. 1.538 + * 1.539 + * This function creates a duplicate of the input string, first 1.540 + * (see sstrdup()). 1.541 * 1.542 * @param string the input string 1.543 * @return the resulting upper case string 1.544 @@ -908,8 +1012,8 @@ 1.545 /** 1.546 * Returns a upper case version of a string. 1.547 * 1.548 - * This function creates a duplicate of the input string, first. See the 1.549 - * documentation of scstrdup_a() for the implications. 1.550 + * This function creates a duplicate of the input string, first 1.551 + * (see scstrdup_a()). 1.552 * 1.553 * @param allocator the allocator used for duplicating the string 1.554 * @param string the input string 1.555 @@ -919,7 +1023,10 @@ 1.556 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string); 1.557 1.558 /** 1.559 - * Alias for scstrupper_a() which automatically converts the argument. 1.560 + * Returns a upper case version of a string. 1.561 + * 1.562 + * This function creates a duplicate of the input string, first 1.563 + * (see sstrdup_a()). 1.564 * 1.565 * @param allocator the allocator used for duplicating the string 1.566 * @param string the input string