src/string.c

Sun, 21 Jan 2018 10:14:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 21 Jan 2018 10:14:47 +0100
changeset 271
47b8ea435902
parent 270
3d80d425543b
child 272
2def28b65328
permissions
-rw-r--r--

makes default_allocator static

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
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
39 sstr_t sstr(char *cstring) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 sstr_t string;
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
41 string.ptr = cstring;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
42 string.length = strlen(cstring);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 return string;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
46 sstr_t sstrn(char *cstring, size_t length) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 sstr_t string;
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
48 string.ptr = cstring;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
49 string.length = length;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 return string;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
53 size_t sstrnlen(size_t n, sstr_t s, ...) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 va_list ap;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 size_t size = s.length;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 va_start(ap, s);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
58 for (size_t i = 1 ; i < n ; i++) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 sstr_t str = va_arg(ap, sstr_t);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 size += str.length;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 }
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
62 va_end(ap);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 return size;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
67 static sstr_t sstrvcat_a(
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
68 UcxAllocator *a,
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
69 size_t count,
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
70 sstr_t s1,
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
71 sstr_t s2,
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
72 va_list ap) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
73 sstr_t str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
74 str.ptr = NULL;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
75 str.length = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
76 if(count < 2) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
77 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
78 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
79
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
80 sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t));
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
81 if(!strings) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
82 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
83 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
84
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
85 // get all args and overall length
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
86 strings[0] = s1;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
87 strings[1] = s2;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
88 size_t strlen = s1.length + s2.length;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
89 for (size_t i=2;i<count;i++) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
90 sstr_t s = va_arg (ap, sstr_t);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
91 strings[i] = s;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
92 strlen += s.length;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
93 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
94
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
95 // create new string
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
96 str.ptr = (char*) almalloc(a, strlen + 1);
180
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
97 str.length = strlen;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
98 if(!str.ptr) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
99 free(strings);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
100 str.length = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
101 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
102 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
103
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
104 // concatenate strings
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
105 size_t pos = 0;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
106 for (size_t i=0;i<count;i++) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
107 sstr_t s = strings[i];
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
108 memcpy(str.ptr + pos, s.ptr, s.length);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
109 pos += s.length;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
110 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
111
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
112 str.ptr[str.length] = '\0';
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 free(strings);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
115
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
116 return str;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
117 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
118
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
119 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
120 va_list ap;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
121 va_start(ap, s2);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
122 sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
123 va_end(ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
124 return s;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
125 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
126
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
127 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) {
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
128 va_list ap;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
129 va_start(ap, s2);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
130 sstr_t s = sstrvcat_a(a, count, s1, s2, ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
131 va_end(ap);
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
132 return s;
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
133 }
2185f19dcc45 added new sstrcat
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
134
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
135 sstr_t sstrsubs(sstr_t s, size_t start) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 return sstrsubsl (s, start, s.length-start);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
139 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 sstr_t new_sstr;
104
9d3dea320d8e fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
141 if (start >= s.length) {
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
142 new_sstr.ptr = NULL;
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
143 new_sstr.length = 0;
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
144 } else {
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
145 if (length > s.length-start) {
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
146 length = s.length-start;
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
147 }
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
148 new_sstr.ptr = &s.ptr[start];
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
149 new_sstr.length = length;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 return new_sstr;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
154 sstr_t sstrchr(sstr_t s, int c) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
155 for(size_t i=0;i<s.length;i++) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
156 if(s.ptr[i] == c) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
157 return sstrsubs(s, i);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
158 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
159 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
160 sstr_t n;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
161 n.ptr = NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
162 n.length = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
163 return n;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
164 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
165
148
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
166 sstr_t sstrrchr(sstr_t s, int c) {
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
167 if (s.length > 0) {
152
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
168 for(size_t i=s.length;i>0;i--) {
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
169 if(s.ptr[i-1] == c) {
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
170 return sstrsubs(s, i-1);
148
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
171 }
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
172 }
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
173 }
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
174 sstr_t n;
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
175 n.ptr = NULL;
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
176 n.length = 0;
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
177 return n;
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
178 }
c27c2425c0b1 added sstrrchr
Mike Becker <universe@uap-core.de>
parents: 146
diff changeset
179
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
180 #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
181 ((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
182
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
183 #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
184 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
185 else ((size_t*)ptable)[index] = src;\
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
186 } while (0);
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
187
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
188 sstr_t sstrstr(sstr_t string, sstr_t match) {
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
189 if (match.length == 0) {
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
190 return string;
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
191 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
192
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
193 /* prepare default return value in case of no match */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
194 sstr_t result = sstrn(NULL, 0);
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
195
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
196 /*
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
197 * IMPORTANT:
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
198 * 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
199 * 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
200 * 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
201 * 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
202 */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
203
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
204 /* static prefix table */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
205 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
206
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
207 /* 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
208 /* if the pattern exceeds static prefix table, allocate on the heap */
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
209 register int useheap = match.length > 255;
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
210 register void* ptable = useheap ?
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
211 calloc(match.length+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
212
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
213 /* keep counter in registers */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
214 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
215
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
216 /* fill prefix table */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
217 i = 0; j = 0;
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
218 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
219 while (i < match.length) {
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
220 while (j >= 1 && match.ptr[j-1] != match.ptr[i]) {
238
27b31c2c959c fixes major bug in construction of KMP prefix table for strstr()
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
221 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
222 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
223 i++; j++;
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
224 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
225 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
226
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
227 /* search */
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
228 i = 0; j = 1;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
229 while (i < string.length) {
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
230 while (j >= 1 && string.ptr[i] != match.ptr[j-1]) {
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
231 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
232 }
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
233 i++; j++;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
234 if (j-1 == match.length) {
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
235 size_t start = i - match.length;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
236 result.ptr = string.ptr + start;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
237 result.length = string.length - start;
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
238 break;
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
239 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
240 }
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
241
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
242 /* 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
243 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
244 free(ptable);
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
245 }
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
246
236
ffc6d0910342 improves sstrstr function by using KMP string search algorithm
Mike Becker <universe@uap-core.de>
parents: 235
diff changeset
247 return result;
214
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
248 }
2bc19726c340 added sstrstr() function + improved sstrsplit variants by using sstrprefix()
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
249
237
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
250 #undef ptable_r
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
251 #undef ptable_w
5ba9de6361ff further performance tweaks to sstrstr() function
Mike Becker <universe@uap-core.de>
parents: 236
diff changeset
252
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
253 sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) {
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
254 return sstrsplit_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
255 }
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
256
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
257 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_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
258 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
259 *n = -1;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
260 return NULL;
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
261 }
231
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
262
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
263 /* 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
264 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
265 /* exact match */
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
266 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
267 *n = 0;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
268 return NULL;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
269 } 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
270 *n = 1;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
271 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
272 if(result) {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
273 *result = sstrdup_a(allocator, s);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
274 } else {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
275 *n = -2;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
276 }
231
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
277 return result;
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
278 }
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
279 }
35490eb48214 improves handling of special cases delimiter size >= string size in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
280
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
281 ssize_t nmax = *n;
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
282 size_t arrlen = 16;
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
283 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
284
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
285 if (result) {
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
286 sstr_t curpos = s;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
287 ssize_t j = 1;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
288 while (1) {
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
289 sstr_t match;
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
290 /* 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
291 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
292 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
293 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
294 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
295 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
296 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
297 }
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
298 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
299 }
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
300 } else {
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
301 match = sstrstr(curpos, d);
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
302 }
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
303 if (match.length > 0) {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
304 /* is this our last try? */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
305 if (nmax == 0 || j < nmax) {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
306 /* copy the current string to the array */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
307 sstr_t item = sstrn(curpos.ptr, match.ptr - curpos.ptr);
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
308 result[j-1] = sstrdup_a(allocator, item);
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
309 size_t processed = item.length + d.length;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
310 curpos.ptr += processed;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
311 curpos.length -= processed;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
312
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
313 /* allocate memory for the next string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
314 j++;
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
315 if (j > arrlen) {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
316 arrlen *= 2;
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
317 size_t reallocsz;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
318 sstr_t* reallocated = NULL;
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
319 if(!ucx_szmul(arrlen, sizeof(sstr_t), &reallocsz)) {
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
320 reallocated = (sstr_t*) alrealloc(
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
321 allocator, result, reallocsz);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
322 }
235
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
323 if (reallocated) {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
324 result = reallocated;
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
325 } else {
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
326 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
327 alfree(allocator, result[i].ptr);
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
328 }
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
329 alfree(allocator, result);
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
330 *n = -2;
7cf1e41833a2 reduces amount of realloc calls in sstrsplit
Mike Becker <universe@uap-core.de>
parents: 234
diff changeset
331 return NULL;
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
332 }
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
333 }
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
334 } else {
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
335 /* nmax reached, copy the _full_ remaining string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
336 result[j-1] = sstrdup_a(allocator, curpos);
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
337 break;
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
338 }
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
339 } else {
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
340 /* no more matches, copy last string */
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
341 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
342 break;
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
343 }
119
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
344 }
233
bd58fdde142d reimplementation of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
345 *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
346 } else {
baa839a7633f completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents: 116
diff changeset
347 *n = -2;
39
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
348 }
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
349
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
350 return result;
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
351 }
bf8ab7bb74ff added sstrsplit function
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
352
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
353 int sstrcmp(sstr_t s1, sstr_t s2) {
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
354 if (s1.length == s2.length) {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
355 return memcmp(s1.ptr, s2.ptr, s1.length);
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
356 } else if (s1.length > s2.length) {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
357 return 1;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
358 } else {
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
359 return -1;
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
360 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
149
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
363 int sstrcasecmp(sstr_t s1, sstr_t s2) {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
364 if (s1.length == s2.length) {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
365 #ifdef _WIN32
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
366 return _strnicmp(s1.ptr, s2.ptr, s1.length);
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
367 #else
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
368 return strncasecmp(s1.ptr, s2.ptr, s1.length);
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
369 #endif
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
370 } else if (s1.length > s2.length) {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
371 return 1;
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
372 } else {
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
373 return -1;
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
374 }
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
375 }
3bf87676d42d added sstrcasecmp
Mike Becker <universe@uap-core.de>
parents: 148
diff changeset
376
68
88dbea299440 removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
377 sstr_t sstrdup(sstr_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
378 return sstrdup_a(ucx_default_allocator(), s);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
379 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
381 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
382 sstr_t newstring;
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
383 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
384 if (newstring.ptr) {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
385 newstring.length = s.length;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
386 newstring.ptr[newstring.length] = 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
387
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
388 memcpy(newstring.ptr, s.ptr, s.length);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
389 } else {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
390 newstring.length = 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
391 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
392
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 return newstring;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 }
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
395
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
396 sstr_t sstrtrim(sstr_t string) {
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
397 sstr_t newstr = string;
98
0a752853f792 fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
398
189
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
399 while (newstr.length > 0 && isspace(*newstr.ptr)) {
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
400 newstr.ptr++;
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
401 newstr.length--;
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
402 }
189
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
403 while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) {
f43bbd33fec0 sstrtrim now uses isspace()
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
404 newstr.length--;
104
9d3dea320d8e fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
405 }
9d3dea320d8e fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
406
96
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
407 return newstr;
fbbff331beba added sstrtrim
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 95
diff changeset
408 }
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
409
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
410 int sstrprefix(sstr_t string, sstr_t prefix) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
411 if (string.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
412 return prefix.length == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
413 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
414 if (prefix.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
415 return 1;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
416 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
417
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
418 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
419 return 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
420 } else {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
421 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
422 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
423 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
424
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
425 int sstrsuffix(sstr_t string, sstr_t suffix) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
426 if (string.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
427 return suffix.length == 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
428 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
429 if (suffix.length == 0) {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
430 return 1;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
431 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
432
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
433 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
434 return 0;
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
435 } else {
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
436 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
437 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
438 }
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
439 }
210
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
440
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
441 sstr_t sstrlower(sstr_t string) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
442 sstr_t ret = sstrdup(string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
443 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
444 ret.ptr[i] = tolower(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
445 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
446 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
447 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
448
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
449 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
450 sstr_t ret = sstrdup_a(allocator, string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
451 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
452 ret.ptr[i] = tolower(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
453 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
454 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
455 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
456
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
457 sstr_t sstrupper(sstr_t string) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
458 sstr_t ret = sstrdup(string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
459 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
460 ret.ptr[i] = toupper(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
461 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
462 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
463 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
464
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
465 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
466 sstr_t ret = sstrdup_a(allocator, string);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
467 for (size_t i = 0; i < ret.length ; i++) {
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
468 ret.ptr[i] = toupper(ret.ptr[i]);
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
469 }
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
470 return ret;
6bdb04d87236 added sstrlower / sstrupper variants
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
471 }

mercurial