[ios, macos] Darwin "collator" implementation

- Uses NSString for comparison
 - Uses NSLocale for loading locales
This commit is contained in:
Chris Loer
2018-06-27 15:01:54 -07:00
committed by Chris Loer
parent 35256c6e5b
commit cb1328d1d0
3 changed files with 66 additions and 0 deletions

View File

@@ -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

View File

@@ -29,6 +29,7 @@ macro(mbgl_platform_core)
PRIVATE platform/darwin/mbgl/storage/reachability.h
PRIVATE platform/darwin/mbgl/storage/reachability.m
PRIVATE platform/darwin/src/CFHandle.hpp
PRIVATE platform/darwin/src/collator.mm
PRIVATE platform/darwin/src/local_glyph_rasterizer.mm
PRIVATE platform/darwin/src/logging_nslog.mm
PRIVATE platform/darwin/src/nsthread.mm

View File

@@ -14,6 +14,7 @@ macro(mbgl_platform_core)
PRIVATE platform/darwin/mbgl/storage/reachability.h
PRIVATE platform/darwin/mbgl/storage/reachability.m
PRIVATE platform/darwin/src/CFHandle.hpp
PRIVATE platform/darwin/src/collator.mm
PRIVATE platform/darwin/src/local_glyph_rasterizer.mm
PRIVATE platform/darwin/src/logging_nslog.mm
PRIVATE platform/darwin/src/nsthread.mm