| 1 | /* Strings.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011-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 "Strings_.h"cxx . . $ `<file>/sys/Strings_.h`/ |
| 20 | #include "../kar/longchar.h"cxx . . $ `<file>/kar/longchar.h`/ |
| 21 | |
| 22 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 23 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 24 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 25 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 26 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 27 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 29 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 30 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 31 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 32 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 33 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 34 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 35 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 36 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 37 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 38 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 39 | #include "Strings_def.h"cxx . . $ `<file>/sys/Strings_def.h`/ |
| 40 | |
| 41 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Strings, Daata, 0); |
| 42 | |
| 43 | static double Strings_minimumLengthcxx . . $ Strings_minimumLength(775293070f0391ef). (Strings me) { |
| 44 | return NUMminimumLengthcxx . . $ NUMminimumLength(ca1a6d5bd822b1c2). (mycxx . . $ `melder/melder.h:44:9`! strings.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 45 | } |
| 46 | static double Strings_maximumLengthcxx . . $ Strings_maximumLength(775293070f0391ef). (Strings me) { |
| 47 | return NUMmaximumLengthcxx . . $ NUMmaximumLength(ca1a6d5bd822b1c2). (mycxx . . $ `melder/melder.h:44:9`! strings.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 48 | } |
| 49 | static double Strings_totalLengthcxx . . $ Strings_totalLength(775293070f0391ef). (Strings me) { |
| 50 | return NUMtotalLengthcxx . . $ NUMtotalLength(eab58d34c81ade32). (mycxx . . $ `melder/melder.h:44:9`! strings.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 51 | } |
| 52 | |
| 53 | void structStrings :: v1_info () { |
| 54 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 55 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of strings: ", ourcxx . . $ `melder/melder.h:42:9`! numberOfStrings); |
| 56 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Total length: ", Strings_totalLengthcxx . . $ Strings_totalLength(775293070f0391ef). (this), U" characters"); |
| 57 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Shortest string: ", Strings_minimumLengthcxx . . $ Strings_minimumLength(775293070f0391ef). (this), U" characters"); |
| 58 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Longest string: ", Strings_maximumLengthcxx . . $ Strings_maximumLength(775293070f0391ef). (this), U" characters"); |
| 59 | } |
| 60 | |
| 61 | conststring32cxx . . $ conststring32# structStrings :: v_getVectorStr (const integercxx . . $ integer# icol) const { |
| 62 | if (icol < 1 || icol > ourcxx . . $ `melder/melder.h:42:9`! numberOfStrings) |
| 63 | return U""; |
| 64 | char32cxx . . $ char32# *stringValue = strings [cxx . . $ _autostringautovector#`operator[]`(56be665fc86fc225).icol].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 65 | return stringValue ? stringValue : U""; |
| 66 | } |
| 67 | |
| 68 | autoStrings Strings_createFromTextscxx . . $ Strings_createFromTexts(b66ca11e948a00b5). (constSTRVECcxx . . $ constSTRVEC# const& texts) { |
| 69 | try { |
| 70 | autoStrings me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Strings); |
| 71 | mycxx . . $ `melder/melder.h:44:9`! strings =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). copy_STRVECcxx . . $ copy_STRVEC(cfa8029879abedac). (texts); |
| 72 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 73 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 74 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 75 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Strings object not created from texts."); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | autoStrings Strings_createAsFileListcxx . . $ Strings_createAsFileList(9acdfa623bce7965). (conststring32cxx . . $ conststring32# path /* cattable */) { |
| 80 | try { |
| 81 | autoStrings me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Strings); |
| 82 | mycxx . . $ `melder/melder.h:44:9`! strings =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). fileNames_STRVECcxx . . $ fileNames_STRVEC(d3c03a92d990d008). (path); |
| 83 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 84 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 85 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 86 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Strings object not created as file list."); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | autoStrings Strings_createAsFolderListcxx . . $ Strings_createAsFolderList(9acdfa623bce7965). (conststring32cxx . . $ conststring32# path /* cattable */) { |
| 91 | try { |
| 92 | autoStrings me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Strings); |
| 93 | mycxx . . $ `melder/melder.h:44:9`! strings =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). folderNames_STRVECcxx . . $ folderNames_STRVEC(d3c03a92d990d008). (path); |
| 94 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 95 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 96 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 97 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Strings object not created as folder list."); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | autoStrings Strings_readFromRawTextFilecxx . . $ Strings_readFromRawTextFile(850e0d38cca1ee08). (MelderFilecxx . . $ MelderFile# file) { |
| 102 | try { |
| 103 | autoStrings me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Strings); |
| 104 | mycxx . . $ `melder/melder.h:44:9`! strings =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). readLinesFromFile_STRVECcxx . . $ readLinesFromFile_STRVEC(963377c6631bedd0). (file); |
| 105 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 106 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 107 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 108 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Strings not read from raw text file ", file, U"."); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void Strings_writeToRawTextFilecxx . . $ Strings_writeToRawTextFile(986e0b375a7a787a). (Strings me, MelderFilecxx . . $ MelderFile# file) { |
| 113 | try { |
| 114 | Thing_assertInvariantscxx . . $ Thing_assertInvariants(97cddd01752945f6). (me); |
| 115 | autoMelderStringcxx . . $ autoMelderString# buffercxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8).; |
| 116 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStrings; i ++) |
| 117 | MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& buffer, mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U"\n"); |
| 118 | MelderFile_writeText_ecxx . . $ MelderFile_writeText_e(4f26204211a3b413). (file, buffer.stringcxx . . $ MelderString#string., Melder_getOutputEncodingcxx . . $ Melder_getOutputEncoding(581d0d572cba43d7). ()); |
| 119 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 120 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Strings not written to raw text file ", file, U"."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void Strings_randomizecxx . . $ Strings_randomize(1f79c244d2d6c1ef). (Strings me) { |
| 125 | for (integercxx . . $ integer# i = 1; i < mycxx . . $ `melder/melder.h:44:9`! numberOfStrings; i ++) { |
| 126 | integercxx . . $ integer# other = NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (i, mycxx . . $ `melder/melder.h:44:9`! numberOfStrings); |
| 127 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).other], mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void Strings_genericizecxx . . $ Strings_genericize(1f79c244d2d6c1ef). (Strings me) { |
| 132 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfStrings == 0) |
| 133 | return; |
| 134 | autostring32cxx . . $ autostring32# buffercxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (Strings_maximumLengthcxx . . $ Strings_maximumLength(775293070f0391ef). (me)) * 3); |
| 135 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStrings; i ++) { |
| 136 | const conststring32cxx . . $ conststring32# string = mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 137 | const char32cxx . . $ char32# *p = & string [0]; |
| 138 | while (*p) { |
| 139 | if (*p > 126) { // backslashes are not converted, i.e. genericize^2 == genericize |
| 140 | Longchar_genericizecxx . . $ Longchar_genericize(329c6326171b971e). (string, buffer.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 141 | mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (buffer.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 142 | break; |
| 143 | } |
| 144 | p ++; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Strings_nativizecxx . . $ Strings_nativize(1f79c244d2d6c1ef). (Strings me) { |
| 150 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfStrings == 0) |
| 151 | return; |
| 152 | autostring32cxx . . $ autostring32# buffercxx . . $ _autostring#_autostring(f153f132b2bc41c2). (Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (Strings_maximumLengthcxx . . $ Strings_maximumLength(775293070f0391ef). (me))); |
| 153 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStrings; i ++) { |
| 154 | Longchar_nativizecxx . . $ Longchar_nativize(35d477e2649d0789). (mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), buffer.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), false); |
| 155 | mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (buffer.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void Strings_sortcxx . . $ Strings_sort(1f79c244d2d6c1ef). (Strings me) { |
| 160 | sort_STRVEC_inoutcxx . . $ sort_STRVEC_inout(276ad51d55a4042f). (mycxx . . $ `melder/melder.h:44:9`! strings.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 161 | } |
| 162 | |
| 163 | void Strings_removecxx . . $ Strings_remove(51f4c61c065c624). (Strings me, integercxx . . $ integer# position) { |
| 164 | try { |
| 165 | mycxx . . $ `melder/melder.h:44:9`! checkStringNumber (position); |
| 166 | mycxx . . $ `melder/melder.h:44:9`! strings. removecxx . . $ _autostringautovector#remove(f1ee9d71b26a60c). (position); |
| 167 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 168 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 169 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": string ", position, U" not removed."); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void Strings_replacecxx . . $ Strings_replace(48d18350c34fd7ae). (Strings me, integercxx . . $ integer# stringNumber, conststring32cxx . . $ conststring32# text) { |
| 174 | try { |
| 175 | mycxx . . $ `melder/melder.h:44:9`! checkStringNumber (stringNumber); |
| 176 | if (Melder_equcxx . . $ `melder/melder_str32.h:124:9`! (mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).stringNumber].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), text)) |
| 177 | return; // nothing to change |
| 178 | /* |
| 179 | Create without change. |
| 180 | */ |
| 181 | autostring32cxx . . $ autostring32# newString = Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (text); |
| 182 | /* |
| 183 | Change without error. |
| 184 | */ |
| 185 | mycxx . . $ `melder/melder.h:44:9`! strings [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).stringNumber] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). newString. movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 186 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 187 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": string ", stringNumber, U" not replaced."); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void Strings_insertcxx . . $ Strings_insert(48d18350c34fd7ae). (Strings me, integercxx . . $ integer# position, conststring32cxx . . $ conststring32# text) { |
| 192 | try { |
| 193 | if (position == 0) |
| 194 | position = mycxx . . $ `melder/melder.h:44:9`! numberOfStrings + 1; |
| 195 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (position >= 1, |
| 196 | U"The element number should be at least 1, not ", position, U"."); |
| 197 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (position <= mycxx . . $ `melder/melder.h:44:9`! numberOfStrings + 1, |
| 198 | U"The element number should be at most the number of elements plus 1 (", mycxx . . $ `melder/melder.h:44:9`! numberOfStrings + 1, U"), not", position, U"."); |
| 199 | mycxx . . $ `melder/melder.h:44:9`! strings. insertcxx . . $ _autostringautovector#insert(cfe533a552fd45e9). (position, text); // size changes only on success |
| 200 | mycxx . . $ `melder/melder.h:44:9`! maintainInvariants (); |
| 201 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 202 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no string inserted at position ", position, U"."); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* End of file Strings.cpp */ |