| 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" |
| 20 |
#include "../kar/longchar.h" |
| 21 |
|
| 22 |
#include "oo_DESTROY.h" |
| 23 |
#include "Strings_def.h" |
| 24 |
#include "oo_COPY.h" |
| 25 |
#include "Strings_def.h" |
| 26 |
#include "oo_EQUAL.h" |
| 27 |
#include "Strings_def.h" |
| 28 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 |
#include "Strings_def.h" |
| 30 |
#include "oo_WRITE_TEXT.h" |
| 31 |
#include "Strings_def.h" |
| 32 |
#include "oo_READ_TEXT.h" |
| 33 |
#include "Strings_def.h" |
| 34 |
#include "oo_WRITE_BINARY.h" |
| 35 |
#include "Strings_def.h" |
| 36 |
#include "oo_READ_BINARY.h" |
| 37 |
#include "Strings_def.h" |
| 38 |
#include "oo_DESCRIPTION.h" |
| 39 |
#include "Strings_def.h" |
| 40 |
|
| 41 |
Thing_implement (Strings, Daata, 0); |
| 42 |
|
| 43 |
static double Strings_minimumLength (Strings me) { |
| 44 |
return NUMminimumLength (my strings.get()); |
| 45 |
} |
| 46 |
static double Strings_maximumLength (Strings me) { |
| 47 |
return NUMmaximumLength (my strings.get()); |
| 48 |
} |
| 49 |
static double Strings_totalLength (Strings me) { |
| 50 |
return NUMtotalLength (my strings.get()); |
| 51 |
} |
| 52 |
|
| 53 |
void structStrings :: v1_info () { |
| 54 |
structDaata :: v1_info (); |
| 55 |
MelderInfo_writeLine (U"Number of strings: ", our numberOfStrings); |
| 56 |
MelderInfo_writeLine (U"Total length: ", Strings_totalLength (this), U" characters"); |
| 57 |
MelderInfo_writeLine (U"Shortest string: ", Strings_minimumLength (this), U" characters"); |
| 58 |
MelderInfo_writeLine (U"Longest string: ", Strings_maximumLength (this), U" characters"); |
| 59 |
} |
| 60 |
|
| 61 |
conststring32 structStrings :: v_getVectorStr (const integer icol) const { |
| 62 |
if (icol < 1 || icol > our numberOfStrings) |
| 63 |
return U""; |
| 64 |
char32 *stringValue = strings [icol].get(); |
| 65 |
return stringValue ? stringValue : U""; |
| 66 |
} |
| 67 |
|
| 68 |
autoStrings Strings_createFromTexts (constSTRVEC const& texts) { |
| 69 |
try { |
| 70 |
autoStrings me = Thing_new (Strings); |
| 71 |
my strings = copy_STRVEC (texts); |
| 72 |
my maintainInvariants (); |
| 73 |
return me; |
| 74 |
} catch (MelderError) { |
| 75 |
Melder_throw (U"Strings object not created from texts."); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
autoStrings Strings_createAsFileList (conststring32 path /* cattable */) { |
| 80 |
try { |
| 81 |
autoStrings me = Thing_new (Strings); |
| 82 |
my strings = fileNames_STRVEC (path); |
| 83 |
my maintainInvariants (); |
| 84 |
return me; |
| 85 |
} catch (MelderError) { |
| 86 |
Melder_throw (U"Strings object not created as file list."); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
autoStrings Strings_createAsFolderList (conststring32 path /* cattable */) { |
| 91 |
try { |
| 92 |
autoStrings me = Thing_new (Strings); |
| 93 |
my strings = folderNames_STRVEC (path); |
| 94 |
my maintainInvariants (); |
| 95 |
return me; |
| 96 |
} catch (MelderError) { |
| 97 |
Melder_throw (U"Strings object not created as folder list."); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
autoStrings Strings_readFromRawTextFile (MelderFile file) { |
| 102 |
try { |
| 103 |
autoStrings me = Thing_new (Strings); |
| 104 |
my strings = readLinesFromFile_STRVEC (file); |
| 105 |
my maintainInvariants (); |
| 106 |
return me; |
| 107 |
} catch (MelderError) { |
| 108 |
Melder_throw (U"Strings not read from raw text file ", file, U"."); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void Strings_writeToRawTextFile (Strings me, MelderFile file) { |
| 113 |
try { |
| 114 |
Thing_assertInvariants (me); |
| 115 |
autoMelderString buffer; |
| 116 |
for (integer i = 1; i <= my numberOfStrings; i ++) |
| 117 |
MelderString_append (& buffer, my strings [i].get(), U"\n"); |
| 118 |
MelderFile_writeText_e (file, buffer.string, Melder_getOutputEncoding ()); |
| 119 |
} catch (MelderError) { |
| 120 |
Melder_throw (U"Strings not written to raw text file ", file, U"."); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void Strings_randomize (Strings me) { |
| 125 |
for (integer i = 1; i < my numberOfStrings; i ++) { |
| 126 |
integer other = NUMrandomInteger (i, my numberOfStrings); |
| 127 |
std::swap (my strings [other], my strings [i]); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void Strings_genericize (Strings me) { |
| 132 |
if (my numberOfStrings == 0) |
| 133 |
return; |
| 134 |
autostring32 buffer (Melder_iround (Strings_maximumLength (me)) * 3); |
| 135 |
for (integer i = 1; i <= my numberOfStrings; i ++) { |
| 136 |
const conststring32 string = my strings [i].get(); |
| 137 |
const char32 *p = & string [0]; |
| 138 |
while (*p) { |
| 139 |
if (*p > 126) { // backslashes are not converted, i.e. genericize^2 == genericize |
| 140 |
Longchar_genericize (string, buffer.get()); |
| 141 |
my strings [i] = Melder_dup (buffer.get()); |
| 142 |
break; |
| 143 |
} |
| 144 |
p ++; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void Strings_nativize (Strings me) { |
| 150 |
if (my numberOfStrings == 0) |
| 151 |
return; |
| 152 |
autostring32 buffer (Melder_iround (Strings_maximumLength (me))); |
| 153 |
for (integer i = 1; i <= my numberOfStrings; i ++) { |
| 154 |
Longchar_nativize (my strings [i].get(), buffer.get(), false); |
| 155 |
my strings [i] = Melder_dup (buffer.get()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void Strings_sort (Strings me) { |
| 160 |
sort_STRVEC_inout (my strings.get()); |
| 161 |
} |
| 162 |
|
| 163 |
void Strings_remove (Strings me, integer position) { |
| 164 |
try { |
| 165 |
my checkStringNumber (position); |
| 166 |
my strings. remove (position); |
| 167 |
my maintainInvariants (); |
| 168 |
} catch (MelderError) { |
| 169 |
Melder_throw (me, U": string ", position, U" not removed."); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void Strings_replace (Strings me, integer stringNumber, conststring32 text) { |
| 174 |
try { |
| 175 |
my checkStringNumber (stringNumber); |
| 176 |
if (Melder_equ (my strings [stringNumber].get(), text)) |
| 177 |
return; // nothing to change |
| 178 |
/* |
| 179 |
Create without change. |
| 180 |
*/ |
| 181 |
autostring32 newString = Melder_dup (text); |
| 182 |
/* |
| 183 |
Change without error. |
| 184 |
*/ |
| 185 |
my strings [stringNumber] = newString. move(); |
| 186 |
} catch (MelderError) { |
| 187 |
Melder_throw (me, U": string ", stringNumber, U" not replaced."); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void Strings_insert (Strings me, integer position, conststring32 text) { |
| 192 |
try { |
| 193 |
if (position == 0) |
| 194 |
position = my numberOfStrings + 1; |
| 195 |
Melder_require (position >= 1, |
| 196 |
U"The element number should be at least 1, not ", position, U"."); |
| 197 |
Melder_require (position <= my numberOfStrings + 1, |
| 198 |
U"The element number should be at most the number of elements plus 1 (", my numberOfStrings + 1, U"), not", position, U"."); |
| 199 |
my strings. insert (position, text); // size changes only on success |
| 200 |
my maintainInvariants (); |
| 201 |
} catch (MelderError) { |
| 202 |
Melder_throw (me, U": no string inserted at position ", position, U"."); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/* End of file Strings.cpp */ |