Mon, 19 Aug 2013 10:44:11 +0200
added sstrrchr
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 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
100
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
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 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #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
|
30 | #include <string.h> |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <stdarg.h> |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include "string.h" |
109
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
34 | #include "allocator.h" |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
36 | sstr_t sstr(char *cstring) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | sstr_t string; |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
38 | string.ptr = cstring; |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
39 | string.length = strlen(cstring); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | return string; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
43 | sstr_t sstrn(char *cstring, size_t length) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | sstr_t string; |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
45 | string.ptr = cstring; |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
46 | string.length = length; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | return string; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | |
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
50 | size_t sstrnlen(size_t n, sstr_t s, ...) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | va_list ap; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | size_t size = s.length; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | va_start(ap, s); |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
55 | for (size_t i = 1 ; i < n ; i++) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | sstr_t str = va_arg(ap, sstr_t); |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | size += str.length; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | } |
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
|
59 | va_end(ap); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | return size; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
123
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
64 | sstr_t sstrncat(sstr_t s, size_t n, sstr_t c1, ...) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | va_list ap; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | va_start(ap, c1); |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | s.ptr[0] = 0; |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
68 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
69 | size_t len = s.length; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
70 | size_t cplen = c1.length > len ? len : c1.length; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
71 | char *ptr = s.ptr; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
72 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
73 | memcpy(ptr, c1.ptr, cplen); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
74 | len -= cplen; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
75 | ptr += cplen; |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
76 | for (size_t i = 1 ; i < n ; i++) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | sstr_t str = va_arg (ap, sstr_t); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
78 | cplen = str.length > len ? len : str.length; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
79 | if(cplen <= 0) { |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
80 | va_end(ap); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
81 | return s; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
82 | } |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
83 | memcpy(ptr, str.ptr, cplen); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
84 | len -= cplen; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
85 | ptr += cplen; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | } |
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
|
87 | va_end(ap); |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
88 | s.length = ptr - s.ptr; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | return s; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | |
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
93 | sstr_t sstrsubs(sstr_t s, size_t start) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | return sstrsubsl (s, start, s.length-start); |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
97 | 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
|
98 | sstr_t new_sstr; |
104
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
99 | if (start >= s.length) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | return s; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | if (length > s.length-start) { |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | length = s.length-start; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | new_sstr.ptr = &s.ptr[start]; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | new_sstr.length = length; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | return new_sstr; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | |
108
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
110 | sstr_t sstrchr(sstr_t s, int c) { |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
111 | for(size_t i=0;i<s.length;i++) { |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
112 | if(s.ptr[i] == c) { |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
113 | return sstrsubs(s, i); |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
114 | } |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
115 | } |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
116 | sstr_t n; |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
117 | n.ptr = NULL; |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
118 | n.length = 0; |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
119 | return n; |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
120 | } |
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
121 | |
148 | 122 | sstr_t sstrrchr(sstr_t s, int c) { |
123 | if (s.length > 0) { | |
124 | for(size_t i=s.length-1;i>=0;i--) { | |
125 | if(s.ptr[i] == c) { | |
126 | return sstrsubs(s, i); | |
127 | } | |
128 | } | |
129 | } | |
130 | sstr_t n; | |
131 | n.ptr = NULL; | |
132 | n.length = 0; | |
133 | return n; | |
134 | } | |
135 | ||
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
136 | sstr_t* sstrsplit(sstr_t s, sstr_t d, size_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
|
137 | 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
|
138 | } |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
139 | |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
140 | sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_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
|
141 | 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
|
142 | *n = -1; |
39 | 143 | return NULL; |
144 | } | |
145 | ||
146 | sstr_t* result; | |
147 | size_t nmax = *n; | |
148 | *n = 1; | |
149 | ||
150 | /* special case: exact match - no processing needed */ | |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
151 | if (sstrcmp(s, d) == 0) { |
71
303dabadff1c
made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
152 | *n = 0; |
303dabadff1c
made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
153 | return NULL; |
39 | 154 | } |
155 | sstr_t sv = sstrdup(s); | |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
156 | if (sv.length == 0) { |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
157 | *n = -2; |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
158 | return NULL; |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
159 | } |
39 | 160 | |
95 | 161 | for (size_t i = 0 ; i < s.length ; i++) { |
39 | 162 | if (sv.ptr[i] == d.ptr[0]) { |
163 | _Bool match = 1; | |
95 | 164 | for (size_t j = 1 ; j < d.length ; j++) { |
39 | 165 | if (j+i < s.length) { |
166 | match &= (sv.ptr[i+j] == d.ptr[j]); | |
167 | } else { | |
168 | match = 0; | |
169 | break; | |
170 | } | |
171 | } | |
172 | if (match) { | |
173 | (*n)++; | |
95 | 174 | for (size_t j = 0 ; j < d.length ; j++) { |
39 | 175 | sv.ptr[i+j] = 0; |
176 | } | |
177 | i += d.length; | |
178 | } | |
179 | } | |
180 | if ((*n) == nmax) break; | |
181 | } | |
120 | 182 | result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n)); |
39 | 183 | |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
184 | if (result) { |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
185 | char *pptr = sv.ptr; |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
186 | for (size_t i = 0 ; i < *n ; i++) { |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
187 | size_t l = strlen(pptr); |
120 | 188 | char* ptr = (char*) allocator->malloc(allocator->pool, l + 1); |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
189 | memcpy(ptr, pptr, l); |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
190 | ptr[l] = 0; |
39 | 191 | |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
192 | result[i] = sstrn(ptr, l); |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
193 | pptr += l + d.length; |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
194 | } |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
195 | } else { |
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
196 | *n = -2; |
39 | 197 | } |
119
baa839a7633f
completed documentation on sstr_t + sstrsplit overhaul + allocator version of sstrsplit
Mike Becker <universe@uap-core.de>
parents:
116
diff
changeset
|
198 | |
39 | 199 | free(sv.ptr); |
200 | ||
201 | return result; | |
202 | } | |
203 | ||
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
204 | 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
|
205 | if (s1.length == s2.length) { |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
206 | return memcmp(s1.ptr, s2.ptr, s1.length); |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
207 | } else if (s1.length > s2.length) { |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
208 | return 1; |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
209 | } else { |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
210 | return -1; |
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
109
diff
changeset
|
211 | } |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | |
68
88dbea299440
removed const from string.*
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
214 | 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
|
215 | return sstrdup_a(ucx_default_allocator(), s); |
109
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
216 | } |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
218 | 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
|
219 | sstr_t newstring; |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
220 | newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
221 | if (newstring.ptr) { |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
222 | newstring.length = s.length; |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
223 | newstring.ptr[newstring.length] = 0; |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
224 | |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
225 | memcpy(newstring.ptr, s.ptr, s.length); |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
226 | } else { |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
227 | newstring.length = 0; |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
228 | } |
75cb6590358b
added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
229 | |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230 | return newstring; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | } |
96 | 232 | |
233 | sstr_t sstrtrim(sstr_t string) { | |
234 | sstr_t newstr = string; | |
98
0a752853f792
fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents:
97
diff
changeset
|
235 | if (string.length == 0) { |
0a752853f792
fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents:
97
diff
changeset
|
236 | return newstr; |
0a752853f792
fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents:
97
diff
changeset
|
237 | } |
0a752853f792
fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents:
97
diff
changeset
|
238 | |
97
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
96
diff
changeset
|
239 | size_t i; |
96 | 240 | for(i=0;i<string.length;i++) { |
241 | char c = string.ptr[i]; | |
242 | if(c > 32) { | |
243 | break; | |
244 | } | |
245 | } | |
246 | newstr.ptr = &string.ptr[i]; | |
247 | newstr.length = string.length - i; | |
248 | ||
104
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
249 | if(newstr.length == 0) { |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
250 | return newstr; |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
251 | } |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
252 | |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
253 | i = newstr.length - 1; |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
254 | for(;;) { |
96 | 255 | char c = newstr.ptr[i]; |
256 | if(c > 32) { | |
257 | break; | |
258 | } | |
104
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
259 | if(i > 0) { |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
260 | i--; |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
261 | } else { |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
262 | break; |
9d3dea320d8e
fixed sstrtrim and some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
263 | } |
96 | 264 | } |
265 | newstr.length = i + 1; | |
266 | ||
267 | return newstr; | |
268 | } | |
146
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
269 | |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
270 | 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
|
271 | if (string.length == 0) { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
272 | return prefix.length == 0; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
273 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
274 | if (prefix.length == 0) { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
275 | return 1; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
276 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
277 | |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
278 | 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
|
279 | return 0; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
280 | } else { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
281 | 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
|
282 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
283 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
284 | |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
285 | 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
|
286 | if (string.length == 0) { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
287 | return suffix.length == 0; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
288 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
289 | if (suffix.length == 0) { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
290 | return 1; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
291 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
292 | |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
293 | 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
|
294 | return 0; |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
295 | } else { |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
296 | 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
|
297 | 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
|
298 | } |
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
125
diff
changeset
|
299 | } |