| 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" |
| 20 | |
| 21 | /********** NUMBER AND STRING COMPARISONS **********/ |
| 22 | |
| 23 | bool Melder_numberMatchesCriterion (double value, kMelder_number which, double criterion) { |
| 24 | return |
| 25 | (which == kMelder_number::EQUAL_TO && value == criterion) || |
| 26 | (which == kMelder_number::NOT_EQUAL_TO && value != criterion) || |
| 27 | (which == kMelder_number::LESS_THAN && value < criterion) || |
| 28 | (which == kMelder_number::LESS_THAN_OR_EQUAL_TO && value <= criterion) || |
| 29 | (which == kMelder_number::GREATER_THAN && value > criterion) || |
| 30 | (which == kMelder_number::GREATER_THAN_OR_EQUAL_TO && value >= criterion); |
| 31 | } |
| 32 | |
| 33 | inline static char32 * str32str_word_optionallyCaseSensitive (conststring32 string, conststring32 find, |
| 34 | bool ink, bool caseSensitive, bool startFree, bool endFree) noexcept |
| 35 | { |
| 36 | const integer length = Melder_length (find); |
| 37 | if (length == 0) |
| 38 | return (char32 *) string; |
| 39 | conststring32 movingString = string; |
| 40 | do { |
| 41 | conststring32 movingFind = find; |
| 42 | char32 firstCharacter = ( caseSensitive ? * movingFind ++ : Melder_toLowerCase (* movingFind ++) ); // optimization |
| 43 | do { |
| 44 | char32 kar; |
| 45 | do { |
| 46 | kar = ( caseSensitive ? * movingString ++ : Melder_toLowerCase (* movingString ++) ); |
| 47 | if (kar == U'\0') |
| 48 | return nullptr; |
| 49 | } while (kar != firstCharacter); |
| 50 | } while (caseSensitive ? str32ncmp (movingString, movingFind, length - 1) : str32ncmp_caseInsensitive (movingString, movingFind, length - 1)); |
| 51 | /* |
| 52 | We found a match. |
| 53 | */ |
| 54 | movingString --; |
| 55 | if ((startFree || movingString == string || ( ink ? Melder_isHorizontalOrVerticalSpace (movingString [-1]) : ! Melder_isWordCharacter (movingString [-1]) )) && |
| 56 | (endFree || movingString [length] == U'\0' || (ink ? Melder_isHorizontalOrVerticalSpace (movingString [length]) : ! Melder_isWordCharacter (movingString [length]) ))) |
| 57 | { |
| 58 | return (char32 *) movingString; |
| 59 | } else { |
| 60 | movingString ++; |
| 61 | } |
| 62 | } while (true); |
| 63 | return nullptr; // can never occur |
| 64 | } |
| 65 | |
| 66 | bool Melder_stringMatchesCriterion (conststring32 value, kMelder_string which, 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_string::UNDEFINED: |
| 74 | { |
| 75 | Melder_crash (U"Melder_stringMatchesCriterion: unknown criterion."); |
| 76 | } |
| 77 | case kMelder_string::EQUAL_TO: |
| 78 | case kMelder_string::NOT_EQUAL_TO: |
| 79 | { |
| 80 | const bool doesMatch = str32equ_optionallyCaseSensitive (value, criterion, caseSensitive); |
| 81 | return which == kMelder_string::EQUAL_TO ? doesMatch : ! doesMatch; |
| 82 | } |
| 83 | case kMelder_string::CONTAINS: |
| 84 | case kMelder_string::DOES_NOT_CONTAIN: |
| 85 | { |
| 86 | const bool doesMatch = !! str32str_optionallyCaseSensitive (value, criterion, caseSensitive); |
| 87 | return which == kMelder_string::CONTAINS ? doesMatch : ! doesMatch; |
| 88 | } |
| 89 | case kMelder_string::STARTS_WITH: |
| 90 | case kMelder_string::DOES_NOT_START_WITH: |
| 91 | { |
| 92 | const bool doesMatch = str32nequ_optionallyCaseSensitive (value, criterion, Melder_length (criterion), caseSensitive); |
| 93 | return which == kMelder_string::STARTS_WITH ? doesMatch : ! doesMatch; |
| 94 | } |
| 95 | case kMelder_string::ENDS_WITH: |
| 96 | case kMelder_string::DOES_NOT_END_WITH: |
| 97 | { |
| 98 | const integer criterionLength = Melder_length (criterion), valueLength = Melder_length (value); |
| 99 | const bool doesMatch = ( criterionLength <= valueLength && |
| 100 | str32equ_optionallyCaseSensitive (value + valueLength - criterionLength, criterion, caseSensitive) ); |
| 101 | return which == kMelder_string::ENDS_WITH ? doesMatch : ! doesMatch; |
| 102 | } |
| 103 | case kMelder_string::CONTAINS_WORD: |
| 104 | case kMelder_string::DOES_NOT_CONTAIN_WORD: |
| 105 | { |
| 106 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, false, caseSensitive, false, false); |
| 107 | return which == kMelder_string::CONTAINS_WORD ? doesMatch : ! doesMatch; |
| 108 | } |
| 109 | case kMelder_string::CONTAINS_WORD_STARTING_WITH: |
| 110 | case kMelder_string::DOES_NOT_CONTAIN_WORD_STARTING_WITH: |
| 111 | { |
| 112 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, false, caseSensitive, false, true); |
| 113 | return which == kMelder_string::CONTAINS_WORD_STARTING_WITH ? doesMatch : ! doesMatch; |
| 114 | } |
| 115 | case kMelder_string::CONTAINS_WORD_ENDING_WITH: |
| 116 | case kMelder_string::DOES_NOT_CONTAIN_WORD_ENDING_WITH: |
| 117 | { |
| 118 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, false, caseSensitive, true, false); |
| 119 | return which == kMelder_string::CONTAINS_WORD_ENDING_WITH ? doesMatch : ! doesMatch; |
| 120 | } |
| 121 | case kMelder_string::CONTAINS_INK: |
| 122 | case kMelder_string::DOES_NOT_CONTAIN_INK: |
| 123 | { |
| 124 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, true, caseSensitive, false, false); |
| 125 | return which == kMelder_string::CONTAINS_INK ? doesMatch : ! doesMatch; |
| 126 | } |
| 127 | case kMelder_string::CONTAINS_INK_STARTING_WITH: |
| 128 | case kMelder_string::DOES_NOT_CONTAIN_INK_STARTING_WITH: |
| 129 | { |
| 130 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, true, caseSensitive, false, true); |
| 131 | return which == kMelder_string::CONTAINS_INK_STARTING_WITH ? doesMatch : ! doesMatch; |
| 132 | } |
| 133 | case kMelder_string::CONTAINS_INK_ENDING_WITH: |
| 134 | case kMelder_string::DOES_NOT_CONTAIN_INK_ENDING_WITH: |
| 135 | { |
| 136 | const bool doesMatch = !! str32str_word_optionallyCaseSensitive (value, criterion, true, caseSensitive, true, false); |
| 137 | return which == kMelder_string::CONTAINS_INK_ENDING_WITH ? doesMatch : ! doesMatch; |
| 138 | } |
| 139 | case kMelder_string::MATCH_REGEXP: |
| 140 | { |
| 141 | char32 *place = nullptr; |
| 142 | regexp *compiled_regexp = CompileRE_throwable (criterion, ! REDFLT_CASE_INSENSITIVE); |
| 143 | if (ExecRE (compiled_regexp, nullptr, value, nullptr, 0, U'\0', U'\0', nullptr, nullptr)) |
| 144 | place = compiled_regexp -> startp [0]; |
| 145 | free (compiled_regexp); |
| 146 | return !! place; |
| 147 | } |
| 148 | } |
| 149 | //return false; // should not occur |
| 150 | } |
| 151 | |
| 152 | /* End of file melder_search.cpp */ |