| 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"cxx . . $ `<file>/sys/Interpreter.h`/ |
| 24 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 25 | |
| 26 | char32cxx . . $ char32# *strstr_regexpcxx . . $ strstr_regexp(1231f2366db14449). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# search_regexp) { |
| 27 | char32cxx . . $ char32# *charp = nullptr; |
| 28 | regexpcxx . . $ regexp# *compiled_regexp = CompileRE_throwablecxx . . $ CompileRE_throwable(883f0876ef3038d2). (search_regexp, 0); |
| 29 | if (ExecREcxx . . $ ExecRE(adb1c5d556f61044). (compiled_regexp, nullptr, string, nullptr, false, U'\0', U'\0', nullptr, nullptr)) |
| 30 | charp = compiled_regexp -> startpcxx . . $ regexp#startp. [0]; |
| 31 | freecxx . . $ free(bd21765a0afc8e3c). (compiled_regexp); |
| 32 | return charp; |
| 33 | } |
| 34 | |
| 35 | static autoSTRVECcxx . . $ autoSTRVEC# string32vector_searchAndReplace_literalcxx . . $ string32vector_searchAndReplace_literal(7b4fa4c9362a14e3). (constSTRVECcxx . . $ constSTRVEC# me, |
| 36 | conststring32cxx . . $ conststring32# search, conststring32cxx . . $ conststring32# replace, int maximumNumberOfReplaces, |
| 37 | integercxx . . $ integer# *out_numberOfMatches, integercxx . . $ integer# *out_numberOfStringMatches) |
| 38 | { |
| 39 | /* |
| 40 | Sanitize input. |
| 41 | */ |
| 42 | if (! search) |
| 43 | search = U""; |
| 44 | if (! replace) |
| 45 | replace = U""; |
| 46 | |
| 47 | autoSTRVECcxx . . $ autoSTRVEC# resultcxx . . $ _autostringautovector#_autostringautovector(1624ee8275b10bda). (me.sizecxx . . $ _conststringvector#size.); |
| 48 | |
| 49 | integercxx . . $ integer# nmatches_sub = 0, nmatches = 0, nstringmatches = 0; |
| 50 | for (integercxx . . $ integer# i = 1; i <= me.sizecxx . . $ _conststringvector#size.; i ++) { |
| 51 | result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). replace_STRcxx . . $ replace_STR(58ee6b7335d69803). (me [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).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 resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).; |
| 62 | } |
| 63 | |
| 64 | static autoSTRVECcxx . . $ autoSTRVEC# string32vector_searchAndReplace_regexpcxx . . $ string32vector_searchAndReplace_regexp(7b4fa4c9362a14e3). (constSTRVECcxx . . $ constSTRVEC# me, |
| 65 | conststring32cxx . . $ conststring32# searchRE, conststring32cxx . . $ conststring32# replaceRE, int maximumNumberOfReplaces, |
| 66 | integercxx . . $ integer# *out_numberOfMatches, integercxx . . $ integer# *out_numberOfStringMatches) |
| 67 | { |
| 68 | /* |
| 69 | Sanitize input. |
| 70 | */ |
| 71 | if (! searchRE) |
| 72 | searchRE = U""; |
| 73 | if (! replaceRE) |
| 74 | replaceRE = U""; |
| 75 | |
| 76 | integercxx . . $ integer# nmatches_sub = 0; |
| 77 | |
| 78 | regexpcxx . . $ regexp# *compiledRE = CompileRE_throwablecxx . . $ CompileRE_throwable(883f0876ef3038d2). (searchRE, 0); |
| 79 | |
| 80 | autoSTRVECcxx . . $ autoSTRVEC# resultcxx . . $ _autostringautovector#_autostringautovector(1624ee8275b10bda). (me.sizecxx . . $ _conststringvector#size.); |
| 81 | |
| 82 | integercxx . . $ integer# nmatches = 0, nstringmatches = 0; |
| 83 | for (integercxx . . $ integer# i = 1; i <= me.sizecxx . . $ _conststringvector#size.; i ++) { |
| 84 | result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). replace_regex_STRcxx . . $ replace_regex_STR(8c698ee9a707cbfd). (me [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).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 resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).; |
| 95 | } |
| 96 | |
| 97 | autoSTRVECcxx . . $ autoSTRVEC# string32vector_searchAndReplacecxx . . $ string32vector_searchAndReplace(97193444c8efba6). (constSTRVECcxx . . $ constSTRVEC# me, |
| 98 | conststring32cxx . . $ conststring32# search, conststring32cxx . . $ conststring32# replace, integercxx . . $ integer# maximumNumberOfReplaces, |
| 99 | integercxx . . $ integer# *nmatches, integercxx . . $ integer# *nstringmatches, bool use_regexp) |
| 100 | { |
| 101 | return use_regexp ? |
| 102 | string32vector_searchAndReplace_regexpcxx . . $ string32vector_searchAndReplace_regexp(7b4fa4c9362a14e3). (me, search, replace, maximumNumberOfReplaces, nmatches, nstringmatches) : |
| 103 | string32vector_searchAndReplace_literalcxx . . $ string32vector_searchAndReplace_literal(7b4fa4c9362a14e3). (me, search, replace, maximumNumberOfReplaces, nmatches, nstringmatches); |
| 104 | } |
| 105 | |
| 106 | char32cxx . . $ char32# * NUMnumber_as_stringWithDotReplacedByUnderscorecxx . . $ NUMnumber_as_stringWithDotReplacedByUnderscore(94c0f4e40d724a86). (double time) { |
| 107 | static char32cxx . . $ char32# string [100]; |
| 108 | conststring32cxx . . $ conststring32# time_string = Melder_doublecxx . . $ Melder_double(f05d861d9dbec9b2). (time); |
| 109 | const char32cxx . . $ char32# *from = time_string; |
| 110 | char32cxx . . $ 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 */ |