Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
utility_module::convert_string Class Reference

Provides utilities for string encoding conversion, Base64 encoding/decoding, and substring operations like splitting or replacing. More...

#include <convert_string.h>

Collaboration diagram for utility_module::convert_string:
Collaboration graph

Public Member Functions

template<typename FromType , typename ToType >
auto convert (const FromType &value, const std::string &from_encoding, const std::string &to_encoding) -> std::tuple< std::optional< ToType >, std::optional< std::string > >
 

Static Public Member Functions

static auto to_string (const std::wstring &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a std::wstring to a std::string using the system encoding.
 
static auto to_string (std::wstring_view value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a std::wstring_view to a std::string using the system encoding.
 
static auto to_wstring (const std::string &value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
 Converts a std::string (system-encoded) to a std::wstring.
 
static auto to_wstring (std::string_view value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
 Converts a std::string_view (system-encoded) to a std::wstring.
 
static auto get_system_code_page () -> int
 Retrieves the system code page used for conversions.
 
static auto system_to_utf8 (const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a system-encoded string to UTF-8.
 
static auto utf8_to_system (const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a UTF-8 encoded string to the system encoding.
 
static auto split (const std::string &source, const std::string &token) -> std::tuple< std::optional< std::vector< std::string > >, std::optional< std::string > >
 Splits a string by a given delimiter.
 
static auto to_array (const std::string &value) -> std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > >
 Converts a system-encoded string to a UTF-8 byte array.
 
static auto to_string (const std::vector< uint8_t > &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a UTF-8 byte array to a system-encoded string.
 
static auto to_base64 (const std::vector< uint8_t > &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Encodes a byte array into a Base64 string.
 
static auto from_base64 (const std::string &base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
 Decodes a Base64 string into a byte array.
 
static auto replace (std::string &source, const std::string &token, const std::string &target) -> std::optional< std::string >
 Replaces all occurrences of token in source with target, in place.
 
static auto replace2 (const std::string &source, const std::string &token, const std::string &target) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Replaces all occurrences of token in source with target, returning a new string.
 
static auto to_string (const std::wstring &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a std::wstring to a std::string using the system encoding.
 
static auto to_string (std::wstring_view value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a std::wstring_view to a std::string using the system encoding.
 
static auto to_wstring (const std::string &value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
 Converts a std::string (system-encoded) to a std::wstring.
 
static auto to_wstring (std::string_view value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
 Converts a std::string_view (system-encoded) to a std::wstring.
 
static auto get_system_code_page () -> int
 Retrieves the system code page used for conversions.
 
static auto system_to_utf8 (const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a system-encoded string to UTF-8.
 
static auto utf8_to_system (const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a UTF-8 encoded string to the system encoding.
 
static auto split (const std::string &source, const std::string &token) -> std::tuple< std::optional< std::vector< std::string > >, std::optional< std::string > >
 Splits a string by a given delimiter.
 
static auto to_array (const std::string &value) -> std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > >
 Converts a system-encoded string to a UTF-8 byte array.
 
static auto to_string (const std::vector< uint8_t > &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Converts a UTF-8 byte array to a system-encoded string.
 
static auto to_base64 (const std::vector< uint8_t > &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Encodes a byte array into a Base64 string.
 
static auto from_base64 (const std::string &base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
 Decodes a Base64 string into a byte array.
 
static auto replace (std::string &source, const std::string &token, const std::string &target) -> std::optional< std::string >
 Replaces all occurrences of token in source with target, in place.
 
static auto replace2 (const std::string &source, const std::string &token, const std::string &target) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
 Replaces all occurrences of token in source with target, returning a new string.
 

Private Types

enum class  endian_types {
  little , big , unknown , little ,
  big , unknown
}
 Possible endianness values for UTF-16 or UTF-32 data. More...
 
enum class  encoding_types {
  utf8 , utf16 , utf32 , utf8 ,
  utf16 , utf32
}
 Supported encoding types for Unicode conversion. More...
 
enum class  endian_types {
  little , big , unknown , little ,
  big , unknown
}
 Possible endianness values for UTF-16 or UTF-32 data. More...
 
enum class  encoding_types {
  utf8 , utf16 , utf32 , utf8 ,
  utf16 , utf32
}
 Supported encoding types for Unicode conversion. More...
 

Static Private Member Functions

static auto get_code_page_name (int code_page) -> std::string
 Retrieves a textual name for a code page (e.g., "CP_ACP" or a locale-based name).
 
static auto get_encoding_name (encoding_types encoding, endian_types endian=endian_types::little) -> std::string
 Returns the encoding name string for the given encoding type and endianness.
 
static auto get_wchar_encoding (endian_types endian=endian_types::little) -> std::string
 Derives the wchar_t encoding name based on its size and endianness.
 
template<typename FromType , typename ToType >
static auto convert (const FromType &value, const std::string &from_encoding, const std::string &to_encoding) -> std::tuple< std::optional< ToType >, std::optional< std::string > >
 Converts from one encoding to another using simdutf.
 
static auto detect_endian (const std::u16string &value) -> endian_types
 Detects the endianness of a UTF-16 string by checking for BOM or content patterns.
 
static auto detect_endian (const std::u32string &value) -> endian_types
 Detects the endianness of a UTF-32 string by checking for BOM or content patterns.
 
static auto has_utf8_bom (const std::string &value) -> bool
 Checks if a string has a UTF-8 BOM (Byte Order Mark).
 
static auto remove_utf8_bom (const std::string &value) -> std::string
 Removes a leading UTF-8 BOM from a string, if present.
 
static auto add_utf8_bom (const std::string &value) -> std::string
 Adds a UTF-8 BOM to a string if it doesn't already have one.
 
static auto base64_encode (const std::vector< uint8_t > &data) -> std::string
 Encodes a byte array into a Base64 string.
 
static auto base64_decode (const std::string &base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
 Decodes a Base64 string into a byte array.
 
static auto get_code_page_name (int code_page) -> std::string
 Retrieves a textual name for a code page (e.g., "CP_ACP" or a locale-based name).
 
static auto get_encoding_name (encoding_types encoding, endian_types endian=endian_types::little) -> std::string
 Returns the encoding name string for the given encoding type and endianness.
 
static auto get_wchar_encoding (endian_types endian=endian_types::little) -> std::string
 Derives the wchar_t encoding name based on its size and endianness.
 
template<typename FromType , typename ToType >
static auto convert (const FromType &value, const std::string &from_encoding, const std::string &to_encoding) -> std::tuple< std::optional< ToType >, std::optional< std::string > >
 Converts from one encoding to another using simdutf.
 
static auto detect_endian (const std::u16string &value) -> endian_types
 Detects the endianness of a UTF-16 string by checking for BOM or content patterns.
 
static auto detect_endian (const std::u32string &value) -> endian_types
 Detects the endianness of a UTF-32 string by checking for BOM or content patterns.
 
static auto has_utf8_bom (const std::string &value) -> bool
 Checks if a string has a UTF-8 BOM (Byte Order Mark).
 
static auto remove_utf8_bom (const std::string &value) -> std::string
 Removes a leading UTF-8 BOM from a string, if present.
 
static auto add_utf8_bom (const std::string &value) -> std::string
 Adds a UTF-8 BOM to a string if it doesn't already have one.
 
static auto base64_encode (const std::vector< uint8_t > &data) -> std::string
 Encodes a byte array into a Base64 string.
 
static auto base64_decode (const std::string &base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
 Decodes a Base64 string into a byte array.
 

Detailed Description

Provides utilities for string encoding conversion, Base64 encoding/decoding, and substring operations like splitting or replacing.

This class uses simdutf for SIMD-accelerated Unicode transcoding between UTF-8, UTF-16, and UTF-32 on all platforms. It also provides methods to handle Base64 encoding/decoding and some basic string manipulation (split, replace).

Encoding Conversions

  • Typically returns a tuple containing either a successful result (std::optional<T>) and an empty error (std::nullopt), or std::nullopt with an error message describing the failure.
  • BOM (Byte Order Mark) is handled in certain functions for UTF-8, UTF-16, and UTF-32.

Base64

  • Encodes and decodes raw byte arrays (std::vector<uint8_t>).

Usage Example

// Converting a wstring to system-encoded string
auto [strOpt, errMsg] = convert_string::to_string(L"안녕하세요");
if (!strOpt) {
std::cerr << "Error: " << *errMsg << std::endl;
} else {
std::cout << "Converted string: " << *strOpt << std::endl;
}

This class uses simdutf for SIMD-accelerated Unicode transcoding between UTF-8, UTF-16, and UTF-32 on all platforms. It also provides methods to handle Base64 encoding/decoding and some basic string manipulation (split, replace).

Encoding Conversions

  • Typically returns a tuple containing either a successful result (std::optional<T>) and an empty error (std::nullopt), or std::nullopt with an error message describing the failure.
  • BOM (Byte Order Mark) is handled in certain functions for UTF-8, UTF-16, and UTF-32.

Base64

  • Encodes and decodes raw byte arrays (std::vector<uint8_t>).

Usage Example

// Converting a wstring to system-encoded string
auto [strOpt, errMsg] = convert_string::to_string(L"안녕하세요");
if (!strOpt) {
std::cerr << "Error: " << *errMsg << std::endl;
} else {
std::cout << "Converted string: " << *strOpt << std::endl;
}

Definition at line 52 of file convert_string.h.

Member Enumeration Documentation

◆ encoding_types [1/2]

Supported encoding types for Unicode conversion.

Enumerator
utf8 

UTF-8 encoding.

utf16 

UTF-16 encoding.

utf32 

UTF-32 encoding.

utf8 

UTF-8 encoding.

utf16 

UTF-16 encoding.

utf32 

UTF-32 encoding.

Definition at line 222 of file convert_string.h.

223 {
224 utf8,
225 utf16,
226 utf32
227 };

◆ encoding_types [2/2]

Supported encoding types for Unicode conversion.

Enumerator
utf8 

UTF-8 encoding.

utf16 

UTF-16 encoding.

utf32 

UTF-32 encoding.

utf8 

UTF-8 encoding.

utf16 

UTF-16 encoding.

utf32 

UTF-32 encoding.

Definition at line 216 of file convert_string.h.

217 {
218 utf8,
219 utf16,
220 utf32
221 };

◆ endian_types [1/2]

Possible endianness values for UTF-16 or UTF-32 data.

Enumerator
little 

Little-endian.

big 

Big-endian.

unknown 

Unknown or not applicable.

little 

Little-endian.

big 

Big-endian.

unknown 

Unknown or not applicable.

Definition at line 214 of file convert_string.h.

215 {
216 little,
217 big,
218 unknown
219 };
@ unknown
Health state cannot be determined.

◆ endian_types [2/2]

Possible endianness values for UTF-16 or UTF-32 data.

Enumerator
little 

Little-endian.

big 

Big-endian.

unknown 

Unknown or not applicable.

little 

Little-endian.

big 

Big-endian.

unknown 

Unknown or not applicable.

Definition at line 208 of file convert_string.h.

209 {
210 little,
211 big,
212 unknown
213 };

Member Function Documentation

◆ add_utf8_bom() [1/2]

static auto utility_module::convert_string::add_utf8_bom ( const std::string & value) -> std::string
staticprivate

Adds a UTF-8 BOM to a string if it doesn't already have one.

Parameters
valueThe input string (UTF-8 encoded).
Returns
A new string guaranteed to have the UTF-8 BOM at the start.

References base64_decode(), and base64_encode().

Here is the call graph for this function:

◆ add_utf8_bom() [2/2]

static auto utility_module::convert_string::add_utf8_bom ( const std::string & value) -> std::string
staticprivate

Adds a UTF-8 BOM to a string if it doesn't already have one.

Parameters
valueThe input string (UTF-8 encoded).
Returns
A new string guaranteed to have the UTF-8 BOM at the start.

◆ base64_decode() [1/2]

auto kcenon::thread::utils::convert_string::base64_decode ( const std::string & base64_str) -> std::tuple<std::vector<uint8_t>, std::optional<std::string>>
staticprivate

Decodes a Base64 string into a byte array.

Parameters
base64_strThe Base64 string.
Returns
A tuple of:
  • std::vector<uint8_t>: The decoded bytes (empty on failure).
  • std::optional<std::string>: An error message if decoding fails, otherwise std::nullopt.

Definition at line 554 of file convert_string.cpp.

556 {
557 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
558 "abcdefghijklmnopqrstuvwxyz"
559 "0123456789+/";
560
561 if (base64_str.length() % 4 != 0)
562 {
563 return { std::vector<uint8_t>(), "Invalid base64 input length" };
564 }
565
566 size_t padding = 0;
567 if (!base64_str.empty())
568 {
569 if (base64_str[base64_str.length() - 1] == '=')
570 padding++;
571 if (base64_str.length() >= 2 && base64_str[base64_str.length() - 2] == '=')
572 padding++;
573 if (padding > 2)
574 {
575 return { std::vector<uint8_t>(), "Invalid padding in base64 string" };
576 }
577 }
578
579 size_t decoded_length = (base64_str.length() / 4) * 3 - padding;
580 std::vector<uint8_t> decoded_data;
581 decoded_data.reserve(decoded_length);
582
583 std::vector<int> decoding_table(256, -1);
584 for (int i = 0; i < 64; i++)
585 {
586 decoding_table[static_cast<unsigned char>(base64_chars[static_cast<size_t>(i)])] = i;
587 }
588
589 uint32_t buffer = 0;
590 int bits_collected = 0;
591 size_t i = 0;
592 for (; i < base64_str.length(); ++i)
593 {
594 char c = base64_str[i];
595 if (c == '=')
596 {
597 if (i < base64_str.length() - padding)
598 {
599 return { std::vector<uint8_t>(), "Invalid padding position in base64 string" };
600 }
601 break;
602 }
603
604 if (decoding_table[static_cast<unsigned char>(c)] == -1)
605 {
606 return { std::vector<uint8_t>(), "Invalid character in base64 string" };
607 }
608
609 buffer = (buffer << 6) | static_cast<uint32_t>(decoding_table[static_cast<unsigned char>(c)]);
610 bits_collected += 6;
611
612 if (bits_collected >= 8)
613 {
614 bits_collected -= 8;
615 decoded_data.push_back((buffer >> bits_collected) & 0xFF);
616 }
617 }
618
619 for (; i < base64_str.length(); ++i)
620 {
621 if (base64_str[i] != '=')
622 {
623 return { std::vector<uint8_t>(),
624 "Invalid character after padding in base64 string" };
625 }
626 }
627
628 return { decoded_data, std::nullopt };
629 }

◆ base64_decode() [2/2]

static auto utility_module::convert_string::base64_decode ( const std::string & base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
staticprivate

Decodes a Base64 string into a byte array.

Parameters
base64_strThe Base64 string.
Returns
A tuple of:
  • std::vector<uint8_t>: The decoded bytes (empty on failure).
  • std::optional<std::string>: An error message if decoding fails, otherwise std::nullopt.

Referenced by add_utf8_bom().

Here is the caller graph for this function:

◆ base64_encode() [1/2]

auto kcenon::thread::utils::convert_string::base64_encode ( const std::vector< uint8_t > & data) -> std::string
staticprivate

Encodes a byte array into a Base64 string.

Parameters
dataThe raw byte array.
Returns
The Base64-encoded representation.

Definition at line 520 of file convert_string.cpp.

521 {
522 static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
523 "abcdefghijklmnopqrstuvwxyz"
524 "0123456789+/";
525
526 std::string encoded_string;
527 size_t i = 0;
528 uint32_t octet_a, octet_b, octet_c;
529 uint32_t triple;
530
531 while (i < data.size())
532 {
533 octet_a = i < data.size() ? data[i++] : 0;
534 octet_b = i < data.size() ? data[i++] : 0;
535 octet_c = i < data.size() ? data[i++] : 0;
536
537 triple = (octet_a << 16) + (octet_b << 8) + octet_c;
538
539 encoded_string += base64_chars[(triple >> 18) & 0x3F];
540 encoded_string += base64_chars[(triple >> 12) & 0x3F];
541 encoded_string += base64_chars[(triple >> 6) & 0x3F];
542 encoded_string += base64_chars[triple & 0x3F];
543 }
544
545 int mod_table[] = { 0, 2, 1 };
546 for (int j = 0; j < mod_table[data.size() % 3]; j++)
547 {
548 encoded_string[encoded_string.size() - 1 - static_cast<size_t>(j)] = '=';
549 }
550
551 return encoded_string;
552 }

◆ base64_encode() [2/2]

static auto utility_module::convert_string::base64_encode ( const std::vector< uint8_t > & data) -> std::string
staticprivate

Encodes a byte array into a Base64 string.

Parameters
dataThe raw byte array.
Returns
The Base64-encoded representation.

Referenced by add_utf8_bom().

Here is the caller graph for this function:

◆ convert() [1/3]

template<typename FromType , typename ToType >
static auto utility_module::convert_string::convert ( const FromType & value,
const std::string & from_encoding,
const std::string & to_encoding ) -> std::tuple< std::optional< ToType >, std::optional< std::string > >
staticprivate

Converts from one encoding to another using simdutf.

Template Parameters
FromTypeThe input string type.
ToTypeThe output string type.
Parameters
valueThe input string.
from_encodingSource encoding name (e.g., "UTF-16LE").
to_encodingTarget encoding name (e.g., "UTF-8").
Returns
A tuple:
  • std::optional<ToType>: The converted string on success, or std::nullopt on failure.
  • std::optional<std::string>: The error message if conversion fails.

◆ convert() [2/3]

template<typename FromType , typename ToType >
auto utility_module::convert_string::convert ( const FromType & value,
const std::string & from_encoding,
const std::string & to_encoding ) -> std::tuple<std::optional<ToType>, std::optional<std::string>>

Definition at line 36 of file convert_string.cpp.

40 {
41 if (value.empty())
42 {
43 return { ToType{}, std::nullopt };
44 }
45
46 if constexpr (std::is_same_v<FromType, ToType>)
47 {
48 if (from_encoding == to_encoding)
49 {
50 return { ToType(value), std::nullopt };
51 }
52 }
53
54 const auto* src_data = value.data();
55 size_t src_len = value.size();
56
57 // UTF-8 -> UTF-16LE (std::string -> std::wstring on Windows, or char16_t-based)
58 if (from_encoding == "UTF-8"
59 && (to_encoding == "UTF-16LE" || to_encoding == "UTF-16"))
60 {
61 if (!simdutf::validate_utf8(reinterpret_cast<const char*>(src_data),
62 src_len * sizeof(typename FromType::value_type)))
63 {
64 return { std::nullopt, "Invalid UTF-8 input" };
65 }
66
67 size_t utf16_len = simdutf::utf16_length_from_utf8(
68 reinterpret_cast<const char*>(src_data),
69 src_len * sizeof(typename FromType::value_type));
70
71 if constexpr (sizeof(typename ToType::value_type) == 2)
72 {
73 ToType result(utf16_len, typename ToType::value_type{});
74 size_t written = simdutf::convert_utf8_to_utf16le(
75 reinterpret_cast<const char*>(src_data),
76 src_len * sizeof(typename FromType::value_type),
77 reinterpret_cast<char16_t*>(result.data()));
78 if (written == 0 && utf16_len > 0)
79 {
80 return { std::nullopt, "UTF-8 to UTF-16LE conversion failed" };
81 }
82 result.resize(written);
83 return { result, std::nullopt };
84 }
85 }
86
87 // UTF-8 -> UTF-32LE (std::string -> std::wstring on Unix)
88 if (from_encoding == "UTF-8"
89 && (to_encoding == "UTF-32LE" || to_encoding == "UTF-32"))
90 {
91 if (!simdutf::validate_utf8(reinterpret_cast<const char*>(src_data),
92 src_len * sizeof(typename FromType::value_type)))
93 {
94 return { std::nullopt, "Invalid UTF-8 input" };
95 }
96
97 size_t utf32_len = simdutf::utf32_length_from_utf8(
98 reinterpret_cast<const char*>(src_data),
99 src_len * sizeof(typename FromType::value_type));
100
101 if constexpr (sizeof(typename ToType::value_type) == 4)
102 {
103 ToType result(utf32_len, typename ToType::value_type{});
104 size_t written = simdutf::convert_utf8_to_utf32(
105 reinterpret_cast<const char*>(src_data),
106 src_len * sizeof(typename FromType::value_type),
107 reinterpret_cast<char32_t*>(result.data()));
108 if (written == 0 && utf32_len > 0)
109 {
110 return { std::nullopt, "UTF-8 to UTF-32 conversion failed" };
111 }
112 result.resize(written);
113 return { result, std::nullopt };
114 }
115 }
116
117 // UTF-16LE -> UTF-8 (std::wstring -> std::string on Windows)
118 if ((from_encoding == "UTF-16LE" || from_encoding == "UTF-16")
119 && to_encoding == "UTF-8")
120 {
121 if constexpr (sizeof(typename FromType::value_type) == 2)
122 {
123 const char16_t* utf16_data
124 = reinterpret_cast<const char16_t*>(src_data);
125
126 if (!simdutf::validate_utf16le(utf16_data, src_len))
127 {
128 return { std::nullopt, "Invalid UTF-16LE input" };
129 }
130
131 size_t utf8_len
132 = simdutf::utf8_length_from_utf16le(utf16_data, src_len);
133
134 if constexpr (std::is_same_v<ToType, std::string>)
135 {
136 std::string result(utf8_len, '\0');
137 size_t written = simdutf::convert_utf16le_to_utf8(
138 utf16_data, src_len, result.data());
139 if (written == 0 && utf8_len > 0)
140 {
141 return { std::nullopt,
142 "UTF-16LE to UTF-8 conversion failed" };
143 }
144 result.resize(written);
145 return { result, std::nullopt };
146 }
147 }
148 }
149
150 // UTF-32LE -> UTF-8 (std::wstring -> std::string on Unix)
151 if ((from_encoding == "UTF-32LE" || from_encoding == "UTF-32")
152 && to_encoding == "UTF-8")
153 {
154 if constexpr (sizeof(typename FromType::value_type) == 4)
155 {
156 const char32_t* utf32_data
157 = reinterpret_cast<const char32_t*>(src_data);
158
159 if (!simdutf::validate_utf32(utf32_data, src_len))
160 {
161 return { std::nullopt, "Invalid UTF-32 input" };
162 }
163
164 size_t utf8_len
165 = simdutf::utf8_length_from_utf32(utf32_data, src_len);
166
167 if constexpr (std::is_same_v<ToType, std::string>)
168 {
169 std::string result(utf8_len, '\0');
170 size_t written = simdutf::convert_utf32_to_utf8(
171 utf32_data, src_len, result.data());
172 if (written == 0 && utf8_len > 0)
173 {
174 return { std::nullopt,
175 "UTF-32 to UTF-8 conversion failed" };
176 }
177 result.resize(written);
178 return { result, std::nullopt };
179 }
180 }
181 }
182
183 return { std::nullopt,
184 std::format("Unsupported encoding conversion: {} -> {}",
185 from_encoding, to_encoding) };
186 }

◆ convert() [3/3]

template<typename FromType , typename ToType >
static auto utility_module::convert_string::convert ( const FromType & value,
const std::string & from_encoding,
const std::string & to_encoding ) -> std::tuple< std::optional< ToType >, std::optional< std::string > >
staticprivate

Converts from one encoding to another using simdutf.

Template Parameters
FromTypeThe input string type.
ToTypeThe output string type.
Parameters
valueThe input string.
from_encodingSource encoding name (e.g., "UTF-16LE").
to_encodingTarget encoding name (e.g., "UTF-8").
Returns
A tuple:
  • std::optional<ToType>: The converted string on success, or std::nullopt on failure.
  • std::optional<std::string>: The error message if conversion fails.

◆ detect_endian() [1/4]

static auto utility_module::convert_string::detect_endian ( const std::u16string & value) -> endian_types
staticprivate

Detects the endianness of a UTF-16 string by checking for BOM or content patterns.

Parameters
valueThe UTF-16 string.
Returns
The detected endianness (little, big, or unknown).

◆ detect_endian() [2/4]

static auto utility_module::convert_string::detect_endian ( const std::u16string & value) -> endian_types
staticprivate

Detects the endianness of a UTF-16 string by checking for BOM or content patterns.

Parameters
valueThe UTF-16 string.
Returns
The detected endianness (little, big, or unknown).

◆ detect_endian() [3/4]

static auto utility_module::convert_string::detect_endian ( const std::u32string & value) -> endian_types
staticprivate

Detects the endianness of a UTF-32 string by checking for BOM or content patterns.

Parameters
valueThe UTF-32 string.
Returns
The detected endianness (little, big, or unknown).

◆ detect_endian() [4/4]

static auto utility_module::convert_string::detect_endian ( const std::u32string & value) -> endian_types
staticprivate

Detects the endianness of a UTF-32 string by checking for BOM or content patterns.

Parameters
valueThe UTF-32 string.
Returns
The detected endianness (little, big, or unknown).

◆ from_base64() [1/2]

auto kcenon::thread::utils::convert_string::from_base64 ( const std::string & base64_str) -> std::tuple<std::vector<uint8_t>, std::optional<std::string>>
static

Decodes a Base64 string into a byte array.

Parameters
base64_strThe Base64-encoded string.
Returns
A tuple of:
  • std::vector<uint8_t>: The decoded data. May be empty if decoding fails.
  • std::optional<std::string>: An error message on failure, or std::nullopt on success.

Definition at line 470 of file convert_string.cpp.

472 {
473 return base64_decode(base64_str);
474 }
static auto base64_decode(const std::string &base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
Decodes a Base64 string into a byte array.

◆ from_base64() [2/2]

static auto utility_module::convert_string::from_base64 ( const std::string & base64_str) -> std::tuple< std::vector< uint8_t >, std::optional< std::string > >
static

Decodes a Base64 string into a byte array.

Parameters
base64_strThe Base64-encoded string.
Returns
A tuple of:
  • std::vector<uint8_t>: The decoded data. May be empty if decoding fails.
  • std::optional<std::string>: An error message on failure, or std::nullopt on success.

◆ get_code_page_name() [1/2]

auto kcenon::thread::utils::convert_string::get_code_page_name ( int code_page) -> std::string
staticprivate

Retrieves a textual name for a code page (e.g., "CP_ACP" or a locale-based name).

Parameters
code_pageThe code page integer.
Returns
A string representing the code page name, or an empty string if unknown.

Definition at line 374 of file convert_string.cpp.

375 {
376 switch (code_page)
377 {
378 case 65001:
379 return "UTF-8";
380 default:
381 return "CP" + std::to_string(code_page);
382 }
383 }

◆ get_code_page_name() [2/2]

static auto utility_module::convert_string::get_code_page_name ( int code_page) -> std::string
staticprivate

Retrieves a textual name for a code page (e.g., "CP_ACP" or a locale-based name).

Parameters
code_pageThe code page integer.
Returns
A string representing the code page name, or an empty string if unknown.

◆ get_encoding_name() [1/2]

auto kcenon::thread::utils::convert_string::get_encoding_name ( encoding_types encoding,
endian_types endian = endian_types::little ) -> std::string
staticprivate

Returns the encoding name string for the given encoding type and endianness.

Parameters
encodingThe base encoding (UTF-8, UTF-16, or UTF-32).
endianThe byte order (little, big) if applicable.
Returns
An encoding name string such as "UTF-8" or "UTF-16LE".

Definition at line 248 of file convert_string.cpp.

250 {
251 switch (encoding)
252 {
253 case encoding_types::utf8:
254 return "UTF-8";
255 case encoding_types::utf16:
256 if (endian == endian_types::little)
257 return "UTF-16LE";
258 else if (endian == endian_types::big)
259 return "UTF-16BE";
260 else
261 return "UTF-16";
262 case encoding_types::utf32:
263 if (endian == endian_types::little)
264 return "UTF-32LE";
265 else if (endian == endian_types::big)
266 return "UTF-32BE";
267 else
268 return "UTF-32";
269 default:
270 throw std::runtime_error("Unknown encoding");
271 }
272 }

◆ get_encoding_name() [2/2]

static auto utility_module::convert_string::get_encoding_name ( encoding_types encoding,
endian_types endian = endian_types::little ) -> std::string
staticprivate

Returns the encoding name string for the given encoding type and endianness.

Parameters
encodingThe base encoding (UTF-8, UTF-16, or UTF-32).
endianThe byte order (little, big) if applicable.
Returns
An encoding name string such as "UTF-8" or "UTF-16LE".

◆ get_system_code_page() [1/2]

auto kcenon::thread::utils::convert_string::get_system_code_page ( ) -> int
static

Retrieves the system code page used for conversions.

Returns
An integer representing the system code page (e.g., CP_ACP on Windows).

On Unix-like systems, returns UTF-8 (65001) as the default. On Windows, uses GetACP() to detect the system code page.

Definition at line 365 of file convert_string.cpp.

366 {
367#ifdef _WIN32
368 return static_cast<int>(GetACP());
369#else
370 return 65001;
371#endif
372 }

◆ get_system_code_page() [2/2]

static auto utility_module::convert_string::get_system_code_page ( ) -> int
static

Retrieves the system code page used for conversions.

Returns
An integer representing the system code page (e.g., CP_ACP on Windows).

On Unix-like systems, returns UTF-8 (65001) as the default. On Windows, uses GetACP() to detect the system code page.

◆ get_wchar_encoding() [1/2]

auto kcenon::thread::utils::convert_string::get_wchar_encoding ( endian_types endian = endian_types::little) -> std::string
staticprivate

Derives the wchar_t encoding name based on its size and endianness.

Parameters
endianThe byte order. Defaults to little-endian.
Returns
For example, "UTF-16LE" on Windows or "UTF-32LE" on some Unix-like systems.

Definition at line 274 of file convert_string.cpp.

275 {
276 if constexpr (sizeof(wchar_t) == 2)
277 {
278 return get_encoding_name(encoding_types::utf16, endian);
279 }
280 else if constexpr (sizeof(wchar_t) == 4)
281 {
282 return get_encoding_name(encoding_types::utf32, endian);
283 }
284 else
285 {
286 throw std::runtime_error("Unsupported wchar_t size");
287 }
288 }
static auto get_encoding_name(encoding_types encoding, endian_types endian=endian_types::little) -> std::string
Returns the encoding name string for the given encoding type and endianness.

◆ get_wchar_encoding() [2/2]

static auto utility_module::convert_string::get_wchar_encoding ( endian_types endian = endian_types::little) -> std::string
staticprivate

Derives the wchar_t encoding name based on its size and endianness.

Parameters
endianThe byte order. Defaults to little-endian.
Returns
For example, "UTF-16LE" on Windows or "UTF-32LE" on some Unix-like systems.

◆ has_utf8_bom() [1/2]

static auto utility_module::convert_string::has_utf8_bom ( const std::string & value) -> bool
staticprivate

Checks if a string has a UTF-8 BOM (Byte Order Mark).

Parameters
valueThe input string (UTF-8 encoded).
Returns
true if the string starts with the UTF-8 BOM (0xEF,0xBB,0xBF), else false.

◆ has_utf8_bom() [2/2]

static auto utility_module::convert_string::has_utf8_bom ( const std::string & value) -> bool
staticprivate

Checks if a string has a UTF-8 BOM (Byte Order Mark).

Parameters
valueThe input string (UTF-8 encoded).
Returns
true if the string starts with the UTF-8 BOM (0xEF,0xBB,0xBF), else false.

◆ remove_utf8_bom() [1/2]

static auto utility_module::convert_string::remove_utf8_bom ( const std::string & value) -> std::string
staticprivate

Removes a leading UTF-8 BOM from a string, if present.

Parameters
valueThe input string (UTF-8 encoded).
Returns
A new string without the BOM. If no BOM was present, returns the original string.

◆ remove_utf8_bom() [2/2]

static auto utility_module::convert_string::remove_utf8_bom ( const std::string & value) -> std::string
staticprivate

Removes a leading UTF-8 BOM from a string, if present.

Parameters
valueThe input string (UTF-8 encoded).
Returns
A new string without the BOM. If no BOM was present, returns the original string.

◆ replace() [1/2]

auto kcenon::thread::utils::convert_string::replace ( std::string & source,
const std::string & token,
const std::string & target ) -> std::optional<std::string>
static

Replaces all occurrences of token in source with target, in place.

Parameters
sourceThe string to modify.
tokenThe substring to find.
targetThe substring to replace token with.
Returns
std::optional<std::string> containing an error message if replacement fails, or std::nullopt on success.

This function modifies source directly. If token is empty, it returns an error.

Definition at line 476 of file convert_string.cpp.

479 {
480 auto [value, value_error] = replace2(source, token, target);
481 if (value_error.has_value())
482 {
483 return value_error;
484 }
485
486 source = value.value();
487 return std::nullopt;
488 }
static auto replace2(const std::string &source, const std::string &token, const std::string &target) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
Replaces all occurrences of token in source with target, returning a new string.

◆ replace() [2/2]

static auto utility_module::convert_string::replace ( std::string & source,
const std::string & token,
const std::string & target ) -> std::optional< std::string >
static

Replaces all occurrences of token in source with target, in place.

Parameters
sourceThe string to modify.
tokenThe substring to find.
targetThe substring to replace token with.
Returns
std::optional<std::string> containing an error message if replacement fails, or std::nullopt on success.

This function modifies source directly. If token is empty, it returns an error.

◆ replace2() [1/2]

auto kcenon::thread::utils::convert_string::replace2 ( const std::string & source,
const std::string & token,
const std::string & target ) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Replaces all occurrences of token in source with target, returning a new string.

Parameters
sourceThe source string (unchanged).
tokenThe substring to find.
targetThe substring to replace token with.
Returns
A tuple of:
  • std::optional<std::string>: The modified string on success.
  • std::optional<std::string>: An error message if replacement fails.

Definition at line 490 of file convert_string.cpp.

494 {
495 if (source.empty())
496 {
497 return { std::nullopt, "Source string is empty" };
498 }
499
500 if (token.empty())
501 {
502 return { std::nullopt, "Token string is empty" };
503 }
504
505 std::string result;
506
507 size_t last_offset = 0;
508 for (size_t offset = source.find(token, last_offset); offset != std::string::npos;
509 last_offset = offset + token.size(), offset = source.find(token, last_offset))
510 {
511 std::format_to(std::back_inserter(result), "{}{}",
512 source.substr(last_offset, offset - last_offset), target);
513 }
514
515 std::format_to(std::back_inserter(result), "{}", source.substr(last_offset));
516
517 return { result, std::nullopt };
518 }

◆ replace2() [2/2]

static auto utility_module::convert_string::replace2 ( const std::string & source,
const std::string & token,
const std::string & target ) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Replaces all occurrences of token in source with target, returning a new string.

Parameters
sourceThe source string (unchanged).
tokenThe substring to find.
targetThe substring to replace token with.
Returns
A tuple of:
  • std::optional<std::string>: The modified string on success.
  • std::optional<std::string>: An error message if replacement fails.

◆ split() [1/2]

auto kcenon::thread::utils::convert_string::split ( const std::string & source,
const std::string & token ) -> std::tuple<std::optional<std::vector<std::string>>, std::optional<std::string>>
static

Splits a string by a given delimiter.

Parameters
sourceThe source string to split.
tokenThe delimiter substring.
Returns
A tuple of:
  • std::optional<std::vector<std::string>>: A vector of tokens on success.
  • std::optional<std::string>: An error message if splitting fails.

If token is empty, splitting cannot proceed and an error is returned.

Definition at line 409 of file convert_string.cpp.

411 {
412 if (token.empty())
413 {
414 return { std::vector{ source }, std::nullopt };
415 }
416
417 std::vector<std::string> result;
418 size_t start_pos = 0;
419 size_t end_pos = source.find(token);
420
421 while (end_pos != std::string::npos)
422 {
423 result.emplace_back(source.substr(start_pos, end_pos - start_pos));
424 start_pos = end_pos + token.length();
425 end_pos = source.find(token, start_pos);
426 }
427
428 result.emplace_back(source.substr(start_pos));
429
430 return { result, std::nullopt };
431 }

◆ split() [2/2]

static auto utility_module::convert_string::split ( const std::string & source,
const std::string & token ) -> std::tuple< std::optional< std::vector< std::string > >, std::optional< std::string > >
static

Splits a string by a given delimiter.

Parameters
sourceThe source string to split.
tokenThe delimiter substring.
Returns
A tuple of:
  • std::optional<std::vector<std::string>>: A vector of tokens on success.
  • std::optional<std::string>: An error message if splitting fails.

If token is empty, splitting cannot proceed and an error is returned.

◆ system_to_utf8() [1/2]

auto kcenon::thread::utils::convert_string::system_to_utf8 ( const std::string & value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Converts a system-encoded string to UTF-8.

Parameters
valueThe input string in system encoding.
Returns
A tuple of:
  • std::optional<std::string>: The UTF-8 encoded string on success.
  • std::optional<std::string>: An error message on failure.

Definition at line 385 of file convert_string.cpp.

387 {
388 int code_page = get_system_code_page();
389 if (code_page == 65001)
390 {
391 return { value, std::nullopt };
392 }
393 return convert<std::string, std::string>(value, get_code_page_name(code_page),
394 get_encoding_name(encoding_types::utf8));
395 }
static auto get_system_code_page() -> int
Retrieves the system code page used for conversions.
static auto get_code_page_name(int code_page) -> std::string
Retrieves a textual name for a code page (e.g., "CP_ACP" or a locale-based name).

◆ system_to_utf8() [2/2]

static auto utility_module::convert_string::system_to_utf8 ( const std::string & value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Converts a system-encoded string to UTF-8.

Parameters
valueThe input string in system encoding.
Returns
A tuple of:
  • std::optional<std::string>: The UTF-8 encoded string on success.
  • std::optional<std::string>: An error message on failure.

◆ to_array() [1/2]

auto kcenon::thread::utils::convert_string::to_array ( const std::string & value) -> std::tuple<std::optional<std::vector<uint8_t>>, std::optional<std::string>>
static

Converts a system-encoded string to a UTF-8 byte array.

Parameters
valueThe input string in system encoding.
Returns
A tuple of:
  • std::optional<std::vector<uint8_t>>: The UTF-8 byte array on success.
  • std::optional<std::string>: An error message on failure.

Definition at line 433 of file convert_string.cpp.

435 {
436 auto [utf8, convert_error] = system_to_utf8(value);
437 if (convert_error.has_value())
438 {
439 return { std::nullopt, convert_error };
440 }
441
442 auto utf8_no_bom = remove_utf8_bom(utf8.value());
443
444 return { std::vector<uint8_t>(utf8_no_bom.begin(), utf8_no_bom.end()), std::nullopt };
445 }
static auto remove_utf8_bom(const std::string &value) -> std::string
Removes a leading UTF-8 BOM from a string, if present.
static auto system_to_utf8(const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
Converts a system-encoded string to UTF-8.

◆ to_array() [2/2]

static auto utility_module::convert_string::to_array ( const std::string & value) -> std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > >
static

Converts a system-encoded string to a UTF-8 byte array.

Parameters
valueThe input string in system encoding.
Returns
A tuple of:
  • std::optional<std::vector<uint8_t>>: The UTF-8 byte array on success.
  • std::optional<std::string>: An error message on failure.

◆ to_base64() [1/2]

auto kcenon::thread::utils::convert_string::to_base64 ( const std::vector< uint8_t > & value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Encodes a byte array into a Base64 string.

Parameters
valueThe raw byte array to encode.
Returns
A tuple of:
  • std::optional<std::string>: The Base64-encoded string on success.
  • std::optional<std::string>: An error message if encoding fails.

Typically, Base64 encoding should not fail unless the input is extremely large and memory allocation fails.

Definition at line 456 of file convert_string.cpp.

458 {
459 try
460 {
461 std::string encoded = base64_encode(value);
462 return { encoded, std::nullopt };
463 }
464 catch (const std::exception& e)
465 {
466 return { std::nullopt, e.what() };
467 }
468 }
static auto base64_encode(const std::vector< uint8_t > &data) -> std::string
Encodes a byte array into a Base64 string.

◆ to_base64() [2/2]

static auto utility_module::convert_string::to_base64 ( const std::vector< uint8_t > & value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Encodes a byte array into a Base64 string.

Parameters
valueThe raw byte array to encode.
Returns
A tuple of:
  • std::optional<std::string>: The Base64-encoded string on success.
  • std::optional<std::string>: An error message if encoding fails.

Typically, Base64 encoding should not fail unless the input is extremely large and memory allocation fails.

◆ to_string() [1/6]

auto kcenon::thread::utils::convert_string::to_string ( const std::vector< uint8_t > & value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Converts a UTF-8 byte array to a system-encoded string.

Parameters
valueThe input byte array.
Returns
A tuple with:
  • std::optional<std::string>: The system-encoded string on success.
  • std::optional<std::string>: An error message on failure.

Definition at line 447 of file convert_string.cpp.

449 {
450 std::string utf8(value.begin(), value.end());
451 auto utf8_no_bom = remove_utf8_bom(utf8);
452
453 return utf8_to_system(utf8);
454 }
static auto utf8_to_system(const std::string &value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
Converts a UTF-8 encoded string to the system encoding.

◆ to_string() [2/6]

static auto utility_module::convert_string::to_string ( const std::vector< uint8_t > & value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Converts a UTF-8 byte array to a system-encoded string.

Parameters
valueThe input byte array.
Returns
A tuple with:
  • std::optional<std::string>: The system-encoded string on success.
  • std::optional<std::string>: An error message on failure.

◆ to_string() [3/6]

auto kcenon::thread::utils::convert_string::to_string ( const std::wstring & value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Converts a std::wstring to a std::string using the system encoding.

Parameters
valueThe wide-string input.
Returns
A tuple of:
  • std::optional<std::string>: The converted narrow string on success, or std::nullopt on failure.
  • std::optional<std::string>: The error message if conversion fails, otherwise std::nullopt.

Uses simdutf for SIMD-accelerated UTF-8/UTF-16/UTF-32 transcoding on all platforms.

Definition at line 188 of file convert_string.cpp.

190 {
191 auto [result, error]
192 = convert<std::wstring, std::string>(value, get_wchar_encoding(endian_types::little),
193 get_encoding_name(encoding_types::utf8));
194
195 if (result.has_value())
196 {
197 return utf8_to_system(result.value());
198 }
199
200 return { std::nullopt, error };
201 }
static auto get_wchar_encoding(endian_types endian=endian_types::little) -> std::string
Derives the wchar_t encoding name based on its size and endianness.
@ error
Error events that might still allow continuation.

◆ to_string() [4/6]

static auto utility_module::convert_string::to_string ( const std::wstring & value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Converts a std::wstring to a std::string using the system encoding.

Parameters
valueThe wide-string input.
Returns
A tuple of:
  • std::optional<std::string>: The converted narrow string on success, or std::nullopt on failure.
  • std::optional<std::string>: The error message if conversion fails, otherwise std::nullopt.

Uses simdutf for SIMD-accelerated UTF-8/UTF-16/UTF-32 transcoding on all platforms.

◆ to_string() [5/6]

auto kcenon::thread::utils::convert_string::to_string ( std::wstring_view value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Converts a std::wstring_view to a std::string using the system encoding.

Parameters
valueThe wide-string view.
Returns
Same tuple semantics as to_string(const std::wstring&).

This is a convenience overload for handling string views without copying the entire wide string first.

Definition at line 203 of file convert_string.cpp.

205 {
206 auto [result, error] = convert<std::wstring_view, std::string>(
207 value, get_wchar_encoding(endian_types::little),
208 get_encoding_name(encoding_types::utf8));
209
210 if (result.has_value())
211 {
212 return utf8_to_system(result.value());
213 }
214
215 return { std::nullopt, error };
216 }

◆ to_string() [6/6]

static auto utility_module::convert_string::to_string ( std::wstring_view value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Converts a std::wstring_view to a std::string using the system encoding.

Parameters
valueThe wide-string view.
Returns
Same tuple semantics as to_string(const std::wstring&).

This is a convenience overload for handling string views without copying the entire wide string first.

◆ to_wstring() [1/4]

auto kcenon::thread::utils::convert_string::to_wstring ( const std::string & value) -> std::tuple<std::optional<std::wstring>, std::optional<std::string>>
static

Converts a std::string (system-encoded) to a std::wstring.

Parameters
valueThe narrow string in system encoding.
Returns
A tuple containing:
  • std::optional<std::wstring>: The converted wide string on success.
  • std::optional<std::string>: An error message if conversion fails.

Definition at line 218 of file convert_string.cpp.

220 {
221 auto [result, err] = system_to_utf8(value);
222 if (!result.has_value())
223 {
224 return { std::nullopt, err };
225 }
226
227 std::string clean_value = remove_utf8_bom(result.value());
228 endian_types endian = endian_types::little;
229 return convert<std::string, std::wstring>(
230 clean_value, get_encoding_name(encoding_types::utf8), get_wchar_encoding(endian));
231 }
endian_types
Possible endianness values for UTF-16 or UTF-32 data.

◆ to_wstring() [2/4]

static auto utility_module::convert_string::to_wstring ( const std::string & value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
static

Converts a std::string (system-encoded) to a std::wstring.

Parameters
valueThe narrow string in system encoding.
Returns
A tuple containing:
  • std::optional<std::wstring>: The converted wide string on success.
  • std::optional<std::string>: An error message if conversion fails.

Referenced by std::formatter< kcenon::thread::job_types, wchar_t >::format(), std::formatter< kcenon::thread::thread_conditions, wchar_t >::format(), std::formatter< kcenon::thread::thread_pool, wchar_t >::format(), std::formatter< kcenon::thread::thread_worker, wchar_t >::format(), std::formatter< kcenon::thread::typed_thread_pool_t< job_type >, wchar_t >::format(), and std::formatter< kcenon::thread::typed_thread_worker_t< job_type >, wchar_t >::format().

Here is the caller graph for this function:

◆ to_wstring() [3/4]

auto kcenon::thread::utils::convert_string::to_wstring ( std::string_view value) -> std::tuple<std::optional<std::wstring>, std::optional<std::string>>
static

Converts a std::string_view (system-encoded) to a std::wstring.

Parameters
valueThe narrow string view in system encoding.
Returns
Same tuple semantics as to_wstring(const std::string&).

Definition at line 233 of file convert_string.cpp.

235 {
236 auto [result, err] = system_to_utf8(std::string(value));
237 if (!result.has_value())
238 {
239 return { std::nullopt, err };
240 }
241
242 std::string clean_value = remove_utf8_bom(result.value());
243 endian_types endian = endian_types::little;
244 return convert<std::string, std::wstring>(
245 clean_value, get_encoding_name(encoding_types::utf8), get_wchar_encoding(endian));
246 }

◆ to_wstring() [4/4]

static auto utility_module::convert_string::to_wstring ( std::string_view value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
static

Converts a std::string_view (system-encoded) to a std::wstring.

Parameters
valueThe narrow string view in system encoding.
Returns
Same tuple semantics as to_wstring(const std::string&).

◆ utf8_to_system() [1/2]

auto kcenon::thread::utils::convert_string::utf8_to_system ( const std::string & value) -> std::tuple<std::optional<std::string>, std::optional<std::string>>
static

Converts a UTF-8 encoded string to the system encoding.

Parameters
valueThe UTF-8 encoded input string.
Returns
Same tuple semantics as system_to_utf8().

Definition at line 397 of file convert_string.cpp.

399 {
400 int code_page = get_system_code_page();
401 if (code_page == 65001)
402 {
403 return { value, std::nullopt };
404 }
405 return convert<std::string, std::string>(value, get_encoding_name(encoding_types::utf8),
406 get_code_page_name(code_page));
407 }

◆ utf8_to_system() [2/2]

static auto utility_module::convert_string::utf8_to_system ( const std::string & value) -> std::tuple< std::optional< std::string >, std::optional< std::string > >
static

Converts a UTF-8 encoded string to the system encoding.

Parameters
valueThe UTF-8 encoded input string.
Returns
Same tuple semantics as system_to_utf8().

The documentation for this class was generated from the following files: