| 1 | /* melder_textencoding.cpp |
| 2 | * |
| 3 | * Copyright (C) 2007-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 | #include "../sys/Preferences.h"cxx . . $ `<file>/sys/Preferences.h`/ |
| 21 | #include "../kar/UnicodeData.h"cxx . . $ `<file>/kar/UnicodeData.h`/ |
| 22 | |
| 23 | #ifdef _WIN32 |
| 24 | #include <windows.h> |
| 25 | #endif |
| 26 | #if defined (macintosh) |
| 27 | #include "macport_on.h" |
| 28 | #include <CoreFoundation/CoreFoundation.h> |
| 29 | #include "macport_off.h" |
| 30 | #endif |
| 31 | |
| 32 | static structcxx . . $ $anonymous_type_1fadc92bd65d6f53_0# { |
| 33 | kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding# inputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#inputEncoding.; |
| 34 | kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# outputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#outputEncoding.; |
| 35 | } preferencescxx . . $ preferences.; |
| 36 | |
| 37 | void Melder_setInputEncodingcxx . . $ Melder_setInputEncoding(e7da480bd5829302). (kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding# encoding) { preferencescxx . . $ preferences.. inputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#inputEncoding. = encoding; } |
| 38 | kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding# Melder_getInputEncodingcxx . . $ Melder_getInputEncoding(d1f14111e3b730d4). () { return preferencescxx . . $ preferences.. inputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#inputEncoding.; } |
| 39 | |
| 40 | void Melder_setOutputEncodingcxx . . $ Melder_setOutputEncoding(96a7a35765e6164e). (kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# encoding) { preferencescxx . . $ preferences.. outputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#outputEncoding. = encoding; } |
| 41 | kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# Melder_getOutputEncodingcxx . . $ Melder_getOutputEncoding(581d0d572cba43d7). () { return preferencescxx . . $ preferences.. outputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#outputEncoding.; } |
| 42 | |
| 43 | void Melder_textEncoding_prefscxx . . $ Melder_textEncoding_prefs(49f6e7a06ebc5aa8). () { |
| 44 | Preferences_addEnumcxx . . $ `sys/Preferences.h:45:9`! (U"TextEncoding.inputEncoding", & preferences. inputEncoding, kMelder_textInputEncoding, kMelder_textInputEncoding::DEFAULT); |
| 45 | Preferences_addEnumcxx . . $ `sys/Preferences.h:45:9`! (U"TextEncoding.outputEncoding", & preferences. outputEncoding, kMelder_textOutputEncoding, kMelder_textOutputEncoding::DEFAULT); |
| 46 | } |
| 47 | |
| 48 | bool Melder_isValidAsciicxx . . $ Melder_isValidAscii(2df281243f91fe91). (conststring32cxx . . $ conststring32# text) { |
| 49 | for (; *text != U'\0'; text ++) { |
| 50 | if (*text > 127) |
| 51 | return false; |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool Melder_str8IsValidAsciicxx . . $ Melder_str8IsValidAscii(c5f7da30cbc0f4e9). (conststring8cxx . . $ conststring8# text) { |
| 57 | for (; *text != U'\0'; text ++) { |
| 58 | if (* (unsigned char *) text > 127) |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool Melder_isEncodablecxx . . $ Melder_isEncodable(8f54aa54c3f23562). (conststring32cxx . . $ conststring32# text, int outputEncoding) { |
| 65 | switch (outputEncoding) { |
| 66 | case kMelder_textOutputEncoding_ASCIIcxx . . $ kMelder_textOutputEncoding_ASCII.: { |
| 67 | for (; *text != U'\0'; text ++) { |
| 68 | if (*text > 127) |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } break; |
| 73 | case kMelder_textOutputEncoding_ISO_LATIN1cxx . . $ kMelder_textOutputEncoding_ISO_LATIN1.: { |
| 74 | for (; *text != U'\0'; text ++) { |
| 75 | if (*text > 255) |
| 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } break; |
| 80 | case (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF8cxx . . $ kMelder_textOutputEncoding#UTF8.: |
| 81 | case (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF16cxx . . $ kMelder_textOutputEncoding#UTF16.: |
| 82 | case (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ASCII_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ASCII_THEN_UTF16.: |
| 83 | case (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ISO_LATIN1_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ISO_LATIN1_THEN_UTF16.: { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | bool Melder_str8IsValidUtf8cxx . . $ Melder_str8IsValidUtf8(c5f7da30cbc0f4e9). (const char *string) { |
| 91 | for (const char8cxx . . $ char8# *p = (const char8cxx . . $ char8# *) & string [0]; *p != '\0'; p ++) { |
| 92 | char32cxx . . $ char32# kar = (char32cxx . . $ char32#) *p; |
| 93 | if (kar <= 0x7F) { |
| 94 | ; |
| 95 | } else if (kar <= 0xC1) { |
| 96 | return false; |
| 97 | } else if (kar <= 0xDF) { |
| 98 | if ((* ++ p & 0xC0) != 0x80) |
| 99 | return false; |
| 100 | } else if (kar <= 0xEF) { |
| 101 | if ((* ++ p & 0xC0) != 0x80) |
| 102 | return false; |
| 103 | if ((* ++ p & 0xC0) != 0x80) |
| 104 | return false; |
| 105 | } else if (kar <= 0xF4) { |
| 106 | if ((* ++ p & 0xC0) != 0x80) |
| 107 | return false; |
| 108 | if ((* ++ p & 0xC0) != 0x80) |
| 109 | return false; |
| 110 | if ((* ++ p & 0xC0) != 0x80) |
| 111 | return false; |
| 112 | } else { |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | integercxx . . $ integer# Melder_killReturns_inplacecxx . . $ Melder_killReturns_inplace(d2cd1729a8aa36c). (char *text) { |
| 120 | const char *from; |
| 121 | char *to; |
| 122 | for (from = text, to = text; *from != '\0'; from ++, to ++) { |
| 123 | if (*from == 13) { // carriage return? |
| 124 | if (from [1] == '\n') { // followed by linefeed? Must be a Windows text |
| 125 | from ++; // ignore carriage return |
| 126 | *to = '\n'; // copy linefeed |
| 127 | } else { // bare carriage return? Must be a Macintosh text |
| 128 | *to = '\n'; // change to linefeed |
| 129 | } |
| 130 | } else { |
| 131 | *to = *from; |
| 132 | } |
| 133 | } |
| 134 | *to = '\0'; // closing null byte |
| 135 | return to - text; |
| 136 | } |
| 137 | |
| 138 | template <class CHAR> |
| 139 | integercxx . . $ integer# Melder_killReturns_inplaceCHARcxx . . $ Melder_killReturns_inplaceCHAR(3b749fa451b4bc45). (CHAR *text) { |
| 140 | const CHAR *from; |
| 141 | CHAR *to; |
| 142 | for (from = text, to = text; *from != '\0'; from ++, to ++) { |
| 143 | if (*from == 13) { // carriage return? |
| 144 | if (from [1] == '\n' || from [1] == 0x0085 /* NextLine */) { // followed by linefeed? Must be a Windows text |
| 145 | from ++; // ignore carriage return |
| 146 | *to = '\n'; // copy linefeed |
| 147 | } else { // bare carriage return? Must be a Macintosh text |
| 148 | *to = '\n'; // change to linefeed (10) |
| 149 | } |
| 150 | } else if (*from == 0x0085 /* NextLine */ || *from == 0x000C /* FormFeed */ || |
| 151 | *from == UNICODE_LINE_SEPARATORcxx . . $ `kar/UnicodeData.h:14802:9`! || *from == UNICODE_PARAGRAPH_SEPARATORcxx . . $ `kar/UnicodeData.h:14804:9`!) |
| 152 | { |
| 153 | *to = '\n'; |
| 154 | } else { |
| 155 | *to = *from; |
| 156 | } |
| 157 | } |
| 158 | *to = '\0'; // closing null character |
| 159 | return to -cxx . . $ operator-(8538f57502d6bea9). text; |
| 160 | } |
| 161 | integercxx . . $ integer# Melder_killReturns_inplacecxx . . $ Melder_killReturns_inplace(9c47b7d7d51fe2c1). (char32cxx . . $ char32# *text) { |
| 162 | return Melder_killReturns_inplaceCHARcxx . . $ Melder_killReturns_inplaceCHAR(3b749fa451b4bc45). <char32cxx . . $ char32#> (text); |
| 163 | } |
| 164 | |
| 165 | int64cxx . . $ int64# Melder_length_utf8cxx . . $ Melder_length_utf8(4ef573ca2b27f34b). (conststring32cxx . . $ conststring32# string, bool nativizeNewlines) { |
| 166 | int64cxx . . $ int64# length = 0; |
| 167 | for (const char32cxx . . $ char32# *p = & string [0]; *p != U'\0'; p ++) { |
| 168 | char32cxx . . $ char32# kar = *p; |
| 169 | if (kar <= 0x00'007F) { |
| 170 | #ifdef _WIN32 |
| 171 | if (nativizeNewlines && kar == U'\n') |
| 172 | length ++; |
| 173 | #else |
| 174 | (void) nativizeNewlines; |
| 175 | #endif |
| 176 | length ++; |
| 177 | } else if (kar <= 0x00'07FF) { |
| 178 | length += 2; |
| 179 | } else if (kar <= 0x00'FFFF) { |
| 180 | length += 3; |
| 181 | } else { |
| 182 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (kar <= 0x10'FFFF); |
| 183 | length += 4; |
| 184 | } |
| 185 | } |
| 186 | return length; |
| 187 | } |
| 188 | |
| 189 | int64cxx . . $ int64# Melder_length_utf16cxx . . $ Melder_length_utf16(4ef573ca2b27f34b). (conststring32cxx . . $ conststring32# string, bool nativizeNewlines) { |
| 190 | int64cxx . . $ int64# length = 0; |
| 191 | for (const char32cxx . . $ char32# *p = & string [0]; *p != U'\0'; p ++) { |
| 192 | char32cxx . . $ char32# kar = *p; |
| 193 | if (kar <= 0x00'007F) { |
| 194 | #ifdef _WIN32 |
| 195 | if (nativizeNewlines && kar == U'\n') |
| 196 | length ++; |
| 197 | #else |
| 198 | (void) nativizeNewlines; |
| 199 | #endif |
| 200 | length ++; |
| 201 | } else if (kar >= 0x01'0000) { |
| 202 | length += 2; |
| 203 | } else { |
| 204 | length += 1; |
| 205 | } |
| 206 | } |
| 207 | return length; |
| 208 | } |
| 209 | |
| 210 | void MelderString_8to32cxx . . $ MelderString_8to32(908334b4533925d1). (MelderStringcxx . . $ MelderString# *me, conststring8cxx . . $ conststring8# textA) { |
| 211 | if (! textA) |
| 212 | textA = ""; |
| 213 | MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (me); |
| 214 | uintegercxx . . $ uinteger# n = strlen (textA); |
| 215 | for (uintegercxx . . $ uinteger# i = 0; i <= n; i ++) { |
| 216 | char8cxx . . $ char8# kar1 = (char8cxx . . $ char8#) textA [i]; // convert sign |
| 217 | if (kar1 <= 0x7F) { |
| 218 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, |
| 219 | (char32cxx . . $ char32#) kar1 |
| 220 | ); |
| 221 | } else if (kar1 <= 0xC1) { |
| 222 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 223 | } else if (kar1 <= 0xDF) { |
| 224 | char8cxx . . $ char8# kar2 = textA [++ i]; |
| 225 | if ((kar2 & 0xC0) != 0x80) |
| 226 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 227 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, |
| 228 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar1 & 0x00'001F) << 6) | |
| 229 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar2 & 0x00'003F) |
| 230 | ); |
| 231 | } else if (kar1 <= 0xEF) { |
| 232 | char8cxx . . $ char8# kar2 = textA [++ i]; |
| 233 | if ((kar2 & 0xC0) != 0x80) |
| 234 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 235 | char8cxx . . $ char8# kar3 = textA [++ i]; |
| 236 | if ((kar3 & 0xC0) != 0x80) |
| 237 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 238 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, |
| 239 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar1 & 0x00'000F) << 12) | |
| 240 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar2 & 0x00'003F) << 6) | |
| 241 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar3 & 0x00'003F) |
| 242 | ); |
| 243 | } else if (kar1 <= 0xF4) { |
| 244 | char8cxx . . $ char8# kar2 = (char8cxx . . $ char8#) textA [++ i]; |
| 245 | if ((kar2 & 0xC0) != 0x80) |
| 246 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 247 | char8cxx . . $ char8# kar3 = (char8cxx . . $ char8#) textA [++ i]; |
| 248 | if ((kar3 & 0xC0) != 0x80) |
| 249 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 250 | char8cxx . . $ char8# kar4 = (char8cxx . . $ char8#) textA [++ i]; |
| 251 | if ((kar4 & 0xC0) != 0x80) |
| 252 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 253 | char32cxx . . $ char32# character = |
| 254 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar1 & 0x00'0007) << 18) | |
| 255 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar2 & 0x00'003F) << 12) | |
| 256 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar3 & 0x00'003F) << 6) | |
| 257 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar4 & 0x00'003F); |
| 258 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, character); |
| 259 | } else { |
| 260 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (me, UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | conststring32cxx . . $ conststring32# Melder_peek8to32_ucxx . . $ Melder_peek8to32_u(4cad97a093ddcc1d). (conststring8cxx . . $ conststring8# textA) { |
| 266 | static MelderStringcxx . . $ MelderString# buffers [19]; |
| 267 | static int ibuffer = 0; |
| 268 | if (++ ibuffer == 19) |
| 269 | ibuffer = 0; |
| 270 | MelderString_8to32cxx . . $ MelderString_8to32(908334b4533925d1). (& buffers [ibuffer], textA); |
| 271 | return buffers [ibuffer].stringcxx . . $ MelderString#string.; |
| 272 | } |
| 273 | |
| 274 | char32cxx . . $ char32# Melder_decodeMacRomancxx . . $ Melder_decodeMacRoman. [256] = { |
| 275 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 276 | 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 277 | 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 278 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 279 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, |
| 280 | 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 281 | 120, 121, 122, 123, 124, 125, 126, 127, |
| 282 | UNICODE_LATIN_CAPITAL_LETTER_A_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:392:9`!, UNICODE_LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVEcxx . . $ `kar/UnicodeData.h:394:9`!, |
| 283 | UNICODE_LATIN_CAPITAL_LETTER_C_WITH_CEDILLAcxx . . $ `kar/UnicodeData.h:398:9`!, UNICODE_LATIN_CAPITAL_LETTER_E_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:402:9`!, |
| 284 | UNICODE_LATIN_CAPITAL_LETTER_N_WITH_TILDEcxx . . $ `kar/UnicodeData.h:418:9`!, UNICODE_LATIN_CAPITAL_LETTER_O_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:428:9`!, |
| 285 | UNICODE_LATIN_CAPITAL_LETTER_U_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:440:9`!, UNICODE_LATIN_SMALL_LETTER_A_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:450:9`!, |
| 286 | UNICODE_LATIN_SMALL_LETTER_A_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:448:9`!, UNICODE_LATIN_SMALL_LETTER_A_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:452:9`!, |
| 287 | UNICODE_LATIN_SMALL_LETTER_A_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:456:9`!, UNICODE_LATIN_SMALL_LETTER_A_WITH_TILDEcxx . . $ `kar/UnicodeData.h:454:9`!, |
| 288 | UNICODE_LATIN_SMALL_LETTER_A_WITH_RING_ABOVEcxx . . $ `kar/UnicodeData.h:458:9`!, UNICODE_LATIN_SMALL_LETTER_C_WITH_CEDILLAcxx . . $ `kar/UnicodeData.h:462:9`!, |
| 289 | UNICODE_LATIN_SMALL_LETTER_E_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:466:9`!, UNICODE_LATIN_SMALL_LETTER_E_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:464:9`!, |
| 290 | |
| 291 | UNICODE_LATIN_SMALL_LETTER_E_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:468:9`!, UNICODE_LATIN_SMALL_LETTER_E_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:470:9`!, |
| 292 | UNICODE_LATIN_SMALL_LETTER_I_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:474:9`!, UNICODE_LATIN_SMALL_LETTER_I_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:472:9`!, |
| 293 | UNICODE_LATIN_SMALL_LETTER_I_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:476:9`!, UNICODE_LATIN_SMALL_LETTER_I_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:478:9`!, |
| 294 | UNICODE_LATIN_SMALL_LETTER_N_WITH_TILDEcxx . . $ `kar/UnicodeData.h:482:9`!, UNICODE_LATIN_SMALL_LETTER_O_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:486:9`!, |
| 295 | UNICODE_LATIN_SMALL_LETTER_O_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:484:9`!, UNICODE_LATIN_SMALL_LETTER_O_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:488:9`!, |
| 296 | UNICODE_LATIN_SMALL_LETTER_O_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:492:9`!, UNICODE_LATIN_SMALL_LETTER_O_WITH_TILDEcxx . . $ `kar/UnicodeData.h:490:9`!, |
| 297 | UNICODE_LATIN_SMALL_LETTER_U_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:500:9`!, UNICODE_LATIN_SMALL_LETTER_U_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:498:9`!, |
| 298 | UNICODE_LATIN_SMALL_LETTER_U_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:502:9`!, UNICODE_LATIN_SMALL_LETTER_U_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:504:9`!, |
| 299 | |
| 300 | UNICODE_DAGGERcxx . . $ `kar/UnicodeData.h:14786:9`!, UNICODE_DEGREE_SIGNcxx . . $ `kar/UnicodeData.h:352:9`!, UNICODE_CENT_SIGNcxx . . $ `kar/UnicodeData.h:324:9`!, UNICODE_POUND_SIGNcxx . . $ `kar/UnicodeData.h:326:9`!, |
| 301 | UNICODE_SECTION_SIGNcxx . . $ `kar/UnicodeData.h:334:9`!, UNICODE_BULLETcxx . . $ `kar/UnicodeData.h:14790:9`!, UNICODE_PILCROW_SIGNcxx . . $ `kar/UnicodeData.h:364:9`!, UNICODE_LATIN_SMALL_LETTER_SHARP_Scxx . . $ `kar/UnicodeData.h:446:9`!, |
| 302 | UNICODE_REGISTERED_SIGNcxx . . $ `kar/UnicodeData.h:348:9`!, UNICODE_COPYRIGHT_SIGNcxx . . $ `kar/UnicodeData.h:338:9`!, UNICODE_TRADE_MARK_SIGNcxx . . $ `kar/UnicodeData.h:15228:9`!, UNICODE_ACUTE_ACCENTcxx . . $ `kar/UnicodeData.h:360:9`!, |
| 303 | UNICODE_DIAERESIScxx . . $ `kar/UnicodeData.h:336:9`!, UNICODE_NOT_EQUAL_TOcxx . . $ `kar/UnicodeData.h:15856:9`!, |
| 304 | UNICODE_LATIN_CAPITAL_LETTER_AEcxx . . $ `kar/UnicodeData.h:396:9`!, UNICODE_LATIN_CAPITAL_LETTER_O_WITH_STROKEcxx . . $ `kar/UnicodeData.h:432:9`!, |
| 305 | |
| 306 | UNICODE_INFINITYcxx . . $ `kar/UnicodeData.h:15724:9`!, UNICODE_PLUS_MINUS_SIGNcxx . . $ `kar/UnicodeData.h:354:9`!, UNICODE_LESS_THAN_OR_EQUAL_TOcxx . . $ `kar/UnicodeData.h:15864:9`!, UNICODE_GREATER_THAN_OR_EQUAL_TOcxx . . $ `kar/UnicodeData.h:15866:9`!, |
| 307 | UNICODE_YEN_SIGNcxx . . $ `kar/UnicodeData.h:330:9`!, UNICODE_MICRO_SIGNcxx . . $ `kar/UnicodeData.h:362:9`!, UNICODE_PARTIAL_DIFFERENTIALcxx . . $ `kar/UnicodeData.h:15668:9`!, UNICODE_N_ARY_SUMMATIONcxx . . $ `kar/UnicodeData.h:15698:9`!, |
| 308 | UNICODE_N_ARY_PRODUCTcxx . . $ `kar/UnicodeData.h:15694:9`!, UNICODE_GREEK_SMALL_LETTER_PIcxx . . $ `kar/UnicodeData.h:1902:9`!, UNICODE_INTEGRALcxx . . $ `kar/UnicodeData.h:15750:9`!, |
| 309 | UNICODE_FEMININE_ORDINAL_INDICATORcxx . . $ `kar/UnicodeData.h:340:9`!, UNICODE_MASCULINE_ORDINAL_INDICATORcxx . . $ `kar/UnicodeData.h:372:9`!, |
| 310 | UNICODE_GREEK_CAPITAL_LETTER_OMEGAcxx . . $ `kar/UnicodeData.h:1856:9`!, UNICODE_LATIN_SMALL_LETTER_AEcxx . . $ `kar/UnicodeData.h:460:9`!, |
| 311 | UNICODE_LATIN_SMALL_LETTER_O_WITH_STROKEcxx . . $ `kar/UnicodeData.h:496:9`!, |
| 312 | |
| 313 | UNICODE_INVERTED_QUESTION_MARKcxx . . $ `kar/UnicodeData.h:382:9`!, UNICODE_INVERTED_EXCLAMATION_MARKcxx . . $ `kar/UnicodeData.h:322:9`!, UNICODE_NOT_SIGNcxx . . $ `kar/UnicodeData.h:344:9`!, UNICODE_SQUARE_ROOTcxx . . $ `kar/UnicodeData.h:15716:9`!, |
| 314 | UNICODE_LATIN_SMALL_LETTER_F_WITH_HOOKcxx . . $ `kar/UnicodeData.h:804:9`!, UNICODE_ALMOST_EQUAL_TOcxx . . $ `kar/UnicodeData.h:15808:9`!, UNICODE_INCREMENTcxx . . $ `kar/UnicodeData.h:15676:9`!, |
| 315 | UNICODE_LEFT_POINTING_DOUBLE_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:342:9`!, UNICODE_RIGHT_POINTING_DOUBLE_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:374:9`!, |
| 316 | UNICODE_HORIZONTAL_ELLIPSIScxx . . $ `kar/UnicodeData.h:14798:9`!, UNICODE_NO_BREAK_SPACEcxx . . $ `kar/UnicodeData.h:320:9`!, UNICODE_LATIN_CAPITAL_LETTER_A_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:384:9`!, |
| 317 | UNICODE_LATIN_CAPITAL_LETTER_A_WITH_TILDEcxx . . $ `kar/UnicodeData.h:390:9`!, UNICODE_LATIN_CAPITAL_LETTER_O_WITH_TILDEcxx . . $ `kar/UnicodeData.h:426:9`!, |
| 318 | UNICODE_LATIN_CAPITAL_LIGATURE_OEcxx . . $ `kar/UnicodeData.h:676:9`!, UNICODE_LATIN_SMALL_LIGATURE_OEcxx . . $ `kar/UnicodeData.h:678:9`!, |
| 319 | |
| 320 | UNICODE_EN_DASHcxx . . $ `kar/UnicodeData.h:14760:9`!, UNICODE_EM_DASHcxx . . $ `kar/UnicodeData.h:14762:9`!, UNICODE_LEFT_DOUBLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14778:9`!, UNICODE_RIGHT_DOUBLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14780:9`!, |
| 321 | UNICODE_LEFT_SINGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14770:9`!, UNICODE_RIGHT_SINGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14772:9`!, UNICODE_DIVISION_SIGNcxx . . $ `kar/UnicodeData.h:494:9`!, UNICODE_LOZENGEcxx . . $ `kar/UnicodeData.h:17518:9`!, |
| 322 | UNICODE_LATIN_SMALL_LETTER_Y_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:510:9`!, UNICODE_LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:752:9`!, |
| 323 | UNICODE_FRACTION_SLASHcxx . . $ `kar/UnicodeData.h:14858:9`!, UNICODE_EURO_SIGNcxx . . $ `kar/UnicodeData.h:15052:9`!, |
| 324 | UNICODE_SINGLE_LEFT_POINTING_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14836:9`!, UNICODE_SINGLE_RIGHT_POINTING_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14838:9`!, |
| 325 | UNICODE_LATIN_SMALL_LIGATURE_FIcxx . . $ `kar/UnicodeData.h:31482:9`!, UNICODE_LATIN_SMALL_LIGATURE_FLcxx . . $ `kar/UnicodeData.h:31484:9`!, |
| 326 | |
| 327 | UNICODE_DOUBLE_DAGGERcxx . . $ `kar/UnicodeData.h:14788:9`!, UNICODE_MIDDLE_DOTcxx . . $ `kar/UnicodeData.h:366:9`!, |
| 328 | UNICODE_SINGLE_LOW_9_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14774:9`!, UNICODE_DOUBLE_LOW_9_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14782:9`!, UNICODE_PER_MILLE_SIGNcxx . . $ `kar/UnicodeData.h:14818:9`!, |
| 329 | UNICODE_LATIN_CAPITAL_LETTER_A_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:388:9`!, UNICODE_LATIN_CAPITAL_LETTER_E_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:404:9`!, |
| 330 | UNICODE_LATIN_CAPITAL_LETTER_A_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:386:9`!, UNICODE_LATIN_CAPITAL_LETTER_E_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:406:9`!, |
| 331 | UNICODE_LATIN_CAPITAL_LETTER_E_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:400:9`!, UNICODE_LATIN_CAPITAL_LETTER_I_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:410:9`!, |
| 332 | UNICODE_LATIN_CAPITAL_LETTER_I_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:412:9`!, UNICODE_LATIN_CAPITAL_LETTER_I_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:414:9`!, |
| 333 | UNICODE_LATIN_CAPITAL_LETTER_I_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:408:9`!, UNICODE_LATIN_CAPITAL_LETTER_O_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:422:9`!, |
| 334 | UNICODE_LATIN_CAPITAL_LETTER_O_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:424:9`!, |
| 335 | |
| 336 | 0xf8ff /* Apple logo */, UNICODE_LATIN_CAPITAL_LETTER_O_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:420:9`!, UNICODE_LATIN_CAPITAL_LETTER_U_WITH_ACUTEcxx . . $ `kar/UnicodeData.h:436:9`!, |
| 337 | UNICODE_LATIN_CAPITAL_LETTER_U_WITH_CIRCUMFLEXcxx . . $ `kar/UnicodeData.h:438:9`!, UNICODE_LATIN_CAPITAL_LETTER_U_WITH_GRAVEcxx . . $ `kar/UnicodeData.h:434:9`!, |
| 338 | UNICODE_LATIN_SMALL_LETTER_DOTLESS_Icxx . . $ `kar/UnicodeData.h:610:9`!, UNICODE_MODIFIER_LETTER_CIRCUMFLEX_ACCENTcxx . . $ `kar/UnicodeData.h:1420:9`!, UNICODE_SMALL_TILDEcxx . . $ `kar/UnicodeData.h:1464:9`!, |
| 339 | UNICODE_MACRONcxx . . $ `kar/UnicodeData.h:350:9`!, UNICODE_BREVEcxx . . $ `kar/UnicodeData.h:1456:9`!, UNICODE_DOT_ABOVEcxx . . $ `kar/UnicodeData.h:1458:9`!, UNICODE_RING_ABOVEcxx . . $ `kar/UnicodeData.h:1460:9`!, UNICODE_CEDILLAcxx . . $ `kar/UnicodeData.h:368:9`!, |
| 340 | UNICODE_DOUBLE_ACUTE_ACCENTcxx . . $ `kar/UnicodeData.h:1466:9`!, UNICODE_OGONEKcxx . . $ `kar/UnicodeData.h:1462:9`!, UNICODE_CARONcxx . . $ `kar/UnicodeData.h:1422:9`! }; |
| 341 | |
| 342 | char32cxx . . $ char32# Melder_decodeWindowsLatin1cxx . . $ Melder_decodeWindowsLatin1. [256] = { |
| 343 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 344 | 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 345 | 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 346 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 347 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, |
| 348 | 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 349 | 120, 121, 122, 123, 124, 125, 126, 127, |
| 350 | UNICODE_EURO_SIGNcxx . . $ `kar/UnicodeData.h:15052:9`!, 129, UNICODE_SINGLE_LOW_9_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14774:9`!, UNICODE_LATIN_SMALL_LETTER_F_WITH_HOOKcxx . . $ `kar/UnicodeData.h:804:9`!, |
| 351 | UNICODE_DOUBLE_LOW_9_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14782:9`!, UNICODE_HORIZONTAL_ELLIPSIScxx . . $ `kar/UnicodeData.h:14798:9`!, UNICODE_DAGGERcxx . . $ `kar/UnicodeData.h:14786:9`!, UNICODE_DOUBLE_DAGGERcxx . . $ `kar/UnicodeData.h:14788:9`!, |
| 352 | UNICODE_MODIFIER_LETTER_CIRCUMFLEX_ACCENTcxx . . $ `kar/UnicodeData.h:1420:9`!, UNICODE_PER_MILLE_SIGNcxx . . $ `kar/UnicodeData.h:14818:9`!, UNICODE_LATIN_CAPITAL_LETTER_S_WITH_CARONcxx . . $ `kar/UnicodeData.h:704:9`!, |
| 353 | UNICODE_SINGLE_LEFT_POINTING_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14836:9`!, UNICODE_LATIN_CAPITAL_LIGATURE_OEcxx . . $ `kar/UnicodeData.h:676:9`!, 141, |
| 354 | UNICODE_LATIN_CAPITAL_LETTER_Z_WITH_CARONcxx . . $ `kar/UnicodeData.h:762:9`!, 143, |
| 355 | 144, UNICODE_LEFT_SINGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14770:9`!, UNICODE_RIGHT_SINGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14772:9`!, UNICODE_LEFT_DOUBLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14778:9`!, |
| 356 | UNICODE_RIGHT_DOUBLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14780:9`!, UNICODE_BULLETcxx . . $ `kar/UnicodeData.h:14790:9`!, UNICODE_EN_DASHcxx . . $ `kar/UnicodeData.h:14760:9`!, UNICODE_EM_DASHcxx . . $ `kar/UnicodeData.h:14762:9`!, |
| 357 | UNICODE_SMALL_TILDEcxx . . $ `kar/UnicodeData.h:1464:9`!, UNICODE_TRADE_MARK_SIGNcxx . . $ `kar/UnicodeData.h:15228:9`!, UNICODE_LATIN_SMALL_LETTER_S_WITH_CARONcxx . . $ `kar/UnicodeData.h:706:9`!, |
| 358 | UNICODE_SINGLE_RIGHT_POINTING_ANGLE_QUOTATION_MARKcxx . . $ `kar/UnicodeData.h:14838:9`!, UNICODE_LATIN_SMALL_LIGATURE_OEcxx . . $ `kar/UnicodeData.h:678:9`!, 157, |
| 359 | UNICODE_LATIN_SMALL_LETTER_Z_WITH_CARONcxx . . $ `kar/UnicodeData.h:764:9`!, UNICODE_LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIScxx . . $ `kar/UnicodeData.h:752:9`!, |
| 360 | 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, |
| 361 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, |
| 362 | 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 363 | 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 364 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 }; |
| 365 | |
| 366 | void Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (conststring8cxx . . $ conststring8# string8, mutablestring32cxx . . $ mutablestring32# string32, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding# inputEncoding) { |
| 367 | const char8cxx . . $ char8# *p = (const char8cxx . . $ char8# *) & string8 [0]; |
| 368 | char32cxx . . $ char32# *q = & string32 [0]; |
| 369 | if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UNDEFINEDcxx . . $ kMelder_textInputEncoding#UNDEFINED.) { |
| 370 | inputEncoding = preferencescxx . . $ preferences.. inputEncodingcxx . . $ $anonymous_type_1fadc92bd65d6f53_0#inputEncoding.; |
| 371 | /* |
| 372 | In case the preferences weren't initialized yet, use the platform defaults: |
| 373 | */ |
| 374 | if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UNDEFINEDcxx . . $ kMelder_textInputEncoding#UNDEFINED.) { |
| 375 | #if defined (macintosh) |
| 376 | inputEncoding = kMelder_textInputEncoding::UTF8_THEN_MACROMAN; |
| 377 | #elif defined (_WIN32) |
| 378 | inputEncoding = kMelder_textInputEncoding::UTF8_THEN_WINDOWS_LATIN1; |
| 379 | #else |
| 380 | inputEncoding = kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_ISO_LATIN1cxx . . $ kMelder_textInputEncoding#UTF8_THEN_ISO_LATIN1.; |
| 381 | #endif |
| 382 | } |
| 383 | } |
| 384 | if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8. || |
| 385 | inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_ISO_LATIN1cxx . . $ kMelder_textInputEncoding#UTF8_THEN_ISO_LATIN1. || |
| 386 | inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_WINDOWS_LATIN1cxx . . $ kMelder_textInputEncoding#UTF8_THEN_WINDOWS_LATIN1. || |
| 387 | inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_MACROMANcxx . . $ kMelder_textInputEncoding#UTF8_THEN_MACROMAN.) |
| 388 | { |
| 389 | if (Melder_str8IsValidUtf8cxx . . $ Melder_str8IsValidUtf8(c5f7da30cbc0f4e9). (string8)) { |
| 390 | inputEncoding = kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.; |
| 391 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_ISO_LATIN1cxx . . $ kMelder_textInputEncoding#UTF8_THEN_ISO_LATIN1.) { |
| 392 | inputEncoding = kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::ISO_LATIN1cxx . . $ kMelder_textInputEncoding#ISO_LATIN1.; |
| 393 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_WINDOWS_LATIN1cxx . . $ kMelder_textInputEncoding#UTF8_THEN_WINDOWS_LATIN1.) { |
| 394 | inputEncoding = kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::WINDOWS_LATIN1cxx . . $ kMelder_textInputEncoding#WINDOWS_LATIN1.; |
| 395 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8_THEN_MACROMANcxx . . $ kMelder_textInputEncoding#UTF8_THEN_MACROMAN.) { |
| 396 | inputEncoding = kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::MACROMANcxx . . $ kMelder_textInputEncoding#MACROMAN.; |
| 397 | } else { |
| 398 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (inputEncoding == kMelder_textInputEncoding::UTF8); |
| 399 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Text is not valid UTF-8; please try a different text input encoding."); |
| 400 | } |
| 401 | } |
| 402 | if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.) { |
| 403 | while (*p != '\0') { |
| 404 | char32cxx . . $ char32# kar1 = *p ++; // convert up without sign extension |
| 405 | if (kar1 <= 0x00'007F) { |
| 406 | *q ++ = kar1; |
| 407 | } else if (kar1 <= 0x00'00DF) { |
| 408 | char32cxx . . $ char32# kar2 = *p ++; // convert up without sign extension |
| 409 | *q ++ = ((kar1 & 0x00'001F) << 6) | (kar2 & 0x00'003F); |
| 410 | } else if (kar1 <= 0x00'00EF) { |
| 411 | char32cxx . . $ char32# kar2 = *p ++, kar3 = *p ++; // convert up without sign extension |
| 412 | *q ++ = ((kar1 & 0x00'000F) << 12) | ((kar2 & 0x00'003F) << 6) | (kar3 & 0x00'003F); |
| 413 | } else if (kar1 <= 0x00'00F4) { |
| 414 | char32cxx . . $ char32# kar2 = *p ++, kar3 = *p ++, kar4 = *p ++; // convert up without sign extension |
| 415 | char32cxx . . $ char32# kar = ((kar1 & 0x00'0007) << 18) | ((kar2 & 0x00'003F) << 12) | ((kar3 & 0x00'003F) << 6) | (kar4 & 0x00'003F); |
| 416 | *q ++ = kar; |
| 417 | } |
| 418 | } |
| 419 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::ISO_LATIN1cxx . . $ kMelder_textInputEncoding#ISO_LATIN1.) { |
| 420 | while (*p != '\0') |
| 421 | *q ++ = *p ++; |
| 422 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::WINDOWS_LATIN1cxx . . $ kMelder_textInputEncoding#WINDOWS_LATIN1.) { |
| 423 | while (*p != '\0') |
| 424 | *q ++ = Melder_decodeWindowsLatin1cxx . . $ Melder_decodeWindowsLatin1. [*p ++]; |
| 425 | } else if (inputEncoding == kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::MACROMANcxx . . $ kMelder_textInputEncoding#MACROMAN.) { |
| 426 | while (*p != '\0') |
| 427 | *q ++ = Melder_decodeMacRomancxx . . $ Melder_decodeMacRoman. [*p ++]; |
| 428 | } else if (inputEncoding != kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.) { |
| 429 | Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Unknown text input encoding ", (int) inputEncoding, U"."); |
| 430 | } |
| 431 | *q = U'\0'; // closing null character |
| 432 | (void) Melder_killReturns_inplaceCHARcxx . . $ Melder_killReturns_inplaceCHAR(3b749fa451b4bc45). <char32cxx . . $ char32#> (string32); |
| 433 | } |
| 434 | |
| 435 | autostring32cxx . . $ autostring32# Melder_8to32_ecxx . . $ Melder_8to32_e(aa783ea934c735af). (const char *string, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding# inputEncoding) { |
| 436 | if (! string) |
| 437 | return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).(); |
| 438 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (string)); |
| 439 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (string, result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), inputEncoding); |
| 440 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 441 | } |
| 442 | |
| 443 | autostring32cxx . . $ autostring32# Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (const char *string) { |
| 444 | if (! string) |
| 445 | return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).(); |
| 446 | autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (string)); |
| 447 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (string, result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.); |
| 448 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 449 | } |
| 450 | |
| 451 | conststring32cxx . . $ conststring32# Melder_peek16to32cxx . . $ Melder_peek16to32(997cc7cc32ad29b). (conststring16cxx . . $ conststring16# text) { |
| 452 | if (! text) |
| 453 | return nullptr; |
| 454 | static MelderStringcxx . . $ MelderString# buffers [19]; |
| 455 | static int bufferNumber = 0; |
| 456 | if (++ bufferNumber == 19) |
| 457 | bufferNumber = 0; |
| 458 | MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (& buffers [bufferNumber]); |
| 459 | for (;;) { |
| 460 | char16cxx . . $ char16# kar1 = *text ++; |
| 461 | if (kar1 == u'\0') |
| 462 | return buffers [bufferNumber].stringcxx . . $ MelderString#string.; |
| 463 | if (kar1 < 0xD800) { |
| 464 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& buffers [bufferNumber], (char32cxx . . $ char32#) kar1); // convert up without sign extension |
| 465 | } else if (kar1 < 0xDC00) { |
| 466 | char16cxx . . $ char16# kar2 = *text ++; |
| 467 | if (kar2 >= 0xDC00 && kar2 <= 0xDFFF) { |
| 468 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& buffers [bufferNumber], |
| 469 | (char32cxx . . $ char32#) (0x01'0000 + |
| 470 | (char32cxx . . $ char32#) (((char32cxx . . $ char32#) kar1 & 0x00'03FF) << 10) + |
| 471 | (char32cxx . . $ char32#) ((char32cxx . . $ char32#) kar2 & 0x00'03FF))); |
| 472 | } else { |
| 473 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& buffers [bufferNumber], UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 474 | } |
| 475 | } else if (kar1 < 0xE000) { |
| 476 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& buffers [bufferNumber], UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!); |
| 477 | } else { |
| 478 | MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& buffers [bufferNumber], (char32cxx . . $ char32#) kar1); // convert up without sign extension |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | autostring32cxx . . $ autostring32# Melder_16to32cxx . . $ Melder_16to32(9cb2d799b2dc7015). (conststring16cxx . . $ conststring16# text) { |
| 484 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_peek16to32cxx . . $ Melder_peek16to32(997cc7cc32ad29b). (text)); |
| 485 | } |
| 486 | |
| 487 | void Melder_32to8_inplacecxx . . $ Melder_32to8_inplace(d86258d5722a82b2). (conststring32cxx . . $ conststring32# string, mutablestring8cxx . . $ mutablestring8# utf8) {; |
| 488 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (utf8); // and unassertable: utf8 should be a long enough buffer |
| 489 | char8cxx . . $ char8# *to = (char8cxx . . $ char8# *) & utf8 [0]; |
| 490 | if (string) { |
| 491 | const char32cxx . . $ char32# *p = & string [0]; |
| 492 | while (*p != U'\0') { |
| 493 | const char32cxx . . $ char32# kar = *p ++; |
| 494 | if (kar <= 0x00'007F) { // 7 bits |
| 495 | #ifdef _WIN32 |
| 496 | if (kar == U'\n') |
| 497 | *to ++ = 13; |
| 498 | #endif |
| 499 | *to ++ = (char8cxx . . $ char8#) kar; // guarded truncation |
| 500 | } else if (kar <= 0x00'07FF) { // 11 bits |
| 501 | *to ++ = (char8cxx . . $ char8#) (0x00'00C0 | (kar >> 6)); // the upper 5 bits yield a number between 0xC4 and 0xDF |
| 502 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F)); // the lower 6 bits yield a number between 0x80 and 0xBF |
| 503 | } else if (kar <= 0x00'FFFF) { // 16 bits |
| 504 | *to ++ = (char8cxx . . $ char8#) (0x00'00E0 | (kar >> 12)); // the upper 4 bits yield a number between 0xE0 and 0xEF |
| 505 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 6) & 0x00'003F)); |
| 506 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F)); |
| 507 | } else { // unguarded truncation to 21 bits |
| 508 | *to ++ = (char8cxx . . $ char8#) (0x00'00F0 | (kar >> 18)); // the upper 3 bits yield a number between 0xF0 and 0xF4 (0x10FFFF >> 18 == 4) |
| 509 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 12) & 0x00'003F)); // the next 6 bits |
| 510 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 6) & 0x00'003F)); // the third 6 bits |
| 511 | *to ++ = (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F)); // the lower 6 bits |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | *to = '\0'; |
| 516 | } |
| 517 | |
| 518 | conststring8cxx . . $ conststring8# Melder_peek32to8cxx . . $ Melder_peek32to8(c6a971952cbb8e31). (conststring32cxx . . $ conststring32# text) { |
| 519 | if (! text) |
| 520 | return nullptr; |
| 521 | static mutablestring8cxx . . $ mutablestring8# buffers [19] { nullptr }; |
| 522 | static int64cxx . . $ int64# bufferSizes [19] { 0 }; |
| 523 | static int bufferNumber = 0; |
| 524 | if (++ bufferNumber == 19) |
| 525 | bufferNumber = 0; |
| 526 | constexpr int64cxx . . $ int64# maximumNumberOfUTF8bytesPerUTF32point = 4; // because we use only the lower 21 bits |
| 527 | const int64cxx . . $ int64# numberOfUTF32points = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 528 | const int64cxx . . $ int64# maximumNumberOfBytesNeeded = numberOfUTF32points * maximumNumberOfUTF8bytesPerUTF32point + 1; |
| 529 | if ((bufferSizes [bufferNumber] - maximumNumberOfBytesNeeded) * (integercxx . . $ integer#) sizeof (char) >= 10'000) { |
| 530 | Melder_freecxx . . $ `melder/melder_alloc.h:38:9`! (buffers [bufferNumber]); |
| 531 | bufferSizes [bufferNumber] = 0; |
| 532 | } |
| 533 | if (maximumNumberOfBytesNeeded > bufferSizes [bufferNumber]) { |
| 534 | const int64cxx . . $ int64# newBufferSize = (int64cxx . . $ int64#) floorcxx . . $ floor(fee4f1d22bc8e617). (maximumNumberOfBytesNeeded * 1.61803) + 100; |
| 535 | buffers [bufferNumber] = (char *) Melder_realloc_fcxx . . $ Melder_realloc_f(d42fd80046fa8ace). (buffers [bufferNumber], newBufferSize * (int64cxx . . $ int64#) sizeof (char)); |
| 536 | bufferSizes [bufferNumber] = newBufferSize; |
| 537 | } |
| 538 | Melder_32to8_inplacecxx . . $ Melder_32to8_inplace(d86258d5722a82b2). (text, buffers [bufferNumber]); |
| 539 | return buffers [bufferNumber]; |
| 540 | } |
| 541 | |
| 542 | autostring8cxx . . $ autostring8# Melder_32to8cxx . . $ Melder_32to8(d0484fb546b1094e). (conststring32cxx . . $ conststring32# string) { |
| 543 | if (! string) |
| 544 | return autostring8cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).(); |
| 545 | autostring8cxx . . $ autostring8# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder_length_utf8cxx . . $ Melder_length_utf8(4ef573ca2b27f34b). (string, true)); |
| 546 | Melder_32to8_inplacecxx . . $ Melder_32to8_inplace(d86258d5722a82b2). (string, result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 547 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 548 | } |
| 549 | |
| 550 | conststring16cxx . . $ conststring16# Melder_peek32to16cxx . . $ Melder_peek32to16(52b763102d02933b). (conststring32cxx . . $ conststring32# text, bool nativizeNewlines) { |
| 551 | if (! text) |
| 552 | return nullptr; |
| 553 | static MelderString16cxx . . $ MelderString16# buffers [19] { }; |
| 554 | static int bufferNumber = 0; |
| 555 | if (++ bufferNumber == 19) |
| 556 | bufferNumber = 0; |
| 557 | MelderString16_emptycxx . . $ MelderString16_empty(144515e9b438f4c0). (& buffers [bufferNumber]); |
| 558 | const int64cxx . . $ int64# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 559 | if (nativizeNewlines) { |
| 560 | for (int64cxx . . $ int64# i = 0; i <= n; i ++) { |
| 561 | #ifdef _WIN32 |
| 562 | if (text [i] == U'\n') |
| 563 | MelderString16_appendCharacter (& buffers [bufferNumber], (char32) 13); |
| 564 | #endif |
| 565 | MelderString16_appendCharactercxx . . $ MelderString16_appendCharacter(8837845903e871f2). (& buffers [bufferNumber], text [i]); |
| 566 | } |
| 567 | } else { |
| 568 | for (int64cxx . . $ int64# i = 0; i <= n; i ++) |
| 569 | MelderString16_appendCharactercxx . . $ MelderString16_appendCharacter(8837845903e871f2). (& buffers [bufferNumber], text [i]); |
| 570 | } |
| 571 | return buffers [bufferNumber].stringcxx . . $ MelderString16#string.; |
| 572 | } |
| 573 | conststring16cxx . . $ conststring16# Melder_peek32to16cxx . . $ Melder_peek32to16(879c53a1c5a150c1). (conststring32cxx . . $ conststring32# text) { |
| 574 | return Melder_peek32to16cxx . . $ Melder_peek32to16(52b763102d02933b). (text, false); |
| 575 | } |
| 576 | |
| 577 | autostring16cxx . . $ autostring16# Melder_32to16cxx . . $ Melder_32to16(7ff9f4a4972d16cf). (conststring32cxx . . $ conststring32# text) { |
| 578 | conststring16cxx . . $ conststring16# text16 = Melder_peek32to16cxx . . $ Melder_peek32to16(879c53a1c5a150c1). (text); |
| 579 | const integercxx . . $ integer# length = Melder16_lengthcxx . . $ Melder16_length(7c057485137234bf). (text16); |
| 580 | autostring16cxx . . $ autostring16# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (length); |
| 581 | str16cpycxx . . $ str16cpy(cee0347b89119601). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), text16); |
| 582 | return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 583 | } |
| 584 | |
| 585 | #if defined (_WIN32) |
| 586 | autostringW Melder_32toW (conststring32 text) { |
| 587 | conststringW textW = Melder_peek32toW (text); |
| 588 | const integer length = Melder16_length ((conststring16) textW); |
| 589 | autostringW result (length); |
| 590 | str16cpy ((mutablestring16) result.get(), (conststring16) textW); |
| 591 | return result; |
| 592 | } |
| 593 | conststringW Melder_peek32toW_fileSystem (conststring32 string) { |
| 594 | static wchar_t buffer [1 + kMelder_MAXPATH]; |
| 595 | NormalizeString (NormalizationC, Melder_peek32toW (string), -1, buffer, 1 + kMelder_MAXPATH); |
| 596 | //FoldStringW (MAP_PRECOMPOSED, Melder_peek32toW (string), -1, buffer, 1 + kMelder_MAXPATH); // this works even on Windows XP |
| 597 | return buffer; |
| 598 | } |
| 599 | autostringW Melder_32toW_fileSystem (conststring32 text) { |
| 600 | conststringW textW = Melder_peek32toW_fileSystem (text); |
| 601 | const integer length = Melder16_length ((conststring16) textW); |
| 602 | autostringW result (length); |
| 603 | str16cpy ((mutablestring16) result.get(), (conststring16) textW); |
| 604 | return result; |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | void Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (conststring32cxx . . $ conststring32# string, char *utf8) { |
| 609 | #if defined (macintosh) |
| 610 | /* |
| 611 | On the Mac, the POSIX path name is stored in canonically decomposed UTF-8 encoding. |
| 612 | The path is probably in precomposed UTF-32. |
| 613 | So we first convert to UTF-16, then turn into CFString, then decompose, then convert to UTF-8. |
| 614 | TODO: why not CFStringGetFileSystemRepresentation? perhaps because it's available from 10.4, whereas CFStringNormalize from 10.2 |
| 615 | */ |
| 616 | UniChar unipath [kMelder_MAXPATH+1]; |
| 617 | const int64 n = Melder_length (string); |
| 618 | int n_utf16 = 0; |
| 619 | for (int64 i = 0; i < n; i ++) { |
| 620 | char32 kar = (char32) string [i]; // change sign (bit 32 is never used) |
| 621 | if (kar <= 0x00'FFFF) { |
| 622 | unipath [n_utf16 ++] = (UniChar) kar; // including null byte; guarded truncation |
| 623 | } else if (kar <= 0x10'FFFF) { |
| 624 | kar -= 0x01'0000; |
| 625 | unipath [n_utf16 ++] = (UniChar) (0x00'D800 | (kar >> 10)); // correct truncation, because UTF-32 has fewer than 27 bits (in fact it has 21 bits) |
| 626 | unipath [n_utf16 ++] = (UniChar) (0x00'DC00 | (kar & 0x00'03FF)); |
| 627 | } else { |
| 628 | unipath [n_utf16 ++] = UNICODE_REPLACEMENT_CHARACTER; |
| 629 | } |
| 630 | } |
| 631 | unipath [n_utf16] = u'\0'; |
| 632 | CFStringRef cfpath = CFStringCreateWithCharacters (nullptr, unipath, n_utf16); |
| 633 | CFMutableStringRef cfpath2 = CFStringCreateMutableCopy (nullptr, 0, cfpath); |
| 634 | CFRelease (cfpath); |
| 635 | CFStringNormalize (cfpath2, kCFStringNormalizationFormD); // Mac requires decomposed characters |
| 636 | CFStringGetCString (cfpath2, (char *) utf8, kMelder_MAXPATH+1, kCFStringEncodingUTF8); // Mac POSIX requires UTF-8 |
| 637 | CFRelease (cfpath2); |
| 638 | #else |
| 639 | Melder_32to8_inplacecxx . . $ Melder_32to8_inplace(d86258d5722a82b2). (string, utf8); |
| 640 | // fallback; should work even on Windows if converted to W later (as in op_open_file 2021-12-16 and flac_fopen 2024-08-11) |
| 641 | #endif |
| 642 | } |
| 643 | conststring8cxx . . $ conststring8# Melder_peek32to8_fileSystemcxx . . $ Melder_peek32to8_fileSystem(c6a971952cbb8e31). (conststring32cxx . . $ conststring32# string) { |
| 644 | static char buffer [1 + kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!]; |
| 645 | Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (string, buffer); |
| 646 | return buffer; |
| 647 | } |
| 648 | |
| 649 | #if defined (macintosh) |
| 650 | const void * Melder_peek32toCfstring (conststring32 text) { |
| 651 | if (! text) |
| 652 | return nullptr; |
| 653 | static CFStringRef cfString [11]; |
| 654 | static int icfString = 0; |
| 655 | if (++ icfString == 11) |
| 656 | icfString = 0; |
| 657 | if (cfString [icfString]) |
| 658 | CFRelease (cfString [icfString]); |
| 659 | cfString [icfString] = CFStringCreateWithCString (nullptr, (const char *) Melder_peek32to8 (text), kCFStringEncodingUTF8); |
| 660 | return cfString [icfString]; |
| 661 | } |
| 662 | const void * Melder_peek32toCfstring_fileSystem (conststring32 text) { |
| 663 | if (! text) |
| 664 | return nullptr; |
| 665 | static CFStringRef cfString [11]; |
| 666 | static int icfString = 0; |
| 667 | if (++ icfString == 11) |
| 668 | icfString = 0; |
| 669 | if (cfString [icfString]) |
| 670 | CFRelease (cfString [icfString]); |
| 671 | cfString [icfString] = CFStringCreateWithCString (nullptr, (const char *) Melder_peek32to8_fileSystem (text), kCFStringEncodingUTF8); |
| 672 | return cfString [icfString]; |
| 673 | } |
| 674 | #endif |
| 675 | |
| 676 | /* End of file melder_textencoding.cpp */ |