| 1 | /* STR.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012-2017 David Weenink, 2008,2018,2020-2022,2024-2026 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 3 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 | static char hexSymbols [] = "0123456789ABCDEF"; |
| 22 | |
| 23 | static uint64 hexSecret = UINT64_C (5'847'171'831'059'823'557); |
| 24 | |
| 25 | autostring32 flatten_STR (constSTRVEC const& value, conststring32 separator) { |
| 26 | autoMelderString string; |
| 27 | MelderString_empty (& string); |
| 28 | if (! NUMisEmpty (value)) { |
| 29 | for (integer i = 1; i < value.size; i ++) |
| 30 | MelderString_append (& string, value [i], separator); |
| 31 | MelderString_append (& string, value [value.size]); |
| 32 | } |
| 33 | return Melder_dup (string. string); |
| 34 | } |
| 35 | |
| 36 | autostring8 hex_STR8 (conststring8 string, uint64 key) { |
| 37 | if (key != 0) |
| 38 | NUMrandom_initializeWithSeedUnsafelyButPredictably (key ^ hexSecret); |
| 39 | autostring8 result (Melder8_length (string) * 2); |
| 40 | char *to = & result [0]; |
| 41 | for (const char8 *from = (char8 *) & string [0]; *from != '\0'; from ++) { |
| 42 | integer value = *from; |
| 43 | Melder_assert (value > 0 && value < 256); |
| 44 | if (key != 0) |
| 45 | value = (value + NUMrandomInteger (0, 255)) % 256; |
| 46 | *to ++ = hexSymbols [value / 16]; |
| 47 | *to ++ = hexSymbols [value % 16]; |
| 48 | } |
| 49 | *to = '\0'; |
| 50 | if (key != 0) |
| 51 | NUMrandom_initializeSafelyAndUnpredictably (); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | autostring32 hex_STR (conststring32 string, uint64 key) { |
| 56 | autostring8 string8 = Melder_32to8 (string); |
| 57 | string8 = hex_STR8 (string8.get(), key); |
| 58 | return Melder_8to32_e (string8.get()); |
| 59 | } |
| 60 | |
| 61 | autostring32 horizontal_STR (constSTRVEC const& value) { |
| 62 | return flatten_STR (value, U" "); |
| 63 | } |
| 64 | |
| 65 | autostring32 left_STR (conststring32 string, integer newLength) { |
| 66 | const integer length = Melder_length (string); |
| 67 | Melder_clip (0_integer, & newLength, length); |
| 68 | autostring32 result (newLength); |
| 69 | str32ncpy (result.get(), string, newLength); |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | autostring32 lowerCase_STR (conststring32 string) { |
| 74 | const integer length = Melder_length (string); |
| 75 | autostring32 result (length); |
| 76 | for (integer i = 0; i < length; i ++) |
| 77 | result [i] = Melder_toLowerCase (string [i]); |
| 78 | return result; |
| 79 | } |
| 80 | autostring32 lowerCamelCase_STR (conststring32 string) { |
| 81 | autostring32 result (Melder_length (string) + 1); |
| 82 | char32 *q = & result [0]; |
| 83 | bool nextLetterShouldBeUpperCase = false; |
| 84 | bool nextLetterShouldBeLowerCase = true; |
| 85 | for (const char32 *p = & string [0]; *p != U'\0'; p ++) { |
| 86 | if (nextLetterShouldBeUpperCase) { |
| 87 | *q ++ = Melder_toUpperCase (*p); |
| 88 | nextLetterShouldBeUpperCase = false; |
| 89 | } else if (nextLetterShouldBeLowerCase) { |
| 90 | *q ++ = Melder_toLowerCase (*p); |
| 91 | nextLetterShouldBeLowerCase = false; |
| 92 | } else if (*p == U'_') { |
| 93 | nextLetterShouldBeUpperCase = true; |
| 94 | } else |
| 95 | *q ++ = *p; |
| 96 | } |
| 97 | *q = U'\0'; // closing null byte |
| 98 | return result; |
| 99 | } |
| 100 | autostring32 lowerSnakeCase_STR (conststring32 string) { |
| 101 | autostring32 result (2 * Melder_length (string) + 1); // make room for extra underscores |
| 102 | char32 *q = & result [0]; |
| 103 | bool nextLetterShouldBeLowerCase = true; |
| 104 | for (const char32 *p = & string [0]; *p != U'\0'; p ++) { |
| 105 | if (nextLetterShouldBeLowerCase) { |
| 106 | *q ++ = Melder_toLowerCase (*p); |
| 107 | nextLetterShouldBeLowerCase = false; |
| 108 | } else if (Melder_isUpperCaseLetter (*p)) { |
| 109 | *q ++ = U'_'; |
| 110 | *q ++ = Melder_toLowerCase (*p); |
| 111 | nextLetterShouldBeLowerCase = true; |
| 112 | } else |
| 113 | *q ++ = *p; |
| 114 | } |
| 115 | *q = U'\0'; // closing null byte |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | autostring32 mid_STR (conststring32 string, integer startingPosition_1, integer numberOfCharacters) { |
| 120 | const integer length = Melder_length (string); |
| 121 | integer endPosition_1 = startingPosition_1 + numberOfCharacters - 1; |
| 122 | if (startingPosition_1 < 1) |
| 123 | startingPosition_1 = 1; |
| 124 | if (endPosition_1 > length) |
| 125 | endPosition_1 = length; |
| 126 | const integer newLength = endPosition_1 - startingPosition_1 + 1; |
| 127 | if (newLength <= 0) |
| 128 | return Melder_dup (U""); |
| 129 | autostring32 result (newLength); |
| 130 | str32ncpy (result.get(), & string [startingPosition_1-1], newLength); |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | autostring32 padLeft_STR (const conststring32 string, const integer width, const conststring32 pad) { |
| 135 | return Melder_dup (Melder_padLeft (string, width, pad)); |
| 136 | } |
| 137 | |
| 138 | autostring32 padOrTruncateLeft_STR (const conststring32 string, const integer width, const conststring32 pad) { |
| 139 | return Melder_dup (Melder_padOrTruncateLeft (string, width, pad)); |
| 140 | } |
| 141 | |
| 142 | autostring32 padOrTruncateRight_STR (const conststring32 string, const integer width, const conststring32 pad) { |
| 143 | return Melder_dup (Melder_padOrTruncateRight (string, width, pad)); |
| 144 | } |
| 145 | |
| 146 | autostring32 padRight_STR (const conststring32 string, const integer width, const conststring32 pad) { |
| 147 | return Melder_dup (Melder_padRight (string, width, pad)); |
| 148 | } |
| 149 | |
| 150 | autostring32 quote_doubleSTR (conststring32 string) { |
| 151 | /* |
| 152 | Put the string `str` between quotes, doubling each quote inside `str`. |
| 153 | */ |
| 154 | constexpr integer maximumNumberOfOutputCharactersPerInputCharacter = 2; // namely, the character " has to be converted to "" |
| 155 | constexpr integer numberOfOutputCharactersNeededForTheQuotes = 2; // namely, the opening quote and the closing quote |
| 156 | autostring32 result (maximumNumberOfOutputCharactersPerInputCharacter * Melder_length (string) + numberOfOutputCharactersNeededForTheQuotes); |
| 157 | // for instance, the string `"""` has to be converted to the string `""""""""` |
| 158 | const char32 *p = & string [0]; |
| 159 | char32 *q = & result [0]; |
| 160 | * q ++ = U'"'; |
| 161 | while (*p != U'\0') { |
| 162 | if (*p == U'"') |
| 163 | * q ++ = U'\"'; |
| 164 | * q ++ = * p ++; |
| 165 | } |
| 166 | * q ++ = U'"'; |
| 167 | *q = U'\0'; |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | autostring32 replace_STR (conststring32 string, |
| 172 | conststring32 search, conststring32 replace, integer maximumNumberOfReplaces, |
| 173 | integer *out_numberOfMatches) |
| 174 | { |
| 175 | /* |
| 176 | Sanitize input. |
| 177 | */ |
| 178 | if (! string) |
| 179 | string = U""; |
| 180 | if (! search) |
| 181 | search = U""; |
| 182 | if (! replace) |
| 183 | replace = U""; |
| 184 | const integer len_string = Melder_length (string); |
| 185 | if (len_string == 0) |
| 186 | maximumNumberOfReplaces = 1; |
| 187 | |
| 188 | const integer len_search = Melder_length (search); |
| 189 | if (len_search == 0) |
| 190 | maximumNumberOfReplaces = 1; |
| 191 | |
| 192 | /* |
| 193 | To allocate memory for 'result' only once, we have to know how many |
| 194 | matches will occur. |
| 195 | */ |
| 196 | |
| 197 | const char32 *pos = & string [0]; // current position / start of current match |
| 198 | integer numberOfMatches = 0; |
| 199 | if (maximumNumberOfReplaces <= 0) |
| 200 | maximumNumberOfReplaces = INTEGER_MAX; |
| 201 | |
| 202 | if (len_search == 0) { /* Search is empty string... */ |
| 203 | if (len_string == 0) |
| 204 | numberOfMatches = 1; /* ...only matches empty string */ |
| 205 | } else { |
| 206 | if (len_string != 0) { /* Because empty string always matches */ |
| 207 | while (!! (pos = str32str (pos, search)) && numberOfMatches < maximumNumberOfReplaces) { |
| 208 | pos += len_search; |
| 209 | numberOfMatches ++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | const integer len_replace = Melder_length (replace); |
| 215 | const integer len_result = len_string + numberOfMatches * (len_replace - len_search); |
| 216 | autostring32 result (len_result); |
| 217 | |
| 218 | const char32 *posp = pos = & string [0]; |
| 219 | integer nchar = 0, result_nchar = 0; |
| 220 | for (integer i = 1; i <= numberOfMatches; i ++) { |
| 221 | pos = str32str (pos, search); |
| 222 | |
| 223 | /* |
| 224 | Copy gap between end of previous match and start of current. |
| 225 | */ |
| 226 | nchar = pos - posp; |
| 227 | if (nchar > 0) { |
| 228 | str32ncpy (& result [result_nchar], posp, nchar); |
| 229 | result_nchar += nchar; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | Insert the replace string in result. |
| 234 | */ |
| 235 | str32ncpy (& result [result_nchar], replace, len_replace); |
| 236 | result_nchar += len_replace; |
| 237 | |
| 238 | /* |
| 239 | Next search starts after the match. |
| 240 | */ |
| 241 | pos += len_search; |
| 242 | posp = pos; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | Copy gap between end of match and end of string. |
| 247 | */ |
| 248 | pos = string + len_string; |
| 249 | nchar = pos - posp; |
| 250 | if (nchar > 0) |
| 251 | str32ncpy (& result [result_nchar], posp, nchar); |
| 252 | if (out_numberOfMatches) |
| 253 | *out_numberOfMatches = numberOfMatches; |
| 254 | return result; |
| 255 | } |
| 256 | |
| 257 | autostring32 replace_regex_STR (conststring32 string, |
| 258 | regexp *compiledSearchRE, conststring32 replaceRE, integer maximumNumberOfReplaces, |
| 259 | integer *out_numberOfMatches) |
| 260 | { |
| 261 | /* |
| 262 | Sanitize input. |
| 263 | */ |
| 264 | if (! string) |
| 265 | string = U""; |
| 266 | if (! compiledSearchRE) |
| 267 | return autostring32(); |
| 268 | if (! replaceRE) |
| 269 | replaceRE = U""; |
| 270 | |
| 271 | integer buf_nchar = 0; // number of characters in 'buf' |
| 272 | integer gap_copied = 0; |
| 273 | integer nchar; |
| 274 | bool reverse = false; |
| 275 | int errorType; |
| 276 | char32 prev_char = U'\0'; |
| 277 | const char32 *pos; // current position in 'string' / start of current match |
| 278 | const char32 *posp; // end of previous match |
| 279 | autostring32 buf; |
| 280 | |
| 281 | if (out_numberOfMatches) |
| 282 | *out_numberOfMatches = 0; |
| 283 | |
| 284 | const integer string_length = Melder_length (string); |
| 285 | //int replace_length = Melder_length (replaceRE); |
| 286 | if (string_length == 0) |
| 287 | maximumNumberOfReplaces = 1; |
| 288 | |
| 289 | integer i = ( maximumNumberOfReplaces > 0 ? 0 : - string_length ); |
| 290 | |
| 291 | /* |
| 292 | We do not know the size of the replaced string in advance, |
| 293 | therefore we allocate a replace buffer twice the size of the |
| 294 | original string. After all replaces have taken place we do a |
| 295 | final realloc to the then exactly known size. |
| 296 | If during the replace, the size of the buffer happens to be too |
| 297 | small (this is signalled by the replaceRE function), |
| 298 | we double its size and restart the replace. |
| 299 | */ |
| 300 | |
| 301 | integer bufferLength = 2 * string_length; |
| 302 | bufferLength = bufferLength < 100 ? 100 : bufferLength; |
| 303 | buf. resize (bufferLength); |
| 304 | |
| 305 | pos = posp = string; |
| 306 | while (ExecRE (compiledSearchRE, nullptr, pos, nullptr, reverse, prev_char, U'\0', nullptr, nullptr) && |
| 307 | i ++ < maximumNumberOfReplaces) |
| 308 | { |
| 309 | /* |
| 310 | Copy gap between the end of the previous match and the start |
| 311 | of the current match. |
| 312 | Check buffer overflow. pos == posp ? '\0' : pos [-1], |
| 313 | */ |
| 314 | pos = compiledSearchRE -> startp [0]; |
| 315 | nchar = pos - posp; |
| 316 | if (nchar > 0 && ! gap_copied) { |
| 317 | if (buf_nchar + nchar > bufferLength) { |
| 318 | bufferLength *= 2; |
| 319 | buf. resize (bufferLength); |
| 320 | } |
| 321 | str32ncpy (buf.get() + buf_nchar, posp, nchar); |
| 322 | buf_nchar += nchar; |
| 323 | } |
| 324 | |
| 325 | gap_copied = 1; |
| 326 | |
| 327 | /* |
| 328 | Do the substitution. We can check for buffer overflow only afterwards. |
| 329 | SubstituteRE puts null byte at last replaced position and signals when overflow. |
| 330 | */ |
| 331 | if (! SubstituteRE (compiledSearchRE, replaceRE, buf.get() + buf_nchar, bufferLength + 1 - buf_nchar, & errorType)) { |
| 332 | if (errorType == 1) { // not enough memory |
| 333 | bufferLength *= 2; |
| 334 | buf. resize (bufferLength); |
| 335 | Melder_clearError (); |
| 336 | i --; // retry |
| 337 | continue; |
| 338 | } |
| 339 | Melder_throw (U"Error during substitution."); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | Buffer is not full; get number of characters added. |
| 344 | */ |
| 345 | nchar = Melder_length (buf.get() + buf_nchar); |
| 346 | buf_nchar += nchar; |
| 347 | |
| 348 | /* |
| 349 | Update next start position in search string. |
| 350 | */ |
| 351 | posp = pos; |
| 352 | pos = (char32 *) compiledSearchRE -> endp [0]; |
| 353 | if (pos != posp) |
| 354 | prev_char = pos [-1]; |
| 355 | gap_copied = 0; |
| 356 | posp = pos; //pb 20080121 |
| 357 | if (out_numberOfMatches) |
| 358 | (*out_numberOfMatches) ++; |
| 359 | // at end of string? |
| 360 | // we need this because .* matches at the end of a string |
| 361 | if (pos - string == string_length) |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | Copy last part of string to destination string |
| 367 | */ |
| 368 | nchar = (string + string_length) - pos; |
| 369 | bufferLength = buf_nchar + nchar; |
| 370 | buf. resize (bufferLength); |
| 371 | str32ncpy (buf.get() + buf_nchar, pos, nchar); |
| 372 | |
| 373 | return buf; |
| 374 | } |
| 375 | |
| 376 | autostring32 right_STR (conststring32 string, integer newLength) { |
| 377 | const integer length = Melder_length (string); |
| 378 | Melder_clip (0_integer, & newLength, length); |
| 379 | return Melder_dup (string + length - newLength); |
| 380 | } |
| 381 | |
| 382 | autostring32 truncateLeft_STR (const conststring32 string, const integer width) { |
| 383 | return Melder_dup (Melder_truncateLeft (string, width)); |
| 384 | } |
| 385 | autostring32 truncateRight_STR (const conststring32 string, const integer width) { |
| 386 | return Melder_dup (Melder_truncateRight (string, width)); |
| 387 | } |
| 388 | |
| 389 | autostring8 unhex_STR8 (conststring8 string, uint64 key) { |
| 390 | if (key != 0) |
| 391 | NUMrandom_initializeWithSeedUnsafelyButPredictably (key ^ hexSecret); |
| 392 | autostring8 result (Melder8_length (string) / 2); |
| 393 | char *to = & result [0]; |
| 394 | for (const char8 *from = (char8 *) & string [0];;) { |
| 395 | char8 code1 = *from ++; |
| 396 | while (Melder_isHorizontalOrVerticalSpace (code1)) |
| 397 | code1 = *from ++; |
| 398 | if (code1 == '\0') |
| 399 | break; |
| 400 | char8 code2 = *from ++; |
| 401 | while (Melder_isHorizontalOrVerticalSpace (code2)) |
| 402 | code2 = *from ++; |
| 403 | if (code2 == '\0') |
| 404 | Melder_throw (U"(unhex$:) incomplete hexadecimal string."); |
| 405 | const char *index1 = strchr (hexSymbols, code1), *index2 = strchr (hexSymbols, code2); |
| 406 | if (! index1 || ! index2) |
| 407 | Melder_throw (U"(unhex$:) not a hexadecimal string: ", Melder_peek8to32_u (string)); |
| 408 | integer value = (index1 - hexSymbols) * 16 + (index2 - hexSymbols); |
| 409 | if (key != 0) |
| 410 | value = (value + 256 - NUMrandomInteger (0, 255)) % 256; |
| 411 | *to ++ = char (value); |
| 412 | } |
| 413 | *to = '\0'; |
| 414 | if (key != 0) |
| 415 | NUMrandom_initializeSafelyAndUnpredictably (); |
| 416 | return result; |
| 417 | } |
| 418 | |
| 419 | autostring32 unhex_STR (conststring32 string, uint64 key) { |
| 420 | autostring8 string8 = Melder_32to8 (string); |
| 421 | string8 = unhex_STR8 (string8.get(), key); |
| 422 | return Melder_8to32_e (string8.get()); |
| 423 | } |
| 424 | |
| 425 | autostring32 upperCase_STR (conststring32 string) { |
| 426 | const integer length = Melder_length (string); |
| 427 | autostring32 result (length); |
| 428 | for (integer i = 0; i < length; i ++) |
| 429 | result [i] = Melder_toUpperCase (string [i]); |
| 430 | return result; |
| 431 | } |
| 432 | autostring32 upperCamelCase_STR (conststring32 string) { |
| 433 | autostring32 result (Melder_length (string) + 1); |
| 434 | char32 *q = & result [0]; |
| 435 | bool nextLetterShouldBeUpperCase = true; |
| 436 | for (const char32 *p = & string [0]; *p != U'\0'; p ++) { |
| 437 | if (nextLetterShouldBeUpperCase) { |
| 438 | *q ++ = Melder_toUpperCase (*p); |
| 439 | nextLetterShouldBeUpperCase = false; |
| 440 | } else if (*p == U'_') { |
| 441 | nextLetterShouldBeUpperCase = true; |
| 442 | } else |
| 443 | *q ++ = *p; |
| 444 | } |
| 445 | *q = U'\0'; // closing null byte |
| 446 | return result; |
| 447 | } |
| 448 | autostring32 upperSnakeCase_STR (conststring32 string) { |
| 449 | autostring32 result = lowerSnakeCase_STR (string); |
| 450 | bool nextLetterShouldBeUpperCase = true; |
| 451 | for (char32 *p = & result [0]; *p != U'\0'; p ++) { |
| 452 | if (nextLetterShouldBeUpperCase) { |
| 453 | *p = Melder_toUpperCase (*p); |
| 454 | nextLetterShouldBeUpperCase = false; |
| 455 | } else if (*p == U'_') { |
| 456 | nextLetterShouldBeUpperCase = true; |
| 457 | } |
| 458 | } |
| 459 | return result; |
| 460 | } |
| 461 | |
| 462 | autostring32 vertical_STR (constSTRVEC const& value) { |
| 463 | return flatten_STR (value, U"\n"); |
| 464 | } |
| 465 | |
| 466 | /* End of file STR.cpp */ |