| 1 |
/* Strings_extensions.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2012,2015-2022 David Weenink, 2015-2021 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 2 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. See the GNU |
| 13 |
* 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 |
/* |
| 20 |
djmw 20011003 |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "Strings_extensions.h" |
| 24 |
#include "NUM2.h" |
| 25 |
|
| 26 |
autoStrings Strings_createFixedLength (integer numberOfStrings) { |
| 27 |
try { |
| 28 |
Melder_require (numberOfStrings >= 0, |
| 29 |
U"The number of strings should not be negative."); |
| 30 |
autoStrings me = Thing_new (Strings); |
| 31 |
my strings = autoSTRVEC (numberOfStrings); |
| 32 |
my numberOfStrings = numberOfStrings; |
| 33 |
return me; |
| 34 |
} catch (MelderError) { |
| 35 |
Melder_throw (U"Strings not created."); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
autoStrings Strings_createAsCharacters (conststring32 string) { |
| 40 |
try { |
| 41 |
autoStrings me = Thing_new (Strings); |
| 42 |
my numberOfStrings = Melder_length (string); |
| 43 |
my strings = autoSTRVEC (my numberOfStrings); |
| 44 |
for (integer i = 1; i <= my numberOfStrings; i ++) |
| 45 |
my strings [i] = Melder_dup (Melder_character (*string ++)); |
| 46 |
return me; |
| 47 |
} catch (MelderError) { |
| 48 |
Melder_throw (U"Strings from characters not created."); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
autoStrings Strings_createAsTokens (conststring32 token_string, conststring32 separator_string) { |
| 53 |
try { |
| 54 |
autoStrings me = Thing_new (Strings); |
| 55 |
/* |
| 56 |
1. make a copy |
| 57 |
2. replace all separators by 0 in the copy |
| 58 |
3. count the items in the copy |
| 59 |
4. copy the tokens from the copy to the Strings object |
| 60 |
|
| 61 |
The algorithm is not the most efficient one since the token string is processed 4 times. |
| 62 |
However the steps taken are easy to follow. |
| 63 |
*/ |
| 64 |
if (! token_string || token_string [0] == U'\0') |
| 65 |
return me; |
| 66 |
|
| 67 |
const conststring32 separators = ( separator_string && separator_string [0] != U'\0' ? separator_string : U" " ); |
| 68 |
autostring32 copy = Melder_dup (token_string); |
| 69 |
mutablestring32 tokens = copy.get(); |
| 70 |
char32 *index = & tokens [0]; |
| 71 |
integer numberOfTokens = 0; |
| 72 |
for (const char32 *indexs = & token_string [0]; *indexs != U'\0'; indexs ++, index ++) { |
| 73 |
for (const char32 *s = & separators [0]; *s != U'\0'; s ++) { |
| 74 |
if (*index == *s) { |
| 75 |
*index = U'\0'; |
| 76 |
if (index > tokens && *(index - 1) != U'\0') |
| 77 |
numberOfTokens ++; |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
if (*(index - 1) != U'\0') // if token_string ends with a non-separator |
| 83 |
numberOfTokens ++; |
| 84 |
my numberOfStrings = numberOfTokens; |
| 85 |
my strings = autoSTRVEC (my numberOfStrings); |
| 86 |
numberOfTokens = 0; |
| 87 |
char32 *start = tokens; |
| 88 |
index = & tokens [0]; |
| 89 |
for (const char32 *indexs = & token_string [0]; *indexs != U'\0'; indexs ++, index ++) { |
| 90 |
if (*index == U'\0' && index > tokens && *(index - 1) != U'\0') |
| 91 |
my strings [++ numberOfTokens] = Melder_dup (start); |
| 92 |
if (*index != U'\0' && index > tokens && *(index - 1) == U'\0') |
| 93 |
start = index; |
| 94 |
} |
| 95 |
if (*(index - 1) != U'\0') |
| 96 |
my strings [++ numberOfTokens] = Melder_dup (start); |
| 97 |
return me; |
| 98 |
} catch (MelderError) { |
| 99 |
Melder_throw (U"Strings as tokens not created."); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
autoStrings Strings_append (OrderedOf<structStrings>* me) { |
| 104 |
try { |
| 105 |
integer index = 1, numberOfStrings = 0; |
| 106 |
for (integer i = 1; i <= my size; i ++) { |
| 107 |
const Strings s = my at [i]; |
| 108 |
numberOfStrings += s -> numberOfStrings; |
| 109 |
} |
| 110 |
autoStrings thee = Strings_createFixedLength (numberOfStrings); |
| 111 |
for (integer i = 1; i <= my size; i ++) { |
| 112 |
const Strings s = my at [i]; |
| 113 |
for (integer j = 1; j <= s -> numberOfStrings; j ++, index ++) |
| 114 |
thy strings [index] = Melder_dup (s -> strings [j].get()); |
| 115 |
} |
| 116 |
return thee; |
| 117 |
} catch (MelderError) { |
| 118 |
Melder_throw (me, U": not appended."); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
autoStrings Strings_change (Strings me, conststring32 search, conststring32 replace, int maximumNumberOfReplaces, integer *nmatches, integer *nstringmatches, bool use_regexp) { |
| 123 |
try { |
| 124 |
autoStrings thee = Thing_new (Strings); |
| 125 |
autoSTRVEC strings = string32vector_searchAndReplace (my strings.get(), |
| 126 |
search, replace, maximumNumberOfReplaces, nmatches, nstringmatches, use_regexp); |
| 127 |
thy numberOfStrings = my numberOfStrings; |
| 128 |
thy strings = std::move (strings); |
| 129 |
return thee; |
| 130 |
} catch (MelderError) { |
| 131 |
Melder_throw (me, U": not changed."); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
autoStrings Strings_createFromSTRVEC (constSTRVEC const& strings) { |
| 136 |
try { |
| 137 |
autoStrings thee = Strings_createFixedLength (strings.size); |
| 138 |
for (integer i = 1; i <= strings.size; i ++) |
| 139 |
thy strings [i] = Melder_dup (strings [i]); |
| 140 |
return thee; |
| 141 |
} catch (MelderError) { |
| 142 |
Melder_throw (U"Strings not created."); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
autoStrings Strings_extractPart (Strings me, integer from, integer to) { |
| 147 |
try { |
| 148 |
Melder_require (from > 0 && from <= to && to <= my numberOfStrings, |
| 149 |
U"Strings_extractPart: begin and end should be in interval [1, ", my numberOfStrings, U"]."); |
| 150 |
return Strings_createFromSTRVEC (my strings.part (from, to)); |
| 151 |
} catch (MelderError) { |
| 152 |
Melder_throw (me, U": no part extracted."); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
autoPermutation Strings_to_Permutation (Strings me, kStrings_sorting sorting) { |
| 157 |
try { |
| 158 |
return Permutation_createAsSortingIndex (my strings.get(), sorting); |
| 159 |
} catch (MelderError) { |
| 160 |
Melder_throw (me, U": no Permutation created."); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
autoStrings Strings_Permutation_permuteStrings (Strings me, Permutation thee) { |
| 165 |
try { |
| 166 |
Melder_require (my numberOfStrings == thy numberOfElements, |
| 167 |
U"Strings_Permutation_permuteStrings: The number of strings and the number of elements in the Permutation should be equal."); |
| 168 |
autoStrings him = Strings_createFixedLength (my numberOfStrings); |
| 169 |
for (integer i = 1; i <= thy numberOfElements; i ++) { |
| 170 |
const integer index = thy p [i]; |
| 171 |
his strings [i] = Melder_dup (my strings [index].get()); |
| 172 |
} |
| 173 |
return him; |
| 174 |
} catch (MelderError) { |
| 175 |
Melder_throw (me, U": no permuted Strings created."); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
autoStringsIndex Stringses_to_StringsIndex (Strings me, Strings classes) { |
| 180 |
try { |
| 181 |
autoStringsIndex tmp = Strings_to_StringsIndex (classes, kStrings_sorting::ALPHABETICAL); |
| 182 |
const integer numberOfClasses = tmp -> classes->size; |
| 183 |
|
| 184 |
autoStringsIndex him = StringsIndex_create (my numberOfStrings); |
| 185 |
for (integer i = 1; i <= numberOfClasses; i ++) { |
| 186 |
const SimpleString t = (SimpleString) tmp -> classes->at [i]; // FIXME cast |
| 187 |
autoSimpleString t2 = Data_copy (t); |
| 188 |
his classes -> addItem_move (t2.move()); |
| 189 |
} |
| 190 |
for (integer j = 1; j <= my numberOfStrings; j ++) { |
| 191 |
integer index = 0; |
| 192 |
const conststring32 stringsj = my strings [j].get(); |
| 193 |
for (integer i = 1; i <= numberOfClasses; i ++) { |
| 194 |
SimpleString ss = (SimpleString) his classes->at [i]; // FIXME cast |
| 195 |
if (Melder_equ (stringsj, ss -> string.get())) { |
| 196 |
index = i; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
his classIndex [j] = index; |
| 201 |
} |
| 202 |
return him; |
| 203 |
} catch (MelderError) { |
| 204 |
Melder_throw (me, U": no StringsIndex created."); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
autoStrings StringsIndex_to_Strings (StringsIndex me) { |
| 210 |
try { |
| 211 |
autoStrings thee = Strings_createFixedLength (my numberOfItems); |
| 212 |
for (integer i = 1; i <= my numberOfItems; i ++) { |
| 213 |
const SimpleString s = (SimpleString) my classes->at [my classIndex [i]]; // FIXME cast, FIXME classIndex |
| 214 |
thy strings [i] = Melder_dup (s -> string.get()); |
| 215 |
} |
| 216 |
return thee; |
| 217 |
} catch (MelderError) { |
| 218 |
Melder_throw (me, U": no Strings created."); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
autoStringsIndex Table_to_StringsIndex_column (Table me, integer column, kStrings_sorting sorting) { |
| 223 |
try { |
| 224 |
Table_checkSpecifiedColumnNumberWithinRange (me, column); |
| 225 |
const integer numberOfRows = my rows.size; |
| 226 |
Table_numericize_a (me, column); |
| 227 |
autoSTRVEC groupLabels (numberOfRows); |
| 228 |
for (integer irow = 1; irow <= numberOfRows; irow ++) |
| 229 |
groupLabels [irow] = Melder_dup (my rows.at [irow] -> cells [column]. string.get()); // TODO: no dup |
| 230 |
|
| 231 |
autoStringsIndex him = StringsIndex_createFromSTRVEC (groupLabels.get(), sorting); |
| 232 |
return him; |
| 233 |
} catch (MelderError) { |
| 234 |
Melder_throw (me, U"No StringsIndex created from column ", column, U"."); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/* End of file Strings_extensions.cpp */ |