universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: ucx: /home/mike/workspace/c/ucx/src/ucx/string.h Source File universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
ucx universe@390:
universe@390:
UAP Common Extensions
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390:
universe@390: universe@390:
universe@390: universe@390: universe@390:
universe@390:
universe@390:
universe@390:
string.h
universe@390:
universe@390:
universe@390: Go to the documentation of this file.
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  */
48 #ifndef UCX_STRING_H
49 #define UCX_STRING_H
50 
51 #include "ucx.h"
52 #include "allocator.h"
53 #include <stddef.h>
54 
55 /*
56  * Use this macro to disable the shortcuts if you experience macro collision.
57  */
58 #ifndef UCX_NO_SSTR_SHORTCUTS
59 
63 #define ST(s) { s, sizeof(s)-1 }
64 
66 #define S(s) sstrn(s, sizeof(s)-1)
67 
69 #define SC(s) scstrn(s, sizeof(s)-1)
70 #endif /* UCX_NO_SSTR_SHORTCUTS */
71 
72 /*
73  * Use this macro to disable the format macros.
74  */
75 #ifndef UCX_NO_SSTR_FORMAT_MACROS
76 
77 #define SFMT(s) (int) (s).length, (s).ptr
78 
80 #define PRIsstr ".*s"
81 #endif /* UCX_NO_SSTR_FORMAT_MACROS */
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
90 typedef struct {
93  char *ptr;
95  size_t length;
96 } sstr_t;
97 
101 typedef struct {
104  const char *ptr;
106  size_t length;
107 } scstr_t;
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 
114 #ifdef __cplusplus
115 
125 inline scstr_t s2scstr(sstr_t s) {
126  scstr_t c;
127  c.ptr = s.ptr;
128  c.length = s.length;
129  return c;
130 }
131 
144 inline scstr_t s2scstr(scstr_t str) {
145  return str;
146 }
147 
153 #define SCSTR(s) s2scstr(s)
154 #else
155 
169 
181 
182 #if __STDC_VERSION__ >= 201112L
183 
188 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
189 
190 #elif defined(__GNUC__) || defined(__clang__)
191 
197 #define SCSTR(str) __builtin_choose_expr( \
198  __builtin_types_compatible_p(typeof(str), sstr_t), \
199  ucx_ss2sc, \
200  ucx_sc2sc)(str)
201 
202 #elif defined(__sun)
203 
209 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
210  scstr_t ucx_tmp_var_c; \
211  ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
212  ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
213  ucx_tmp_var_c; })
214 #else /* no generics and no builtins */
215 
227 
233 #define SCSTR(str) ucx_ss2c_s(str)
234 #endif /* C11 feature test */
235 
236 #endif /* C++ */
237 
238 #ifdef __cplusplus
239 extern "C" {
240 #endif
241 
242 
258 sstr_t sstr(char *cstring);
259 
275 sstr_t sstrn(char *cstring, size_t length);
276 
290 scstr_t scstr(const char *cstring);
291 
292 
305 scstr_t scstrn(const char *cstring, size_t length);
306 
317 size_t scstrnlen(size_t count, ...);
318 
329 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
330 
345 sstr_t scstrcat(size_t count, scstr_t s1, ...);
346 
361 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
362 
380 sstr_t scstrcat_a(UcxAllocator *alloc, size_t count, scstr_t s1, ...);
381 
399 #define sstrcat_a(alloc, count, s1, ...) \
400  scstrcat_a(alloc, count, SCSTR(s1), __VA_ARGS__)
401 
416 sstr_t sstrsubs(sstr_t string, size_t start);
417 
434 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
435 
451 scstr_t scstrsubs(scstr_t string, size_t start);
452 
470 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
471 
484 sstr_t sstrchr(sstr_t string, int chr);
485 
498 sstr_t sstrrchr(sstr_t string, int chr);
499 
512 scstr_t scstrchr(scstr_t string, int chr);
513 
526 scstr_t scstrrchr(scstr_t string, int chr);
527 
543 sstr_t scstrsstr(sstr_t string, scstr_t match);
544 
560 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
561 
577 scstr_t scstrscstr(scstr_t string, scstr_t match);
578 
594 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
595 
643 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
644 
692 #define sstrsplit(string, delim, count) \
693  scstrsplit(SCSTR(string), SCSTR(delim), count)
694 
714 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
715  ssize_t *count);
716 
736 #define sstrsplit_a(allocator, string, delim, count) \
737  scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
738 
751 int scstrcmp(scstr_t s1, scstr_t s2);
752 
765 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
766 
780 int scstrcasecmp(scstr_t s1, scstr_t s2);
781 
795 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
796 
811 sstr_t scstrdup(scstr_t string);
812 
827 #define sstrdup(string) scstrdup(SCSTR(string))
828 
845 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
846 
863 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
864 
865 
883 sstr_t sstrtrim(sstr_t string);
884 
902 scstr_t scstrtrim(scstr_t string);
903 
911 int scstrprefix(scstr_t string, scstr_t prefix);
912 
920 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
921 
929 int scstrsuffix(scstr_t string, scstr_t suffix);
930 
938 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
939 
947 int scstrcaseprefix(scstr_t string, scstr_t prefix);
948 
956 #define sstrcaseprefix(string, prefix) \
957  scstrcaseprefix(SCSTR(string), SCSTR(prefix))
958 
966 int scstrcasesuffix(scstr_t string, scstr_t suffix);
967 
975 #define sstrcasesuffix(string, suffix) \
976  scstrcasesuffix(SCSTR(string), SCSTR(suffix))
977 
988 sstr_t scstrlower(scstr_t string);
989 
999 #define sstrlower(string) scstrlower(SCSTR(string))
1000 
1012 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
1013 
1014 
1025 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
1026 
1037 sstr_t scstrupper(scstr_t string);
1038 
1048 #define sstrupper(string) scstrupper(SCSTR(string))
1049 
1061 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
1062 
1073 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
1074 
1075 #ifdef __cplusplus
1076 }
1077 #endif
1078 
1079 #endif /* UCX_STRING_H */
sstr_t sstr(char *cstring)
Creates a new sstr_t based on a C string.
Definition: string.c:43
universe@390:
sstr_t scstrdup(scstr_t string)
Creates a duplicate of the specified string.
Definition: string.c:520
universe@390:
The UCX string structure.
Definition: string.h:90
universe@390:
sstr_t scstrlower(scstr_t string)
Returns a lower case version of a string.
Definition: string.c:633
universe@390:
sstr_t sstrsubsl(sstr_t string, size_t start, size_t length)
Returns a substring with the given length starting at the specified location.
Definition: string.c:201
universe@390:
scstr_t scstrtrim(scstr_t string)
Omits leading and trailing spaces.
Definition: string.c:563
universe@390:
sstr_t * scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim, ssize_t *count)
Performing scstrsplit() using a UcxAllocator.
Definition: string.c:400
universe@390:
Main UCX Header providing most common definitions.
universe@390:
scstr_t scstrrchr(scstr_t string, int chr)
Returns an immutable substring starting at the location of the last occurrence of the specified chara...
Definition: string.c:270
universe@390:
scstr_t ucx_sc2sc(scstr_t str)
One of two type adjustment functions that return an scstr_t.
Definition: string.c:666
universe@390:
int scstrprefix(scstr_t string, scstr_t prefix)
Checks, if a string has a specific prefix.
Definition: string.c:570
universe@390:
sstr_t sstrrchr(sstr_t string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character...
Definition: string.c:254
universe@390:
sstr_t sstrtrim(sstr_t string)
Omits leading and trailing spaces.
Definition: string.c:556
universe@390:
const char * ptr
A constant pointer to the immutable string (not necessarily NULL-terminated)
Definition: string.h:104
universe@390:
The UCX string structure for immutable (constant) strings.
Definition: string.h:101
universe@390:
size_t scstrnlen(size_t count,...)
Returns the accumulated length of all specified strings.
Definition: string.c:72
universe@390:
sstr_t sstrsubs(sstr_t string, size_t start)
Returns a substring starting at the specified location.
Definition: string.c:197
universe@390:
int scstrcasecmp(scstr_t s1, scstr_t s2)
Compares two UCX strings ignoring the case.
Definition: string.c:506
universe@390:
scstr_t ucx_ss2sc(sstr_t str)
One of two type adjustment functions that return an scstr_t.
Definition: string.c:669
universe@390:
int scstrcmp(scstr_t s1, scstr_t s2)
Compares two UCX strings with standard memcmp().
Definition: string.c:496
universe@390:
char * ptr
A pointer to the string (not necessarily NULL-terminated)
Definition: string.h:93
universe@390:
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
universe@390:
sstr_t * scstrsplit(scstr_t string, scstr_t delim, ssize_t *count)
Splits a string into parts by using a delimiter string.
Definition: string.c:396
universe@390:
sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string)
Returns a lower case version of a string.
Definition: string.c:641
universe@390:
size_t length
The length of the string.
Definition: string.h:95
universe@390:
sstr_t sstrchr(sstr_t string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character...
Definition: string.c:246
universe@390:
sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string)
Creates a duplicate of the specified string using a UcxAllocator.
Definition: string.c:524
universe@390:
scstr_t scstrn(const char *cstring, size_t length)
Creates a new scstr_t of the specified length based on a constant C string.
Definition: string.c:64
universe@390:
scstr_t ucx_ss2c_s()
Converts a UCX string to an immutable UCX string (scstr_t).
universe@390:
int scstrsuffix(scstr_t string, scstr_t suffix)
Checks, if a string has a specific suffix.
Definition: string.c:585
universe@390:
scstr_t scstr(const char *cstring)
Creates a new scstr_t based on a constant C string.
Definition: string.c:57
universe@390:
Allocator for custom memory management.
universe@390:
scstr_t scstrscstr(scstr_t string, scstr_t match)
Returns an immutable substring starting at the location of the first occurrence of the specified immu...
Definition: string.c:375
universe@390:
sstr_t scstrcat_a(UcxAllocator *alloc, size_t count, scstr_t s1,...)
Concatenates two or more strings using a UcxAllocator.
Definition: string.c:167
universe@390:
sstr_t scstrcat(size_t count, scstr_t s1,...)
Concatenates two or more strings.
Definition: string.c:159
universe@390:
sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string)
Returns a upper case version of a string.
Definition: string.c:657
universe@390:
sstr_t scstrupper(scstr_t string)
Returns a upper case version of a string.
Definition: string.c:649
universe@390:
scstr_t scstrsubsl(scstr_t string, size_t start, size_t length)
Returns a substring of an immutable string with a maximum length starting at the specified location...
Definition: string.c:214
universe@390:
sstr_t sstrn(char *cstring, size_t length)
Creates a new sstr_t of the specified length based on a C string.
Definition: string.c:50
universe@390:
size_t length
The length of the string.
Definition: string.h:106
universe@390:
int scstrcasesuffix(scstr_t string, scstr_t suffix)
Checks, if a string has a specific suffix, ignoring the case.
Definition: string.c:617
universe@390:
scstr_t scstrchr(scstr_t string, int chr)
Returns an immutable substring starting at the location of the first occurrence of the specified char...
Definition: string.c:262
universe@390:
scstr_t scstrsubs(scstr_t string, size_t start)
Returns a substring of an immutable string starting at the specified location.
Definition: string.c:210
universe@390:
sstr_t scstrsstr(sstr_t string, scstr_t match)
Returns a substring starting at the location of the first occurrence of the specified string...
Definition: string.c:357
universe@390:
int scstrcaseprefix(scstr_t string, scstr_t prefix)
Checks, if a string has a specific prefix, ignoring the case.
Definition: string.c:601
universe@390:
universe@390: universe@390: universe@390: universe@390: