| 1 | /* melder_search.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2018,2020-2022,2025 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 "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | /********** NUMBER AND STRING COMPARISONS **********/ |
| 22 | |
| 23 | bool Melder_numberMatchesCriterioncxx . . $ Melder_numberMatchesCriterion(42bb268e9f136965). (double value, kMelder_numbercxx . . $ kMelder_number# which, double criterion) { |
| 24 | return |
| 25 | (which == kMelder_numbercxx . . $ kMelder_number#::EQUAL_TOcxx . . $ kMelder_number#EQUAL_TO. && value == criterion) || |
| 26 | (which == kMelder_numbercxx . . $ kMelder_number#::NOT_EQUAL_TOcxx . . $ kMelder_number#NOT_EQUAL_TO. && value != criterion) || |
| 27 | (which == kMelder_numbercxx . . $ kMelder_number#::LESS_THANcxx . . $ kMelder_number#LESS_THAN. && value < criterion) || |
| 28 | (which == kMelder_numbercxx . . $ kMelder_number#::LESS_THAN_OR_EQUAL_TOcxx . . $ kMelder_number#LESS_THAN_OR_EQUAL_TO. && value <= criterion) || |
| 29 | (which == kMelder_numbercxx . . $ kMelder_number#::GREATER_THANcxx . . $ kMelder_number#GREATER_THAN. && value > criterion) || |
| 30 | (which == kMelder_numbercxx . . $ kMelder_number#::GREATER_THAN_OR_EQUAL_TOcxx . . $ kMelder_number#GREATER_THAN_OR_EQUAL_TO. && value >= criterion); |
| 31 | } |
| 32 | |
| 33 | inline static char32cxx . . $ char32# * str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# find, |
| 34 | bool ink, bool caseSensitive, bool startFree, bool endFree) noexcept |
| 35 | { |
| 36 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (find); |
| 37 | if (length == 0) |
| 38 | return (char32cxx . . $ char32# *) string; |
| 39 | conststring32cxx . . $ conststring32# movingString = string; |
| 40 | do { |
| 41 | conststring32cxx . . $ conststring32# movingFind = find; |
| 42 | char32cxx . . $ char32# firstCharacter = ( caseSensitive ? * movingFind ++ : Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (* movingFind ++) ); // optimization |
| 43 | do { |
| 44 | char32cxx . . $ char32# kar; |
| 45 | do { |
| 46 | kar = ( caseSensitive ? * movingString ++ : Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (* movingString ++) ); |
| 47 | if (kar == U'\0') |
| 48 | return nullptr; |
| 49 | } while (kar != firstCharacter); |
| 50 | } while (caseSensitive ? str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (movingString, movingFind, length - 1) : str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (movingString, movingFind, length - 1)); |
| 51 | /* |
| 52 | We found a match. |
| 53 | */ |
| 54 | movingString --; |
| 55 | if ((startFree || movingString == string || ( ink ? Melder_isHorizontalOrVerticalSpacecxx . . $ Melder_isHorizontalOrVerticalSpace(b8d0204ce8416907). (movingString [-1]) : ! Melder_isWordCharactercxx . . $ Melder_isWordCharacter(b8d0204ce8416907). (movingString [-1]) )) && |
| 56 | (endFree || movingString [length] == U'\0' || (ink ? Melder_isHorizontalOrVerticalSpacecxx . . $ Melder_isHorizontalOrVerticalSpace(b8d0204ce8416907). (movingString [length]) : ! Melder_isWordCharactercxx . . $ Melder_isWordCharacter(b8d0204ce8416907). (movingString [length]) ))) |
| 57 | { |
| 58 | return (char32cxx . . $ char32# *) movingString; |
| 59 | } else { |
| 60 | movingString ++; |
| 61 | } |
| 62 | } while (true); |
| 63 | return nullptr; // can never occur |
| 64 | } |
| 65 | |
| 66 | bool Melder_stringMatchesCriterioncxx . . $ Melder_stringMatchesCriterion(3e677ada61cce709). (conststring32cxx . . $ conststring32# value, kMelder_stringcxx . . $ kMelder_string# which, conststring32cxx . . $ conststring32# criterion, bool caseSensitive) { |
| 67 | if (! value) |
| 68 | value = U""; // regard null strings as empty strings, as is usual in Praat |
| 69 | if (! criterion) |
| 70 | criterion = U""; // regard null strings as empty strings, as is usual in Praat |
| 71 | switch (which) |
| 72 | { |
| 73 | case kMelder_stringcxx . . $ kMelder_string#::UNDEFINEDcxx . . $ kMelder_string#UNDEFINED.: |
| 74 | { |
| 75 | Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Melder_stringMatchesCriterion: unknown criterion."); |
| 76 | } |
| 77 | case kMelder_stringcxx . . $ kMelder_string#::EQUAL_TOcxx . . $ kMelder_string#EQUAL_TO.: |
| 78 | case kMelder_stringcxx . . $ kMelder_string#::NOT_EQUAL_TOcxx . . $ kMelder_string#NOT_EQUAL_TO.: |
| 79 | { |
| 80 | const bool doesMatch = str32equ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:128:9`! (value, criterion, caseSensitive); |
| 81 | return which == kMelder_stringcxx . . $ kMelder_string#::EQUAL_TOcxx . . $ kMelder_string#EQUAL_TO. ? doesMatch : ! doesMatch; |
| 82 | } |
| 83 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINScxx . . $ kMelder_string#CONTAINS.: |
| 84 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAINcxx . . $ kMelder_string#DOES_NOT_CONTAIN.: |
| 85 | { |
| 86 | const bool doesMatch = !! str32str_optionallyCaseSensitivecxx . . $ str32str_optionallyCaseSensitive(7e4d3d07cdd0a044). (value, criterion, caseSensitive); |
| 87 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINScxx . . $ kMelder_string#CONTAINS. ? doesMatch : ! doesMatch; |
| 88 | } |
| 89 | case kMelder_stringcxx . . $ kMelder_string#::STARTS_WITHcxx . . $ kMelder_string#STARTS_WITH.: |
| 90 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_START_WITHcxx . . $ kMelder_string#DOES_NOT_START_WITH.: |
| 91 | { |
| 92 | const bool doesMatch = str32nequ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:129:9`! (value, criterion, Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (criterion), caseSensitive); |
| 93 | return which == kMelder_stringcxx . . $ kMelder_string#::STARTS_WITHcxx . . $ kMelder_string#STARTS_WITH. ? doesMatch : ! doesMatch; |
| 94 | } |
| 95 | case kMelder_stringcxx . . $ kMelder_string#::ENDS_WITHcxx . . $ kMelder_string#ENDS_WITH.: |
| 96 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_END_WITHcxx . . $ kMelder_string#DOES_NOT_END_WITH.: |
| 97 | { |
| 98 | const integercxx . . $ integer# criterionLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (criterion), valueLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (value); |
| 99 | const bool doesMatch = ( criterionLength <= valueLength && |
| 100 | str32equ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:128:9`! (value + valueLength - criterionLength, criterion, caseSensitive) ); |
| 101 | return which == kMelder_stringcxx . . $ kMelder_string#::ENDS_WITHcxx . . $ kMelder_string#ENDS_WITH. ? doesMatch : ! doesMatch; |
| 102 | } |
| 103 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORDcxx . . $ kMelder_string#CONTAINS_WORD.: |
| 104 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_WORDcxx . . $ kMelder_string#DOES_NOT_CONTAIN_WORD.: |
| 105 | { |
| 106 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, false, caseSensitive, false, false); |
| 107 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORDcxx . . $ kMelder_string#CONTAINS_WORD. ? doesMatch : ! doesMatch; |
| 108 | } |
| 109 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORD_STARTING_WITHcxx . . $ kMelder_string#CONTAINS_WORD_STARTING_WITH.: |
| 110 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_WORD_STARTING_WITHcxx . . $ kMelder_string#DOES_NOT_CONTAIN_WORD_STARTING_WITH.: |
| 111 | { |
| 112 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, false, caseSensitive, false, true); |
| 113 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORD_STARTING_WITHcxx . . $ kMelder_string#CONTAINS_WORD_STARTING_WITH. ? doesMatch : ! doesMatch; |
| 114 | } |
| 115 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORD_ENDING_WITHcxx . . $ kMelder_string#CONTAINS_WORD_ENDING_WITH.: |
| 116 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_WORD_ENDING_WITHcxx . . $ kMelder_string#DOES_NOT_CONTAIN_WORD_ENDING_WITH.: |
| 117 | { |
| 118 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, false, caseSensitive, true, false); |
| 119 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_WORD_ENDING_WITHcxx . . $ kMelder_string#CONTAINS_WORD_ENDING_WITH. ? doesMatch : ! doesMatch; |
| 120 | } |
| 121 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INKcxx . . $ kMelder_string#CONTAINS_INK.: |
| 122 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_INKcxx . . $ kMelder_string#DOES_NOT_CONTAIN_INK.: |
| 123 | { |
| 124 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, true, caseSensitive, false, false); |
| 125 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INKcxx . . $ kMelder_string#CONTAINS_INK. ? doesMatch : ! doesMatch; |
| 126 | } |
| 127 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INK_STARTING_WITHcxx . . $ kMelder_string#CONTAINS_INK_STARTING_WITH.: |
| 128 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_INK_STARTING_WITHcxx . . $ kMelder_string#DOES_NOT_CONTAIN_INK_STARTING_WITH.: |
| 129 | { |
| 130 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, true, caseSensitive, false, true); |
| 131 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INK_STARTING_WITHcxx . . $ kMelder_string#CONTAINS_INK_STARTING_WITH. ? doesMatch : ! doesMatch; |
| 132 | } |
| 133 | case kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INK_ENDING_WITHcxx . . $ kMelder_string#CONTAINS_INK_ENDING_WITH.: |
| 134 | case kMelder_stringcxx . . $ kMelder_string#::DOES_NOT_CONTAIN_INK_ENDING_WITHcxx . . $ kMelder_string#DOES_NOT_CONTAIN_INK_ENDING_WITH.: |
| 135 | { |
| 136 | const bool doesMatch = !! str32str_word_optionallyCaseSensitivecxx . . $ str32str_word_optionallyCaseSensitive(be1379449b62a5ec). (value, criterion, true, caseSensitive, true, false); |
| 137 | return which == kMelder_stringcxx . . $ kMelder_string#::CONTAINS_INK_ENDING_WITHcxx . . $ kMelder_string#CONTAINS_INK_ENDING_WITH. ? doesMatch : ! doesMatch; |
| 138 | } |
| 139 | case kMelder_stringcxx . . $ kMelder_string#::MATCH_REGEXPcxx . . $ kMelder_string#MATCH_REGEXP.: |
| 140 | { |
| 141 | char32cxx . . $ char32# *place = nullptr; |
| 142 | regexpcxx . . $ regexp# *compiled_regexp = CompileRE_throwablecxx . . $ CompileRE_throwable(883f0876ef3038d2). (criterion, ! REDFLT_CASE_INSENSITIVEcxx . . $ REDFLT_CASE_INSENSITIVE.); |
| 143 | if (ExecREcxx . . $ ExecRE(adb1c5d556f61044). (compiled_regexp, nullptr, value, nullptr, 0, U'\0', U'\0', nullptr, nullptr)) |
| 144 | place = compiled_regexp -> startpcxx . . $ regexp#startp. [0]; |
| 145 | freecxx . . $ free(bd21765a0afc8e3c). (compiled_regexp); |
| 146 | return !! place; |
| 147 | } |
| 148 | } |
| 149 | //return false; // should not occur |
| 150 | } |
| 151 | |
| 152 | /* End of file melder_search.cpp */ |