refs #16, support for VS2019, templated char size dispatching in utf8 decoder

This commit is contained in:
Steffen Schümann 2019-05-15 08:10:20 +02:00
parent 61182d039f
commit 33e3a36044
2 changed files with 26 additions and 21 deletions

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <iomanip>
#include <chrono>
#include <string>
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include(<filesystem>)
#include <filesystem>

View File

@ -1247,17 +1247,17 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT
return result;
}
template <typename charT, typename traits, typename Alloc>
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 1)>::type* = nullptr>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
using StringType = std::basic_string<charT, traits, Alloc>;
if (sizeof(typename StringType::value_type) == 1) {
return std::string(unicodeString.begin(), unicodeString.end());
}
}
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 2)>::type* = nullptr>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
std::string result;
result.reserve(unicodeString.length());
if (sizeof(typename StringType::value_type) == 2) {
for (typename StringType::const_iterator iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) {
for (auto iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) {
char32_t c = *iter;
if (is_surrogate(c)) {
++iter;
@ -1272,12 +1272,16 @@ inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicode
appendUTF8(result, c);
}
}
}
else {
for (char32_t c : unicodeString) {
return result;
}
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 4)>::type* = nullptr>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
std::string result;
for (auto c : unicodeString) {
appendUTF8(result, c);
}
}
return result;
}