MCPcopy Create free account
hub / github.com/Krazal/nppopenai / stringToWstring

Function stringToWstring

src/EncodingUtils.cpp:23–40  ·  view source on GitHub ↗

* Converts a UTF-8 encoded std::string to a UTF-16 (wide) std::wstring * * Uses the standard library's codecvt facilities with a fallback mechanism * in case the conversion fails (for non-valid UTF-8 sequences). * * @param str UTF-8 encoded string to convert * @return UTF-16 encoded wide string */

Source from the content-addressed store, hash-verified

21 * @return UTF-16 encoded wide string
22 */
23std::wstring stringToWstring(const std::string &str)
24{
25 try
26 {
27 static std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
28 return conv.from_bytes(str);
29 }
30 catch (...)
31 {
32 // Fallback conversion for non-UTF8 input
33 std::wstring result;
34 for (unsigned char c : str)
35 {
36 result += static_cast<wchar_t>(c);
37 }
38 return result;
39 }
40}
41
42/**
43 * Converts a UTF-16 (wide) std::wstring to a UTF-8 encoded std::string

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected