Fri, 20 Dec 2024 16:51:01 +0100
add cxJsonArrIter() - fixes #524
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 | |
ba0c4ff6698e
first proposal for the string header
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "cx/string.h" |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
30 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
31 | #include <string.h> |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
32 | #include <stdarg.h> |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
33 | #include <ctype.h> |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
34 | |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
35 | #ifndef _WIN32 |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
36 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
37 | #include <strings.h> // for strncasecmp() |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
38 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
39 | #endif // _WIN32 |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
40 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
41 | cxmutstr cx_mutstr(char *cstring) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
42 | return (cxmutstr) {cstring, strlen(cstring)}; |
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 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
45 | cxmutstr cx_mutstrn( |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
46 | char *cstring, |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
47 | size_t length |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
48 | ) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
49 | return (cxmutstr) {cstring, length}; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
50 | } |
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 | cxstring cx_str(const char *cstring) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
53 | return (cxstring) {cstring, strlen(cstring)}; |
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 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
56 | cxstring cx_strn( |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
57 | const char *cstring, |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
58 | size_t length |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
59 | ) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
60 | return (cxstring) {cstring, length}; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
61 | } |
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 | cxstring cx_strcast(cxmutstr str) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
64 | return (cxstring) {str.ptr, str.length}; |
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 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
67 | void cx_strfree(cxmutstr *str) { |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
962
diff
changeset
|
68 | if (str == NULL) return; |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
69 | free(str->ptr); |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
70 | str->ptr = NULL; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
71 | str->length = 0; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
72 | } |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
73 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
74 | 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
|
75 | const CxAllocator *alloc, |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
76 | cxmutstr *str |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
77 | ) { |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
962
diff
changeset
|
78 | if (str == NULL) return; |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
79 | cxFree(alloc, str->ptr); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
80 | str->ptr = NULL; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
81 | str->length = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
82 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
83 | |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
84 | size_t cx_strlen( |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
85 | size_t count, |
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 | ) { |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
88 | if (count == 0) return 0; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
89 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
90 | va_list ap; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
91 | va_start(ap, count); |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
92 | size_t size = 0; |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
93 | for (size_t i = 0; i < count; i++) { |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
94 | cxstring str = va_arg(ap, cxstring); |
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 |
697
ebdce4bf262b
add cx_strcat_m() and cx_strcat_ma() for in-place concatenation
Mike Becker <universe@uap-core.de>
parents:
693
diff
changeset
|
125 | size_t slen = str.length; |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
126 | for (size_t i = 0; i < count; i++) { |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
127 | cxstring s = va_arg (ap, cxstring); |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
128 | strings[i] = s; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
129 | slen += s.length; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
130 | } |
697
ebdce4bf262b
add cx_strcat_m() and cx_strcat_ma() for in-place concatenation
Mike Becker <universe@uap-core.de>
parents:
693
diff
changeset
|
131 | va_end(ap); |
579
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 | // 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
|
134 | 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
|
135 | 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
|
136 | 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
|
137 | } else { |
1001
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
138 | 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
|
139 | } |
1001
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
140 | if (newstr == NULL) { |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
141 | free(strings); |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
142 | return (cxmutstr) {NULL, 0}; |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
143 | } |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
144 | str.ptr = newstr; |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
145 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
146 | // 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
|
147 | 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
|
148 | str.length = slen; |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
149 | for (size_t i = 0; i < count; i++) { |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
150 | 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
|
151 | memcpy(str.ptr + pos, s.ptr, s.length); |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
152 | pos += s.length; |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
153 | } |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
154 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
155 | // 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
|
156 | str.ptr[str.length] = '\0'; |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
157 | |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
158 | // free temporary array |
1001
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
159 | if (strings != strings_stack) { |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
160 | free(strings); |
5c9ec5a0a4ef
change cx_strcat variants to allow handling of ENOMEM
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
161 | } |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
162 | |
697
ebdce4bf262b
add cx_strcat_m() and cx_strcat_ma() for in-place concatenation
Mike Becker <universe@uap-core.de>
parents:
693
diff
changeset
|
163 | return str; |
579
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
164 | } |
bbc46dcd5255
start implementing string functions
Mike Becker <universe@uap-core.de>
parents:
576
diff
changeset
|
165 | |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
166 | cxstring cx_strsubs( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
167 | cxstring string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
168 | size_t start |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
169 | ) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
170 | return cx_strsubsl(string, start, string.length - start); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
171 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
172 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
173 | cxmutstr cx_strsubs_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
174 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
175 | size_t start |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
176 | ) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
177 | 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
|
178 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
179 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
180 | cxstring cx_strsubsl( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
181 | cxstring string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
182 | size_t start, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
183 | size_t length |
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 | if (start > string.length) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
186 | return (cxstring) {NULL, 0}; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
187 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
188 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
189 | size_t rem_len = string.length - start; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
190 | if (length > rem_len) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
191 | length = rem_len; |
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 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
194 | return (cxstring) {string.ptr + start, length}; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
195 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
196 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
197 | cxmutstr cx_strsubsl_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
198 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
199 | size_t start, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
200 | size_t length |
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 | 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
|
203 | return (cxmutstr) {(char *) result.ptr, result.length}; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
204 | } |
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 | cxstring cx_strchr( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
207 | cxstring string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
208 | int chr |
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 | chr = 0xFF & chr; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
211 | // TODO: improve by comparing multiple bytes at once |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
212 | for (size_t i = 0; i < string.length; i++) { |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
213 | if (string.ptr[i] == chr) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
214 | return cx_strsubs(string, i); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
215 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
216 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
217 | return (cxstring) {NULL, 0}; |
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 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
220 | cxmutstr cx_strchr_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
221 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
222 | int chr |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
223 | ) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
224 | cxstring result = cx_strchr(cx_strcast(string), chr); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
225 | return (cxmutstr) {(char *) result.ptr, result.length}; |
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 | cxstring cx_strrchr( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
229 | cxstring 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 | chr = 0xFF & chr; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
233 | size_t i = string.length; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
234 | while (i > 0) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
235 | i--; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
236 | // TODO: improve by comparing multiple bytes at once |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
237 | if (string.ptr[i] == chr) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
238 | return cx_strsubs(string, i); |
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 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
241 | return (cxstring) {NULL, 0}; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
242 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
243 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
244 | cxmutstr cx_strrchr_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
245 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
246 | int chr |
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 | cxstring result = cx_strrchr(cx_strcast(string), chr); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
249 | return (cxmutstr) {(char *) result.ptr, result.length}; |
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 | |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
252 | #ifndef CX_STRSTR_SBO_SIZE |
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
253 | #define CX_STRSTR_SBO_SIZE 512 |
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
254 | #endif |
926
8fdd8d78c14b
fix several survivors of east-const and some missing consts
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
255 | 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
|
256 | |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
257 | cxstring cx_strstr( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
258 | cxstring haystack, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
259 | cxstring needle |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
260 | ) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
261 | if (needle.length == 0) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
262 | return haystack; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
263 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
264 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
265 | // optimize for single-char needles |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
266 | if (needle.length == 1) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
267 | return cx_strchr(haystack, *needle.ptr); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
268 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
269 | |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
270 | /* |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
271 | * IMPORTANT: |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
272 | * 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
|
273 | * 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
|
274 | * 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
|
275 | * and we want to avoid that. |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
276 | */ |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
277 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
278 | // local prefix table |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
279 | 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
|
280 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
281 | // 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
|
282 | // if the pattern exceeds static prefix table, allocate on the heap |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
283 | 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
|
284 | 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
|
285 | sizeof(size_t)) : s_prefix_table; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
286 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
287 | // keep counter in registers |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
288 | register size_t i, j; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
289 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
290 | // fill prefix table |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
291 | i = 0; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
292 | j = 0; |
591
7df0bcaecffa
fix over-optimization of strstr
Mike Becker <universe@uap-core.de>
parents:
590
diff
changeset
|
293 | ptable[i] = j; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
294 | while (i < needle.length) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
295 | 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
|
296 | j = ptable[j - 1]; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
297 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
298 | i++; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
299 | j++; |
591
7df0bcaecffa
fix over-optimization of strstr
Mike Becker <universe@uap-core.de>
parents:
590
diff
changeset
|
300 | ptable[i] = j; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
301 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
302 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
303 | // search |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
304 | cxstring result = {NULL, 0}; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
305 | i = 0; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
306 | j = 1; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
307 | while (i < haystack.length) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
308 | 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
|
309 | j = ptable[j - 1]; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
310 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
311 | i++; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
312 | j++; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
313 | if (j - 1 == needle.length) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
314 | size_t start = i - needle.length; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
315 | result.ptr = haystack.ptr + start; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
316 | result.length = haystack.length - start; |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
317 | break; |
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 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
320 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
321 | // if prefix table was allocated on the heap, free it |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
322 | if (ptable != s_prefix_table) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
323 | free(ptable); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
324 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
325 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
326 | return result; |
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 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
329 | cxmutstr cx_strstr_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
330 | cxmutstr haystack, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
331 | cxstring needle |
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 | cxstring result = cx_strstr(cx_strcast(haystack), needle); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
334 | return (cxmutstr) {(char *) result.ptr, result.length}; |
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 | size_t cx_strsplit( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
338 | cxstring string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
339 | cxstring delim, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
340 | size_t limit, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
341 | cxstring *output |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
342 | ) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
343 | // 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
|
344 | if (limit == 0) return 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
345 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
346 | // special case: delimiter is empty |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
347 | if (delim.length == 0) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
348 | output[0] = string; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
349 | return 1; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
350 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
351 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
352 | // 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
|
353 | if (delim.length >= string.length) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
354 | // exact match |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
355 | if (cx_strcmp(string, delim) == 0) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
356 | 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
|
357 | 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
|
358 | return 2; |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
359 | } else { |
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
360 | // no match possible |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
361 | output[0] = string; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
362 | return 1; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
363 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
364 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
365 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
366 | size_t n = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
367 | cxstring curpos = string; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
368 | while (1) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
369 | ++n; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
370 | cxstring match = cx_strstr(curpos, delim); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
371 | if (match.length > 0) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
372 | // is the limit reached? |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
373 | if (n < limit) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
374 | // 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
|
375 | 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
|
376 | output[n - 1] = item; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
377 | 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
|
378 | curpos.ptr += processed; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
379 | curpos.length -= processed; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
380 | } else { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
381 | // 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
|
382 | output[n - 1] = curpos; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
383 | break; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
384 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
385 | } else { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
386 | // 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
|
387 | output[n - 1] = curpos; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
388 | break; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
389 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
390 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
391 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
392 | return n; |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
393 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
394 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
395 | 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
|
396 | const CxAllocator *allocator, |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
397 | cxstring string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
398 | cxstring delim, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
399 | size_t limit, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
400 | cxstring **output |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
401 | ) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
402 | // 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
|
403 | size_t n = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
404 | cxstring curpos = string; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
405 | while (1) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
406 | ++n; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
407 | cxstring match = cx_strstr(curpos, delim); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
408 | if (match.length > 0) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
409 | // is the limit reached? |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
410 | if (n < limit) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
411 | 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
|
412 | curpos.ptr += processed; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
413 | curpos.length -= processed; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
414 | } else { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
415 | // limit reached |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
416 | break; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
417 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
418 | } else { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
419 | // no more matches |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
420 | break; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
421 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
422 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
423 | *output = cxCalloc(allocator, n, sizeof(cxstring)); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
424 | return cx_strsplit(string, delim, n, *output); |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
425 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
426 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
427 | size_t cx_strsplit_m( |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
428 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
429 | cxstring delim, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
430 | size_t limit, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
431 | cxmutstr *output |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
432 | ) { |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
433 | return cx_strsplit(cx_strcast(string), |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
434 | delim, limit, (cxstring *) output); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
435 | } |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
436 | |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
437 | 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
|
438 | const CxAllocator *allocator, |
580
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
439 | cxmutstr string, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
440 | cxstring delim, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
441 | size_t limit, |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
442 | cxmutstr **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 | return cx_strsplit_a(allocator, cx_strcast(string), |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
445 | delim, limit, (cxstring **) output); |
aac47db8da0b
more implementations of string functions
Mike Becker <universe@uap-core.de>
parents:
579
diff
changeset
|
446 | } |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
447 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
448 | int cx_strcmp( |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
449 | cxstring s1, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
450 | cxstring s2 |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
451 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
452 | if (s1.length == s2.length) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
453 | return memcmp(s1.ptr, s2.ptr, s1.length); |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
454 | } else if (s1.length > s2.length) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
455 | return 1; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
456 | } else { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
457 | return -1; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
458 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
459 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
460 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
461 | int cx_strcasecmp( |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
462 | cxstring s1, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
463 | cxstring s2 |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
464 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
465 | if (s1.length == s2.length) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
466 | #ifdef _WIN32 |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
467 | return _strnicmp(s1.ptr, s2.ptr, s1.length); |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
468 | #else |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
469 | return strncasecmp(s1.ptr, s2.ptr, s1.length); |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
470 | #endif |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
471 | } else if (s1.length > s2.length) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
472 | return 1; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
473 | } else { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
474 | return -1; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
475 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
476 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
477 | |
657
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
478 | 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
|
479 | const void *s1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
480 | const void *s2 |
657
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
481 | ) { |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
482 | const cxstring *left = s1; |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
483 | const cxstring *right = s2; |
657
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
484 | return cx_strcmp(*left, *right); |
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
485 | } |
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
486 | |
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
487 | 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
|
488 | const void *s1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
489 | const void *s2 |
657
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
490 | ) { |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
491 | const cxstring *left = s1; |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
492 | const cxstring *right = s2; |
657
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
493 | return cx_strcasecmp(*left, *right); |
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
494 | } |
3eeadf666d6b
add CxListComparator compatible string compare functions
Mike Becker <universe@uap-core.de>
parents:
645
diff
changeset
|
495 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
496 | 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
|
497 | const CxAllocator *allocator, |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
498 | cxstring string |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
499 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
500 | cxmutstr result = { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
501 | cxMalloc(allocator, string.length + 1), |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
502 | string.length |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
503 | }; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
504 | if (result.ptr == NULL) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
505 | result.length = 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
506 | return result; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
507 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
508 | memcpy(result.ptr, string.ptr, string.length); |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
509 | result.ptr[string.length] = '\0'; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
510 | return result; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
511 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
512 | |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
513 | cxstring cx_strtrim(cxstring string) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
514 | cxstring result = string; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
515 | // TODO: optimize by comparing multiple bytes at once |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
516 | while (result.length > 0 && isspace(*result.ptr)) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
517 | result.ptr++; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
518 | result.length--; |
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 | while (result.length > 0 && isspace(result.ptr[result.length - 1])) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
521 | result.length--; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
522 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
523 | return result; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
524 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
525 | |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
526 | cxmutstr cx_strtrim_m(cxmutstr string) { |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
527 | cxstring result = cx_strtrim(cx_strcast(string)); |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
528 | return (cxmutstr) {(char *) result.ptr, result.length}; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
529 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
530 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
531 | bool cx_strprefix( |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
532 | cxstring string, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
533 | cxstring prefix |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
534 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
535 | if (string.length < prefix.length) return false; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
536 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
537 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
538 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
539 | bool cx_strsuffix( |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
540 | cxstring string, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
541 | cxstring suffix |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
542 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
543 | if (string.length < suffix.length) return false; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
544 | return memcmp(string.ptr + string.length - suffix.length, |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
545 | suffix.ptr, suffix.length) == 0; |
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_strcaseprefix( |
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 | #ifdef _WIN32 |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
554 | return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
555 | #else |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
556 | return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
557 | #endif |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
558 | } |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
559 | |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
560 | bool cx_strcasesuffix( |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
561 | cxstring string, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
562 | cxstring suffix |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
563 | ) { |
581
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
564 | if (string.length < suffix.length) return false; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
565 | #ifdef _WIN32 |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
566 | return _strnicmp(string.ptr+string.length-suffix.length, |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
567 | suffix.ptr, suffix.length) == 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
568 | #else |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
569 | return strncasecmp(string.ptr + string.length - suffix.length, |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
570 | suffix.ptr, suffix.length) == 0; |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
571 | #endif |
c067394737ca
implement more string functions
Mike Becker <universe@uap-core.de>
parents:
580
diff
changeset
|
572 | } |
582
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
573 | |
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
574 | void cx_strlower(cxmutstr string) { |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
575 | for (size_t i = 0; i < string.length; i++) { |
593
ea9b41b5ebbc
explicitly cast int to char
Mike Becker <universe@uap-core.de>
parents:
591
diff
changeset
|
576 | string.ptr[i] = (char) tolower(string.ptr[i]); |
582
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
577 | } |
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
578 | } |
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
579 | |
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
580 | void cx_strupper(cxmutstr string) { |
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
926
diff
changeset
|
581 | for (size_t i = 0; i < string.length; i++) { |
593
ea9b41b5ebbc
explicitly cast int to char
Mike Becker <universe@uap-core.de>
parents:
591
diff
changeset
|
582 | string.ptr[i] = (char) toupper(string.ptr[i]); |
582
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
583 | } |
96fa7fa6af4f
implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents:
581
diff
changeset
|
584 | } |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
585 | |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
586 | #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
|
587 | #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
|
588 | #endif |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
589 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
590 | struct cx_strreplace_ibuf { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
591 | size_t *buf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
592 | struct cx_strreplace_ibuf *next; |
590
02a56701a5cb
fix missing zero-termination in strreplace
Mike Becker <universe@uap-core.de>
parents:
583
diff
changeset
|
593 | unsigned int len; |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
594 | }; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
595 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
596 | static void cx_strrepl_free_ibuf(struct cx_strreplace_ibuf *buf) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
597 | while (buf) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
598 | 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
|
599 | free(buf->buf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
600 | free(buf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
601 | buf = next; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
602 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
603 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
604 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
605 | 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
|
606 | const CxAllocator *allocator, |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
607 | cxstring str, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
608 | cxstring pattern, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
609 | cxstring replacement, |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
610 | size_t replmax |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
611 | ) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
612 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
613 | 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
|
614 | return cx_strdup_a(allocator, str); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
615 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
616 | // Compute expected buffer length |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
617 | 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
|
618 | 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
|
619 | 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
|
620 | 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
|
621 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
622 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
623 | // Allocate first index buffer |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
624 | struct cx_strreplace_ibuf *firstbuf, *curbuf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
625 | firstbuf = curbuf = 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
|
626 | if (!firstbuf) return cx_mutstrn(NULL, 0); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
627 | firstbuf->buf = calloc(ibuflen, sizeof(size_t)); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
628 | if (!firstbuf->buf) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
629 | free(firstbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
630 | return cx_mutstrn(NULL, 0); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
631 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
632 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
633 | // Search occurrences |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
634 | cxstring searchstr = str; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
635 | size_t found = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
636 | do { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
637 | cxstring match = cx_strstr(searchstr, pattern); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
638 | if (match.length > 0) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
639 | // 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
|
640 | if (curbuf->len == ibuflen) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
641 | struct cx_strreplace_ibuf *nextbuf = |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
642 | 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
|
643 | if (!nextbuf) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
644 | cx_strrepl_free_ibuf(firstbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
645 | return cx_mutstrn(NULL, 0); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
646 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
647 | 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
|
648 | if (!nextbuf->buf) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
649 | free(nextbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
650 | cx_strrepl_free_ibuf(firstbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
651 | return cx_mutstrn(NULL, 0); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
652 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
653 | curbuf->next = nextbuf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
654 | curbuf = nextbuf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
655 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
656 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
657 | // Record match index |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
658 | found++; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
659 | 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
|
660 | curbuf->buf[curbuf->len++] = idx; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
661 | searchstr.ptr = match.ptr + pattern.length; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
662 | 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
|
663 | } else { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
664 | break; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
665 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
666 | } while (searchstr.length > 0 && found < replmax); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
667 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
668 | // Allocate result string |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
669 | cxmutstr result; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
670 | { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
671 | 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
|
672 | size_t rcount = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
673 | curbuf = firstbuf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
674 | do { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
675 | rcount += curbuf->len; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
676 | curbuf = curbuf->next; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
677 | } while (curbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
678 | result.length = str.length + rcount * adjlen; |
590
02a56701a5cb
fix missing zero-termination in strreplace
Mike Becker <universe@uap-core.de>
parents:
583
diff
changeset
|
679 | 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
|
680 | if (!result.ptr) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
681 | cx_strrepl_free_ibuf(firstbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
682 | return cx_mutstrn(NULL, 0); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
683 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
684 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
685 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
686 | // Build result string |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
687 | curbuf = firstbuf; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
688 | size_t srcidx = 0; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
689 | char *destptr = result.ptr; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
690 | do { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
691 | 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
|
692 | // 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
|
693 | size_t idx = curbuf->buf[i]; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
694 | size_t srclen = idx - srcidx; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
695 | if (srclen > 0) { |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
696 | memcpy(destptr, str.ptr + srcidx, srclen); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
697 | destptr += srclen; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
698 | srcidx += srclen; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
699 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
700 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
701 | // 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
|
702 | srcidx += pattern.length; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
703 | memcpy(destptr, replacement.ptr, replacement.length); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
704 | destptr += replacement.length; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
705 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
706 | curbuf = curbuf->next; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
707 | } while (curbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
708 | 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
|
709 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
710 | // 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
|
711 | result.ptr[result.length] = '\0'; |
02a56701a5cb
fix missing zero-termination in strreplace
Mike Becker <universe@uap-core.de>
parents:
583
diff
changeset
|
712 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
593
diff
changeset
|
713 | // Free index buffer |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
714 | cx_strrepl_free_ibuf(firstbuf); |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
715 | |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
716 | return result; |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
717 | } |
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
718 | |
645
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
719 | CxStrtokCtx cx_strtok( |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
720 | cxstring str, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
721 | cxstring delim, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
722 | size_t limit |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
723 | ) { |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
724 | CxStrtokCtx ctx; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
725 | ctx.str = str; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
726 | ctx.delim = delim; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
727 | ctx.limit = limit; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
728 | ctx.pos = 0; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
729 | ctx.next_pos = 0; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
730 | ctx.delim_pos = 0; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
731 | ctx.found = 0; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
732 | ctx.delim_more = NULL; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
733 | ctx.delim_more_count = 0; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
734 | return ctx; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
735 | } |
583
0f3c9662f9b5
add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents:
582
diff
changeset
|
736 | |
645
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
737 | CxStrtokCtx cx_strtok_m( |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
738 | cxmutstr str, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
739 | cxstring delim, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
740 | size_t limit |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
741 | ) { |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
742 | return cx_strtok(cx_strcast(str), delim, 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 | |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
745 | bool cx_strtok_next( |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
746 | CxStrtokCtx *ctx, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
747 | cxstring *token |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
748 | ) { |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
749 | // abortion criteria |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
750 | 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
|
751 | return false; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
752 | } |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
753 | |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
754 | // determine the search start |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
755 | 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
|
756 | |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
757 | // search the next delimiter |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
758 | cxstring delim = cx_strstr(haystack, ctx->delim); |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
759 | |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
760 | // if found, make delim capture exactly the delimiter |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
761 | if (delim.length > 0) { |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
762 | delim.length = ctx->delim.length; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
763 | } |
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 | // if more delimiters are specified, check them now |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
766 | 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
|
767 | 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
|
768 | 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
|
769 | 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
|
770 | delim.ptr = d.ptr; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
771 | delim.length = ctx->delim_more[i].length; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
772 | } |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
773 | } |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
774 | } |
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 | // store the token information and adjust the context |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
777 | ctx->found++; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
778 | ctx->pos = ctx->next_pos; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
779 | token->ptr = &ctx->str.ptr[ctx->pos]; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
780 | ctx->delim_pos = delim.length == 0 ? |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
781 | 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
|
782 | token->length = ctx->delim_pos - ctx->pos; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
783 | ctx->next_pos = ctx->delim_pos + delim.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 | return true; |
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 | bool cx_strtok_next_m( |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
789 | CxStrtokCtx *ctx, |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
790 | cxmutstr *token |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
791 | ) { |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
792 | return cx_strtok_next(ctx, (cxstring *) token); |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
793 | } |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
794 | |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
795 | void cx_strtok_delim( |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
796 | CxStrtokCtx *ctx, |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
806
diff
changeset
|
797 | const cxstring *delim, |
645
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
798 | size_t count |
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 | ctx->delim_more = delim; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
801 | ctx->delim_more_count = count; |
ec50abb285ad
add strtok API - fixes #220
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
802 | } |