int Melder_stringMatchesCriterion (const wchar_t *value, int which_kMelder_string, const wchar_t *criterion) { if (value == NULL) { value = L""; /* Regard null strings as empty strings, as is usual in Praat. */ } if (criterion == NULL) { criterion = L""; /* Regard null strings as empty strings, as is usual in Praat. */ } if (which_kMelder_string <= kMelder_string_NOT_EQUAL_TO) { int matchPositiveCriterion = wcsequ (value, criterion); return (which_kMelder_string == kMelder_string_EQUAL_TO) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_CONTAIN) { int matchPositiveCriterion = wcsstr (value, criterion) != NULL; return (which_kMelder_string == kMelder_string_CONTAINS) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_START_WITH) { int matchPositiveCriterion = wcsnequ (value, criterion, wcslen (criterion)); return (which_kMelder_string == kMelder_string_STARTS_WITH) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_END_WITH) { int criterionLength = wcslen (criterion), valueLength = wcslen (value); int matchPositiveCriterion = criterionLength <= valueLength && wcsequ (value + valueLength - criterionLength, criterion); return (which_kMelder_string == kMelder_string_ENDS_WITH) == matchPositiveCriterion; } if (which_kMelder_string == kMelder_string_MATCH_REGEXP) { wchar_t *place = NULL, *errorMessage; regexp *compiled_regexp = CompileRE (criterion, & errorMessage, 0); if (compiled_regexp == NULL) return FALSE; // BUG: what about removing errorMessage? if (ExecRE (compiled_regexp, NULL, value, NULL, 0, '\0', '\0', NULL, NULL, NULL)) place = compiled_regexp -> startp [0]; free (compiled_regexp); return place != NULL; } return 0; /* Should not occur. */ }
void OrderedOfString_changeStrings (OrderedOfString me, wchar_t *search, wchar_t *replace, int maximumNumberOfReplaces, long *nmatches, long *nstringmatches, int use_regexp) { regexp *compiled_search = NULL; try { const wchar_t *compileMsg; wchar_t *r; if (search == NULL) { Melder_throw ("Missing search string."); } if (replace == NULL) { Melder_throw ("Missing replace string."); } if (use_regexp) { compiled_search = CompileRE ( (regularExp_CHAR *) search, &compileMsg, 0); if (compiled_search == NULL) { Melder_throw (compileMsg); } } for (long i = 1; i <= my size; i++) { SimpleString ss = (SimpleString) my item[i]; long nmatches_sub; if (use_regexp) { r = str_replace_regexp (ss -> string, compiled_search, replace, maximumNumberOfReplaces, &nmatches_sub); } else r = str_replace_literal (ss -> string, search, replace, maximumNumberOfReplaces, &nmatches_sub); // Change without error: Melder_free (ss -> string); ss -> string = r; if (nmatches_sub > 0) { *nmatches += nmatches_sub; (*nstringmatches) ++; } } if (use_regexp) { free (compiled_search); } } catch (MelderError) { if (use_regexp) { free (compiled_search); } Melder_throw ("Replace not completed."); } }
char32 *strstr_regexp (const char32 *string, const char32 *search_regexp) { char32 *charp = 0; const char32 *compileMsg; regexp *compiled_regexp = CompileRE (search_regexp, &compileMsg, 0); if (compiled_regexp == 0) { Melder_throw (U"No regexp"); } if (ExecRE (compiled_regexp, NULL, string, NULL, 0, '\0', '\0', NULL, NULL, NULL)) { charp = compiled_regexp -> startp[0]; } free (compiled_regexp); return charp; }