diff options
author | Chris Loer <chris.loer@gmail.com> | 2018-06-27 15:01:54 -0700 |
---|---|---|
committer | Chris Loer <chris.loer@mapbox.com> | 2018-07-03 10:03:05 -0700 |
commit | cb1328d1d049d5ac7376d4bd07c3b08f2c5f7c7a (patch) | |
tree | 4b1540b5b7ac60a5c23bda8375fa17bc720ea71f /platform/darwin | |
parent | 35256c6e5bb1c217fde45c3e89b0db259d9c9f9b (diff) | |
download | qtlocation-mapboxgl-cb1328d1d049d5ac7376d4bd07c3b08f2c5f7c7a.tar.gz |
[ios, macos] Darwin "collator" implementation
- Uses NSString for comparison
- Uses NSLocale for loading locales
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/collator.mm | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/platform/darwin/src/collator.mm b/platform/darwin/src/collator.mm new file mode 100644 index 0000000000..5a87ab3c9a --- /dev/null +++ b/platform/darwin/src/collator.mm @@ -0,0 +1,64 @@ +#include <mbgl/style/expression/collator.hpp> + +#include <sstream> + +#import <Foundation/Foundation.h> + +namespace mbgl { +namespace style { +namespace expression { + +class Collator::Impl { +public: + Impl(bool caseSensitive, bool diacriticSensitive, optional<std::string> locale_) + : options((caseSensitive ? 0 : NSCaseInsensitiveSearch) | + (diacriticSensitive ? 0 : NSDiacriticInsensitiveSearch)) + , locale(locale_ ? + [[NSLocale alloc] initWithLocaleIdentifier:@((*locale_).c_str())] : + [NSLocale currentLocale]) + {} + + bool operator==(const Impl& other) const { + return options == other.options && + [[locale localeIdentifier] isEqualToString:[other.locale localeIdentifier]]; + } + + int compare(const std::string& lhs, const std::string& rhs) const { + NSString* nsLhs = @(lhs.c_str()); + NSString* nsRhs = @(rhs.c_str()); + // Limiting the compare range to the length of the LHS seems weird, but + // experimentally we've checked that if LHS is a prefix of RHS compare returns -1 + // https://developer.apple.com/documentation/foundation/nsstring/1414561-compare + NSRange compareRange = NSMakeRange(0, nsLhs.length); + + return [nsLhs compare:nsRhs options:options range:compareRange locale:locale]; + } + + std::string resolvedLocale() const { + return [locale localeIdentifier].UTF8String; + } +private: + NSStringCompareOptions options; + NSLocale* locale; +}; + + +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 |