Sat, 08 Nov 2025 23:06:11 +0100
update ucx
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
3 | * |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
5 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
8 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
15 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
| 888 | 28 | #ifdef MEMRCHR_NEED_GNU |
| 29 | #define _GNU_SOURCE | |
| 30 | #endif | |
| 31 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
32 | #include "cx/string.h" |
|
335
c1bc13faadaa
updates ucx to version 1.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
314
diff
changeset
|
33 | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include <string.h> |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <stdarg.h> |
| 852 | 36 | #include <assert.h> |
| 37 | #include <errno.h> | |
| 38 | #include <limits.h> | |
| 39 | #include <float.h> | |
| 888 | 40 | #include <ctype.h> |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
41 | |
| 852 | 42 | #ifdef _WIN32 |
| 43 | #define cx_strcasecmp_impl _strnicmp | |
| 44 | #else | |
| 45 | #include <strings.h> | |
| 46 | #define cx_strcasecmp_impl strncasecmp | |
| 47 | #endif | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
48 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
49 | cxmutstr cx_mutstr(char *cstring) { |
| 888 | 50 | return (cxmutstr) {cstring, cstring == NULL ? 0 : strlen(cstring)}; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
53 | cxmutstr cx_mutstrn( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
54 | char *cstring, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
55 | size_t length |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
56 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
57 | return (cxmutstr) {cstring, length}; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
58 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
59 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
60 | cxstring cx_str(const char *cstring) { |
| 888 | 61 | return (cxstring) {cstring, cstring == NULL ? 0 : strlen(cstring)}; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
64 | cxstring cx_strn( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
65 | const char *cstring, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
66 | size_t length |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
67 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
68 | return (cxstring) {cstring, length}; |
| 505 | 69 | } |
| 70 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
71 | void cx_strfree(cxmutstr *str) { |
| 852 | 72 | if (str == NULL) return; |
| 888 | 73 | cxFreeDefault(str->ptr); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
74 | str->ptr = NULL; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
75 | str->length = 0; |
| 505 | 76 | } |
| 77 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
78 | void cx_strfree_a( |
| 852 | 79 | const CxAllocator *alloc, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
80 | cxmutstr *str |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
81 | ) { |
| 852 | 82 | if (str == NULL) return; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
83 | cxFree(alloc, str->ptr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
84 | str->ptr = NULL; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
85 | str->length = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
86 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | |
| 888 | 88 | int cx_strcpy_a( |
| 89 | const CxAllocator *alloc, | |
| 90 | cxmutstr *dest, | |
| 91 | cxstring src | |
| 92 | ) { | |
| 93 | if (cxReallocate(alloc, &dest->ptr, src.length + 1)) { | |
| 94 | return 1; | |
| 95 | } | |
| 96 | ||
| 97 | memcpy(dest->ptr, src.ptr, src.length); | |
| 98 | dest->length = src.length; | |
| 99 | dest->ptr[dest->length] = '\0'; | |
| 100 | ||
| 101 | return 0; | |
| 102 | } | |
| 103 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
104 | size_t cx_strlen( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
105 | size_t count, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
106 | ... |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
107 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
108 | if (count == 0) return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
109 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
110 | va_list ap; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
111 | va_start(ap, count); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
112 | size_t size = 0; |
| 852 | 113 | for (size_t i = 0; i < count; i++) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
114 | cxstring str = va_arg(ap, cxstring); |
| 852 | 115 | if (size > SIZE_MAX - str.length) errno = EOVERFLOW; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | size += str.length; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | va_end(ap); |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | return size; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | |
| 748 | 123 | cxmutstr cx_strcat_ma( |
| 852 | 124 | const CxAllocator *alloc, |
| 748 | 125 | cxmutstr str, |
| 70 | 126 | size_t count, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
127 | ... |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
128 | ) { |
| 748 | 129 | if (count == 0) return str; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
130 | va_list ap; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
131 | va_start(ap, count); |
| 888 | 132 | va_list ap2; |
| 133 | va_copy(ap2, ap); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
134 | |
| 888 | 135 | // compute overall length |
| 852 | 136 | bool overflow = false; |
| 748 | 137 | size_t slen = str.length; |
| 852 | 138 | for (size_t i = 0; i < count; i++) { |
| 888 | 139 | cxstring s = va_arg(ap, cxstring); |
| 852 | 140 | if (slen > SIZE_MAX - str.length) overflow = true; |
| 505 | 141 | slen += s.length; |
| 142 | } | |
| 748 | 143 | va_end(ap); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
144 | |
| 852 | 145 | // abort in case of overflow |
| 146 | if (overflow) { | |
| 888 | 147 | va_end(ap2); |
| 852 | 148 | errno = EOVERFLOW; |
| 149 | return (cxmutstr) { NULL, 0 }; | |
| 150 | } | |
| 151 | ||
| 748 | 152 | // reallocate or create new string |
| 852 | 153 | char *newstr; |
| 748 | 154 | if (str.ptr == NULL) { |
| 852 | 155 | newstr = cxMalloc(alloc, slen + 1); |
| 748 | 156 | } else { |
| 852 | 157 | newstr = cxRealloc(alloc, str.ptr, slen + 1); |
| 748 | 158 | } |
| 852 | 159 | if (newstr == NULL) { |
| 888 | 160 | va_end(ap2); |
| 852 | 161 | return (cxmutstr) {NULL, 0}; |
| 162 | } | |
| 163 | str.ptr = newstr; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
164 | |
| 70 | 165 | // concatenate strings |
| 748 | 166 | size_t pos = str.length; |
| 167 | str.length = slen; | |
| 852 | 168 | for (size_t i = 0; i < count; i++) { |
| 888 | 169 | cxstring s = va_arg(ap2, cxstring); |
| 748 | 170 | memcpy(str.ptr + pos, s.ptr, s.length); |
| 70 | 171 | pos += s.length; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | } |
| 888 | 173 | va_end(ap2); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
174 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
175 | // terminate string |
| 748 | 176 | str.ptr[str.length] = '\0'; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
177 | |
| 748 | 178 | return str; |
| 70 | 179 | } |
| 180 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
181 | cxstring cx_strsubs( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
182 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
183 | size_t start |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
184 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
185 | return cx_strsubsl(string, start, string.length - start); |
| 505 | 186 | } |
| 187 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
188 | cxmutstr cx_strsubs_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
189 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
190 | size_t start |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
191 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
192 | return cx_strsubsl_m(string, start, string.length - start); |
| 70 | 193 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
195 | cxstring cx_strsubsl( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
196 | cxstring string, |
| 505 | 197 | size_t start, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
198 | size_t length |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
199 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
200 | if (start > string.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
201 | return (cxstring) {NULL, 0}; |
| 505 | 202 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
203 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
204 | size_t rem_len = string.length - start; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
205 | if (length > rem_len) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
206 | length = rem_len; |
| 505 | 207 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
208 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
209 | return (cxstring) {string.ptr + start, length}; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
212 | cxmutstr cx_strsubsl_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
213 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
214 | size_t start, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
215 | size_t length |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
216 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
217 | cxstring result = cx_strsubsl(cx_strcast(string), start, length); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
218 | return (cxmutstr) {(char *) result.ptr, result.length}; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
221 | cxstring cx_strchr( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
222 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
223 | int chr |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
224 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
225 | char *ret = memchr(string.ptr, 0xFF & chr, string.length); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
226 | if (ret == NULL) return (cxstring) {NULL, 0}; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
227 | return (cxstring) {ret, string.length - (ret - string.ptr)}; |
| 505 | 228 | } |
| 229 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
230 | cxmutstr cx_strchr_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
231 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
232 | int chr |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
233 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
234 | cxstring result = cx_strchr(cx_strcast(string), chr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
235 | return (cxmutstr) {(char *) result.ptr, result.length}; |
| 505 | 236 | } |
| 237 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
238 | cxstring cx_strrchr( |
| 888 | 239 | cxstring string, |
| 240 | int chr | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
241 | ) { |
| 888 | 242 | #ifdef WITH_MEMRCHR |
| 243 | char *ret = memrchr(string.ptr, 0xFF & chr, string.length); | |
| 244 | if (ret == NULL) return (cxstring) {NULL, 0}; | |
| 245 | return (cxstring) {ret, string.length - (ret - string.ptr)}; | |
| 246 | #else | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
247 | chr = 0xFF & chr; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
248 | size_t i = string.length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
249 | while (i > 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
250 | i--; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
251 | if (string.ptr[i] == chr) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
252 | return cx_strsubs(string, i); |
| 505 | 253 | } |
| 254 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
255 | return (cxstring) {NULL, 0}; |
| 888 | 256 | #endif |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
257 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
258 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
259 | cxmutstr cx_strrchr_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
260 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
261 | int chr |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
262 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
263 | cxstring result = cx_strrchr(cx_strcast(string), chr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
264 | return (cxmutstr) {(char *) result.ptr, result.length}; |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
265 | } |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
266 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
267 | #ifndef CX_STRSTR_SBO_SIZE |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
268 | #define CX_STRSTR_SBO_SIZE 128 |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
269 | #endif |
| 852 | 270 | const unsigned cx_strstr_sbo_size = CX_STRSTR_SBO_SIZE; |
| 505 | 271 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
272 | cxstring cx_strstr( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
273 | cxstring haystack, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
274 | cxstring needle |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
275 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
276 | if (needle.length == 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
277 | return haystack; |
|
255
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
278 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
279 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
280 | // optimize for single-char needles |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
281 | if (needle.length == 1) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
282 | return cx_strchr(haystack, *needle.ptr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
283 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
284 | |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
285 | /* |
|
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
286 | * IMPORTANT: |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
287 | * Our prefix table contains the prefix length PLUS ONE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
288 | * this is our decision, because we want to use the full range of size_t. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
289 | * The original algorithm needs a (-1) at one single place, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
290 | * and we want to avoid that. |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
291 | */ |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
292 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
293 | // local prefix table |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
294 | size_t s_prefix_table[CX_STRSTR_SBO_SIZE]; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
295 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
296 | // check needle length and use appropriate prefix table |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
297 | // if the pattern exceeds static prefix table, allocate on the heap |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
298 | const bool useheap = needle.length >= CX_STRSTR_SBO_SIZE; |
| 888 | 299 | register size_t *ptable = useheap |
| 300 | ? cxCallocDefault(needle.length + 1, sizeof(size_t)) | |
| 301 | : s_prefix_table; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
302 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
303 | // keep counter in registers |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
304 | register size_t i, j; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
305 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
306 | // fill prefix table |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
307 | i = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
308 | j = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
309 | ptable[i] = j; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
310 | while (i < needle.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
311 | while (j >= 1 && needle.ptr[j - 1] != needle.ptr[i]) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
312 | j = ptable[j - 1]; |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
313 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
314 | i++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
315 | j++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
316 | ptable[i] = j; |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
317 | } |
|
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
318 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
319 | // search |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
320 | cxstring result = {NULL, 0}; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
321 | i = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
322 | j = 1; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
323 | while (i < haystack.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
324 | while (j >= 1 && haystack.ptr[i] != needle.ptr[j - 1]) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
325 | j = ptable[j - 1]; |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
326 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
327 | i++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
328 | j++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
329 | if (j - 1 == needle.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
330 | size_t start = i - needle.length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
331 | result.ptr = haystack.ptr + start; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
332 | result.length = haystack.length - start; |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
333 | break; |
|
255
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
334 | } |
|
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
335 | } |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
336 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
337 | // if prefix table was allocated on the heap, free it |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
338 | if (useheap) { |
| 888 | 339 | cxFreeDefault(ptable); |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
340 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341 | |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
342 | return result; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
343 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
345 | cxmutstr cx_strstr_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
346 | cxmutstr haystack, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
347 | cxstring needle |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
348 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
349 | cxstring result = cx_strstr(cx_strcast(haystack), needle); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
350 | return (cxmutstr) {(char *) result.ptr, result.length}; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
351 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
352 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
353 | size_t cx_strsplit( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
354 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
355 | cxstring delim, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
356 | size_t limit, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
357 | cxstring *output |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
358 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
359 | // special case: output limit is zero |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
360 | if (limit == 0) return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
361 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
362 | // special case: delimiter is empty |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
363 | if (delim.length == 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
364 | output[0] = string; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
365 | return 1; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
366 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
367 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
368 | // special cases: delimiter is at least as large as the string |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
369 | if (delim.length >= string.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
370 | // exact match |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
371 | if (cx_strcmp(string, delim) == 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
372 | output[0] = cx_strn(string.ptr, 0); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
373 | output[1] = cx_strn(string.ptr + string.length, 0); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
374 | return 2; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
375 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
376 | // no match possible |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
377 | output[0] = string; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
378 | return 1; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
379 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
380 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
381 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
382 | size_t n = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
383 | cxstring curpos = string; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
384 | while (1) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
385 | ++n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
386 | cxstring match = cx_strstr(curpos, delim); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
387 | if (match.length > 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
388 | // is the limit reached? |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
389 | if (n < limit) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
390 | // copy the current string to the array |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
391 | cxstring item = cx_strn(curpos.ptr, match.ptr - curpos.ptr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
392 | output[n - 1] = item; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
393 | size_t processed = item.length + delim.length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
394 | curpos.ptr += processed; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
395 | curpos.length -= processed; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
396 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
397 | // limit reached, copy the _full_ remaining string |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
398 | output[n - 1] = curpos; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
399 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
400 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
401 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
402 | // no more matches, copy last string |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
403 | output[n - 1] = curpos; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
404 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
405 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
406 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
407 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
408 | return n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
409 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
410 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
411 | size_t cx_strsplit_a( |
| 852 | 412 | const CxAllocator *allocator, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
413 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
414 | cxstring delim, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
415 | size_t limit, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
416 | cxstring **output |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
417 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
418 | // find out how many splits we're going to make and allocate memory |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
419 | size_t n = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
420 | cxstring curpos = string; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
421 | while (1) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
422 | ++n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
423 | cxstring match = cx_strstr(curpos, delim); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
424 | if (match.length > 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
425 | // is the limit reached? |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
426 | if (n < limit) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
427 | size_t processed = match.ptr - curpos.ptr + delim.length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
428 | curpos.ptr += processed; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
429 | curpos.length -= processed; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
430 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
431 | // limit reached |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
432 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
433 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
434 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
435 | // no more matches |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
436 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
437 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
438 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
439 | *output = cxCalloc(allocator, n, sizeof(cxstring)); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
440 | return cx_strsplit(string, delim, n, *output); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
441 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
442 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
443 | size_t cx_strsplit_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
444 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
445 | cxstring delim, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
446 | size_t limit, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
447 | cxmutstr *output |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
448 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
449 | return cx_strsplit(cx_strcast(string), |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
450 | delim, limit, (cxstring *) output); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
451 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
452 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
453 | size_t cx_strsplit_ma( |
| 852 | 454 | const CxAllocator *allocator, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
455 | cxmutstr string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
456 | cxstring delim, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
457 | size_t limit, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
458 | cxmutstr **output |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
459 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
460 | return cx_strsplit_a(allocator, cx_strcast(string), |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
461 | delim, limit, (cxstring **) output); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
462 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
463 | |
| 888 | 464 | int cx_strcmp_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
465 | cxstring s1, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
466 | cxstring s2 |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
467 | ) { |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
468 | if (s1.length == s2.length) { |
| 852 | 469 | return strncmp(s1.ptr, s2.ptr, s1.length); |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
470 | } else if (s1.length > s2.length) { |
| 852 | 471 | int r = strncmp(s1.ptr, s2.ptr, s2.length); |
| 472 | if (r != 0) return r; | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
473 | return 1; |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
474 | } else { |
| 852 | 475 | int r = strncmp(s1.ptr, s2.ptr, s1.length); |
| 476 | if (r != 0) return r; | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
477 | return -1; |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
478 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
479 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
480 | |
| 888 | 481 | int cx_strcasecmp_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
482 | cxstring s1, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
483 | cxstring s2 |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
484 | ) { |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
485 | if (s1.length == s2.length) { |
| 852 | 486 | return cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length); |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
487 | } else if (s1.length > s2.length) { |
| 852 | 488 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s2.length); |
| 489 | if (r != 0) return r; | |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
490 | return 1; |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
491 | } else { |
| 852 | 492 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length); |
| 493 | if (r != 0) return r; | |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
494 | return -1; |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
495 | } |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
496 | } |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
497 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
498 | int cx_strcmp_p( |
| 852 | 499 | const void *s1, |
| 500 | const void *s2 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
501 | ) { |
| 852 | 502 | const cxstring *left = s1; |
| 503 | const cxstring *right = s2; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
504 | return cx_strcmp(*left, *right); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
505 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
506 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
507 | int cx_strcasecmp_p( |
| 852 | 508 | const void *s1, |
| 509 | const void *s2 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
510 | ) { |
| 852 | 511 | const cxstring *left = s1; |
| 512 | const cxstring *right = s2; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
513 | return cx_strcasecmp(*left, *right); |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
514 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
515 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
516 | cxmutstr cx_strdup_a_( |
| 852 | 517 | const CxAllocator *allocator, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
518 | cxstring string |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
519 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
520 | cxmutstr result = { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
521 | cxMalloc(allocator, string.length + 1), |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
522 | string.length |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
523 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
524 | if (result.ptr == NULL) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
525 | result.length = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
526 | return result; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
527 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
528 | memcpy(result.ptr, string.ptr, string.length); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
529 | result.ptr[string.length] = '\0'; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
530 | return result; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
531 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
532 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
533 | cxstring cx_strtrim(cxstring string) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
534 | cxstring result = string; |
| 888 | 535 | while (result.length > 0 && isspace((unsigned char)(result.ptr[0]))) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
536 | result.ptr++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
537 | result.length--; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
538 | } |
| 888 | 539 | while (result.length > 0 && isspace((unsigned char)result.ptr[result.length - 1])) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
540 | result.length--; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
541 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
542 | return result; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
543 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
544 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
545 | cxmutstr cx_strtrim_m(cxmutstr string) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
546 | cxstring result = cx_strtrim(cx_strcast(string)); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
547 | return (cxmutstr) {(char *) result.ptr, result.length}; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
548 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
549 | |
| 888 | 550 | bool cx_strprefix_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
551 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
552 | cxstring prefix |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
553 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
554 | if (string.length < prefix.length) return false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
555 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
556 | } |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
557 | |
| 888 | 558 | bool cx_strsuffix_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
559 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
560 | cxstring suffix |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
561 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
562 | if (string.length < suffix.length) return false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
563 | return memcmp(string.ptr + string.length - suffix.length, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
564 | suffix.ptr, suffix.length) == 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
565 | } |
| 505 | 566 | |
| 888 | 567 | bool cx_strcaseprefix_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
568 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
569 | cxstring prefix |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
570 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
571 | if (string.length < prefix.length) return false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
572 | #ifdef _WIN32 |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
573 | return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
574 | #else |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
575 | return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
576 | #endif |
| 505 | 577 | } |
| 578 | ||
| 888 | 579 | bool cx_strcasesuffix_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
580 | cxstring string, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
581 | cxstring suffix |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
582 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
583 | if (string.length < suffix.length) return false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
584 | #ifdef _WIN32 |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
585 | return _strnicmp(string.ptr+string.length-suffix.length, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
586 | suffix.ptr, suffix.length) == 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
587 | #else |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
588 | return strncasecmp(string.ptr + string.length - suffix.length, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
589 | suffix.ptr, suffix.length) == 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
590 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
591 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
592 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
593 | cxmutstr cx_strreplacen_a( |
| 852 | 594 | const CxAllocator *allocator, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
595 | cxstring str, |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
596 | cxstring search, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
597 | cxstring replacement, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
598 | size_t replmax |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
599 | ) { |
| 888 | 600 | // special cases |
| 601 | if (search.length == 0 || search.length > str.length || replmax == 0) { | |
| 602 | return cx_strdup_a(allocator, str); | |
| 603 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
604 | |
| 888 | 605 | size_t in_len = str.length; |
| 606 | size_t search_len = search.length; | |
| 607 | size_t repl_len = replacement.length; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
608 | |
| 888 | 609 | // first run, count the occurrences |
| 610 | // and remember where the first is | |
| 611 | size_t occurrences = 1; | |
| 612 | cxstring first = cx_strstr(str, search); | |
| 613 | if (first.length == 0) { | |
| 614 | // special case, no replacements | |
| 615 | return cx_strdup_a(allocator, str); | |
| 616 | } | |
| 617 | cxstring tmp = cx_strsubs(first, search_len); | |
| 618 | while (occurrences < replmax && | |
| 619 | (tmp = cx_strstr(tmp, search)).length > 0) { | |
| 620 | occurrences++; | |
| 621 | tmp = cx_strsubs(tmp, search_len); | |
|
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
622 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
623 | |
| 888 | 624 | // calculate necessary memory |
| 625 | signed long long diff_len = (signed long long) repl_len - search_len; | |
| 626 | size_t out_len = in_len + diff_len * occurrences; | |
| 627 | cxmutstr out = { | |
| 628 | cxMalloc(allocator, out_len + 1), | |
| 629 | out_len | |
| 630 | }; | |
| 631 | if (out.ptr == NULL) return out; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
632 | |
| 888 | 633 | // second run: perform the replacements |
| 634 | // but start where we found the first occurrence | |
| 635 | const char *inp = str.ptr; | |
| 636 | tmp = first; | |
| 637 | char *outp = out.ptr; | |
| 638 | while (occurrences-- > 0 && (tmp = cx_strstr(tmp, search)).length > 0) { | |
| 639 | size_t copylen = tmp.ptr - inp; | |
| 640 | memcpy(outp, inp, copylen); | |
| 641 | outp += copylen; | |
| 642 | memcpy(outp, replacement.ptr, repl_len); | |
| 643 | outp += repl_len; | |
| 644 | inp += copylen + search_len; | |
| 645 | tmp = cx_strsubs(tmp, search_len); | |
|
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
646 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
647 | |
| 888 | 648 | // add the remaining string |
| 649 | size_t copylen = in_len - (inp - str.ptr); | |
| 650 | memcpy(outp, inp, copylen); | |
| 651 | out.ptr[out_len] = '\0'; | |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
652 | |
| 888 | 653 | return out; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
654 | } |
|
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
655 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
656 | CxStrtokCtx cx_strtok_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
657 | cxstring str, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
658 | cxstring delim, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
659 | size_t limit |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
660 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
661 | CxStrtokCtx ctx; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
662 | ctx.str = str; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
663 | ctx.delim = delim; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
664 | ctx.limit = limit; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
665 | ctx.pos = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
666 | ctx.next_pos = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
667 | ctx.delim_pos = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
668 | ctx.found = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
669 | ctx.delim_more = NULL; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
670 | ctx.delim_more_count = 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
671 | return ctx; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
672 | } |
|
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
673 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
674 | bool cx_strtok_next( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
675 | CxStrtokCtx *ctx, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
676 | cxstring *token |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
677 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
678 | // abortion criteria |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
679 | if (ctx->found >= ctx->limit || ctx->delim_pos >= ctx->str.length) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
680 | return false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
681 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
682 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
683 | // determine the search start |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
684 | cxstring haystack = cx_strsubs(ctx->str, ctx->next_pos); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
685 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
686 | // search the next delimiter |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
687 | cxstring delim = cx_strstr(haystack, ctx->delim); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
688 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
689 | // if found, make delim capture exactly the delimiter |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
690 | if (delim.length > 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
691 | delim.length = ctx->delim.length; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
692 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
693 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
694 | // if more delimiters are specified, check them now |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
695 | if (ctx->delim_more_count > 0) { |
| 852 | 696 | for (size_t i = 0; i < ctx->delim_more_count; i++) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
697 | cxstring d = cx_strstr(haystack, ctx->delim_more[i]); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
698 | if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
699 | delim.ptr = d.ptr; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
700 | delim.length = ctx->delim_more[i].length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
701 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
702 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
703 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
704 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
705 | // store the token information and adjust the context |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
706 | ctx->found++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
707 | ctx->pos = ctx->next_pos; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
708 | token->ptr = &ctx->str.ptr[ctx->pos]; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
709 | ctx->delim_pos = delim.length == 0 ? |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
710 | ctx->str.length : (size_t) (delim.ptr - ctx->str.ptr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
711 | token->length = ctx->delim_pos - ctx->pos; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
712 | ctx->next_pos = ctx->delim_pos + delim.length; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
713 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
714 | return true; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
715 | } |
| 505 | 716 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
717 | bool cx_strtok_next_m( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
718 | CxStrtokCtx *ctx, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
719 | cxmutstr *token |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
720 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
721 | return cx_strtok_next(ctx, (cxstring *) token); |
| 505 | 722 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
723 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
724 | void cx_strtok_delim( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
725 | CxStrtokCtx *ctx, |
| 852 | 726 | const cxstring *delim, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
727 | size_t count |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
728 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
729 | ctx->delim_more = delim; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
730 | ctx->delim_more_count = count; |
| 505 | 731 | } |
| 852 | 732 | |
| 733 | #define cx_strtoX_signed_impl(rtype, rmin, rmax) \ | |
| 734 | long long result; \ | |
| 735 | if (cx_strtoll_lc(str, &result, base, groupsep)) { \ | |
| 736 | return -1; \ | |
| 737 | } \ | |
| 738 | if (result < rmin || result > rmax) { \ | |
| 739 | errno = ERANGE; \ | |
| 740 | return -1; \ | |
| 741 | } \ | |
| 742 | *output = (rtype) result; \ | |
| 743 | return 0 | |
| 744 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
745 | int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep) { |
| 852 | 746 | cx_strtoX_signed_impl(short, SHRT_MIN, SHRT_MAX); |
| 747 | } | |
| 748 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
749 | int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep) { |
| 852 | 750 | cx_strtoX_signed_impl(int, INT_MIN, INT_MAX); |
| 751 | } | |
| 752 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
753 | int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep) { |
| 852 | 754 | cx_strtoX_signed_impl(long, LONG_MIN, LONG_MAX); |
| 755 | } | |
| 756 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
757 | int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep) { |
| 852 | 758 | // strategy: parse as unsigned, check range, negate if required |
| 759 | bool neg = false; | |
| 760 | size_t start_unsigned = 0; | |
| 761 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
762 | // emptiness check |
| 852 | 763 | if (str.length == 0) { |
| 764 | errno = EINVAL; | |
| 765 | return -1; | |
| 766 | } | |
| 767 | ||
| 768 | // test if we have a negative sign character | |
| 769 | if (str.ptr[start_unsigned] == '-') { | |
| 770 | neg = true; | |
| 771 | start_unsigned++; | |
| 772 | // must not be followed by positive sign character | |
| 773 | if (str.length == 1 || str.ptr[start_unsigned] == '+') { | |
| 774 | errno = EINVAL; | |
| 775 | return -1; | |
| 776 | } | |
| 777 | } | |
| 778 | ||
| 779 | // now parse the number with strtoull | |
| 780 | unsigned long long v; | |
| 781 | cxstring ustr = start_unsigned == 0 ? str | |
| 782 | : cx_strn(str.ptr + start_unsigned, str.length - start_unsigned); | |
| 783 | int ret = cx_strtoull_lc(ustr, &v, base, groupsep); | |
| 784 | if (ret != 0) return ret; | |
| 785 | if (neg) { | |
| 786 | if (v - 1 > LLONG_MAX) { | |
| 787 | errno = ERANGE; | |
| 788 | return -1; | |
| 789 | } | |
| 790 | *output = -(long long) v; | |
| 791 | return 0; | |
| 792 | } else { | |
| 793 | if (v > LLONG_MAX) { | |
| 794 | errno = ERANGE; | |
| 795 | return -1; | |
| 796 | } | |
| 797 | *output = (long long) v; | |
| 798 | return 0; | |
| 799 | } | |
| 800 | } | |
| 801 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
802 | int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep) { |
| 852 | 803 | cx_strtoX_signed_impl(int8_t, INT8_MIN, INT8_MAX); |
| 804 | } | |
| 805 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
806 | int cx_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep) { |
| 852 | 807 | cx_strtoX_signed_impl(int16_t, INT16_MIN, INT16_MAX); |
| 808 | } | |
| 809 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
810 | int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep) { |
| 852 | 811 | cx_strtoX_signed_impl(int32_t, INT32_MIN, INT32_MAX); |
| 812 | } | |
| 813 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
814 | int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep) { |
| 852 | 815 | assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms |
| 816 | return cx_strtoll_lc(str, (long long*) output, base, groupsep); | |
| 817 | } | |
| 818 | ||
| 819 | #define cx_strtoX_unsigned_impl(rtype, rmax) \ | |
| 820 | uint64_t result; \ | |
| 821 | if (cx_strtou64_lc(str, &result, base, groupsep)) { \ | |
| 822 | return -1; \ | |
| 823 | } \ | |
| 824 | if (result > rmax) { \ | |
| 825 | errno = ERANGE; \ | |
| 826 | return -1; \ | |
| 827 | } \ | |
| 828 | *output = (rtype) result; \ | |
| 829 | return 0 | |
| 830 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
831 | int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep) { |
| 852 | 832 | cx_strtoX_unsigned_impl(unsigned short, USHRT_MAX); |
| 833 | } | |
| 834 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
835 | int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep) { |
| 852 | 836 | cx_strtoX_unsigned_impl(unsigned int, UINT_MAX); |
| 837 | } | |
| 838 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
839 | int cx_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep) { |
| 852 | 840 | cx_strtoX_unsigned_impl(unsigned long, ULONG_MAX); |
| 841 | } | |
| 842 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
843 | int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep) { |
| 852 | 844 | // some sanity checks |
| 845 | if (str.length == 0) { | |
| 846 | errno = EINVAL; | |
| 847 | return -1; | |
| 848 | } | |
| 849 | if (!(base == 2 || base == 8 || base == 10 || base == 16)) { | |
| 850 | errno = EINVAL; | |
| 851 | return -1; | |
| 852 | } | |
| 853 | if (groupsep == NULL) groupsep = ""; | |
| 854 | ||
| 855 | // find the actual start of the number | |
| 856 | if (str.ptr[0] == '+') { | |
| 857 | str.ptr++; | |
| 858 | str.length--; | |
| 859 | if (str.length == 0) { | |
| 860 | errno = EINVAL; | |
| 861 | return -1; | |
| 862 | } | |
| 863 | } | |
| 864 | size_t start = 0; | |
| 865 | ||
| 866 | // if base is 2 or 16, some leading stuff may appear | |
| 867 | if (base == 2) { | |
| 868 | if ((str.ptr[0] | 32) == 'b') { | |
| 869 | start = 1; | |
| 870 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 871 | if ((str.ptr[1] | 32) == 'b') { | |
| 872 | start = 2; | |
| 873 | } | |
| 874 | } | |
| 875 | } else if (base == 16) { | |
| 876 | if ((str.ptr[0] | 32) == 'x' || str.ptr[0] == '#') { | |
| 877 | start = 1; | |
| 878 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 879 | if ((str.ptr[1] | 32) == 'x') { | |
| 880 | start = 2; | |
| 881 | } | |
| 882 | } | |
| 883 | } | |
| 884 | ||
| 885 | // check if there are digits left | |
| 886 | if (start >= str.length) { | |
| 887 | errno = EINVAL; | |
| 888 | return -1; | |
| 889 | } | |
| 890 | ||
| 891 | // now parse the number | |
| 892 | unsigned long long result = 0; | |
| 893 | for (size_t i = start; i < str.length; i++) { | |
| 894 | // ignore group separators | |
| 895 | if (strchr(groupsep, str.ptr[i])) continue; | |
| 896 | ||
| 897 | // determine the digit value of the character | |
| 898 | unsigned char c = str.ptr[i]; | |
| 899 | if (c >= 'a') c = 10 + (c - 'a'); | |
| 900 | else if (c >= 'A') c = 10 + (c - 'A'); | |
| 901 | else if (c >= '0') c = c - '0'; | |
| 902 | else c = 255; | |
| 903 | if (c >= base) { | |
| 904 | errno = EINVAL; | |
| 905 | return -1; | |
| 906 | } | |
| 907 | ||
| 908 | // now combine the digit with what we already have | |
| 909 | unsigned long right = (result & 0xff) * base + c; | |
| 910 | unsigned long long left = (result >> 8) * base + (right >> 8); | |
| 911 | if (left > (ULLONG_MAX >> 8)) { | |
| 912 | errno = ERANGE; | |
| 913 | return -1; | |
| 914 | } | |
| 915 | result = (left << 8) + (right & 0xff); | |
| 916 | } | |
| 917 | ||
| 918 | *output = result; | |
| 919 | return 0; | |
| 920 | } | |
| 921 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
922 | int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep) { |
| 852 | 923 | cx_strtoX_unsigned_impl(uint8_t, UINT8_MAX); |
| 924 | } | |
| 925 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
926 | int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep) { |
| 852 | 927 | cx_strtoX_unsigned_impl(uint16_t, UINT16_MAX); |
| 928 | } | |
| 929 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
930 | int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep) { |
| 852 | 931 | cx_strtoX_unsigned_impl(uint32_t, UINT32_MAX); |
| 932 | } | |
| 933 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
934 | int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep) { |
| 852 | 935 | assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms |
| 936 | return cx_strtoull_lc(str, (unsigned long long*) output, base, groupsep); | |
| 937 | } | |
| 938 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
939 | int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep) { |
| 852 | 940 | #if SIZE_MAX == UINT32_MAX |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
941 | return cx_strtou32_lc_(str, (uint32_t*) output, base, groupsep); |
| 852 | 942 | #elif SIZE_MAX == UINT64_MAX |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
943 | return cx_strtoull_lc_(str, (unsigned long long *) output, base, groupsep); |
| 852 | 944 | #else |
| 945 | #error "unsupported size_t size" | |
| 946 | #endif | |
| 947 | } | |
| 948 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
949 | int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep) { |
| 852 | 950 | // use string to double and add a range check |
| 951 | double d; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
952 | int ret = cx_strtod_lc_(str, &d, decsep, groupsep); |
| 852 | 953 | if (ret != 0) return ret; |
| 954 | // note: FLT_MIN is the smallest POSITIVE number that can be represented | |
| 955 | double test = d < 0 ? -d : d; | |
| 956 | if (test < FLT_MIN || test > FLT_MAX) { | |
| 957 | errno = ERANGE; | |
| 958 | return -1; | |
| 959 | } | |
| 960 | *output = (float) d; | |
| 961 | return 0; | |
| 962 | } | |
| 963 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
964 | int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep) { |
| 852 | 965 | // TODO: overflow check |
| 966 | // TODO: increase precision | |
| 967 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
968 | // emptiness check |
| 852 | 969 | if (str.length == 0) { |
| 970 | errno = EINVAL; | |
| 971 | return -1; | |
| 972 | } | |
| 973 | ||
| 974 | double result = 0.; | |
| 975 | int sign = 1; | |
| 976 | ||
| 977 | // check if there is a sign | |
| 978 | if (str.ptr[0] == '-') { | |
| 979 | sign = -1; | |
| 980 | str.ptr++; | |
| 981 | str.length--; | |
| 982 | } else if (str.ptr[0] == '+') { | |
| 983 | str.ptr++; | |
| 984 | str.length--; | |
| 985 | } | |
| 986 | ||
| 987 | // there must be at least one char to parse | |
| 988 | if (str.length == 0) { | |
| 989 | errno = EINVAL; | |
| 990 | return -1; | |
| 991 | } | |
| 992 | ||
| 993 | // parse all digits until we find the decsep | |
| 994 | size_t pos = 0; | |
| 995 | do { | |
| 888 | 996 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 997 | result = result * 10 + (str.ptr[pos] - '0'); |
| 998 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 999 | break; | |
| 1000 | } | |
| 1001 | } while (++pos < str.length); | |
| 1002 | ||
| 1003 | // already done? | |
| 1004 | if (pos == str.length) { | |
| 1005 | *output = result * sign; | |
| 1006 | return 0; | |
| 1007 | } | |
| 1008 | ||
| 1009 | // is the next char the decsep? | |
| 1010 | if (str.ptr[pos] == decsep) { | |
| 1011 | pos++; | |
| 1012 | // it may end with the decsep, if it did not start with it | |
| 1013 | if (pos == str.length) { | |
| 1014 | if (str.length == 1) { | |
| 1015 | errno = EINVAL; | |
| 1016 | return -1; | |
| 1017 | } else { | |
| 1018 | *output = result * sign; | |
| 1019 | return 0; | |
| 1020 | } | |
| 1021 | } | |
| 1022 | // parse everything until exponent or end | |
| 1023 | double factor = 1.; | |
| 1024 | do { | |
| 888 | 1025 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 1026 | factor *= 0.1; |
| 1027 | result = result + factor * (str.ptr[pos] - '0'); | |
| 1028 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1029 | break; | |
| 1030 | } | |
| 1031 | } while (++pos < str.length); | |
| 1032 | } | |
| 1033 | ||
| 1034 | // no exponent? | |
| 1035 | if (pos == str.length) { | |
| 1036 | *output = result * sign; | |
| 1037 | return 0; | |
| 1038 | } | |
| 1039 | ||
| 1040 | // now the next separator MUST be the exponent separator | |
| 1041 | // and at least one char must follow | |
| 1042 | if ((str.ptr[pos] | 32) != 'e' || str.length <= pos + 1) { | |
| 1043 | errno = EINVAL; | |
| 1044 | return -1; | |
| 1045 | } | |
| 1046 | pos++; | |
| 1047 | ||
| 1048 | // check if we have a sign for the exponent | |
| 1049 | double factor = 10.; | |
| 1050 | if (str.ptr[pos] == '-') { | |
| 1051 | factor = .1; | |
| 1052 | pos++; | |
| 1053 | } else if (str.ptr[pos] == '+') { | |
| 1054 | pos++; | |
| 1055 | } | |
| 1056 | ||
| 1057 | // at least one digit must follow | |
| 1058 | if (pos == str.length) { | |
| 1059 | errno = EINVAL; | |
| 1060 | return -1; | |
| 1061 | } | |
| 1062 | ||
| 1063 | // parse the exponent | |
| 1064 | unsigned int exp = 0; | |
| 1065 | do { | |
| 888 | 1066 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 1067 | exp = 10 * exp + (str.ptr[pos] - '0'); |
| 1068 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1069 | errno = EINVAL; | |
| 1070 | return -1; | |
| 1071 | } | |
| 1072 | } while (++pos < str.length); | |
| 1073 | ||
| 1074 | // apply the exponent by fast exponentiation | |
| 1075 | do { | |
| 1076 | if (exp & 1) { | |
| 1077 | result *= factor; | |
| 1078 | } | |
| 1079 | factor *= factor; | |
| 1080 | } while ((exp >>= 1) > 0); | |
| 1081 | ||
| 1082 | // store the result and exit | |
| 1083 | *output = result * sign; | |
| 1084 | return 0; | |
| 1085 | } |