src/string.c

Sat, 12 May 2018 14:50:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 12 May 2018 14:50:09 +0200
changeset 298
fffe3a16a3de
parent 272
2def28b65328
child 275
96f643d30ff1
permissions
-rw-r--r--

adds a code sample for UcxMap

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

mercurial