src/ucx/string.h

branch
constsstr
changeset 275
96f643d30ff1
parent 259
2f5dea574a75
child 276
f1b2146d4805
equal deleted inserted replaced
274:0923c036b913 275:96f643d30ff1
59 #define S(s) sstrn((char*)s, sizeof(s)-1) 59 #define S(s) sstrn((char*)s, sizeof(s)-1)
60 60
61 #ifdef __cplusplus 61 #ifdef __cplusplus
62 extern "C" { 62 extern "C" {
63 #endif 63 #endif
64
65 /** 64 /**
66 * The UCX string structure. 65 * The UCX string structure.
67 */ 66 */
68 typedef struct { 67 typedef struct {
69 /** A reference to the string (<b>not necessarily <code>NULL</code> 68 /** A reference to the string (<b>not necessarily <code>NULL</code>
71 char *ptr; 70 char *ptr;
72 /** The length of the string */ 71 /** The length of the string */
73 size_t length; 72 size_t length;
74 } sstr_t; 73 } sstr_t;
75 74
75 typedef struct {
76 const char *ptr;
77 size_t length;
78 } scstr_t;
79 #ifdef __cplusplus
80 }
81 #endif
82
83
84 #ifdef __cplusplus
85 inline scstr_t s2scstr(sstr_t s) {
86 scstr_t c;
87 c.ptr = s.ptr;
88 c.length = s.ptr;
89 return c;
90 }
91 inline scstr_t s2scstr(scstr_t c) {
92 return c;
93 }
94 #define SCSTR s2scstr
95 #else
96
97 scstr_t ucx_sc2sc(scstr_t c);
98 scstr_t ucx_ss2sc(sstr_t str);
99 #if __STDC_VERSION__ >= 201112L
100 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
101 #elif defined(__GNUC__) || defined(__clang__)
102 #define SCSTR(str) __builtin_choose_expr( \
103 __builtin_types_compatible_p(typeof(str), sstr_t), \
104 ucx_ss2sc, \
105 ucx_sc2sc)(str)
106 #elif defined(__sun)
107 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
108 scstr_t ucx_tmp_var_c; \
109 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
110 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
111 ucx_tmp_var_c; })
112 #else
113 scstr_t ucx_ss2c_s();
114 #define SCSTR ucx_ss2c_s
115 #endif /* C11 feature test */
116
117 #endif /* C++ */
118
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
122
123
76 /** 124 /**
77 * Creates a new sstr_t based on a C string. 125 * Creates a new sstr_t based on a C string.
78 * 126 *
79 * The length is implicitly inferred by using a call to <code>strlen()</code>. 127 * The length is implicitly inferred by using a call to <code>strlen()</code>.
80 * 128 *
101 * @see sstr() 149 * @see sstr()
102 * @see S() 150 * @see S()
103 */ 151 */
104 sstr_t sstrn(char *cstring, size_t length); 152 sstr_t sstrn(char *cstring, size_t length);
105 153
154
155 scstr_t scstr(const char *cstring);
156 scstr_t scstrn(const char *cstring, size_t length);
106 157
107 /** 158 /**
108 * Returns the cumulated length of all specified strings. 159 * Returns the cumulated length of all specified strings.
109 * 160 *
110 * At least one string must be specified. 161 * At least one string must be specified.
346 * 397 *
347 * @param string the string to duplicate 398 * @param string the string to duplicate
348 * @return a duplicate of the string 399 * @return a duplicate of the string
349 * @see sstrdup_a() 400 * @see sstrdup_a()
350 */ 401 */
351 sstr_t sstrdup(sstr_t string); 402 sstr_t scstrdup(scstr_t string);
403
404 #define sstrdup(s) scstrdup(SCSTR(s))
352 405
353 /** 406 /**
354 * Creates a duplicate of the specified string using a UcxAllocator. 407 * Creates a duplicate of the specified string using a UcxAllocator.
355 * 408 *
356 * The new sstr_t will contain a copy allocated by the allocators 409 * The new sstr_t will contain a copy allocated by the allocators
364 * @param allocator a valid instance of a UcxAllocator 417 * @param allocator a valid instance of a UcxAllocator
365 * @param string the string to duplicate 418 * @param string the string to duplicate
366 * @return a duplicate of the string 419 * @return a duplicate of the string
367 * @see sstrdup() 420 * @see sstrdup()
368 */ 421 */
369 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); 422 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
423
424 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
370 425
371 /** 426 /**
372 * Omits leading and trailing spaces. 427 * Omits leading and trailing spaces.
373 * 428 *
374 * This function returns a new sstr_t containing a trimmed version of the 429 * This function returns a new sstr_t containing a trimmed version of the
391 * Checks, if a string has a specific prefix. 446 * Checks, if a string has a specific prefix.
392 * @param string the string to check 447 * @param string the string to check
393 * @param prefix the prefix the string should have 448 * @param prefix the prefix the string should have
394 * @return 1, if and only if the string has the specified prefix, 0 otherwise 449 * @return 1, if and only if the string has the specified prefix, 0 otherwise
395 */ 450 */
396 int sstrprefix(sstr_t string, sstr_t prefix); 451 int ucx_strprefix(scstr_t string, scstr_t prefix);
452
453 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
397 454
398 /** 455 /**
399 * Checks, if a string has a specific suffix. 456 * Checks, if a string has a specific suffix.
400 * @param string the string to check 457 * @param string the string to check
401 * @param suffix the suffix the string should have 458 * @param suffix the suffix the string should have
402 * @return 1, if and only if the string has the specified suffix, 0 otherwise 459 * @return 1, if and only if the string has the specified suffix, 0 otherwise
403 */ 460 */
404 int sstrsuffix(sstr_t string, sstr_t suffix); 461 int ucx_strsuffix(scstr_t string, scstr_t suffix);
462
463 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
405 464
406 /** 465 /**
407 * Returns a lower case version of a string. 466 * Returns a lower case version of a string.
408 * 467 *
409 * This function creates a duplicate of the input string, first. See the 468 * This function creates a duplicate of the input string, first. See the
411 * 470 *
412 * @param string the input string 471 * @param string the input string
413 * @return the resulting lower case string 472 * @return the resulting lower case string
414 * @see sstrdup() 473 * @see sstrdup()
415 */ 474 */
416 sstr_t sstrlower(sstr_t string); 475 sstr_t ucx_strlower(scstr_t string);
476
477 #define sstrlower(string) ucx_strlower(SCSTR(string))
417 478
418 /** 479 /**
419 * Returns a lower case version of a string. 480 * Returns a lower case version of a string.
420 * 481 *
421 * This function creates a duplicate of the input string, first. See the 482 * This function creates a duplicate of the input string, first. See the
424 * @param allocator the allocator used for duplicating the string 485 * @param allocator the allocator used for duplicating the string
425 * @param string the input string 486 * @param string the input string
426 * @return the resulting lower case string 487 * @return the resulting lower case string
427 * @see sstrdup_a() 488 * @see sstrdup_a()
428 */ 489 */
429 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); 490 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
491
492 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
430 493
431 /** 494 /**
432 * Returns a upper case version of a string. 495 * Returns a upper case version of a string.
433 * 496 *
434 * This function creates a duplicate of the input string, first. See the 497 * This function creates a duplicate of the input string, first. See the
436 * 499 *
437 * @param string the input string 500 * @param string the input string
438 * @return the resulting upper case string 501 * @return the resulting upper case string
439 * @see sstrdup() 502 * @see sstrdup()
440 */ 503 */
441 sstr_t sstrupper(sstr_t string); 504 sstr_t ucx_strupper(scstr_t string);
505
506 #define sstrupper(string) ucx_strupper(SCSTR(string))
442 507
443 /** 508 /**
444 * Returns a upper case version of a string. 509 * Returns a upper case version of a string.
445 * 510 *
446 * This function creates a duplicate of the input string, first. See the 511 * This function creates a duplicate of the input string, first. See the
449 * @param allocator the allocator used for duplicating the string 514 * @param allocator the allocator used for duplicating the string
450 * @param string the input string 515 * @param string the input string
451 * @return the resulting upper case string 516 * @return the resulting upper case string
452 * @see sstrdup_a() 517 * @see sstrdup_a()
453 */ 518 */
454 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); 519 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
520
521 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
455 522
456 #ifdef __cplusplus 523 #ifdef __cplusplus
457 } 524 }
458 #endif 525 #endif
459 526

mercurial