| 1 | /* NUMstring.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012,2013,2015-2020 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 20121005 First version |
| 21 | */ |
| 22 | |
| 23 | #include "Interpreter.h" |
| 24 | #include "NUM2.h" |
| 25 | |
| 26 | char32 *strstr_regexp (conststring32 string, conststring32 search_regexp) { |
| 27 | char32 *charp = nullptr; |
| 28 | regexp *compiled_regexp = CompileRE_throwable (search_regexp, 0); |
| 29 | if (ExecRE (compiled_regexp, nullptr, string, nullptr, false, U'\0', U'\0', nullptr, nullptr)) |
| 30 | charp = compiled_regexp -> startp [0]; |
| 31 | free (compiled_regexp); |
| 32 | return charp; |
| 33 | } |
| 34 | |
| 35 | static autoSTRVEC string32vector_searchAndReplace_literal (constSTRVEC me, |
| 36 | conststring32 search, conststring32 replace, int maximumNumberOfReplaces, |
| 37 | integer *out_numberOfMatches, integer *out_numberOfStringMatches) |
| 38 | { |
| 39 | /* |
| 40 | Sanitize input. |
| 41 | */ |
| 42 | if (! search) |
| 43 | search = U""; |
| 44 | if (! replace) |
| 45 | replace = U""; |
| 46 | |
| 47 | autoSTRVEC result (me.size); |
| 48 | |
| 49 | integer nmatches_sub = 0, nmatches = 0, nstringmatches = 0; |
| 50 | for (integer i = 1; i <= me.size; i ++) { |
| 51 | result [i] = replace_STR (me [i], search, replace, maximumNumberOfReplaces, & nmatches_sub); |
| 52 | if (nmatches_sub > 0) { |
| 53 | nmatches += nmatches_sub; |
| 54 | nstringmatches ++; |
| 55 | } |
| 56 | } |
| 57 | if (out_numberOfMatches) |
| 58 | *out_numberOfMatches = nmatches; |
| 59 | if (out_numberOfStringMatches) |
| 60 | *out_numberOfStringMatches = nstringmatches; |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | static autoSTRVEC string32vector_searchAndReplace_regexp (constSTRVEC me, |
| 65 | conststring32 searchRE, conststring32 replaceRE, int maximumNumberOfReplaces, |
| 66 | integer *out_numberOfMatches, integer *out_numberOfStringMatches) |
| 67 | { |
| 68 | /* |
| 69 | Sanitize input. |
| 70 | */ |
| 71 | if (! searchRE) |
| 72 | searchRE = U""; |
| 73 | if (! replaceRE) |
| 74 | replaceRE = U""; |
| 75 | |
| 76 | integer nmatches_sub = 0; |
| 77 | |
| 78 | regexp *compiledRE = CompileRE_throwable (searchRE, 0); |
| 79 | |
| 80 | autoSTRVEC result (me.size); |
| 81 | |
| 82 | integer nmatches = 0, nstringmatches = 0; |
| 83 | for (integer i = 1; i <= me.size; i ++) { |
| 84 | result [i] = replace_regex_STR (me [i], compiledRE, replaceRE, maximumNumberOfReplaces, & nmatches_sub); |
| 85 | if (nmatches_sub > 0) { |
| 86 | nmatches += nmatches_sub; |
| 87 | nstringmatches ++; |
| 88 | } |
| 89 | } |
| 90 | if (out_numberOfMatches) |
| 91 | *out_numberOfMatches = nmatches; |
| 92 | if (out_numberOfStringMatches) |
| 93 | *out_numberOfStringMatches = nstringmatches; |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | autoSTRVEC string32vector_searchAndReplace (constSTRVEC me, |
| 98 | conststring32 search, conststring32 replace, integer maximumNumberOfReplaces, |
| 99 | integer *nmatches, integer *nstringmatches, bool use_regexp) |
| 100 | { |
| 101 | return use_regexp ? |
| 102 | string32vector_searchAndReplace_regexp (me, search, replace, maximumNumberOfReplaces, nmatches, nstringmatches) : |
| 103 | string32vector_searchAndReplace_literal (me, search, replace, maximumNumberOfReplaces, nmatches, nstringmatches); |
| 104 | } |
| 105 | |
| 106 | char32 * NUMnumber_as_stringWithDotReplacedByUnderscore (double time) { |
| 107 | static char32 string [100]; |
| 108 | conststring32 time_string = Melder_double (time); |
| 109 | const char32 *from = time_string; |
| 110 | char32 *to = & string [0]; |
| 111 | while (*from != U'\0') { |
| 112 | if (*from != U'.') |
| 113 | *to = *from; |
| 114 | else |
| 115 | *to = U'_'; |
| 116 | to ++; from ++; |
| 117 | } |
| 118 | *to = U'\0'; |
| 119 | return string; |
| 120 | } |
| 121 | |
| 122 | /* End of file NUMstring.cpp */ |