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