@@ -1247,40 +1247,44 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT
1247
1247
return result;
1248
1248
}
1249
1249
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 >
1251
1257
inline std::string toUtf8 (const std::basic_string<charT, traits, Alloc>& unicodeString)
1252
1258
{
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
- }
1257
1259
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 );
1270
1266
}
1271
1267
else {
1272
- appendUTF8 (result, c );
1268
+ appendUTF8 (result, 0xfffd );
1273
1269
}
1274
1270
}
1275
- }
1276
- else {
1277
- for (char32_t c : unicodeString) {
1271
+ else {
1278
1272
appendUTF8 (result, c);
1279
1273
}
1280
1274
}
1281
1275
return result;
1282
1276
}
1283
1277
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
+
1284
1288
template <typename SourceType>
1285
1289
inline std::string toUtf8 (const SourceType* unicodeString)
1286
1290
{
0 commit comments