Skip to content

Commit 33e3a36

Browse files
committed
refs #16, support for VS2019, templated char size dispatching in utf8 decoder
1 parent 61182d0 commit 33e3a36

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

examples/dir.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <iomanip>
33
#include <chrono>
4+
#include <string>
45

56
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include(<filesystem>)
67
#include <filesystem>

include/ghc/filesystem.hpp

+25-21
Original file line numberDiff line numberDiff line change
@@ -1247,40 +1247,44 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT
12471247
return result;
12481248
}
12491249

1250-
template <typename charT, typename traits, typename Alloc>
1250+
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 1)>::type* = nullptr>
1251+
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
1252+
{
1253+
return std::string(unicodeString.begin(), unicodeString.end());
1254+
}
1255+
1256+
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 2)>::type* = nullptr>
12511257
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
12521258
{
1253-
using StringType = std::basic_string<charT, traits, Alloc>;
1254-
if (sizeof(typename StringType::value_type) == 1) {
1255-
return std::string(unicodeString.begin(), unicodeString.end());
1256-
}
12571259
std::string result;
1258-
result.reserve(unicodeString.length());
1259-
if (sizeof(typename StringType::value_type) == 2) {
1260-
for (typename StringType::const_iterator iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) {
1261-
char32_t c = *iter;
1262-
if (is_surrogate(c)) {
1263-
++iter;
1264-
if (iter != unicodeString.end() && is_high_surrogate(c) && is_low_surrogate(*iter)) {
1265-
appendUTF8(result, (char32_t(c) << 10) + *iter - 0x35fdc00);
1266-
}
1267-
else {
1268-
appendUTF8(result, 0xfffd);
1269-
}
1260+
for (auto iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) {
1261+
char32_t c = *iter;
1262+
if (is_surrogate(c)) {
1263+
++iter;
1264+
if (iter != unicodeString.end() && is_high_surrogate(c) && is_low_surrogate(*iter)) {
1265+
appendUTF8(result, (char32_t(c) << 10) + *iter - 0x35fdc00);
12701266
}
12711267
else {
1272-
appendUTF8(result, c);
1268+
appendUTF8(result, 0xfffd);
12731269
}
12741270
}
1275-
}
1276-
else {
1277-
for (char32_t c : unicodeString) {
1271+
else {
12781272
appendUTF8(result, c);
12791273
}
12801274
}
12811275
return result;
12821276
}
12831277

1278+
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 4)>::type* = nullptr>
1279+
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
1280+
{
1281+
std::string result;
1282+
for (auto c : unicodeString) {
1283+
appendUTF8(result, c);
1284+
}
1285+
return result;
1286+
}
1287+
12841288
template <typename SourceType>
12851289
inline std::string toUtf8(const SourceType* unicodeString)
12861290
{

0 commit comments

Comments
 (0)