src/string.c

Sun, 03 Nov 2019 15:35:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Nov 2019 15:35:29 +0100
changeset 362
39cef8f37eb5
parent 361
8ee9e23adbd2
child 363
8175ba2b3bcb
permissions
-rw-r--r--

adds automatic c11 detection to configure.ac

20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
3 *
259
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
10 * notice, this list of conditions and the following disclaimer.
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 100
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
251
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
29 #include "ucx/string.h"
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
30
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
31 #include "ucx/allocator.h"
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
32
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <stdlib.h>
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 68
diff changeset
34 #include <string.h>
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <stdarg.h>
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
36 #include <stdint.h>
189
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
37 #include <ctype.h>
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
361
8ee9e23adbd2 adds missing include for strncasecmp() to avoid an implicit declaration
Mike Becker <universe@uap-core.de>
parents: 319
diff changeset
39 #ifndef _WIN32
8ee9e23adbd2 adds missing include for strncasecmp() to avoid an implicit declaration
Mike Becker <universe@uap-core.de>
parents: 319
diff changeset
40 #include <strings.h> /* for strncasecmp() */
8ee9e23adbd2 adds missing include for strncasecmp() to avoid an implicit declaration
Mike Becker <universe@uap-core.de>
parents: 319
diff changeset
41 #endif /* _WIN32 */
8ee9e23adbd2 adds missing include for strncasecmp() to avoid an implicit declaration
Mike Becker <universe@uap-core.de>
parents: 319
diff changeset
42
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
43 sstr_t sstr(char *cstring) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 sstr_t string;
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
45 string.ptr = cstring;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
46 string.length = strlen(cstring);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return string;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
50 sstr_t sstrn(char *cstring, size_t length) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 sstr_t string;
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
52 string.ptr = cstring;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
53 string.length = length;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return string;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
275
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
57 scstr_t scstr(const char *cstring) {
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
58 scstr_t string;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
59 string.ptr = cstring;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
60 string.length = strlen(cstring);
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
61 return string;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
62 }
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
63
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
64 scstr_t scstrn(const char *cstring, size_t length) {
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
65 scstr_t string;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
66 string.ptr = cstring;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
67 string.length = length;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
68 return string;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
69 }
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
70
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
71
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
72 size_t scstrnlen(size_t n, ...) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 va_list ap;
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
74 va_start(ap, n);
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
75
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
76 size_t size = 0;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
78 for (size_t i = 0 ; i < n ; i++) {
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
79 scstr_t str = va_arg(ap, scstr_t);
317
ebae0e434898 overflow of sstrnlen should return SIZE_MAX instead of zero
Mike Becker <universe@uap-core.de>
parents: 316
diff changeset
80 if(SIZE_MAX - str.length < size) {
ebae0e434898 overflow of sstrnlen should return SIZE_MAX instead of zero
Mike Becker <universe@uap-core.de>
parents: 316
diff changeset
81 size = SIZE_MAX;
272
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
82 break;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
83 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 size += str.length;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
24
e04822101291 changed make clean + added dlist_clone with copy test + added va_end statements to string.c
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
86 va_end(ap);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 return size;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
91 static sstr_t sstrvcat_a(
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
92 UcxAllocator *a,
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
93 size_t count,
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
94 scstr_t s1,
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
95 va_list ap) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
96 sstr_t str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
97 str.ptr = NULL;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
98 str.length = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
99 if(count < 2) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
100 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
101 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
102
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
103 scstr_t s2 = va_arg (ap, scstr_t);
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
104
272
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
105 if(((size_t)-1) - s1.length < s2.length) {
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
106 return str;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
107 }
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
108
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
109 scstr_t *strings = (scstr_t*) calloc(count, sizeof(scstr_t));
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
110 if(!strings) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
111 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
112 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
113
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
114 // get all args and overall length
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
115 strings[0] = s1;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
116 strings[1] = s2;
272
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
117 size_t slen = s1.length + s2.length;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
118 int error = 0;
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
119 for (size_t i=2;i<count;i++) {
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
120 scstr_t s = va_arg (ap, scstr_t);
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
121 strings[i] = s;
272
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
122 if(((size_t)-1) - s.length < slen) {
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
123 error = 1;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
124 break;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
125 }
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
126 slen += s.length;
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
127 }
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
128 if(error) {
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
129 free(strings);
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
130 return str;
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
131 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
132
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
133 // create new string
272
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
134 str.ptr = (char*) almalloc(a, slen + 1);
2def28b65328 adds integer overflow checks to sstrlen and sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 270
diff changeset
135 str.length = slen;
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
136 if(!str.ptr) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
137 free(strings);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
138 str.length = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
139 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
140 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
141
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
142 // concatenate strings
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
143 size_t pos = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
144 for (size_t i=0;i<count;i++) {
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
145 scstr_t s = strings[i];
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
146 memcpy(str.ptr + pos, s.ptr, s.length);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
147 pos += s.length;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
148 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
149
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
150 str.ptr[str.length] = '\0';
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
151
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
152 free(strings);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
153
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
154 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
155 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
156
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
157 sstr_t scstrcat(size_t count, scstr_t s1, ...) {
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
158 va_list ap;
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
159 va_start(ap, s1);
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
160 sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, ap);
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
161 va_end(ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
162 return s;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
163 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
164
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
165 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...) {
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
166 va_list ap;
288
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
167 va_start(ap, s1);
6af5798342e8 makes sstrcat and sstrnlen scstr_t compatible
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
168 sstr_t s = sstrvcat_a(a, count, s1, ap);
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
169 va_end(ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
170 return s;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
171 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
172
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
173 static int ucx_substring(
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
174 size_t str_length,
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
175 size_t start,
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
176 size_t length,
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
177 size_t *newlen,
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
178 size_t *newpos)
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
179 {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
180 *newlen = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
181 *newpos = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
182
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
183 if(start > str_length) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
184 return 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
185 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
186
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
187 if(length > str_length - start) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
188 length = str_length - start;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
189 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
190 *newlen = length;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
191 *newpos = start;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
192 return 1;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
193 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
194
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
195 sstr_t sstrsubs(sstr_t s, size_t start) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 return sstrsubsl (s, start, s.length-start);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
199 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
200 size_t pos;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
201 sstr_t ret = { NULL, 0 };
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
202 if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
203 ret.ptr = s.ptr + pos;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
204 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
205 return ret;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
206 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
207
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
208 scstr_t scstrsubs(scstr_t string, size_t start) {
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
209 return scstrsubsl(string, start, string.length-start);
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
210 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
211
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
212 scstr_t scstrsubsl(scstr_t s, size_t start, size_t length) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
213 size_t pos;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
214 scstr_t ret = { NULL, 0 };
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
215 if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
216 ret.ptr = s.ptr + pos;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
217 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
218 return ret;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
219 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
220
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
221
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
222 static int ucx_strchr(const char *str, size_t length, int chr, size_t *pos) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
223 for(size_t i=0;i<length;i++) {
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
224 if(str[i] == chr) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
225 *pos = i;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
226 return 1;
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
227 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
229 return 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
230 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
231
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
232 static int ucx_strrchr(const char *str, size_t length, int chr, size_t *pos) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
233 if(length > 0) {
306
90b6d69bb499 merges constsstr branch
Mike Becker <universe@uap-core.de>
parents: 300
diff changeset
234 for(size_t i=length ; i>0 ; i--) {
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
235 if(str[i-1] == chr) {
306
90b6d69bb499 merges constsstr branch
Mike Becker <universe@uap-core.de>
parents: 300
diff changeset
236 *pos = i-1;
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
237 return 1;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
238 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
239 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
240 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
241 return 0;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
244 sstr_t sstrchr(sstr_t s, int c) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
245 size_t pos = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
246 if(ucx_strchr(s.ptr, s.length, c, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
247 return sstrsubs(s, pos);
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
248 }
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
249 return sstrn(NULL, 0);
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
250 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
251
148
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
252 sstr_t sstrrchr(sstr_t s, int c) {
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
253 size_t pos = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
254 if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
255 return sstrsubs(s, pos);
148
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
256 }
300
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
257 return sstrn(NULL, 0);
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
258 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
259
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
260 scstr_t scstrchr(scstr_t s, int c) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
261 size_t pos = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
262 if(ucx_strchr(s.ptr, s.length, c, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
263 return scstrsubs(s, pos);
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
264 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
265 return scstrn(NULL, 0);
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
266 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
267
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
268 scstr_t scstrrchr(scstr_t s, int c) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
269 size_t pos = 0;
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
270 if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
271 return scstrsubs(s, pos);
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
272 }
d1f814633049 completes conversion to scstr
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 288
diff changeset
273 return scstrn(NULL, 0);
148
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
274 }
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
275
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
276 #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
277 ((size_t*)ptable)[index] : (size_t) ((uint8_t*)ptable)[index])
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
278
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
279 #define ptable_w(useheap, ptable, index, src) do {\
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
280 if (!useheap) ((uint8_t*)ptable)[index] = (uint8_t) src;\
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
281 else ((size_t*)ptable)[index] = src;\
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
282 } while (0);
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
283
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
284
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
285 static const char* ucx_strstr(
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
286 const char *str,
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
287 size_t length,
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
288 const char *match,
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
289 size_t matchlen,
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
290 size_t *newlen)
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
291 {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
292 *newlen = length;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
293 if (matchlen == 0) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
294 return str;
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
295 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
296
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
297 const char *result = NULL;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
298 size_t resultlen = 0;
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
299
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
300 /*
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
301 * IMPORTANT:
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
302 * our prefix table contains the prefix length PLUS ONE
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
303 * this is our decision, because we want to use the full range of size_t
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
304 * the original algorithm needs a (-1) at one single place
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
305 * and we want to avoid that
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
306 */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
307
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
308 /* static prefix table */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
309 static uint8_t s_prefix_table[256];
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
310
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
311 /* check pattern length and use appropriate prefix table */
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
312 /* if the pattern exceeds static prefix table, allocate on the heap */
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
313 register int useheap = matchlen > 255;
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
314 register void* ptable = useheap ?
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
315 calloc(matchlen+1, sizeof(size_t)): s_prefix_table;
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
316
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
317 /* keep counter in registers */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
318 register size_t i, j;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
319
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
320 /* fill prefix table */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
321 i = 0; j = 0;
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
322 ptable_w(useheap, ptable, i, j);
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
323 while (i < matchlen) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
324 while (j >= 1 && match[j-1] != match[i]) {
238
27b31c2c959c fixes major bug in construction of KMP prefix table for strstr()
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
325 ptable_r(j, useheap, ptable, j-1);
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
326 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
327 i++; j++;
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
328 ptable_w(useheap, ptable, i, j);
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
329 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
330
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
331 /* search */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
332 i = 0; j = 1;
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
333 while (i < length) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
334 while (j >= 1 && str[i] != match[j-1]) {
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
335 ptable_r(j, useheap, ptable, j-1);
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
336 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
337 i++; j++;
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
338 if (j-1 == matchlen) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
339 size_t start = i - matchlen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
340 result = str + start;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
341 resultlen = length - start;
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
342 break;
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
343 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
344 }
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
345
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
346 /* if prefix table was allocated on the heap, free it */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
347 if (ptable != s_prefix_table) {
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
348 free(ptable);
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
349 }
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
350
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
351 *newlen = resultlen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
352 return result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
353 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
354
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
355 sstr_t scstrsstr(sstr_t string, scstr_t match) {
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
356 sstr_t result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
357
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
358 size_t reslen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
359 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen);
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
360 if(!resstr) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
361 result.ptr = NULL;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
362 result.length = 0;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
363 return result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
364 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
365
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
366 size_t pos = resstr - string.ptr;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
367 result.ptr = string.ptr + pos;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
368 result.length = reslen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
369
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
370 return result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
371 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
372
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
373 scstr_t scstrscstr(scstr_t string, scstr_t match) {
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
374 scstr_t result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
375
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
376 size_t reslen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
377 const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen);
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
378 if(!resstr) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
379 result.ptr = NULL;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
380 result.length = 0;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
381 return result;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
382 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
383
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
384 size_t pos = resstr - string.ptr;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
385 result.ptr = string.ptr + pos;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
386 result.length = reslen;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
387
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
388 return result;
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
389 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
390
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
391 #undef ptable_r
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
392 #undef ptable_w
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
393
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
394 sstr_t* scstrsplit(scstr_t s, scstr_t d, ssize_t *n) {
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
395 return scstrsplit_a(ucx_default_allocator(), s, d, n);
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
396 }
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
397
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
398 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t s, scstr_t d, ssize_t *n) {
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
399 if (s.length == 0 || d.length == 0) {
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
400 *n = -1;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
401 return NULL;
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
402 }
231
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
403
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
404 /* special cases: delimiter is at least as large as the string */
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
405 if (d.length >= s.length) {
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
406 /* exact match */
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
407 if (sstrcmp(s, d) == 0) {
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
408 *n = 0;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
409 return NULL;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
410 } else /* no match possible */ {
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
411 *n = 1;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
412 sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t));
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
413 if(result) {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
414 *result = sstrdup_a(allocator, s);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
415 } else {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
416 *n = -2;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
417 }
231
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
418 return result;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
419 }
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
420 }
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
421
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
422 ssize_t nmax = *n;
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
423 size_t arrlen = 16;
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
424 sstr_t* result = (sstr_t*) alcalloc(allocator, arrlen, sizeof(sstr_t));
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
425
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
426 if (result) {
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
427 scstr_t curpos = s;
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
428 ssize_t j = 1;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
429 while (1) {
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
430 scstr_t match;
234
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
431 /* optimize for one byte delimiters */
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
432 if (d.length == 1) {
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
433 match = curpos;
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
434 for (size_t i = 0 ; i < curpos.length ; i++) {
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
435 if (curpos.ptr[i] == *(d.ptr)) {
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
436 match.ptr = curpos.ptr + i;
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
437 break;
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
438 }
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
439 match.length--;
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
440 }
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
441 } else {
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
442 match = scstrscstr(curpos, d);
234
7a63b4986b5b adds an optimization for one-byte delimiters to sstrsplit (will take effect after planned reimplementation of sstrstr)
Mike Becker <universe@uap-core.de>
parents: 233
diff changeset
443 }
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
444 if (match.length > 0) {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
445 /* is this our last try? */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
446 if (nmax == 0 || j < nmax) {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
447 /* copy the current string to the array */
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
448 scstr_t item = scstrn(curpos.ptr, match.ptr - curpos.ptr);
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
449 result[j-1] = sstrdup_a(allocator, item);
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
450 size_t processed = item.length + d.length;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
451 curpos.ptr += processed;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
452 curpos.length -= processed;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
453
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
454 /* allocate memory for the next string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
455 j++;
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
456 if (j > arrlen) {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
457 arrlen *= 2;
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
458 size_t reallocsz;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
459 sstr_t* reallocated = NULL;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
460 if(!ucx_szmul(arrlen, sizeof(sstr_t), &reallocsz)) {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
461 reallocated = (sstr_t*) alrealloc(
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
462 allocator, result, reallocsz);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
463 }
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
464 if (reallocated) {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
465 result = reallocated;
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
466 } else {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
467 for (ssize_t i = 0 ; i < j-1 ; i++) {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
468 alfree(allocator, result[i].ptr);
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
469 }
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
470 alfree(allocator, result);
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
471 *n = -2;
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
472 return NULL;
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
473 }
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
474 }
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
475 } else {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
476 /* nmax reached, copy the _full_ remaining string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
477 result[j-1] = sstrdup_a(allocator, curpos);
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
478 break;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
479 }
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
480 } else {
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
481 /* no more matches, copy last string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
482 result[j-1] = sstrdup_a(allocator, curpos);
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
483 break;
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
484 }
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
485 }
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
486 *n = j;
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
487 } else {
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
488 *n = -2;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
489 }
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
490
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
491 return result;
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
492 }
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
493
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
494 int scstrcmp(scstr_t s1, scstr_t s2) {
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
495 if (s1.length == s2.length) {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
496 return memcmp(s1.ptr, s2.ptr, s1.length);
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
497 } else if (s1.length > s2.length) {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
498 return 1;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
499 } else {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
500 return -1;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
501 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
504 int scstrcasecmp(scstr_t s1, scstr_t s2) {
149
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
505 if (s1.length == s2.length) {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
506 #ifdef _WIN32
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
507 return _strnicmp(s1.ptr, s2.ptr, s1.length);
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
508 #else
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
509 return strncasecmp(s1.ptr, s2.ptr, s1.length);
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
510 #endif
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
511 } else if (s1.length > s2.length) {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
512 return 1;
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
513 } else {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
514 return -1;
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
515 }
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
516 }
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
517
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
518 sstr_t scstrdup(scstr_t s) {
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
519 return sstrdup_a(ucx_default_allocator(), s);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
520 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
522 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
523 sstr_t newstring;
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
524 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
525 if (newstring.ptr) {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
526 newstring.length = s.length;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
527 newstring.ptr[newstring.length] = 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
528
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
529 memcpy(newstring.ptr, s.ptr, s.length);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
530 } else {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
531 newstring.length = 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
532 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
533
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 return newstring;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 }
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
536
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
537
318
348fd9cb7b14 adds remaining documentation for the scstr functions
Mike Becker <universe@uap-core.de>
parents: 317
diff changeset
538 static size_t ucx_strtrim(const char *s, size_t len, size_t *newlen) {
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
539 const char *newptr = s;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
540 size_t length = len;
98
0a752853f792 fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
541
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
542 while(length > 0 && isspace(*newptr)) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
543 newptr++;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
544 length--;
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
545 }
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
546 while(length > 0 && isspace(newptr[length-1])) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
547 length--;
104
9d3dea320d8e fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
548 }
9d3dea320d8e fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
549
276
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
550 *newlen = length;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
551 return newptr - s;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
552 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
553
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
554 sstr_t sstrtrim(sstr_t string) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
555 sstr_t newstr;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
556 newstr.ptr = string.ptr
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
557 + ucx_strtrim(string.ptr, string.length, &newstr.length);
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
558 return newstr;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
559 }
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
560
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
561 scstr_t scstrtrim(scstr_t string) {
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
562 scstr_t newstr;
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
563 newstr.ptr = string.ptr
f1b2146d4805 adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
564 + ucx_strtrim(string.ptr, string.length, &newstr.length);
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
565 return newstr;
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
566 }
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
567
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
568 int scstrprefix(scstr_t string, scstr_t prefix) {
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
569 if (string.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
570 return prefix.length == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
571 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
572 if (prefix.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
573 return 1;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
574 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
575
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
576 if (prefix.length > string.length) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
577 return 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
578 } else {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
579 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
580 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
581 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
582
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
583 int scstrsuffix(scstr_t string, scstr_t suffix) {
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
584 if (string.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
585 return suffix.length == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
586 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
587 if (suffix.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
588 return 1;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
589 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
590
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
591 if (suffix.length > string.length) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
592 return 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
593 } else {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
594 return memcmp(string.ptr+string.length-suffix.length,
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
595 suffix.ptr, suffix.length) == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
596 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
597 }
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
598
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
599 sstr_t scstrlower(scstr_t string) {
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
600 sstr_t ret = sstrdup(string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
601 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
602 ret.ptr[i] = tolower(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
603 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
604 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
605 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
606
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
607 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string) {
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
608 sstr_t ret = sstrdup_a(allocator, string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
609 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
610 ret.ptr[i] = tolower(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
611 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
612 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
613 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
614
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
615 sstr_t scstrupper(scstr_t string) {
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
616 sstr_t ret = sstrdup(string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
617 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
618 ret.ptr[i] = toupper(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
619 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
620 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
621 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
622
319
0380e438a7ce unifies naming of sstr_t related and scstr_t related functions
Mike Becker <universe@uap-core.de>
parents: 318
diff changeset
623 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string) {
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
624 sstr_t ret = sstrdup_a(allocator, string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
625 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
626 ret.ptr[i] = toupper(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
627 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
628 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
629 }
275
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
630
316
be0f6bd10b52 adjusts documentation of UCX string types, converters, and constructors
Mike Becker <universe@uap-core.de>
parents: 315
diff changeset
631 // type adjustment functions
be0f6bd10b52 adjusts documentation of UCX string types, converters, and constructors
Mike Becker <universe@uap-core.de>
parents: 315
diff changeset
632 scstr_t ucx_sc2sc(scstr_t str) {
be0f6bd10b52 adjusts documentation of UCX string types, converters, and constructors
Mike Becker <universe@uap-core.de>
parents: 315
diff changeset
633 return str;
275
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
634 }
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
635 scstr_t ucx_ss2sc(sstr_t str) {
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
636 scstr_t cs;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
637 cs.ptr = str.ptr;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
638 cs.length = str.length;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
639 return cs;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
640 }
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
641 scstr_t ucx_ss2c_s(scstr_t c) {
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
642 return c;
96f643d30ff1 adds scstr_t struct for const strings and adapts some string functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 272
diff changeset
643 }

mercurial