[core] unify UTF-8/16 conversion to <boost/locale/encoding_utf.hpp>

This commit is contained in:
Konstantin Käfer
2018-10-18 17:33:27 +02:00
parent 729eeeb3f8
commit 282c71e8e9
12 changed files with 34 additions and 68 deletions

View File

@@ -69,7 +69,6 @@ macro(mbgl_filesource)
target_add_mason_package(mbgl-filesource PUBLIC jni.hpp)
target_link_libraries(mbgl-filesource
PRIVATE codecvt
PUBLIC sqlite
PUBLIC -llog
PUBLIC -landroid
@@ -84,8 +83,11 @@ add_library(mapbox-gl SHARED
platform/android/src/main.cpp
)
target_include_directories(mapbox-gl
PRIVATE src
)
target_link_libraries(mapbox-gl
PRIVATE codecvt
PRIVATE mbgl-core
PRIVATE mbgl-filesource
)
@@ -106,7 +108,6 @@ macro(mbgl_platform_test)
)
target_link_libraries(mbgl-test
PRIVATE codecvt
PRIVATE mbgl-core
PRIVATE mbgl-filesource
)

View File

@@ -1,30 +0,0 @@
#pragma once
// This is a minimal polyfill that'll only work exactly for how we use codecvt
#include <string>
#include <boost/locale/encoding_utf.hpp>
namespace std {
template <typename Codecvt, typename Elem = wchar_t>
class wstring_convert {
public:
static_assert(std::is_same<Elem, typename Codecvt::Elem>::value, "type mismatch");
inline std::basic_string<Elem> from_bytes(const string& str) {
return boost::locale::conv::utf_to_utf<Elem>(str);
}
inline string to_bytes(const std::basic_string<Elem>& str) {
return boost::locale::conv::utf_to_utf<char>(str);
}
};
template <typename E>
class codecvt_utf8_utf16 {
public:
using Elem = E;
};
} // namespace std

View File

@@ -0,0 +1,17 @@
#pragma once
// This file replaces the default implementation in jni.hpp.
#include <mbgl/util/utf.hpp>
namespace jni {
inline std::u16string convertUTF8ToUTF16(const std::string& str) {
return mbgl::util::convertUTF8ToUTF16(str);
}
inline std::string convertUTF16ToUTF8(const std::u16string& str) {
return mbgl::util::convertUTF16ToUTF8(str);
}
} // namespace jni

View File

@@ -1,13 +1,16 @@
#include <mbgl/util/utf.hpp>
#include <locale>
#include <codecvt>
#include <boost/locale/encoding_utf.hpp>
namespace mbgl {
namespace util {
std::u16string utf8_to_utf16::convert(const std::string& utf8) {
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>().from_bytes(utf8);
std::u16string convertUTF8ToUTF16(const std::string& str) {
return boost::locale::conv::utf_to_utf<char16_t>(str);
}
std::string convertUTF16ToUTF8(const std::u16string& str) {
return boost::locale::conv::utf_to_utf<char>(str);
}
} // namespace util

View File

@@ -5,7 +5,7 @@
namespace mbgl {
namespace util {
std::u16string utf8_to_utf16::convert(std::string const& utf8) {
std::u16string convertUTF8ToUTF16(std::string const& utf8) {
auto utf16 = QString::fromUtf8(utf8.data(), utf8.length());
// Newers Qt have QString::toStdU16String(), but this is how it is