diff options
| author | Carl Hetherington <cth@carlh.net> | 2023-01-17 17:21:55 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2023-01-17 17:21:55 +0100 |
| commit | 1671e097a24cd169f1ad4ea89b0cb3ae105b1e70 (patch) | |
| tree | d73310cfeaed193f374794f6ba646fb8a01008cc /src/lib | |
| parent | 9ec2dbd7c1127406dfc08f9f86ef540cdc820579 (diff) | |
Add basic Collator::find() method.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/collator.cc | 21 | ||||
| -rw-r--r-- | src/lib/collator.h | 1 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/collator.cc b/src/lib/collator.cc index 7bab36fa6..ba504fb6e 100644 --- a/src/lib/collator.cc +++ b/src/lib/collator.cc @@ -25,6 +25,7 @@ #include <unicode/ucol.h> #include <unicode/uiter.h> #include <unicode/utypes.h> +#include <unicode/usearch.h> #include <unicode/ustring.h> #include <boost/scoped_array.hpp> #include <cstring> @@ -79,3 +80,23 @@ Collator::compare (string const& utf8_a, string const& utf8_b) const } } + +bool +Collator::find(string pattern, string text) const +{ + if (_collator) { + auto utf16_pattern = utf8_to_utf16(pattern); + auto utf16_text = utf8_to_utf16(text); + UErrorCode status = U_ZERO_ERROR; + auto search = usearch_openFromCollator(utf16_pattern.data(), -1, utf16_text.data(), -1, _collator, nullptr, &status); + DCPOMATIC_ASSERT(search); + auto const index = usearch_first(search, &status); + usearch_close(search); + return index != -1; + } else { + transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower); + transform(text.begin(), text.end(), text.begin(), ::tolower); + return pattern.find(text) != string::npos; + } +} + diff --git a/src/lib/collator.h b/src/lib/collator.h index d42278124..1768c43c0 100644 --- a/src/lib/collator.h +++ b/src/lib/collator.h @@ -39,6 +39,7 @@ public: Collator& operator=(Collator const&) = delete; int compare(std::string const& utf8_a, std::string const& utf8_b) const; + bool find(std::string pattern, std::string text) const; private: UCollator* _collator = nullptr; |
