use platform-specific NSString for case changes on OS X/iOS

refs #309
This commit is contained in:
Konstantin Käfer
2014-08-22 12:37:23 +02:00
parent b9c7e1bdd9
commit 6610a6370b
7 changed files with 57 additions and 4 deletions

View File

@@ -0,0 +1,25 @@
#include <mbgl/platform/platform.hpp>
#include <locale>
namespace mbgl {
namespace platform {
std::string uppercase(const std::string& string) {
// TODO: Use a proper Unicode Special Casing-aware algorithm.
const auto &convert = std::use_facet<std::ctype<char>>(std::locale());
std::string converted = string;
convert.toupper(&converted[0], &converted[0] + converted.size());
return converted;
}
std::string lowercase(const std::string& string) {
// TODO: Use a proper Unicode Special Casing-aware algorithm.
const auto &convert = std::use_facet<std::ctype<char>>(std::locale());
std::string converted = string;
convert.tolower(&converted[0], &converted[0] + converted.size());
return converted;
}
}
}

View File

@@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
#include <mbgl/platform/platform.hpp>
namespace mbgl {
namespace platform {
std::string uppercase(const std::string &string) {
NSString *nsstring = [[NSString alloc] initWithBytesNoCopy:const_cast<char *>(string.data()) length:string.size() encoding:NSUTF8StringEncoding freeWhenDone:NO];
nsstring = [nsstring uppercaseString];
return { [nsstring cStringUsingEncoding:NSUTF8StringEncoding], [nsstring lengthOfBytesUsingEncoding:NSUTF8StringEncoding] };
}
std::string lowercase(const std::string &string) {
NSString *nsstring = [[NSString alloc] initWithBytesNoCopy:const_cast<char *>(string.data()) length:string.size() encoding:NSUTF8StringEncoding freeWhenDone:NO];
nsstring = [nsstring lowercaseString];
return { [nsstring cStringUsingEncoding:NSUTF8StringEncoding], [nsstring lengthOfBytesUsingEncoding:NSUTF8StringEncoding] };
}
}
}

View File

@@ -28,6 +28,12 @@ std::shared_ptr<Request> request_http(const std::string &url,
std::function<void(Response *)> callback,
std::shared_ptr<uv::loop> loop = nullptr);
// Uppercase a string, potentially using platform-specific routines.
std::string uppercase(const std::string &string);
// Lowercase a string, potentially using platform-specific routines.
std::string lowercase(const std::string &string);
// Cancels an HTTP request.
void cancel_request_http(const std::shared_ptr<Request> &req);

View File

@@ -12,6 +12,7 @@
'./main.cpp',
'../common/settings_json.cpp',
'../common/settings_json.hpp',
'../common/platform_default.cpp',
'../common/glfw_view.hpp',
'../common/glfw_view.cpp',
'../common/curl_request.cpp',

View File

@@ -12,6 +12,7 @@
'./main.mm',
'../common/settings_nsuserdefaults.hpp',
'../common/settings_nsuserdefaults.mm',
'../common/platform_nsstring.mm',
'../common/glfw_view.hpp',
'../common/glfw_view.cpp',
'../common/foundation_request.h',

View File

@@ -75,11 +75,10 @@ std::vector<SymbolFeature> SymbolBucket::processFeatures(const VectorTileLayer &
if (text) {
std::string u8string = util::replaceTokens(properties.text.field, feature.properties);
auto &convert = std::use_facet<std::ctype<char>>(std::locale());
if (properties.text.transform == TextTransformType::Uppercase) {
convert.toupper(&u8string[0], &u8string[0] + u8string.size());
u8string = platform::uppercase(u8string);
} else if (properties.text.transform == TextTransformType::Lowercase) {
convert.tolower(&u8string[0], &u8string[0] + u8string.size());
u8string = platform::lowercase(u8string);
}
ft.label = ucs4conv.convert(u8string);