diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index 24e35e2..5932be3 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -1208,15 +1208,12 @@ GHC_INLINE unsigned consumeUtf8Fragment(const unsigned state, const uint8_t frag namespace detail { -template ::type* = nullptr> -inline StringType fromUtf8(const std::string& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type()) -{ - return StringType(utf8String.begin(), utf8String.end(), alloc); -} - -template ::type* = nullptr> +template inline StringType fromUtf8(const std::string& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type()) { + if (sizeof(typename StringType::value_type) == 1) { + return StringType(utf8String.begin(), utf8String.end(), alloc); + } StringType result(alloc); result.reserve(utf8String.length()); std::string::const_iterator iter = utf8String.begin(); @@ -1224,13 +1221,18 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT std::uint32_t codepoint = 0; while (iter < utf8String.end()) { if ((utf8_state = consumeUtf8Fragment(utf8_state, (uint8_t)*iter++, codepoint)) == S_STRT) { - if (codepoint <= 0xffff) { - result += (typename StringType::value_type)codepoint; + if (sizeof(typename StringType::value_type) == 4) { + result += codepoint; } else { - codepoint -= 0x10000; - result += (typename StringType::value_type)((codepoint >> 10) + 0xd800); - result += (typename StringType::value_type)((codepoint & 0x3ff) + 0xdc00); + if (codepoint <= 0xffff) { + result += (typename StringType::value_type)codepoint; + } + else { + codepoint -= 0x10000; + result += (typename StringType::value_type)((codepoint >> 10) + 0xd800); + result += (typename StringType::value_type)((codepoint & 0x3ff) + 0xdc00); + } } codepoint = 0; } @@ -1246,31 +1248,6 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT return result; } -template ::type* = nullptr> -inline StringType fromUtf8(const std::string& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type()) -{ - StringType result(alloc); - result.reserve(utf8String.length()); - std::string::const_iterator iter = utf8String.begin(); - unsigned utf8_state = S_STRT; - std::uint32_t codepoint = 0; - while (iter < utf8String.end()) { - if ((utf8_state = consumeUtf8Fragment(utf8_state, (uint8_t)*iter++, codepoint)) == S_STRT) { - result += codepoint; - codepoint = 0; - } - else if (utf8_state == S_RJCT) { - result += (typename StringType::value_type)0xfffd; - utf8_state = S_STRT; - codepoint = 0; - } - } - if (utf8_state) { - result += (typename StringType::value_type)0xfffd; - } - return result; -} - template ::type* = nullptr> inline std::string toUtf8(const std::basic_string& unicodeString) {