Allow Collators to have their language specified, and specify it for tests.
[dcpomatic.git] / src / lib / collator.cc
index 7bab36fa6be1f8136ae6f041a97d4df17adb3e5c..8de1857abeb6c24c988f9e5b2536be94b67b2c6a 100644 (file)
@@ -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>
@@ -35,10 +36,10 @@ using std::string;
 using std::vector;
 
 
-Collator::Collator()
+Collator::Collator(char const* locale)
 {
        UErrorCode status = U_ZERO_ERROR;
-       _collator = ucol_open(nullptr, &status);
+       _collator = ucol_open(locale, &status);
        if (_collator) {
                ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
                /* Ignore case and character encoding (and probably some other things) */
@@ -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;
+       }
+}
+