| 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"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | static char hexSymbolscxx . . $ hexSymbols. [] = "0123456789ABCDEF"; |
| 22 | |
| 23 | static uint64cxx . . $ uint64# hexSecretcxx . . $ hexSecret. = UINT64_Ccxx . . $ `<external>/3415975807581488833/stdint.h:251:11`! (5'847'171'831'059'823'557); |
| 24 | |
| 25 | autostring32cxx . . $ autostring32# flatten_STRcxx . . $ flatten_STR(50b64ba3cdc027dd). (constSTRVECcxx . . $ constSTRVEC# const& value, conststring32cxx . . $ conststring32# separator) { |
| 26 | autoMelderStringcxx . . $ autoMelderString# stringcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8).; |
| 27 | MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (& string); |
| 28 | if (! NUMisEmptycxx . . $ NUMisEmpty(72eec41a35d7e37c). (value)) { |
| 29 | for (integercxx . . $ integer# i = 1; i < value.sizecxx . . $ _conststringvector#size.; i ++) |
| 30 | MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& string, value [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).i], separator); |
| 31 | MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& string, value [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).value.sizecxx . . $ _conststringvector#size.]); |
| 32 | } |
| 33 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (string. stringcxx . . $ MelderString#string.); |
| 34 | } |
| 35 | |
| 36 | autostring8cxx . . $ autostring8# hex_STR8cxx . . $ hex_STR8(3f4f548efc2492f3). (conststring8cxx . . $ conststring8# string, uint64cxx . . $ uint64# key) { |
| 37 | if (key != 0) |
| 38 | NUMrandom_initializeWithSeedUnsafelyButPredictablycxx . . $ NUMrandom_initializeWithSeedUnsafelyButPredictably(2c44cd906ae4e829). (key ^ hexSecretcxx . . $ hexSecret.); |
| 39 | autostring8cxx . . $ autostring8# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (string) * 2); |
| 40 | char *to = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 41 | for (const char8cxx . . $ char8# *from = (char8cxx . . $ char8# *) & string [0]; *from != '\0'; from ++) { |
| 42 | integercxx . . $ integer# value = *from; |
| 43 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (value > 0 && value < 256); |
| 44 | if (key != 0) |
| 45 | value = (value + NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (0, 255)) % 256; |
| 46 | *to ++ = hexSymbolscxx . . $ hexSymbols. [value / 16]; |
| 47 | *to ++ = hexSymbolscxx . . $ hexSymbols. [value % 16]; |
| 48 | } |
| 49 | *to = '\0'; |
| 50 | if (key != 0) |
| 51 | NUMrandom_initializeSafelyAndUnpredictablycxx . . $ NUMrandom_initializeSafelyAndUnpredictably(49f6e7a06ebc5aa8). (); |
| 52 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 53 | } |
| 54 | |
| 55 | autostring32cxx . . $ autostring32# hex_STRcxx . . $ hex_STR(f3b5e12a66e309ab). (conststring32cxx . . $ conststring32# string, uint64cxx . . $ uint64# key) { |
| 56 | autostring8cxx . . $ autostring8# string8 = Melder_32to8cxx . . $ Melder_32to8(d0484fb546b1094e). (string); |
| 57 | string8 =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). hex_STR8cxx . . $ hex_STR8(3f4f548efc2492f3). (string8.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), key); |
| 58 | return Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (string8.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 59 | } |
| 60 | |
| 61 | autostring32cxx . . $ autostring32# horizontal_STRcxx . . $ horizontal_STR(e6ef03305d70aad8). (constSTRVECcxx . . $ constSTRVEC# const& value) { |
| 62 | return flatten_STRcxx . . $ flatten_STR(50b64ba3cdc027dd). (value, U" "); |
| 63 | } |
| 64 | |
| 65 | autostring32cxx . . $ autostring32# left_STRcxx . . $ left_STR(c02f217f21fae7fe). (conststring32cxx . . $ conststring32# string, integercxx . . $ integer# newLength) { |
| 66 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 67 | Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (0_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & newLength, length); |
| 68 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (newLength); |
| 69 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), string, newLength); |
| 70 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 71 | } |
| 72 | |
| 73 | autostring32cxx . . $ autostring32# lowerCase_STRcxx . . $ lowerCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 74 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 75 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (length); |
| 76 | for (integercxx . . $ integer# i = 0; i < length; i ++) |
| 77 | result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (string [i]); |
| 78 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 79 | } |
| 80 | autostring32cxx . . $ autostring32# lowerCamelCase_STRcxx . . $ lowerCamelCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 81 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string) + 1); |
| 82 | char32cxx . . $ char32# *q = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 83 | bool nextLetterShouldBeUpperCase = false; |
| 84 | bool nextLetterShouldBeLowerCase = true; |
| 85 | for (const char32cxx . . $ char32# *p = & string [0]; *p != U'\0'; p ++) { |
| 86 | if (nextLetterShouldBeUpperCase) { |
| 87 | *q ++ = Melder_toUpperCasecxx . . $ Melder_toUpperCase(cd1afca5b9692c51). (*p); |
| 88 | nextLetterShouldBeUpperCase = false; |
| 89 | } else if (nextLetterShouldBeLowerCase) { |
| 90 | *q ++ = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*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 resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 99 | } |
| 100 | autostring32cxx . . $ autostring32# lowerSnakeCase_STRcxx . . $ lowerSnakeCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 101 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (2 * Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string) + 1); // make room for extra underscores |
| 102 | char32cxx . . $ char32# *q = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 103 | bool nextLetterShouldBeLowerCase = true; |
| 104 | for (const char32cxx . . $ char32# *p = & string [0]; *p != U'\0'; p ++) { |
| 105 | if (nextLetterShouldBeLowerCase) { |
| 106 | *q ++ = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*p); |
| 107 | nextLetterShouldBeLowerCase = false; |
| 108 | } else if (Melder_isUpperCaseLettercxx . . $ Melder_isUpperCaseLetter(b8d0204ce8416907). (*p)) { |
| 109 | *q ++ = U'_'; |
| 110 | *q ++ = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*p); |
| 111 | nextLetterShouldBeLowerCase = true; |
| 112 | } else |
| 113 | *q ++ = *p; |
| 114 | } |
| 115 | *q = U'\0'; // closing null byte |
| 116 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 117 | } |
| 118 | |
| 119 | autostring32cxx . . $ autostring32# mid_STRcxx . . $ mid_STR(190ff3abf8d9ea16). (conststring32cxx . . $ conststring32# string, integercxx . . $ integer# startingPosition_1, integercxx . . $ integer# numberOfCharacters) { |
| 120 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 121 | integercxx . . $ 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 integercxx . . $ integer# newLength = endPosition_1 - startingPosition_1 + 1; |
| 127 | if (newLength <= 0) |
| 128 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U""); |
| 129 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (newLength); |
| 130 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), & string [startingPosition_1-1], newLength); |
| 131 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 132 | } |
| 133 | |
| 134 | autostring32cxx . . $ autostring32# padLeft_STRcxx . . $ padLeft_STR(b76faa58d5a4b3dc). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width, const conststring32cxx . . $ conststring32# pad) { |
| 135 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_padLeftcxx . . $ Melder_padLeft(179e3723cc18632b). (string, width, pad)); |
| 136 | } |
| 137 | |
| 138 | autostring32cxx . . $ autostring32# padOrTruncateLeft_STRcxx . . $ padOrTruncateLeft_STR(b76faa58d5a4b3dc). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width, const conststring32cxx . . $ conststring32# pad) { |
| 139 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_padOrTruncateLeftcxx . . $ Melder_padOrTruncateLeft(179e3723cc18632b). (string, width, pad)); |
| 140 | } |
| 141 | |
| 142 | autostring32cxx . . $ autostring32# padOrTruncateRight_STRcxx . . $ padOrTruncateRight_STR(b76faa58d5a4b3dc). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width, const conststring32cxx . . $ conststring32# pad) { |
| 143 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_padOrTruncateRightcxx . . $ Melder_padOrTruncateRight(179e3723cc18632b). (string, width, pad)); |
| 144 | } |
| 145 | |
| 146 | autostring32cxx . . $ autostring32# padRight_STRcxx . . $ padRight_STR(b76faa58d5a4b3dc). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width, const conststring32cxx . . $ conststring32# pad) { |
| 147 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_padRightcxx . . $ Melder_padRight(179e3723cc18632b). (string, width, pad)); |
| 148 | } |
| 149 | |
| 150 | autostring32cxx . . $ autostring32# quote_doubleSTRcxx . . $ quote_doubleSTR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 151 | /* |
| 152 | Put the string `str` between quotes, doubling each quote inside `str`. |
| 153 | */ |
| 154 | constexpr integercxx . . $ integer# maximumNumberOfOutputCharactersPerInputCharacter = 2; // namely, the character " has to be converted to "" |
| 155 | constexpr integercxx . . $ integer# numberOfOutputCharactersNeededForTheQuotes = 2; // namely, the opening quote and the closing quote |
| 156 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (maximumNumberOfOutputCharactersPerInputCharacter * Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string) + numberOfOutputCharactersNeededForTheQuotes); |
| 157 | // for instance, the string `"""` has to be converted to the string `""""""""` |
| 158 | const char32cxx . . $ char32# *p = & string [0]; |
| 159 | char32cxx . . $ char32# *q = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).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 resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 169 | } |
| 170 | |
| 171 | autostring32cxx . . $ autostring32# replace_STRcxx . . $ replace_STR(58ee6b7335d69803). (conststring32cxx . . $ conststring32# string, |
| 172 | conststring32cxx . . $ conststring32# search, conststring32cxx . . $ conststring32# replace, integercxx . . $ integer# maximumNumberOfReplaces, |
| 173 | integercxx . . $ 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 integercxx . . $ integer# len_string = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 185 | if (len_string == 0) |
| 186 | maximumNumberOfReplaces = 1; |
| 187 | |
| 188 | const integercxx . . $ integer# len_search = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (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 char32cxx . . $ char32# *pos = & string [0]; // current position / start of current match |
| 198 | integercxx . . $ integer# numberOfMatches = 0; |
| 199 | if (maximumNumberOfReplaces <= 0) |
| 200 | maximumNumberOfReplaces = INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!; |
| 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 = str32strcxx . . $ str32str(2de17cd8f7c584a6). (pos, search)) && numberOfMatches < maximumNumberOfReplaces) { |
| 208 | pos += len_search; |
| 209 | numberOfMatches ++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | const integercxx . . $ integer# len_replace = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (replace); |
| 215 | const integercxx . . $ integer# len_result = len_string + numberOfMatches * (len_replace - len_search); |
| 216 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (len_result); |
| 217 | |
| 218 | const char32cxx . . $ char32# *posp = pos = & string [0]; |
| 219 | integercxx . . $ integer# nchar = 0, result_nchar = 0; |
| 220 | for (integercxx . . $ integer# i = 1; i <= numberOfMatches; i ++) { |
| 221 | pos = str32strcxx . . $ str32str(2de17cd8f7c584a6). (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 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (& result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).result_nchar], posp, nchar); |
| 229 | result_nchar += nchar; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | Insert the replace string in result. |
| 234 | */ |
| 235 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (& result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).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 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (& result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).result_nchar], posp, nchar); |
| 252 | if (out_numberOfMatches) |
| 253 | *out_numberOfMatches = numberOfMatches; |
| 254 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 255 | } |
| 256 | |
| 257 | autostring32cxx . . $ autostring32# replace_regex_STRcxx . . $ replace_regex_STR(8c698ee9a707cbfd). (conststring32cxx . . $ conststring32# string, |
| 258 | regexpcxx . . $ regexp# *compiledSearchRE, conststring32cxx . . $ conststring32# replaceRE, integercxx . . $ integer# maximumNumberOfReplaces, |
| 259 | integercxx . . $ integer# *out_numberOfMatches) |
| 260 | { |
| 261 | /* |
| 262 | Sanitize input. |
| 263 | */ |
| 264 | if (! string) |
| 265 | string = U""; |
| 266 | if (! compiledSearchRE) |
| 267 | return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).(); |
| 268 | if (! replaceRE) |
| 269 | replaceRE = U""; |
| 270 | |
| 271 | integercxx . . $ integer# buf_nchar = 0; // number of characters in 'buf' |
| 272 | integercxx . . $ integer# gap_copied = 0; |
| 273 | integercxx . . $ integer# nchar; |
| 274 | bool reverse = false; |
| 275 | int errorType; |
| 276 | char32cxx . . $ char32# prev_char = U'\0'; |
| 277 | const char32cxx . . $ char32# *pos; // current position in 'string' / start of current match |
| 278 | const char32cxx . . $ char32# *posp; // end of previous match |
| 279 | autostring32cxx . . $ autostring32# bufcxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 280 | |
| 281 | if (out_numberOfMatches) |
| 282 | *out_numberOfMatches = 0; |
| 283 | |
| 284 | const integercxx . . $ integer# string_length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 285 | //int replace_length = Melder_length (replaceRE); |
| 286 | if (string_length == 0) |
| 287 | maximumNumberOfReplaces = 1; |
| 288 | |
| 289 | integercxx . . $ 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 | integercxx . . $ integer# bufferLength = 2 * string_length; |
| 302 | bufferLength = bufferLength < 100 ? 100 : bufferLength; |
| 303 | buf. resizecxx . . $ _autostring#resize(1624ee8275b10bda). (bufferLength); |
| 304 | |
| 305 | pos = posp = string; |
| 306 | while (ExecREcxx . . $ ExecRE(adb1c5d556f61044). (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 -> startpcxx . . $ regexp#startp. [0]; |
| 315 | nchar = pos - posp; |
| 316 | if (nchar > 0 && ! gap_copied) { |
| 317 | if (buf_nchar + nchar > bufferLength) { |
| 318 | bufferLength *= 2; |
| 319 | buf. resizecxx . . $ _autostring#resize(1624ee8275b10bda). (bufferLength); |
| 320 | } |
| 321 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (buf.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + 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 (! SubstituteREcxx . . $ SubstituteRE(282979b5b66f071a). (compiledSearchRE, replaceRE, buf.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + buf_nchar, bufferLength + 1 - buf_nchar, & errorType)) { |
| 332 | if (errorType == 1) { // not enough memory |
| 333 | bufferLength *= 2; |
| 334 | buf. resizecxx . . $ _autostring#resize(1624ee8275b10bda). (bufferLength); |
| 335 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 336 | i --; // retry |
| 337 | continue; |
| 338 | } |
| 339 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Error during substitution."); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | Buffer is not full; get number of characters added. |
| 344 | */ |
| 345 | nchar = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (buf.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + buf_nchar); |
| 346 | buf_nchar += nchar; |
| 347 | |
| 348 | /* |
| 349 | Update next start position in search string. |
| 350 | */ |
| 351 | posp = pos; |
| 352 | pos = (char32cxx . . $ char32# *) compiledSearchRE -> endpcxx . . $ regexp#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. resizecxx . . $ _autostring#resize(1624ee8275b10bda). (bufferLength); |
| 371 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (buf.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + buf_nchar, pos, nchar); |
| 372 | |
| 373 | return bufcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 374 | } |
| 375 | |
| 376 | autostring32cxx . . $ autostring32# right_STRcxx . . $ right_STR(c02f217f21fae7fe). (conststring32cxx . . $ conststring32# string, integercxx . . $ integer# newLength) { |
| 377 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 378 | Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (0_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & newLength, length); |
| 379 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (string + length - newLength); |
| 380 | } |
| 381 | |
| 382 | autostring32cxx . . $ autostring32# truncateLeft_STRcxx . . $ truncateLeft_STR(c02f217f21fae7fe). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width) { |
| 383 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_truncateLeftcxx . . $ Melder_truncateLeft(2444e786ec90af51). (string, width)); |
| 384 | } |
| 385 | autostring32cxx . . $ autostring32# truncateRight_STRcxx . . $ truncateRight_STR(c02f217f21fae7fe). (const conststring32cxx . . $ conststring32# string, const integercxx . . $ integer# width) { |
| 386 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_truncateRightcxx . . $ Melder_truncateRight(2444e786ec90af51). (string, width)); |
| 387 | } |
| 388 | |
| 389 | autostring8cxx . . $ autostring8# unhex_STR8cxx . . $ unhex_STR8(3f4f548efc2492f3). (conststring8cxx . . $ conststring8# string, uint64cxx . . $ uint64# key) { |
| 390 | if (key != 0) |
| 391 | NUMrandom_initializeWithSeedUnsafelyButPredictablycxx . . $ NUMrandom_initializeWithSeedUnsafelyButPredictably(2c44cd906ae4e829). (key ^ hexSecretcxx . . $ hexSecret.); |
| 392 | autostring8cxx . . $ autostring8# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (string) / 2); |
| 393 | char *to = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 394 | for (const char8cxx . . $ char8# *from = (char8cxx . . $ char8# *) & string [0];;) { |
| 395 | char8cxx . . $ char8# code1 = *from ++; |
| 396 | while (Melder_isHorizontalOrVerticalSpacecxx . . $ Melder_isHorizontalOrVerticalSpace(b8d0204ce8416907). (code1)) |
| 397 | code1 = *from ++; |
| 398 | if (code1 == '\0') |
| 399 | break; |
| 400 | char8cxx . . $ char8# code2 = *from ++; |
| 401 | while (Melder_isHorizontalOrVerticalSpacecxx . . $ Melder_isHorizontalOrVerticalSpace(b8d0204ce8416907). (code2)) |
| 402 | code2 = *from ++; |
| 403 | if (code2 == '\0') |
| 404 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"(unhex$:) incomplete hexadecimal string."); |
| 405 | const char *index1 = strchr (hexSymbolscxx . . $ hexSymbols., code1), *index2 = strchr (hexSymbolscxx . . $ hexSymbols., code2); |
| 406 | if (! index1 || ! index2) |
| 407 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"(unhex$:) not a hexadecimal string: ", Melder_peek8to32_u (string)); |
| 408 | integercxx . . $ integer# value = (index1 - hexSymbolscxx . . $ hexSymbols.) * 16 + (index2 - hexSymbolscxx . . $ hexSymbols.); |
| 409 | if (key != 0) |
| 410 | value = (value + 256 - NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (0, 255)) % 256; |
| 411 | *to ++ = char (value); |
| 412 | } |
| 413 | *to = '\0'; |
| 414 | if (key != 0) |
| 415 | NUMrandom_initializeSafelyAndUnpredictablycxx . . $ NUMrandom_initializeSafelyAndUnpredictably(49f6e7a06ebc5aa8). (); |
| 416 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 417 | } |
| 418 | |
| 419 | autostring32cxx . . $ autostring32# unhex_STRcxx . . $ unhex_STR(f3b5e12a66e309ab). (conststring32cxx . . $ conststring32# string, uint64cxx . . $ uint64# key) { |
| 420 | autostring8cxx . . $ autostring8# string8 = Melder_32to8cxx . . $ Melder_32to8(d0484fb546b1094e). (string); |
| 421 | string8 =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). unhex_STR8cxx . . $ unhex_STR8(3f4f548efc2492f3). (string8.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), key); |
| 422 | return Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (string8.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 423 | } |
| 424 | |
| 425 | autostring32cxx . . $ autostring32# upperCase_STRcxx . . $ upperCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 426 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string); |
| 427 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (length); |
| 428 | for (integercxx . . $ integer# i = 0; i < length; i ++) |
| 429 | result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = Melder_toUpperCasecxx . . $ Melder_toUpperCase(cd1afca5b9692c51). (string [i]); |
| 430 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 431 | } |
| 432 | autostring32cxx . . $ autostring32# upperCamelCase_STRcxx . . $ upperCamelCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 433 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string) + 1); |
| 434 | char32cxx . . $ char32# *q = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 435 | bool nextLetterShouldBeUpperCase = true; |
| 436 | for (const char32cxx . . $ char32# *p = & string [0]; *p != U'\0'; p ++) { |
| 437 | if (nextLetterShouldBeUpperCase) { |
| 438 | *q ++ = Melder_toUpperCasecxx . . $ Melder_toUpperCase(cd1afca5b9692c51). (*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 resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 447 | } |
| 448 | autostring32cxx . . $ autostring32# upperSnakeCase_STRcxx . . $ upperSnakeCase_STR(87bf156b1f548402). (conststring32cxx . . $ conststring32# string) { |
| 449 | autostring32cxx . . $ autostring32# result = lowerSnakeCase_STRcxx . . $ lowerSnakeCase_STR(87bf156b1f548402). (string); |
| 450 | bool nextLetterShouldBeUpperCase = true; |
| 451 | for (char32cxx . . $ char32# *p = & result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; *p != U'\0'; p ++) { |
| 452 | if (nextLetterShouldBeUpperCase) { |
| 453 | *p = Melder_toUpperCasecxx . . $ Melder_toUpperCase(cd1afca5b9692c51). (*p); |
| 454 | nextLetterShouldBeUpperCase = false; |
| 455 | } else if (*p == U'_') { |
| 456 | nextLetterShouldBeUpperCase = true; |
| 457 | } |
| 458 | } |
| 459 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 460 | } |
| 461 | |
| 462 | autostring32cxx . . $ autostring32# vertical_STRcxx . . $ vertical_STR(e6ef03305d70aad8). (constSTRVECcxx . . $ constSTRVEC# const& value) { |
| 463 | return flatten_STRcxx . . $ flatten_STR(50b64ba3cdc027dd). (value, U"\n"); |
| 464 | } |
| 465 | |
| 466 | /* End of file STR.cpp */ |