Debian Codegraph @

melder/melder_str32.cpp

raw symbols SCIP
1 /* melder_str32.cpp
2 *
3 * Copyright (C) 1992-2018,2020-2022 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
20
21 #pragma mark - first one 8-bit string function...
22
23 integercxx . . $ integer# Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (const conststring8cxx . . $ conststring8# stringOrNull) {
24 if (! stringOrNull)
25 return 0;
26 const char *p = & stringOrNull [0];
27 while (*p != '\0')
28 ++ p;
29 const integercxx . . $ integer# result = p - stringOrNull;
30 if (sizeof (integercxx . . $ integer#) == 4)
31 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (result >= 0);
32 return result;
33 }
34
35 #pragma mark - ...then two 16-bit string functions...
36
37 integercxx . . $ integer# Melder16_lengthcxx . . $ Melder16_length(7c057485137234bf). (const conststring16cxx . . $ conststring16# stringOrNull) noexcept {
38 if (! stringOrNull)
39 return 0;
40 const char16cxx . . $ char16# *p = & stringOrNull [0];
41 while (*p != u'\0')
42 ++ p;
43 return p - stringOrNull;
44 }
45 mutablestring16cxx . . $ mutablestring16# str16cpycxx . . $ str16cpy(cee0347b89119601). (const mutablestring16cxx . . $ mutablestring16# target, /*mut*/ conststring16cxx . . $ conststring16# source) noexcept {
46 char16cxx . . $ char16# *p = & target [0];
47 while (* source != u'\0')
48 * p ++ = * source ++;
49 *p = u'\0';
50 return target;
51 }
52
53 #pragma mark - ...and the remainder are 32-bit string functions
54
55 mutablestring32cxx . . $ mutablestring32# str32cpycxx . . $ str32cpy(6ab934322573b179). (const mutablestring32cxx . . $ mutablestring32# target, /*mut*/ conststring32cxx . . $ conststring32# source) noexcept {
56 char32cxx . . $ char32# *p = & target [0];
57 while (* source != U'\0')
58 * p ++ = * source ++;
59 *p = U'\0';
60 return target;
61 }
62 mutablestring32cxx . . $ mutablestring32# str32catcxx . . $ str32cat(6ab934322573b179). (const mutablestring32cxx . . $ mutablestring32# target, /*mut*/ conststring32cxx . . $ conststring32# source) noexcept {
63 char32cxx . . $ char32# *p = & target [0];
64 while (*p != U'\0')
65 ++ p;
66 while (* source != U'\0')
67 * p ++ = * source ++;
68 *p = U'\0';
69 return target;
70 }
71 char32cxx . . $ char32# * stp32cpycxx . . $ stp32cpy(6ab934322573b179). (const mutablestring32cxx . . $ mutablestring32# target, /*mut*/ conststring32cxx . . $ conststring32# source) noexcept {
72 char32cxx . . $ char32# *p = & target [0];
73 while (* source != U'\0')
74 * p ++ = * source ++;
75 *p = U'\0';
76 return p;
77 }
78 mutablestring32cxx . . $ mutablestring32# str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (const mutablestring32cxx . . $ mutablestring32# target, /*mut*/ conststring32cxx . . $ conststring32# source, /*mut*/ integercxx . . $ integer# n) noexcept {
79 char32cxx . . $ char32# *p = & target [0];
80 for (; n > 0 && *source != U'\0'; -- n)
81 * p ++ = * source ++;
82 for (; n > 0; -- n)
83 * p ++ = U'\0';
84 return target;
85 }
86
87 #pragma mark - SEARCHING
88
89 char32cxx . . $ char32# * str32strcxx . . $ str32str(2de17cd8f7c584a6). (/*mut*/ conststring32cxx . . $ conststring32# string, /*mut*/ conststring32cxx . . $ conststring32# find) noexcept {
90 const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (find);
91 if (length == 0)
92 return (char32cxx . . $ char32# *) string;
93 char32cxx . . $ char32# firstCharacter = * find ++; // optimization
94 do {
95 char32cxx . . $ char32# kar;
96 do {
97 kar = * string ++;
98 if (kar == U'\0')
99 return nullptr;
100 } while (kar != firstCharacter);
101 } while (str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (string, find, length - 1));
102 return (char32cxx . . $ char32# *) (string - 1);
103 }
104 char32cxx . . $ char32# * str32str_caseInsensitivecxx . . $ str32str_caseInsensitive(2de17cd8f7c584a6). (/*mut*/ conststring32cxx . . $ conststring32# string, /*mut*/ conststring32cxx . . $ conststring32# find) noexcept {
105 const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (find);
106 if (length == 0)
107 return (char32cxx . . $ char32# *) string;
108 char32cxx . . $ char32# firstCharacter = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (* find ++); // optimization
109 do {
110 char32cxx . . $ char32# kar;
111 do {
112 kar = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (* string ++);
113 if (kar == U'\0')
114 return nullptr;
115 } while (kar != firstCharacter);
116 } while (str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (string, find, length - 1));
117 return (char32cxx . . $ char32# *) (string - 1);
118 }
119 char32cxx . . $ char32# * str32str_optionallyCaseSensitivecxx . . $ str32str_optionallyCaseSensitive(7e4d3d07cdd0a044). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# find, bool caseSensitive) noexcept {
120 return caseSensitive ? str32strcxx . . $ str32str(2de17cd8f7c584a6). (string, find) : str32str_caseInsensitivecxx . . $ str32str_caseInsensitive(2de17cd8f7c584a6). (string, find);
121 }
122
123 #pragma mark - EQUIVALENCE OF TWO STRINGS
124
125 int str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
126 for (;; ++ string1, ++ string2) {
127 const char32cxx . . $ char32# kar1 = *string1, kar2 = *string2;
128 if (kar1 < kar2)
129 return -1;
130 if (kar1 > kar2)
131 return +1;
132 /*
133 kar1 is now equal to kar2
134 */
135 if (kar1 == U'\0')
136 return 0;
137 }
138 }
139 int Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
140 if (! string1) string1 = U"";
141 if (! string2) string2 = U"";
142 return str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (string1, string2);
143 }
144
145 #pragma mark - SORTING OF TWO STRINGS
146
147 int str32collcxx . . $ str32coll(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
148 for (;; ++ string1, ++ string2) {
149 const char32cxx . . $ char32# kar1 = *string1, kar2 = *string2;
150 if (kar1 < kar2)
151 return -1;
152 if (kar1 > kar2)
153 return +1;
154 /*
155 kar1 is now equal to kar2
156 */
157 if (kar1 == U'\0')
158 return 0;
159 }
160 }
161 int Melder_collcxx . . $ Melder_coll(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
162 if (! string1) string1 = U"";
163 if (! string2) string2 = U"";
164 return str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (string1, string2);
165 }
166
167 int str32coll_numberAwarecxx . . $ str32coll_numberAware(8be1707edab15857). (const conststring32cxx . . $ conststring32# string1, const conststring32cxx . . $ conststring32# string2, const bool caseAware) noexcept {
168 auto isZero = [] (const char32cxx . . $ char32# kar) -> bool {
169 return Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (kar) && (kar & 0xF) == 0;
170 };
171 /*
172 First round.
173 */
174 conststring32cxx . . $ conststring32# pstring1 = string1, pstring2 = string2;
175 integercxx . . $ integer# totalLeadingZeroes1 = 0, totalLeadingZeroes2 = 0;
176 for (;;) {
177 const char32cxx . . $ char32# kar1 = ( caseAware ? Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*pstring1) : *pstring1 );
178 const char32cxx . . $ char32# kar2 = ( caseAware ? Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*pstring2) : *pstring2 );
179 if (kar1 == U'\0') {
180 if (kar2 != U'\0')
181 return -1;
182 break; // the two strings are "equivalent", i.e. equal except perhaps with respect to leading zeroes
183 }
184 if (kar2 == U'\0')
185 return +1;
186 const bool isDigit1 = Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (kar1);
187 const bool isDigit2 = Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (kar2);
188 if (isDigit1 != isDigit2)
189 return kar1 < kar2 ? -1 : +1;
190 if (isDigit1) {
191 integercxx . . $ integer# leadingZeroes1 = 0, leadingZeroes2 = 0;
192 while (isZero (cxx . . $ str32coll_numberAware(8be1707edab15857).$anonymous_type_0#`operator()`(81c042403a1a357c).pstring1 [leadingZeroes1]))
193 leadingZeroes1 ++;
194 while (isZero (cxx . . $ str32coll_numberAware(8be1707edab15857).$anonymous_type_0#`operator()`(81c042403a1a357c).pstring2 [leadingZeroes2]))
195 leadingZeroes2 ++;
196 totalLeadingZeroes1 += leadingZeroes1;
197 totalLeadingZeroes2 += leadingZeroes2;
198 pstring1 += leadingZeroes1;
199 pstring2 += leadingZeroes2;
200 integercxx . . $ integer# digits1 = 0, digits2 = 0;
201 while (Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (pstring1 [digits1]))
202 digits1 ++;
203 while (Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (pstring2 [digits2]))
204 digits2 ++;
205 if (digits1 < digits2)
206 return -1;
207 if (digits1 > digits2)
208 return +1;
209 /*
210 The two digit spans are now equally long.
211 */
212 for (integercxx . . $ integer# idigit = 0; idigit < digits1; idigit ++) {
213 const char32cxx . . $ char32# digit1 = pstring1 [idigit];
214 const char32cxx . . $ char32# digit2 = pstring2 [idigit];
215 if (digit1 < digit2)
216 return -1;
217 if (digit1 > digit2)
218 return +1;
219 }
220 /*
221 The two digit sequences are the same.
222 */
223 pstring1 += digits1;
224 pstring2 += digits1;
225 } else {
226 if (kar1 < kar2)
227 return -1;
228 if (kar1 > kar2)
229 return +1;
230 pstring1 ++;
231 pstring2 ++;
232 }
233 }
234 /*
235 Second round.
236 */
237 if (totalLeadingZeroes1 < totalLeadingZeroes2)
238 return -1;
239 if (totalLeadingZeroes1 > totalLeadingZeroes2)
240 return +1;
241 /*
242 The two strings are equivalent numberwise,
243 and even have the same total number of leading zeroes.
244 Cycle again from left to right,
245 now returning as soon as the local numbers of leading zeroes are different.
246 */
247 pstring1 = string1;
248 pstring2 = string2;
249 for (;;) {
250 const char32cxx . . $ char32# kar1 = ( caseAware ? Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*pstring1) : *pstring1 );
251 const char32cxx . . $ char32# kar2 = ( caseAware ? Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*pstring2) : *pstring2 );
252 if (kar1 == U'\0') {
253 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (kar2 == U'\0');
254 /*
255 The two strings are identical (not just equivalent numberwise).
256 They could still differ in case.
257 */
258 return caseAware ? str32collcxx . . $ str32coll(937dacc22d2a7919). (string1, string2) : 0;
259 }
260 const bool isDigit = Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (kar1);
261 if (isDigit) {
262 integercxx . . $ integer# leadingZeroes1 = 0, leadingZeroes2 = 0;
263 while (isZero (cxx . . $ str32coll_numberAware(8be1707edab15857).$anonymous_type_0#`operator()`(81c042403a1a357c).pstring1 [leadingZeroes1]))
264 leadingZeroes1 ++;
265 while (isZero (cxx . . $ str32coll_numberAware(8be1707edab15857).$anonymous_type_0#`operator()`(81c042403a1a357c).pstring2 [leadingZeroes2]))
266 leadingZeroes2 ++;
267 if (leadingZeroes1 < leadingZeroes2)
268 return -1;
269 if (leadingZeroes1 > leadingZeroes2)
270 return +1;
271 pstring1 += leadingZeroes1;
272 pstring2 += leadingZeroes1;
273 integercxx . . $ integer# digits = 0;
274 while (Melder_isDecimalNumbercxx . . $ Melder_isDecimalNumber(b8d0204ce8416907). (pstring1 [digits]))
275 digits ++;
276 pstring1 += digits;
277 pstring2 += digits;
278 } else {
279 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (kar1 == kar2);
280 pstring1 ++;
281 pstring2 ++;
282 }
283 }
284 }
285 int Melder_coll_numberAwarecxx . . $ Melder_coll_numberAware(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
286 if (! string1) string1 = U"";
287 if (! string2) string2 = U"";
288 return str32coll_numberAwarecxx . . $ str32coll_numberAware(8be1707edab15857). (string1, string2);
289 }
290
291 int str32cmp_caseInsensitivecxx . . $ str32cmp_caseInsensitive(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
292 for (;; ++ string1, ++ string2) {
293 const char32cxx . . $ char32# kar1 = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string1);
294 const char32cxx . . $ char32# kar2 = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string2);
295 if (kar1 < kar2)
296 return -1;
297 if (kar1 > kar2)
298 return +1;
299 if (kar1 == U'\0')
300 return 0;
301 }
302 }
303 int Melder_cmp_caseInsensitivecxx . . $ Melder_cmp_caseInsensitive(937dacc22d2a7919). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
304 if (! string1) string1 = U"";
305 if (! string2) string2 = U"";
306 return str32cmp_caseInsensitivecxx . . $ str32cmp_caseInsensitive(937dacc22d2a7919). (string1, string2);
307 }
308
309 int str32cmp_optionallyCaseSensitivecxx . . $ str32cmp_optionallyCaseSensitive(8be1707edab15857). (const conststring32cxx . . $ conststring32# string1, const conststring32cxx . . $ conststring32# string2, bool caseSensitive) noexcept {
310 return caseSensitive ? str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (string1, string2) : str32cmp_caseInsensitivecxx . . $ str32cmp_caseInsensitive(937dacc22d2a7919). (string1, string2);
311 }
312
313 int str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2, /*mut*/ integercxx . . $ integer# n) noexcept {
314 for (; n > 0; -- n, ++ string1, ++ string2) {
315 const char32cxx . . $ char32# kar1 = *string1, kar2 = *string2;
316 if (kar1 < kar2)
317 return -1;
318 if (kar1 > kar2)
319 return +1;
320 if (kar1 == U'\0')
321 return 0;
322 }
323 return 0;
324 }
325 int Melder_ncmpcxx . . $ Melder_ncmp(e53dc8530ec1035d). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2, const integercxx . . $ integer# n) noexcept {
326 if (! string1) string1 = U"";
327 if (! string2) string2 = U"";
328 return str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (string1, string2, n);
329 }
330
331 int str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2, /*mut*/ integercxx . . $ integer# n) noexcept {
332 for (; n > 0; -- n, ++ string1, ++ string2) {
333 const char32cxx . . $ char32# kar1 = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string1);
334 const char32cxx . . $ char32# kar2 = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string2);
335 if (kar1 < kar2)
336 return -1;
337 if (kar1 > kar2)
338 return +1;
339 if (kar1 == U'\0')
340 return 0;
341 }
342 return 0;
343 }
344 int Melder_ncmp_caseInsensitivecxx . . $ Melder_ncmp_caseInsensitive(e53dc8530ec1035d). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2, const integercxx . . $ integer# n) noexcept {
345 if (! string1) string1 = U"";
346 if (! string2) string2 = U"";
347 return str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (string1, string2, n);
348 }
349
350 int str32ncmp_optionallyCaseSensitivecxx . . $ str32ncmp_optionallyCaseSensitive(41c907af11e46a9a). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, integercxx . . $ integer# n, bool caseSensitive) noexcept {
351 return caseSensitive ? str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (string1, string2, n) : str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (string1, string2, n);
352 }
353
354 bool Melder_equ_firstCharacterCaseInsensitivecxx . . $ Melder_equ_firstCharacterCaseInsensitive(6eccf49f7b301074). (/*mut*/ conststring32cxx . . $ conststring32# string1, /*mut*/ conststring32cxx . . $ conststring32# string2) noexcept {
355 if (! string1) string1 = U"";
356 if (! string2) string2 = U"";
357 if (string1 [0] == U'\0')
358 return string2 [0] == U'\0';
359 if (Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (string1 [0]) != Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (string2 [0]))
360 return false;
361 return str32equcxx . . $ `melder/melder_str32.h:122:9`! (string1 + 1, string2 + 1);
362 }
363
364 /* End of file melder_str32.cpp */