src/string.c

Tue, 13 Sep 2022 20:11:26 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 13 Sep 2022 20:11:26 +0200
changeset 584
184e9ebfc3cc
parent 583
0f3c9662f9b5
child 590
02a56701a5cb
permissions
-rw-r--r--

disallow NULL for cx_str() and cx_mutstr()

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 #include "cx/utils.h"
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
31
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
32 #include <string.h>
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
33 #include <stdarg.h>
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
34 #include <stdint.h>
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
35 #include <ctype.h>
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
36
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
37 #ifndef _WIN32
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
38
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
39 #include <strings.h> /* for strncasecmp() */
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
40
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
41 #endif /* _WIN32 */
579
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
42
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
43 cxmutstr cx_mutstr(char *cstring) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
44 return (cxmutstr) {cstring, strlen(cstring)};
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
45 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
46
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
47 cxmutstr cx_mutstrn(
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
48 char *cstring,
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
49 size_t 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 return (cxmutstr) {cstring, length};
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
52 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
53
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
54 cxstring cx_str(const char *cstring) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
55 return (cxstring) {cstring, strlen(cstring)};
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
56 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
57
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
58 cxstring cx_strn(
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
59 const char *cstring,
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
60 size_t 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 return (cxstring) {cstring, length};
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
63 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
64
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
65 cxstring cx_strcast(cxmutstr str) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
66 return (cxstring) {str.ptr, str.length};
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
67 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
68
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
69 void cx_strfree(cxmutstr *str) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
70 free(str->ptr);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
71 str->ptr = NULL;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
72 str->length = 0;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
73 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
74
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
75 void cx_strfree_a(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
76 CxAllocator *alloc,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
77 cxmutstr *str
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
78 ) {
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;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
93 cx_for_n(i, count) {
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
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
102 cxmutstr cx_strcat_a(
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
103 CxAllocator *alloc,
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
104 size_t count,
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
105 ...
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 cxstring *strings = calloc(count, sizeof(cxstring));
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
108 if (!strings) abort();
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
109
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
110 va_list ap;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
111 va_start(ap, count);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
112
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
113 // get all args and overall length
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
114 size_t slen = 0;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
115 cx_for_n(i, count) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
116 cxstring s = va_arg (ap, cxstring);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
117 strings[i] = s;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
118 slen += s.length;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
119 }
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 // create new string
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
122 cxmutstr result;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
123 result.ptr = cxMalloc(alloc, slen + 1);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
124 result.length = slen;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
125 if (result.ptr == NULL) abort();
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
126
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
127 // concatenate strings
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
128 size_t pos = 0;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
129 cx_for_n(i, count) {
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
130 cxstring s = strings[i];
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
131 memcpy(result.ptr + pos, s.ptr, s.length);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
132 pos += s.length;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
133 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
134
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
135 // terminate string
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
136 result.ptr[result.length] = '\0';
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
137
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
138 // free temporary array
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
139 free(strings);
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
140
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
141 return result;
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
142 }
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
143
580
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
144 cxstring cx_strsubs(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
145 cxstring string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
146 size_t start
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
147 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
148 return cx_strsubsl(string, start, string.length - start);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
149 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
150
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
151 cxmutstr cx_strsubs_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
152 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
153 size_t start
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
154 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
155 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
156 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
157
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
158 cxstring cx_strsubsl(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
159 cxstring string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
160 size_t start,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
161 size_t length
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
162 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
163 if (start > string.length) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
164 return (cxstring) {NULL, 0};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
165 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
166
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
167 size_t rem_len = string.length - start;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
168 if (length > rem_len) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
169 length = rem_len;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
170 }
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 return (cxstring) {string.ptr + start, length};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
173 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
174
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
175 cxmutstr cx_strsubsl_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
176 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
177 size_t start,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
178 size_t length
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 result = cx_strsubsl(cx_strcast(string), start, length);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
181 return (cxmutstr) {(char *) result.ptr, result.length};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
182 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
183
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
184 cxstring cx_strchr(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
185 cxstring string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
186 int chr
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 chr = 0xFF & chr;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
189 // TODO: improve by comparing multiple bytes at once
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
190 cx_for_n(i, string.length) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
191 if (string.ptr[i] == chr) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
192 return cx_strsubs(string, i);
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 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
195 return (cxstring) {NULL, 0};
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
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
198 cxmutstr cx_strchr_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
199 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
200 int chr
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_strchr(cx_strcast(string), chr);
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_strrchr(
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 size_t i = string.length;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
212 while (i > 0) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
213 i--;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
214 // TODO: improve by comparing multiple bytes at once
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
215 if (string.ptr[i] == chr) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
216 return cx_strsubs(string, i);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
217 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
218 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
219 return (cxstring) {NULL, 0};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
220 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
221
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
222 cxmutstr cx_strrchr_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
223 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
224 int chr
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
225 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
226 cxstring result = cx_strrchr(cx_strcast(string), chr);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
227 return (cxmutstr) {(char *) result.ptr, result.length};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
228 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
229
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
230 #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
231 ((size_t*)ptable)[index] : (size_t) ((uint8_t*)ptable)[index])
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
232
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
233 #define ptable_w(useheap, ptable, index, src) do {\
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
234 if (!useheap) ((uint8_t*)ptable)[index] = (uint8_t) src;\
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
235 else ((size_t*)ptable)[index] = src;\
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
236 } while (0)
579
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
237
bbc46dcd5255 start implementing string functions
Mike Becker <universe@uap-core.de>
parents: 576
diff changeset
238
580
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
239 cxstring cx_strstr(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
240 cxstring haystack,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
241 cxstring needle
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 if (needle.length == 0) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
244 return haystack;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
245 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
246
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
247 /* optimize for single-char needles */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
248 if (needle.length == 1) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
249 return cx_strchr(haystack, *needle.ptr);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
250 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
251
580
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
252 /*
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
253 * IMPORTANT:
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
254 * 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
255 * 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
256 * 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
257 * and we want to avoid that.
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
258 */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
259
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
260 /* static prefix table */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
261 static uint8_t s_prefix_table[512];
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
262
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
263 /* check pattern length and use appropriate prefix table */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
264 /* if the pattern exceeds static prefix table, allocate on the heap */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
265 register int useheap = needle.length >= 512;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
266 register void *ptable = useheap ? calloc(needle.length + 1,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
267 sizeof(size_t)) : s_prefix_table;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
268
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
269 /* keep counter in registers */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
270 register size_t i, j;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
271
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
272 /* fill prefix table */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
273 i = 0;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
274 j = 0;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
275 ptable_w(useheap, ptable, i, j);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
276 while (i < needle.length) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
277 while (j >= 1 && needle.ptr[j - 1] != needle.ptr[i]) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
278 ptable_r(j, useheap, ptable, j - 1);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
279 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
280 i++;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
281 j++;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
282 ptable_w(useheap, ptable, i, j);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
283 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
284
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
285 /* search */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
286 cxstring result = {NULL, 0};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
287 i = 0;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
288 j = 1;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
289 while (i < haystack.length) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
290 while (j >= 1 && haystack.ptr[i] != needle.ptr[j - 1]) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
291 ptable_r(j, useheap, ptable, j - 1);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
292 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
293 i++;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
294 j++;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
295 if (j - 1 == needle.length) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
296 size_t start = i - needle.length;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
297 result.ptr = haystack.ptr + start;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
298 result.length = haystack.length - start;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
299 break;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
300 }
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
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
303 /* if prefix table was allocated on the heap, free it */
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
304 if (ptable != s_prefix_table) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
305 free(ptable);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
306 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
307
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
308 return result;
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
309 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
310
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
311 cxmutstr cx_strstr_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
312 cxmutstr haystack,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
313 cxstring needle
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
314 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
315 cxstring result = cx_strstr(cx_strcast(haystack), needle);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
316 return (cxmutstr) {(char *) result.ptr, result.length};
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
317 }
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 size_t cx_strsplit(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
320 cxstring string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
321 cxstring delim,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
322 size_t limit,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
323 cxstring *output
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
324 ) {
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
325 /* special case: output limit is zero */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
326 if (limit == 0) return 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
327
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
328 /* special case: delimiter is empty */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
329 if (delim.length == 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
330 output[0] = string;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
331 return 1;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
332 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
333
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
334 /* special cases: delimiter is at least as large as the string */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
335 if (delim.length >= string.length) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
336 /* exact match */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
337 if (cx_strcmp(string, delim) == 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
338 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
339 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
340 return 2;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
341 } else /* no match possible */ {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
342 output[0] = string;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
343 return 1;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
344 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
345 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
346
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
347 size_t n = 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
348 cxstring curpos = string;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
349 while (1) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
350 ++n;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
351 cxstring match = cx_strstr(curpos, delim);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
352 if (match.length > 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
353 /* is the limit reached? */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
354 if (n < limit) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
355 /* copy the current string to the array */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
356 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
357 output[n - 1] = item;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
358 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
359 curpos.ptr += processed;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
360 curpos.length -= processed;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
361 } else {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
362 /* limit reached, copy the _full_ remaining string */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
363 output[n - 1] = curpos;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
364 break;
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 } else {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
367 /* no more matches, copy last string */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
368 output[n - 1] = curpos;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
369 break;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
370 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
371 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
372
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
373 return n;
580
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
374 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
375
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
376 size_t cx_strsplit_a(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
377 CxAllocator *allocator,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
378 cxstring string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
379 cxstring delim,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
380 size_t limit,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
381 cxstring **output
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
382 ) {
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
383 /* find out how many splits we're going to make and allocate memory */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
384 size_t n = 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
385 cxstring curpos = string;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
386 while (1) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
387 ++n;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
388 cxstring match = cx_strstr(curpos, delim);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
389 if (match.length > 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
390 /* is the limit reached? */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
391 if (n < limit) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
392 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
393 curpos.ptr += processed;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
394 curpos.length -= processed;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
395 } else {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
396 /* limit reached */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
397 break;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
398 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
399 } else {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
400 /* no more matches */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
401 break;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
402 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
403 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
404 *output = cxCalloc(allocator, n, sizeof(cxstring));
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
405 return cx_strsplit(string, delim, n, *output);
580
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
406 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
407
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
408 size_t cx_strsplit_m(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
409 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
410 cxstring delim,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
411 size_t limit,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
412 cxmutstr *output
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
413 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
414 return cx_strsplit(cx_strcast(string),
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
415 delim, limit, (cxstring *) output);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
416 }
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
417
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
418 size_t cx_strsplit_ma(
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
419 CxAllocator *allocator,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
420 cxmutstr string,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
421 cxstring delim,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
422 size_t limit,
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
423 cxmutstr **output
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
424 ) {
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
425 return cx_strsplit_a(allocator, cx_strcast(string),
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
426 delim, limit, (cxstring **) output);
aac47db8da0b more implementations of string functions
Mike Becker <universe@uap-core.de>
parents: 579
diff changeset
427 }
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
428
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
429 int cx_strcmp(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
430 cxstring s1,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
431 cxstring s2
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
432 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
433 if (s1.length == s2.length) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
434 return memcmp(s1.ptr, s2.ptr, s1.length);
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
435 } else if (s1.length > s2.length) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
436 return 1;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
437 } else {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
438 return -1;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
439 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
440 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
441
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
442 int cx_strcasecmp(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
443 cxstring s1,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
444 cxstring s2
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
445 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
446 if (s1.length == s2.length) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
447 #ifdef _WIN32
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
448 return _strnicmp(s1.ptr, s2.ptr, s1.length);
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
449 #else
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
450 return strncasecmp(s1.ptr, s2.ptr, s1.length);
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
451 #endif
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
452 } else if (s1.length > s2.length) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
453 return 1;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
454 } else {
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 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
457 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
458
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
459 cxmutstr cx_strdup_a(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
460 CxAllocator *allocator,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
461 cxstring string
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
462 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
463 cxmutstr result = {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
464 cxMalloc(allocator, string.length + 1),
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
465 string.length
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
466 };
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
467 if (result.ptr == NULL) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
468 result.length = 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
469 return result;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
470 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
471 memcpy(result.ptr, string.ptr, string.length);
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
472 result.ptr[string.length] = '\0';
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
473 return result;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
474 }
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 cxstring cx_strtrim(cxstring string) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
477 cxstring result = string;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
478 // TODO: optimize by comparing multiple bytes at once
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
479 while (result.length > 0 && isspace(*result.ptr)) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
480 result.ptr++;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
481 result.length--;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
482 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
483 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
484 result.length--;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
485 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
486 return result;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
487 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
488
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
489 cxmutstr cx_strtrim_m(cxmutstr string) {
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
490 cxstring result = cx_strtrim(cx_strcast(string));
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
491 return (cxmutstr) {(char *) result.ptr, result.length};
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
492 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
493
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
494 bool cx_strprefix(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
495 cxstring string,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
496 cxstring prefix
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
497 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
498 if (string.length < prefix.length) return false;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
499 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
500 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
501
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
502 bool cx_strsuffix(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
503 cxstring string,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
504 cxstring suffix
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
505 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
506 if (string.length < suffix.length) return false;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
507 return memcmp(string.ptr + string.length - suffix.length,
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
508 suffix.ptr, suffix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
509 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
510
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
511 bool cx_strcaseprefix(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
512 cxstring string,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
513 cxstring prefix
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
514 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
515 if (string.length < prefix.length) return false;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
516 #ifdef _WIN32
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
517 return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
518 #else
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
519 return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
520 #endif
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
521 }
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
522
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
523 bool cx_strcasesuffix(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
524 cxstring string,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
525 cxstring suffix
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
526 ) {
581
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
527 if (string.length < suffix.length) return false;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
528 #ifdef _WIN32
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
529 return _strnicmp(string.ptr+string.length-suffix.length,
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
530 suffix.ptr, suffix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
531 #else
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
532 return strncasecmp(string.ptr + string.length - suffix.length,
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
533 suffix.ptr, suffix.length) == 0;
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
534 #endif
c067394737ca implement more string functions
Mike Becker <universe@uap-core.de>
parents: 580
diff changeset
535 }
582
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
536
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
537 void cx_strlower(cxmutstr string) {
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
538 cx_for_n(i, string.length) {
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
539 string.ptr[i] = tolower(string.ptr[i]);
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
540 }
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
541 }
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
542
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
543 void cx_strupper(cxmutstr string) {
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
544 cx_for_n(i, string.length) {
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
545 string.ptr[i] = toupper(string.ptr[i]);
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
546 }
96fa7fa6af4f implement strupper and strlower
Mike Becker <universe@uap-core.de>
parents: 581
diff changeset
547 }
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
548
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
549 #define REPLACE_INDEX_BUFFER_MAX 100
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
550
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
551 struct cx_strreplace_ibuf {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
552 size_t *buf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
553 unsigned int len; /* small indices */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
554 struct cx_strreplace_ibuf *next;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
555 };
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
556
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
557 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
558 while (buf) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
559 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
560 free(buf->buf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
561 free(buf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
562 buf = next;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
563 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
564 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
565
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
566 cxmutstr cx_strreplacen_a(
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
567 CxAllocator *allocator,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
568 cxstring str,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
569 cxstring pattern,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
570 cxstring replacement,
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
571 size_t replmax
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
572 ) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
573
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
574 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
575 return cx_strdup_a(allocator, str);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
576
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
577 /* Compute expected buffer length */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
578 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
579 size_t ibuflen = replmax < ibufmax ? replmax : ibufmax;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
580 if (ibuflen > REPLACE_INDEX_BUFFER_MAX) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
581 ibuflen = REPLACE_INDEX_BUFFER_MAX;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
582 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
583
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
584 /* Allocate first index buffer */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
585 struct cx_strreplace_ibuf *firstbuf, *curbuf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
586 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
587 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
588 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
589 if (!firstbuf->buf) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
590 free(firstbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
591 return cx_mutstrn(NULL, 0);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
592 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
593
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
594 /* Search occurrences */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
595 cxstring searchstr = str;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
596 size_t found = 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
597 do {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
598 cxstring match = cx_strstr(searchstr, pattern);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
599 if (match.length > 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
600 /* Allocate next buffer in chain, if required */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
601 if (curbuf->len == ibuflen) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
602 struct cx_strreplace_ibuf *nextbuf =
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
603 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
604 if (!nextbuf) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
605 cx_strrepl_free_ibuf(firstbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
606 return cx_mutstrn(NULL, 0);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
607 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
608 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
609 if (!nextbuf->buf) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
610 free(nextbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
611 cx_strrepl_free_ibuf(firstbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
612 return cx_mutstrn(NULL, 0);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
613 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
614 curbuf->next = nextbuf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
615 curbuf = nextbuf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
616 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
617
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
618 /* Record match index */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
619 found++;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
620 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
621 curbuf->buf[curbuf->len++] = idx;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
622 searchstr.ptr = match.ptr + pattern.length;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
623 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
624 } else {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
625 break;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
626 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
627 } while (searchstr.length > 0 && found < replmax);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
628
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
629 /* Allocate result string */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
630 cxmutstr result;
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 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
633 size_t rcount = 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
634 curbuf = firstbuf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
635 do {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
636 rcount += curbuf->len;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
637 curbuf = curbuf->next;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
638 } while (curbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
639 result.length = str.length + rcount * adjlen;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
640 result.ptr = cxMalloc(allocator, result.length);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
641 if (!result.ptr) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
642 cx_strrepl_free_ibuf(firstbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
643 return cx_mutstrn(NULL, 0);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
644 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
645 }
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 /* Build result string */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
648 curbuf = firstbuf;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
649 size_t srcidx = 0;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
650 char *destptr = result.ptr;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
651 do {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
652 for (size_t i = 0; i < curbuf->len; i++) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
653 /* Copy source part up to next match*/
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
654 size_t idx = curbuf->buf[i];
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
655 size_t srclen = idx - srcidx;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
656 if (srclen > 0) {
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
657 memcpy(destptr, str.ptr + srcidx, srclen);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
658 destptr += srclen;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
659 srcidx += srclen;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
660 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
661
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
662 /* Copy the replacement and skip the source pattern */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
663 srcidx += pattern.length;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
664 memcpy(destptr, replacement.ptr, replacement.length);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
665 destptr += replacement.length;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
666 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
667 curbuf = curbuf->next;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
668 } while (curbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
669 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
670
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
671 /* Free index buffer */
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
672 cx_strrepl_free_ibuf(firstbuf);
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
673
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
674 return result;
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
675 }
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
676
0f3c9662f9b5 add tests and missing implementations for strings
Mike Becker <universe@uap-core.de>
parents: 582
diff changeset
677

mercurial