From 00f1268e31d11e95ee13b0d2938ba9d5a4313663 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Sat, 29 Jun 2024 03:19:22 +0300 Subject: [PATCH] helper function to convert between utf-8 and utf-16 --- helpers/common_helpers.cpp | 36 +++++++++++++++++++++++ helpers/common_helpers/common_helpers.hpp | 3 ++ 2 files changed, 39 insertions(+) diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index eb6c8908..ee9a00df 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -1,9 +1,11 @@ #include "common_helpers/common_helpers.hpp" +#include "utfcpp/utf8.h" #include #include #include #include #include +#include // 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 {}; +} diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index aef1c7d9..ea30c4ed 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -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); + }