From 33e3a3604411cafab06ace6069d3c993bd65ca00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Sch=C3=BCmann?= Date: Wed, 15 May 2019 08:10:20 +0200 Subject: [PATCH] refs #16, support for VS2019, templated char size dispatching in utf8 decoder --- examples/dir.cpp | 1 + include/ghc/filesystem.hpp | 46 +++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/examples/dir.cpp b/examples/dir.cpp index b9fcff6..67c7b84 100644 --- a/examples/dir.cpp +++ b/examples/dir.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include() #include diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index e63e88c..803a984 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -1247,40 +1247,44 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT return result; } -template +template ::type* = nullptr> +inline std::string toUtf8(const std::basic_string& unicodeString) +{ + return std::string(unicodeString.begin(), unicodeString.end()); +} + +template ::type* = nullptr> inline std::string toUtf8(const std::basic_string& unicodeString) { - using StringType = std::basic_string; - if (sizeof(typename StringType::value_type) == 1) { - return std::string(unicodeString.begin(), unicodeString.end()); - } 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) { - char32_t c = *iter; - if (is_surrogate(c)) { - ++iter; - if (iter != unicodeString.end() && is_high_surrogate(c) && is_low_surrogate(*iter)) { - appendUTF8(result, (char32_t(c) << 10) + *iter - 0x35fdc00); - } - else { - appendUTF8(result, 0xfffd); - } + for (auto iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) { + char32_t c = *iter; + if (is_surrogate(c)) { + ++iter; + if (iter != unicodeString.end() && is_high_surrogate(c) && is_low_surrogate(*iter)) { + appendUTF8(result, (char32_t(c) << 10) + *iter - 0x35fdc00); } else { - appendUTF8(result, c); + appendUTF8(result, 0xfffd); } } - } - else { - for (char32_t c : unicodeString) { + else { appendUTF8(result, c); } } return result; } +template ::type* = nullptr> +inline std::string toUtf8(const std::basic_string& unicodeString) +{ + std::string result; + for (auto c : unicodeString) { + appendUTF8(result, c); + } + return result; +} + template inline std::string toUtf8(const SourceType* unicodeString) {