Wed, 31 Dec 2025 16:41:16 +0100
update ucx to version 4.0
|
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 | */ |
| 891 | 28 | |
| 29 | #ifdef WITH_MEMRCHR | |
| 889 | 30 | #define _GNU_SOURCE |
| 31 | #endif | |
| 32 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
33 | #include "cx/string.h" |
|
335
c1bc13faadaa
updates ucx to version 1.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
314
diff
changeset
|
34 | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <string.h> |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #include <stdarg.h> |
| 852 | 37 | #include <assert.h> |
| 38 | #include <errno.h> | |
| 39 | #include <limits.h> | |
| 40 | #include <float.h> | |
| 889 | 41 | #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
|
42 | |
| 852 | 43 | #ifdef _WIN32 |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
44 | static int cx_fixed_strnicmp(const char* s1, const char* s2, size_t count) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
45 | // Microsoft's implementation crashes when count == 0 and either string is NULL |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
46 | if (count == 0) return 0; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
47 | return _strnicmp(s1, s2, count); |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
48 | } |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
49 | #define cx_strcasecmp_impl cx_fixed_strnicmp |
| 852 | 50 | #else |
| 51 | #include <strings.h> | |
| 52 | #define cx_strcasecmp_impl strncasecmp | |
| 53 | #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
|
54 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
55 | void cx_strfree(cxmutstr *str) { |
| 852 | 56 | if (str == NULL) return; |
| 886 | 57 | 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
|
58 | 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
|
59 | str->length = 0; |
| 505 | 60 | } |
| 61 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
62 | void cx_strfree_a( |
| 852 | 63 | 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
|
64 | 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
|
65 | ) { |
| 852 | 66 | 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
|
67 | 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
|
68 | 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
|
69 | 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
|
70 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
72 | int cx_strcpy_a_( |
| 886 | 73 | const CxAllocator *alloc, |
| 74 | cxmutstr *dest, | |
| 75 | cxstring src | |
| 76 | ) { | |
| 77 | if (cxReallocate(alloc, &dest->ptr, src.length + 1)) { | |
| 891 | 78 | return 1; // LCOV_EXCL_LINE |
| 886 | 79 | } |
| 80 | ||
| 81 | memcpy(dest->ptr, src.ptr, src.length); | |
| 82 | dest->length = src.length; | |
| 83 | dest->ptr[dest->length] = '\0'; | |
| 84 | ||
| 85 | return 0; | |
| 86 | } | |
| 87 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
88 | 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
|
89 | 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
|
90 | ... |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
91 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
92 | 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
|
93 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
94 | 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
|
95 | 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
|
96 | size_t size = 0; |
| 852 | 97 | 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
|
98 | cxstring str = va_arg(ap, cxstring); |
| 852 | 99 | if (size > SIZE_MAX - str.length) errno = EOVERFLOW; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | size += str.length; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | va_end(ap); |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | return size; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
107 | cxmutstr cx_strcat_a( |
| 852 | 108 | const CxAllocator *alloc, |
| 748 | 109 | cxmutstr str, |
| 70 | 110 | 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
|
111 | ... |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
112 | ) { |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
113 | if (count == 0) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
114 | if (cxReallocate(alloc, &str.ptr, str.length + 1)) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
115 | return CX_NULLSTR; // LCOV_EXCL_LINE |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
116 | } |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
117 | str.ptr[str.length] = '\0'; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
118 | return str; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
119 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
120 | 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
|
121 | va_start(ap, count); |
| 886 | 122 | va_list ap2; |
| 123 | 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
|
124 | |
| 886 | 125 | // compute overall length |
| 852 | 126 | bool overflow = false; |
| 748 | 127 | size_t slen = str.length; |
| 852 | 128 | for (size_t i = 0; i < count; i++) { |
| 886 | 129 | cxstring s = va_arg(ap, cxstring); |
| 891 | 130 | if (slen > SIZE_MAX - s.length) overflow = true; |
| 505 | 131 | slen += s.length; |
| 132 | } | |
| 748 | 133 | 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
|
134 | |
| 852 | 135 | // abort in case of overflow |
| 136 | if (overflow) { | |
| 886 | 137 | va_end(ap2); |
| 852 | 138 | errno = EOVERFLOW; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
139 | return CX_NULLSTR; |
| 852 | 140 | } |
| 141 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
142 | // reallocate or create a new string |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
143 | if (cxReallocate(alloc, &str.ptr, slen + 1)) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
144 | // LCOV_EXCL_START |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
145 | va_end(ap2); |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
146 | return CX_NULLSTR; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
147 | // LCOV_EXCL_STOP |
| 748 | 148 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
149 | |
| 70 | 150 | // concatenate strings |
| 748 | 151 | size_t pos = str.length; |
| 152 | str.length = slen; | |
| 852 | 153 | for (size_t i = 0; i < count; i++) { |
| 886 | 154 | cxstring s = va_arg(ap2, cxstring); |
| 748 | 155 | memcpy(str.ptr + pos, s.ptr, s.length); |
| 70 | 156 | pos += s.length; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | } |
| 886 | 158 | 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
|
159 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
160 | // terminate string |
| 748 | 161 | 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
|
162 | |
| 748 | 163 | return str; |
| 70 | 164 | } |
| 165 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
166 | cxstring cx_strsubs_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
167 | 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
|
168 | 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
|
169 | ) { |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
170 | return cx_strsubsl_(string, start, string.length); |
| 505 | 171 | } |
| 172 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
173 | cxstring cx_strsubsl_( |
|
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 | cxstring string, |
| 505 | 175 | 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
|
176 | 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
|
177 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
178 | 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
|
179 | return (cxstring) {NULL, 0}; |
| 505 | 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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
182 | 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
|
183 | 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
|
184 | length = rem_len; |
| 505 | 185 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
186 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
187 | return (cxstring) {string.ptr + start, length}; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
190 | cxstring cx_strchr_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
191 | 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
|
192 | 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
|
193 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
194 | 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
|
195 | if (ret == NULL) return (cxstring) {NULL, 0}; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
196 | return (cxstring) {ret, string.length - (ret - string.ptr)}; |
| 505 | 197 | } |
| 198 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
199 | cxstring cx_strrchr_( |
| 889 | 200 | cxstring string, |
| 201 | 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
|
202 | ) { |
| 889 | 203 | #ifdef WITH_MEMRCHR |
| 204 | char *ret = memrchr(string.ptr, 0xFF & chr, string.length); | |
| 205 | if (ret == NULL) return (cxstring) {NULL, 0}; | |
| 206 | return (cxstring) {ret, string.length - (ret - string.ptr)}; | |
| 207 | #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
|
208 | 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
|
209 | 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
|
210 | 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
|
211 | i--; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
212 | 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
|
213 | return cx_strsubs(string, i); |
| 505 | 214 | } |
| 215 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
216 | return (cxstring) {NULL, 0}; |
| 889 | 217 | #endif |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
218 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
219 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
220 | #ifndef CX_STRSTR_SBO_SIZE |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
221 | #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
|
222 | #endif |
| 852 | 223 | const unsigned cx_strstr_sbo_size = CX_STRSTR_SBO_SIZE; |
| 505 | 224 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
225 | cxstring cx_strstr_(cxstring haystack, cxstring needle) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
226 | 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
|
227 | return haystack; |
|
255
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
228 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
229 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
230 | // 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
|
231 | 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
|
232 | 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
|
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 | |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
235 | /* |
|
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
236 | * 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
|
237 | * 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
|
238 | * 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
|
239 | * 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
|
240 | * 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
|
241 | */ |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
242 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
243 | // 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
|
244 | 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
|
245 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
246 | // 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
|
247 | // 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
|
248 | const bool useheap = needle.length >= CX_STRSTR_SBO_SIZE; |
| 886 | 249 | register size_t *ptable = useheap |
| 250 | ? cxCallocDefault(needle.length + 1, sizeof(size_t)) | |
| 251 | : 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
|
252 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
253 | // 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
|
254 | 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
|
255 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
256 | // 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
|
257 | 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
|
258 | 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
|
259 | 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
|
260 | 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
|
261 | 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
|
262 | 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
|
263 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
264 | i++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
265 | j++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
266 | 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
|
267 | } |
|
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
268 | |
|
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 | // search |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
270 | 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
|
271 | 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
|
272 | 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
|
273 | 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
|
274 | 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
|
275 | 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
|
276 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
277 | i++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
278 | j++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
279 | 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
|
280 | 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
|
281 | 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
|
282 | 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
|
283 | break; |
|
255
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
284 | } |
|
bf19378aed58
updates UCX to version 0.11
Mike Becker <universe@uap-core.de>
parents:
174
diff
changeset
|
285 | } |
|
314
8722a668fb2a
updates ucx version (most importantly this adds the new sstrstr implementation)
Mike Becker <universe@uap-core.de>
parents:
255
diff
changeset
|
286 | |
|
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 | // 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
|
288 | if (useheap) { |
| 886 | 289 | 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
|
290 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | return result; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
295 | size_t cx_strsplit_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
296 | 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
|
297 | 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
|
298 | 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
|
299 | 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
|
300 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
301 | // 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
|
302 | 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
|
303 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
304 | // 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
|
305 | 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
|
306 | 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
|
307 | 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
|
308 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
309 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
310 | // 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
|
311 | 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
|
312 | // 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
|
313 | 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
|
314 | 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
|
315 | 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
|
316 | 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
|
317 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
318 | // 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
|
319 | 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
|
320 | 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
|
321 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
322 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
323 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
324 | 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
|
325 | 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
|
326 | 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
|
327 | ++n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
328 | 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
|
329 | 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
|
330 | // 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
|
331 | 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
|
332 | // 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
|
333 | 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
|
334 | 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
|
335 | 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
|
336 | 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
|
337 | 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
|
338 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
339 | // 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
|
340 | 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
|
341 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
342 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
343 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
344 | // 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
|
345 | 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
|
346 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
347 | } |
|
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 | |
|
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 n; |
|
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 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
353 | size_t cx_strsplit_a_( |
| 852 | 354 | 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
|
355 | 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
|
356 | 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
|
357 | 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
|
358 | 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
|
359 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
360 | // 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
|
361 | 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
|
362 | 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
|
363 | 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
|
364 | ++n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
365 | 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
|
366 | 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
|
367 | // 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
|
368 | 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
|
369 | 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
|
370 | 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
|
371 | 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
|
372 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
373 | // 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
|
374 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
375 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
376 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
377 | // 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
|
378 | break; |
|
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 | *output = cxCalloc(allocator, n, sizeof(cxstring)); |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
382 | return cx_strsplit_(string, delim, n, *output); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
383 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
384 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
385 | size_t cx_strsplit_m_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
386 | 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
|
387 | 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
|
388 | 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
|
389 | 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
|
390 | ) { |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
391 | return cx_strsplit_(cx_strcast(string), |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
392 | 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
|
393 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
394 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
395 | size_t cx_strsplit_ma_( |
| 852 | 396 | 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
|
397 | 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
|
398 | 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
|
399 | 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
|
400 | 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
|
401 | ) { |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
402 | return cx_strsplit_a_(allocator, cx_strcast(string), |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
403 | 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
|
404 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
405 | |
| 889 | 406 | 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
|
407 | 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
|
408 | 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
|
409 | ) { |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
410 | if (s1.length == s2.length) { |
| 852 | 411 | 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
|
412 | } else if (s1.length > s2.length) { |
| 852 | 413 | int r = strncmp(s1.ptr, s2.ptr, s2.length); |
| 414 | 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
|
415 | return 1; |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
416 | } else { |
| 852 | 417 | int r = strncmp(s1.ptr, s2.ptr, s1.length); |
| 418 | 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
|
419 | return -1; |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
420 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | |
| 889 | 423 | 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
|
424 | 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
|
425 | 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
|
426 | ) { |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
427 | if (s1.length == s2.length) { |
| 852 | 428 | 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
|
429 | } else if (s1.length > s2.length) { |
| 852 | 430 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s2.length); |
| 431 | if (r != 0) return r; | |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
432 | return 1; |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
433 | } else { |
| 852 | 434 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length); |
| 435 | if (r != 0) return r; | |
|
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
436 | return -1; |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
437 | } |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
438 | } |
|
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
17
diff
changeset
|
439 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
440 | int cx_strcmp_p( |
| 852 | 441 | const void *s1, |
| 442 | 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
|
443 | ) { |
| 852 | 444 | const cxstring *left = s1; |
| 445 | 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
|
446 | 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
|
447 | } |
|
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 | int cx_strcasecmp_p( |
| 852 | 450 | const void *s1, |
| 451 | 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
|
452 | ) { |
| 852 | 453 | const cxstring *left = s1; |
| 454 | 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
|
455 | 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
|
456 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
457 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
458 | cxmutstr cx_strdup_a_( |
| 852 | 459 | 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
|
460 | 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
|
461 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
462 | 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
|
463 | 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
|
464 | 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
|
465 | }; |
| 891 | 466 | // LCOV_EXCL_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
|
467 | 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
|
468 | 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
|
469 | return result; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
470 | } |
| 891 | 471 | // LCOV_EXCL_STOP |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
472 | 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
|
473 | 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
|
474 | 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
|
475 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
476 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
477 | cxstring cx_strtrim_(cxstring string) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
478 | cxstring result = string; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
479 | while (isspace((unsigned char)cx_strat(result, 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
|
480 | 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
|
481 | 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
|
482 | } |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
483 | while (isspace((unsigned char)cx_strat(result, -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
|
484 | 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
|
485 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
486 | 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
|
487 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
488 | |
| 889 | 489 | 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
|
490 | 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
|
491 | 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
|
492 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
493 | 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
|
494 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
495 | } |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
496 | |
| 889 | 497 | 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
|
498 | 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
|
499 | 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
|
500 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | } |
| 505 | 505 | |
| 889 | 506 | 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
|
507 | 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
|
508 | 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
|
509 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
510 | 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
|
511 | #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
|
512 | 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
|
513 | #else |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
514 | 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
|
515 | #endif |
| 505 | 516 | } |
| 517 | ||
| 889 | 518 | 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
|
519 | 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
|
520 | 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
|
521 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
522 | 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
|
523 | #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
|
524 | 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
|
525 | 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
|
526 | #else |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
527 | 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
|
528 | 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
|
529 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
530 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
531 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
532 | cxmutstr cx_strreplace_( |
| 852 | 533 | 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
|
534 | cxstring str, |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
535 | 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
|
536 | 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
|
537 | 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
|
538 | ) { |
| 886 | 539 | // special cases |
| 540 | if (search.length == 0 || search.length > str.length || replmax == 0) { | |
| 541 | return cx_strdup_a(allocator, str); | |
| 542 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
543 | |
| 886 | 544 | size_t in_len = str.length; |
| 545 | size_t search_len = search.length; | |
| 546 | 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
|
547 | |
| 886 | 548 | // first run, count the occurrences |
| 549 | // and remember where the first is | |
| 550 | size_t occurrences = 1; | |
| 551 | cxstring first = cx_strstr(str, search); | |
| 552 | if (first.length == 0) { | |
| 553 | // special case, no replacements | |
| 554 | return cx_strdup_a(allocator, str); | |
| 555 | } | |
| 556 | cxstring tmp = cx_strsubs(first, search_len); | |
| 557 | while (occurrences < replmax && | |
| 558 | (tmp = cx_strstr(tmp, search)).length > 0) { | |
| 559 | occurrences++; | |
| 560 | tmp = cx_strsubs(tmp, search_len); | |
|
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
561 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
562 | |
| 886 | 563 | // calculate necessary memory |
| 564 | signed long long diff_len = (signed long long) repl_len - search_len; | |
| 565 | size_t out_len = in_len + diff_len * occurrences; | |
| 566 | cxmutstr out = { | |
| 567 | cxMalloc(allocator, out_len + 1), | |
| 568 | out_len | |
| 569 | }; | |
| 570 | 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
|
571 | |
| 886 | 572 | // second run: perform the replacements |
| 573 | // but start where we found the first occurrence | |
| 574 | const char *inp = str.ptr; | |
| 575 | tmp = first; | |
| 576 | char *outp = out.ptr; | |
| 577 | while (occurrences-- > 0 && (tmp = cx_strstr(tmp, search)).length > 0) { | |
| 578 | size_t copylen = tmp.ptr - inp; | |
| 579 | memcpy(outp, inp, copylen); | |
| 580 | outp += copylen; | |
| 581 | memcpy(outp, replacement.ptr, repl_len); | |
| 582 | outp += repl_len; | |
| 583 | inp += copylen + search_len; | |
| 584 | tmp = cx_strsubs(tmp, search_len); | |
|
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
585 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
586 | |
| 886 | 587 | // add the remaining string |
| 588 | size_t copylen = in_len - (inp - str.ptr); | |
| 589 | memcpy(outp, inp, copylen); | |
| 590 | 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
|
591 | |
| 886 | 592 | return out; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
593 | } |
|
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
594 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
595 | 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
|
596 | 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
|
597 | 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
|
598 | 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
|
599 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
600 | 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
|
601 | 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
|
602 | 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
|
603 | 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
|
604 | 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
|
605 | 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
|
606 | 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
|
607 | 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
|
608 | 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
|
609 | 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
|
610 | return ctx; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
611 | } |
|
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
612 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
613 | bool cx_strtok_next_( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
614 | 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
|
615 | 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
|
616 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
617 | // 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
|
618 | 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
|
619 | 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
|
620 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
621 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
622 | // 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
|
623 | 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
|
624 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
625 | // 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
|
626 | 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
|
627 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
628 | // 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
|
629 | 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
|
630 | 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
|
631 | } |
|
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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
633 | // 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
|
634 | if (ctx->delim_more_count > 0) { |
| 852 | 635 | 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
|
636 | 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
|
637 | 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
|
638 | 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
|
639 | 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
|
640 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
641 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
642 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
643 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
644 | // 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
|
645 | 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
|
646 | 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
|
647 | 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
|
648 | 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
|
649 | 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
|
650 | 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
|
651 | 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
|
652 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
653 | return true; |
|
174
e7e56c56d126
updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents:
110
diff
changeset
|
654 | } |
| 505 | 655 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
656 | 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
|
657 | CxStrtokCtx *ctx, |
| 852 | 658 | 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
|
659 | 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
|
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 | 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
|
662 | ctx->delim_more_count = count; |
| 505 | 663 | } |
| 852 | 664 | |
| 665 | #define cx_strtoX_signed_impl(rtype, rmin, rmax) \ | |
| 666 | long long result; \ | |
| 667 | if (cx_strtoll_lc(str, &result, base, groupsep)) { \ | |
| 668 | return -1; \ | |
| 669 | } \ | |
| 670 | if (result < rmin || result > rmax) { \ | |
| 671 | errno = ERANGE; \ | |
| 672 | return -1; \ | |
| 673 | } \ | |
| 674 | *output = (rtype) result; \ | |
| 675 | return 0 | |
| 676 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
677 | int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep) { |
| 852 | 678 | cx_strtoX_signed_impl(short, SHRT_MIN, SHRT_MAX); |
| 679 | } | |
| 680 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
681 | int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep) { |
| 852 | 682 | cx_strtoX_signed_impl(int, INT_MIN, INT_MAX); |
| 683 | } | |
| 684 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
685 | int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep) { |
| 852 | 686 | cx_strtoX_signed_impl(long, LONG_MIN, LONG_MAX); |
| 687 | } | |
| 688 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
689 | int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep) { |
| 852 | 690 | // strategy: parse as unsigned, check range, negate if required |
| 691 | bool neg = false; | |
| 692 | size_t start_unsigned = 0; | |
| 693 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
694 | // emptiness check |
| 852 | 695 | if (str.length == 0) { |
| 696 | errno = EINVAL; | |
| 697 | return -1; | |
| 698 | } | |
| 699 | ||
| 700 | // test if we have a negative sign character | |
| 701 | if (str.ptr[start_unsigned] == '-') { | |
| 702 | neg = true; | |
| 703 | start_unsigned++; | |
| 704 | // must not be followed by positive sign character | |
| 705 | if (str.length == 1 || str.ptr[start_unsigned] == '+') { | |
| 706 | errno = EINVAL; | |
| 707 | return -1; | |
| 708 | } | |
| 709 | } | |
| 710 | ||
| 711 | // now parse the number with strtoull | |
| 712 | unsigned long long v; | |
| 713 | cxstring ustr = start_unsigned == 0 ? str | |
| 714 | : cx_strn(str.ptr + start_unsigned, str.length - start_unsigned); | |
| 715 | int ret = cx_strtoull_lc(ustr, &v, base, groupsep); | |
| 716 | if (ret != 0) return ret; | |
| 717 | if (neg) { | |
| 718 | if (v - 1 > LLONG_MAX) { | |
| 719 | errno = ERANGE; | |
| 720 | return -1; | |
| 721 | } | |
| 722 | *output = -(long long) v; | |
| 723 | return 0; | |
| 724 | } else { | |
| 725 | if (v > LLONG_MAX) { | |
| 726 | errno = ERANGE; | |
| 727 | return -1; | |
| 728 | } | |
| 729 | *output = (long long) v; | |
| 730 | return 0; | |
| 731 | } | |
| 732 | } | |
| 733 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
734 | int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep) { |
| 852 | 735 | cx_strtoX_signed_impl(int8_t, INT8_MIN, INT8_MAX); |
| 736 | } | |
| 737 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
738 | int cx_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep) { |
| 852 | 739 | cx_strtoX_signed_impl(int16_t, INT16_MIN, INT16_MAX); |
| 740 | } | |
| 741 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
742 | int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep) { |
| 852 | 743 | cx_strtoX_signed_impl(int32_t, INT32_MIN, INT32_MAX); |
| 744 | } | |
| 745 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
746 | int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep) { |
| 852 | 747 | assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms |
| 748 | return cx_strtoll_lc(str, (long long*) output, base, groupsep); | |
| 749 | } | |
| 750 | ||
| 751 | #define cx_strtoX_unsigned_impl(rtype, rmax) \ | |
| 752 | uint64_t result; \ | |
| 753 | if (cx_strtou64_lc(str, &result, base, groupsep)) { \ | |
| 754 | return -1; \ | |
| 755 | } \ | |
| 756 | if (result > rmax) { \ | |
| 757 | errno = ERANGE; \ | |
| 758 | return -1; \ | |
| 759 | } \ | |
| 760 | *output = (rtype) result; \ | |
| 761 | return 0 | |
| 762 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
763 | int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep) { |
| 852 | 764 | cx_strtoX_unsigned_impl(unsigned short, USHRT_MAX); |
| 765 | } | |
| 766 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
767 | int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep) { |
| 852 | 768 | cx_strtoX_unsigned_impl(unsigned int, UINT_MAX); |
| 769 | } | |
| 770 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
771 | int cx_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep) { |
| 852 | 772 | cx_strtoX_unsigned_impl(unsigned long, ULONG_MAX); |
| 773 | } | |
| 774 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
775 | int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep) { |
| 852 | 776 | // some sanity checks |
| 777 | if (str.length == 0) { | |
| 778 | errno = EINVAL; | |
| 779 | return -1; | |
| 780 | } | |
| 781 | if (!(base == 2 || base == 8 || base == 10 || base == 16)) { | |
| 782 | errno = EINVAL; | |
| 783 | return -1; | |
| 784 | } | |
| 785 | if (groupsep == NULL) groupsep = ""; | |
| 786 | ||
| 787 | // find the actual start of the number | |
| 788 | if (str.ptr[0] == '+') { | |
| 789 | str.ptr++; | |
| 790 | str.length--; | |
| 791 | if (str.length == 0) { | |
| 792 | errno = EINVAL; | |
| 793 | return -1; | |
| 794 | } | |
| 795 | } | |
| 796 | size_t start = 0; | |
| 797 | ||
| 798 | // if base is 2 or 16, some leading stuff may appear | |
| 799 | if (base == 2) { | |
| 800 | if ((str.ptr[0] | 32) == 'b') { | |
| 801 | start = 1; | |
| 802 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 803 | if ((str.ptr[1] | 32) == 'b') { | |
| 804 | start = 2; | |
| 805 | } | |
| 806 | } | |
| 807 | } else if (base == 16) { | |
| 808 | if ((str.ptr[0] | 32) == 'x' || str.ptr[0] == '#') { | |
| 809 | start = 1; | |
| 810 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 811 | if ((str.ptr[1] | 32) == 'x') { | |
| 812 | start = 2; | |
| 813 | } | |
| 814 | } | |
| 815 | } | |
| 816 | ||
| 817 | // check if there are digits left | |
| 818 | if (start >= str.length) { | |
| 819 | errno = EINVAL; | |
| 820 | return -1; | |
| 821 | } | |
| 822 | ||
| 823 | // now parse the number | |
| 824 | unsigned long long result = 0; | |
| 825 | for (size_t i = start; i < str.length; i++) { | |
| 826 | // ignore group separators | |
| 827 | if (strchr(groupsep, str.ptr[i])) continue; | |
| 828 | ||
| 829 | // determine the digit value of the character | |
| 830 | unsigned char c = str.ptr[i]; | |
| 831 | if (c >= 'a') c = 10 + (c - 'a'); | |
| 832 | else if (c >= 'A') c = 10 + (c - 'A'); | |
| 833 | else if (c >= '0') c = c - '0'; | |
| 834 | else c = 255; | |
| 835 | if (c >= base) { | |
| 836 | errno = EINVAL; | |
| 837 | return -1; | |
| 838 | } | |
| 839 | ||
| 840 | // now combine the digit with what we already have | |
| 841 | unsigned long right = (result & 0xff) * base + c; | |
| 842 | unsigned long long left = (result >> 8) * base + (right >> 8); | |
| 843 | if (left > (ULLONG_MAX >> 8)) { | |
| 844 | errno = ERANGE; | |
| 845 | return -1; | |
| 846 | } | |
| 847 | result = (left << 8) + (right & 0xff); | |
| 848 | } | |
| 849 | ||
| 850 | *output = result; | |
| 851 | return 0; | |
| 852 | } | |
| 853 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
854 | int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep) { |
| 852 | 855 | cx_strtoX_unsigned_impl(uint8_t, UINT8_MAX); |
| 856 | } | |
| 857 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
858 | int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep) { |
| 852 | 859 | cx_strtoX_unsigned_impl(uint16_t, UINT16_MAX); |
| 860 | } | |
| 861 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
862 | int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep) { |
| 852 | 863 | cx_strtoX_unsigned_impl(uint32_t, UINT32_MAX); |
| 864 | } | |
| 865 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
866 | int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep) { |
| 852 | 867 | assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms |
| 868 | return cx_strtoull_lc(str, (unsigned long long*) output, base, groupsep); | |
| 869 | } | |
| 870 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
871 | int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep) { |
| 852 | 872 | #if SIZE_MAX == UINT32_MAX |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
873 | return cx_strtou32_lc_(str, (uint32_t*) output, base, groupsep); |
| 852 | 874 | #elif SIZE_MAX == UINT64_MAX |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
875 | return cx_strtoull_lc_(str, (unsigned long long *) output, base, groupsep); |
| 852 | 876 | #else |
| 877 | #error "unsupported size_t size" | |
| 878 | #endif | |
| 879 | } | |
| 880 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
881 | int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep) { |
| 852 | 882 | // use string to double and add a range check |
| 883 | double d; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
884 | int ret = cx_strtod_lc_(str, &d, decsep, groupsep); |
| 852 | 885 | if (ret != 0) return ret; |
| 886 | // note: FLT_MIN is the smallest POSITIVE number that can be represented | |
| 887 | double test = d < 0 ? -d : d; | |
| 888 | if (test < FLT_MIN || test > FLT_MAX) { | |
| 889 | errno = ERANGE; | |
| 890 | return -1; | |
| 891 | } | |
| 892 | *output = (float) d; | |
| 893 | return 0; | |
| 894 | } | |
| 895 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
896 | int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep) { |
| 852 | 897 | // TODO: overflow check |
| 898 | // TODO: increase precision | |
| 899 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
900 | // emptiness check |
| 852 | 901 | if (str.length == 0) { |
| 902 | errno = EINVAL; | |
| 903 | return -1; | |
| 904 | } | |
| 905 | ||
| 906 | double result = 0.; | |
| 907 | int sign = 1; | |
| 908 | ||
| 909 | // check if there is a sign | |
| 910 | if (str.ptr[0] == '-') { | |
| 911 | sign = -1; | |
| 912 | str.ptr++; | |
| 913 | str.length--; | |
| 914 | } else if (str.ptr[0] == '+') { | |
| 915 | str.ptr++; | |
| 916 | str.length--; | |
| 917 | } | |
| 918 | ||
| 919 | // there must be at least one char to parse | |
| 920 | if (str.length == 0) { | |
| 921 | errno = EINVAL; | |
| 922 | return -1; | |
| 923 | } | |
| 924 | ||
| 925 | // parse all digits until we find the decsep | |
| 926 | size_t pos = 0; | |
| 927 | do { | |
| 889 | 928 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 929 | result = result * 10 + (str.ptr[pos] - '0'); |
| 930 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 931 | break; | |
| 932 | } | |
| 933 | } while (++pos < str.length); | |
| 934 | ||
| 935 | // already done? | |
| 936 | if (pos == str.length) { | |
| 937 | *output = result * sign; | |
| 938 | return 0; | |
| 939 | } | |
| 940 | ||
| 941 | // is the next char the decsep? | |
| 942 | if (str.ptr[pos] == decsep) { | |
| 943 | pos++; | |
| 944 | // it may end with the decsep, if it did not start with it | |
| 945 | if (pos == str.length) { | |
| 946 | if (str.length == 1) { | |
| 947 | errno = EINVAL; | |
| 948 | return -1; | |
| 949 | } else { | |
| 950 | *output = result * sign; | |
| 951 | return 0; | |
| 952 | } | |
| 953 | } | |
| 954 | // parse everything until exponent or end | |
| 955 | double factor = 1.; | |
| 956 | do { | |
| 889 | 957 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 958 | factor *= 0.1; |
| 959 | result = result + factor * (str.ptr[pos] - '0'); | |
| 960 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 961 | break; | |
| 962 | } | |
| 963 | } while (++pos < str.length); | |
| 964 | } | |
| 965 | ||
| 966 | // no exponent? | |
| 967 | if (pos == str.length) { | |
| 968 | *output = result * sign; | |
| 969 | return 0; | |
| 970 | } | |
| 971 | ||
| 972 | // now the next separator MUST be the exponent separator | |
| 973 | // and at least one char must follow | |
| 974 | if ((str.ptr[pos] | 32) != 'e' || str.length <= pos + 1) { | |
| 975 | errno = EINVAL; | |
| 976 | return -1; | |
| 977 | } | |
| 978 | pos++; | |
| 979 | ||
| 980 | // check if we have a sign for the exponent | |
| 981 | double factor = 10.; | |
| 982 | if (str.ptr[pos] == '-') { | |
| 983 | factor = .1; | |
| 984 | pos++; | |
| 985 | } else if (str.ptr[pos] == '+') { | |
| 986 | pos++; | |
| 987 | } | |
| 988 | ||
| 989 | // at least one digit must follow | |
| 990 | if (pos == str.length) { | |
| 991 | errno = EINVAL; | |
| 992 | return -1; | |
| 993 | } | |
| 994 | ||
| 995 | // parse the exponent | |
| 996 | unsigned int exp = 0; | |
| 997 | do { | |
| 889 | 998 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 852 | 999 | exp = 10 * exp + (str.ptr[pos] - '0'); |
| 1000 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1001 | errno = EINVAL; | |
| 1002 | return -1; | |
| 1003 | } | |
| 1004 | } while (++pos < str.length); | |
| 1005 | ||
| 1006 | // apply the exponent by fast exponentiation | |
| 1007 | do { | |
| 1008 | if (exp & 1) { | |
| 1009 | result *= factor; | |
| 1010 | } | |
| 1011 | factor *= factor; | |
| 1012 | } while ((exp >>= 1) > 0); | |
| 1013 | ||
| 1014 | // store the result and exit | |
| 1015 | *output = result * sign; | |
| 1016 | return 0; | |
| 1017 | } |