summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Loer <chris.loer@gmail.com>2018-06-27 15:01:54 -0700
committerChris Loer <chris.loer@mapbox.com>2018-07-03 10:03:05 -0700
commit35256c6e5bb1c217fde45c3e89b0db259d9c9f9b (patch)
tree29c94cbd8052c3691bff958d0d175ded7ea5a17d
parent9ff5d34ef2ed2a236cc495f0ad84919cedce9abc (diff)
downloadqtlocation-mapboxgl-35256c6e5bb1c217fde45c3e89b0db259d9c9f9b.tar.gz
[core] Default "collator" implementation
- Based on nunicode - Not locale-aware - Used by linux and Qt builds
-rw-r--r--platform/default/collator.cpp79
-rw-r--r--platform/default/unaccent.cpp43
-rw-r--r--platform/default/unaccent.hpp13
-rw-r--r--platform/linux/config.cmake3
-rw-r--r--platform/qt/config.cmake2
-rw-r--r--platform/qt/qt.cmake5
6 files changed, 143 insertions, 2 deletions
diff --git a/platform/default/collator.cpp b/platform/default/collator.cpp
new file mode 100644
index 0000000000..b7f256756e
--- /dev/null
+++ b/platform/default/collator.cpp
@@ -0,0 +1,79 @@
+#include <mbgl/style/expression/collator.hpp>
+#include <mbgl/util/platform.hpp>
+#include <libnu/strcoll.h>
+#include <unaccent.hpp>
+
+/*
+ The default implementation of Collator ignores locale.
+ Case sensitivity and collation order are based on
+ Default Unicode Collation Element Table (DUCET).
+
+ Diacritic-insensitivity is implemented with nunicode's
+ non-standard "unaccent" functionality, which is tailored
+ to European languages.
+
+ It would be possible to implement locale awareness using ICU,
+ but would require bundling locale data.
+*/
+
+namespace mbgl {
+namespace style {
+namespace expression {
+
+class Collator::Impl {
+public:
+ Impl(bool caseSensitive_, bool diacriticSensitive_, optional<std::string>)
+ : caseSensitive(caseSensitive_)
+ , diacriticSensitive(diacriticSensitive_)
+ {}
+
+ bool operator==(const Impl& other) const {
+ return caseSensitive == other.caseSensitive &&
+ diacriticSensitive == other.diacriticSensitive;
+ }
+
+ int compare(const std::string& lhs, const std::string& rhs) const {
+ if (caseSensitive && diacriticSensitive) {
+ return nu_strcoll(lhs.c_str(), rhs.c_str(),
+ nu_utf8_read, nu_utf8_read);
+ } else if (!caseSensitive && diacriticSensitive) {
+ return nu_strcasecoll(lhs.c_str(), rhs.c_str(),
+ nu_utf8_read, nu_utf8_read);
+ } else if (caseSensitive && !diacriticSensitive) {
+ return nu_strcoll(platform::unaccent(lhs).c_str(), platform::unaccent(rhs).c_str(),
+ nu_utf8_read, nu_utf8_read);
+ } else {
+ return nu_strcasecoll(platform::unaccent(lhs).c_str(), platform::unaccent(rhs).c_str(),
+ nu_utf8_read, nu_utf8_read);
+ }
+ }
+
+ std::string resolvedLocale() const {
+ return "";
+ }
+private:
+ bool caseSensitive;
+ bool diacriticSensitive;
+};
+
+
+Collator::Collator(bool caseSensitive, bool diacriticSensitive, optional<std::string> locale_)
+ : impl(std::make_shared<Impl>(caseSensitive, diacriticSensitive, std::move(locale_)))
+{}
+
+bool Collator::operator==(const Collator& other) const {
+ return *impl == *(other.impl);
+}
+
+int Collator::compare(const std::string& lhs, const std::string& rhs) const {
+ return impl->compare(lhs, rhs);
+}
+
+std::string Collator::resolvedLocale() const {
+ return impl->resolvedLocale();
+}
+
+
+} // namespace expression
+} // namespace style
+} // namespace mbgl
diff --git a/platform/default/unaccent.cpp b/platform/default/unaccent.cpp
new file mode 100644
index 0000000000..faefb4b4cd
--- /dev/null
+++ b/platform/default/unaccent.cpp
@@ -0,0 +1,43 @@
+#include <mbgl/util/platform.hpp>
+#include <libnu/unaccent.h>
+#include <unaccent.hpp>
+
+#include <cstring>
+#include <sstream>
+
+namespace mbgl { namespace platform {
+
+std::string unaccent(const std::string& str)
+{
+ std::stringstream output;
+ char const *itr = str.c_str(), *nitr;
+ char const *end = itr + str.length();
+ char lo[5] = { 0 };
+
+ for (; itr < end; itr = nitr)
+ {
+ uint32_t code_point = 0;
+ char const* buf = nullptr;
+
+ nitr = _nu_tounaccent(itr, end, nu_utf8_read, &code_point, &buf, nullptr);
+ if (buf != nullptr)
+ {
+ do
+ {
+ buf = NU_CASEMAP_DECODING_FUNCTION(buf, &code_point);
+ if (code_point == 0) break;
+ output.write(lo, nu_utf8_write(code_point, lo) - lo);
+ }
+ while (code_point != 0);
+ }
+ else
+ {
+ output.write(itr, nitr - itr);
+ }
+ }
+
+ return output.str();
+}
+
+} // namespace platform
+} // namespace mbgl
diff --git a/platform/default/unaccent.hpp b/platform/default/unaccent.hpp
new file mode 100644
index 0000000000..85ac37a7de
--- /dev/null
+++ b/platform/default/unaccent.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <string>
+
+namespace mbgl {
+namespace platform {
+
+// Non-locale-aware diacritic folding based on nunicode
+// Used as a fallback when locale-aware comparisons aren't available
+std::string unaccent(const std::string &string);
+
+} // namespace platform
+} // namespace mbgl
diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake
index fe4cd5fcc3..08376cbfa3 100644
--- a/platform/linux/config.cmake
+++ b/platform/linux/config.cmake
@@ -58,8 +58,11 @@ macro(mbgl_platform_core)
PRIVATE platform/default/string_stdlib.cpp
PRIVATE platform/default/thread.cpp
PRIVATE platform/default/bidi.cpp
+ PRIVATE platform/default/collator.cpp
PRIVATE platform/default/local_glyph_rasterizer.cpp
PRIVATE platform/default/thread_local.cpp
+ PRIVATE platform/default/unaccent.cpp
+ PRIVATE platform/default/unaccent.hpp
PRIVATE platform/default/utf.cpp
# Image handling
diff --git a/platform/qt/config.cmake b/platform/qt/config.cmake
index fdbc463601..757c0ac44a 100644
--- a/platform/qt/config.cmake
+++ b/platform/qt/config.cmake
@@ -51,8 +51,6 @@ macro(mbgl_platform_core)
target_sources(mbgl-core PRIVATE platform/qt/src/bidi.cpp)
endif()
- target_sources(mbgl-core PRIVATE platform/default/local_glyph_rasterizer.cpp)
-
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
target_add_mason_package(mbgl-core PRIVATE optional)
target_add_mason_package(mbgl-core PRIVATE tao_tuple)
diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake
index dda15174fb..fafbb895dc 100644
--- a/platform/qt/qt.cmake
+++ b/platform/qt/qt.cmake
@@ -38,6 +38,11 @@ set(MBGL_QT_CORE_FILES
PRIVATE platform/qt/src/timer.cpp
PRIVATE platform/qt/src/timer_impl.hpp
PRIVATE platform/qt/src/utf.cpp
+
+ PRIVATE platform/default/local_glyph_rasterizer.cpp
+ PRIVATE platform/default/collator.cpp
+ PRIVATE platform/default/unaccent.cpp
+ PRIVATE platform/default/unaccent.hpp
)
set(MBGL_QT_FILESOURCE_FILES