| 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" |
| 20 |
|
| 21 |
#pragma mark - first one 8-bit string function... |
| 22 |
|
| 23 |
integer Melder8_length (const conststring8 stringOrNull) { |
| 24 |
if (! stringOrNull) |
| 25 |
return 0; |
| 26 |
const char *p = & stringOrNull [0]; |
| 27 |
while (*p != '\0') |
| 28 |
++ p; |
| 29 |
const integer result = p - stringOrNull; |
| 30 |
if (sizeof (integer) == 4) |
| 31 |
Melder_assert (result >= 0); |
| 32 |
return result; |
| 33 |
} |
| 34 |
|
| 35 |
#pragma mark - ...then two 16-bit string functions... |
| 36 |
|
| 37 |
integer Melder16_length (const conststring16 stringOrNull) noexcept { |
| 38 |
if (! stringOrNull) |
| 39 |
return 0; |
| 40 |
const char16 *p = & stringOrNull [0]; |
| 41 |
while (*p != u'\0') |
| 42 |
++ p; |
| 43 |
return p - stringOrNull; |
| 44 |
} |
| 45 |
mutablestring16 str16cpy (const mutablestring16 target, /*mut*/ conststring16 source) noexcept { |
| 46 |
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 |
mutablestring32 str32cpy (const mutablestring32 target, /*mut*/ conststring32 source) noexcept { |
| 56 |
char32 *p = & target [0]; |
| 57 |
while (* source != U'\0') |
| 58 |
* p ++ = * source ++; |
| 59 |
*p = U'\0'; |
| 60 |
return target; |
| 61 |
} |
| 62 |
mutablestring32 str32cat (const mutablestring32 target, /*mut*/ conststring32 source) noexcept { |
| 63 |
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 |
char32 * stp32cpy (const mutablestring32 target, /*mut*/ conststring32 source) noexcept { |
| 72 |
char32 *p = & target [0]; |
| 73 |
while (* source != U'\0') |
| 74 |
* p ++ = * source ++; |
| 75 |
*p = U'\0'; |
| 76 |
return p; |
| 77 |
} |
| 78 |
mutablestring32 str32ncpy (const mutablestring32 target, /*mut*/ conststring32 source, /*mut*/ integer n) noexcept { |
| 79 |
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 |
char32 * str32str (/*mut*/ conststring32 string, /*mut*/ conststring32 find) noexcept { |
| 90 |
const integer length = Melder_length (find); |
| 91 |
if (length == 0) |
| 92 |
return (char32 *) string; |
| 93 |
char32 firstCharacter = * find ++; // optimization |
| 94 |
do { |
| 95 |
char32 kar; |
| 96 |
do { |
| 97 |
kar = * string ++; |
| 98 |
if (kar == U'\0') |
| 99 |
return nullptr; |
| 100 |
} while (kar != firstCharacter); |
| 101 |
} while (str32ncmp (string, find, length - 1)); |
| 102 |
return (char32 *) (string - 1); |
| 103 |
} |
| 104 |
char32 * str32str_caseInsensitive (/*mut*/ conststring32 string, /*mut*/ conststring32 find) noexcept { |
| 105 |
const integer length = Melder_length (find); |
| 106 |
if (length == 0) |
| 107 |
return (char32 *) string; |
| 108 |
char32 firstCharacter = Melder_toLowerCase (* find ++); // optimization |
| 109 |
do { |
| 110 |
char32 kar; |
| 111 |
do { |
| 112 |
kar = Melder_toLowerCase (* string ++); |
| 113 |
if (kar == U'\0') |
| 114 |
return nullptr; |
| 115 |
} while (kar != firstCharacter); |
| 116 |
} while (str32ncmp_caseInsensitive (string, find, length - 1)); |
| 117 |
return (char32 *) (string - 1); |
| 118 |
} |
| 119 |
char32 * str32str_optionallyCaseSensitive (conststring32 string, conststring32 find, bool caseSensitive) noexcept { |
| 120 |
return caseSensitive ? str32str (string, find) : str32str_caseInsensitive (string, find); |
| 121 |
} |
| 122 |
|
| 123 |
#pragma mark - EQUIVALENCE OF TWO STRINGS |
| 124 |
|
| 125 |
int str32cmp (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 126 |
for (;; ++ string1, ++ string2) { |
| 127 |
const 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_cmp (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 140 |
if (! string1) string1 = U""; |
| 141 |
if (! string2) string2 = U""; |
| 142 |
return str32cmp (string1, string2); |
| 143 |
} |
| 144 |
|
| 145 |
#pragma mark - SORTING OF TWO STRINGS |
| 146 |
|
| 147 |
int str32coll (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 148 |
for (;; ++ string1, ++ string2) { |
| 149 |
const 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_coll (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 162 |
if (! string1) string1 = U""; |
| 163 |
if (! string2) string2 = U""; |
| 164 |
return str32cmp (string1, string2); |
| 165 |
} |
| 166 |
|
| 167 |
int str32coll_numberAware (const conststring32 string1, const conststring32 string2, const bool caseAware) noexcept { |
| 168 |
auto isZero = [] (const char32 kar) -> bool { |
| 169 |
return Melder_isDecimalNumber (kar) && (kar & 0xF) == 0; |
| 170 |
}; |
| 171 |
/* |
| 172 |
First round. |
| 173 |
*/ |
| 174 |
conststring32 pstring1 = string1, pstring2 = string2; |
| 175 |
integer totalLeadingZeroes1 = 0, totalLeadingZeroes2 = 0; |
| 176 |
for (;;) { |
| 177 |
const char32 kar1 = ( caseAware ? Melder_toLowerCase (*pstring1) : *pstring1 ); |
| 178 |
const char32 kar2 = ( caseAware ? Melder_toLowerCase (*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_isDecimalNumber (kar1); |
| 187 |
const bool isDigit2 = Melder_isDecimalNumber (kar2); |
| 188 |
if (isDigit1 != isDigit2) |
| 189 |
return kar1 < kar2 ? -1 : +1; |
| 190 |
if (isDigit1) { |
| 191 |
integer leadingZeroes1 = 0, leadingZeroes2 = 0; |
| 192 |
while (isZero (pstring1 [leadingZeroes1])) |
| 193 |
leadingZeroes1 ++; |
| 194 |
while (isZero (pstring2 [leadingZeroes2])) |
| 195 |
leadingZeroes2 ++; |
| 196 |
totalLeadingZeroes1 += leadingZeroes1; |
| 197 |
totalLeadingZeroes2 += leadingZeroes2; |
| 198 |
pstring1 += leadingZeroes1; |
| 199 |
pstring2 += leadingZeroes2; |
| 200 |
integer digits1 = 0, digits2 = 0; |
| 201 |
while (Melder_isDecimalNumber (pstring1 [digits1])) |
| 202 |
digits1 ++; |
| 203 |
while (Melder_isDecimalNumber (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 (integer idigit = 0; idigit < digits1; idigit ++) { |
| 213 |
const char32 digit1 = pstring1 [idigit]; |
| 214 |
const 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 char32 kar1 = ( caseAware ? Melder_toLowerCase (*pstring1) : *pstring1 ); |
| 251 |
const char32 kar2 = ( caseAware ? Melder_toLowerCase (*pstring2) : *pstring2 ); |
| 252 |
if (kar1 == U'\0') { |
| 253 |
Melder_assert (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 ? str32coll (string1, string2) : 0; |
| 259 |
} |
| 260 |
const bool isDigit = Melder_isDecimalNumber (kar1); |
| 261 |
if (isDigit) { |
| 262 |
integer leadingZeroes1 = 0, leadingZeroes2 = 0; |
| 263 |
while (isZero (pstring1 [leadingZeroes1])) |
| 264 |
leadingZeroes1 ++; |
| 265 |
while (isZero (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 |
integer digits = 0; |
| 274 |
while (Melder_isDecimalNumber (pstring1 [digits])) |
| 275 |
digits ++; |
| 276 |
pstring1 += digits; |
| 277 |
pstring2 += digits; |
| 278 |
} else { |
| 279 |
Melder_assert (kar1 == kar2); |
| 280 |
pstring1 ++; |
| 281 |
pstring2 ++; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
int Melder_coll_numberAware (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 286 |
if (! string1) string1 = U""; |
| 287 |
if (! string2) string2 = U""; |
| 288 |
return str32coll_numberAware (string1, string2); |
| 289 |
} |
| 290 |
|
| 291 |
int str32cmp_caseInsensitive (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 292 |
for (;; ++ string1, ++ string2) { |
| 293 |
const char32 kar1 = Melder_toLowerCase (*string1); |
| 294 |
const char32 kar2 = Melder_toLowerCase (*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_caseInsensitive (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2) noexcept { |
| 304 |
if (! string1) string1 = U""; |
| 305 |
if (! string2) string2 = U""; |
| 306 |
return str32cmp_caseInsensitive (string1, string2); |
| 307 |
} |
| 308 |
|
| 309 |
int str32cmp_optionallyCaseSensitive (const conststring32 string1, const conststring32 string2, bool caseSensitive) noexcept { |
| 310 |
return caseSensitive ? str32cmp (string1, string2) : str32cmp_caseInsensitive (string1, string2); |
| 311 |
} |
| 312 |
|
| 313 |
int str32ncmp (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2, /*mut*/ integer n) noexcept { |
| 314 |
for (; n > 0; -- n, ++ string1, ++ string2) { |
| 315 |
const 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_ncmp (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2, const integer n) noexcept { |
| 326 |
if (! string1) string1 = U""; |
| 327 |
if (! string2) string2 = U""; |
| 328 |
return str32ncmp (string1, string2, n); |
| 329 |
} |
| 330 |
|
| 331 |
int str32ncmp_caseInsensitive (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2, /*mut*/ integer n) noexcept { |
| 332 |
for (; n > 0; -- n, ++ string1, ++ string2) { |
| 333 |
const char32 kar1 = Melder_toLowerCase (*string1); |
| 334 |
const char32 kar2 = Melder_toLowerCase (*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_caseInsensitive (/*mut*/ conststring32 string1, /*mut*/ conststring32 string2, const integer n) noexcept { |
| 345 |
if (! string1) string1 = U""; |
| 346 |
if (! string2) string2 = U""; |
| 347 |
return str32ncmp_caseInsensitive (string1, string2, n); |
| 348 |
} |
| 349 |
|
| 350 |
int str32ncmp_optionallyCaseSensitive (conststring32 string1, conststring32 string2, integer n, bool caseSensitive) noexcept { |
| 351 |
return caseSensitive ? str32ncmp (string1, string2, n) : str32ncmp_caseInsensitive (string1, string2, n); |
| 352 |
} |
| 353 |
|
| 354 |
bool Melder_equ_firstCharacterCaseInsensitive (/*mut*/ conststring32 string1, /*mut*/ 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_toLowerCase (string1 [0]) != Melder_toLowerCase (string2 [0])) |
| 360 |
return false; |
| 361 |
return str32equ (string1 + 1, string2 + 1); |
| 362 |
} |
| 363 |
|
| 364 |
/* End of file melder_str32.cpp */ |