helper function to convert between utf-8 and utf-16

This commit is contained in:
otavepto 2024-06-29 03:19:22 +03:00
parent efc062f6be
commit 00f1268e31
2 changed files with 39 additions and 0 deletions

View File

@ -1,9 +1,11 @@
#include "common_helpers/common_helpers.hpp"
#include "utfcpp/utf8.h"
#include <fstream>
#include <cwchar>
#include <algorithm>
#include <cctype>
#include <random>
#include <iterator>
// for gmtime_s()
#define __STDC_WANT_LIB_EXT1__ 1
@ -463,3 +465,37 @@ std::string common_helpers::get_utc_time()
}
return time_str;
}
std::wstring common_helpers::to_wstr(std::string_view str)
{
// test a path like this: "C:\test\命定奇谭ğğğğğÜÜÜÜ"
if (str.empty() || !utf8::is_valid(str)) {
return {};
}
try {
std::wstring wstr{};
utf8::utf8to16(str.begin(), str.end(), std::back_inserter(wstr));
return wstr;
}
catch (...) {}
return {};
}
std::string common_helpers::to_str(std::wstring_view wstr)
{
// test a path like this: "C:\test\命定奇谭ğğğğğÜÜÜÜ"
if (wstr.empty()) {
return {};
}
try {
std::string str{};
utf8::utf16to8(wstr.begin(), wstr.end(), std::back_inserter(str));
return str;
} catch(...) { }
return {};
}

View File

@ -107,4 +107,7 @@ size_t rand_number(size_t max);
std::string get_utc_time();
std::wstring to_wstr(std::string_view str);
std::string to_str(std::wstring_view wstr);
}