src/string.c

Sun, 26 Jan 2025 14:37:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Jan 2025 14:37:07 +0100
changeset 1161
747c4baed44f
parent 1155
b77d56a27e9c
child 1162
e3bb67b72d33
permissions
-rw-r--r--

improve cx_strchr()

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

mercurial