Skip to content

Commit

Permalink
refs #16, support for VS2019, templated char size dispatching in utf8…
Browse files Browse the repository at this point in the history
… decoder
  • Loading branch information
gulrak committed May 15, 2019
1 parent 61182d0 commit 33e3a36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/dir.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
46 changes: 25 additions & 21 deletions include/ghc/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,40 +1247,44 @@ 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)
{
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)
{
using StringType = std::basic_string<charT, traits, Alloc>;
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 <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;
}

template <typename SourceType>
inline std::string toUtf8(const SourceType* unicodeString)
{
Expand Down

0 comments on commit 33e3a36

Please sign in to comment.