Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
convert_string.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
13#include <tuple>
14#include <vector>
15#include <string>
16#include <cstdint>
17#include <optional>
18#include <algorithm>
19#include <string_view>
20
21namespace utility_module
22{
53 {
54 public:
67 static auto to_string(const std::wstring& value)
68 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
69
78 static auto to_string(std::wstring_view value)
79 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
80
88 static auto to_wstring(const std::string& value)
89 -> std::tuple<std::optional<std::wstring>, std::optional<std::string>>;
90
96 static auto to_wstring(std::string_view value)
97 -> std::tuple<std::optional<std::wstring>, std::optional<std::string>>;
98
106 static auto get_system_code_page() -> int;
107
115 static auto system_to_utf8(const std::string& value)
116 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
117
123 static auto utf8_to_system(const std::string& value)
124 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
125
136 static auto split(const std::string& source, const std::string& token)
137 -> std::tuple<std::optional<std::vector<std::string>>, std::optional<std::string>>;
138
146 static auto to_array(const std::string& value)
147 -> std::tuple<std::optional<std::vector<uint8_t>>, std::optional<std::string>>;
148
156 static auto to_string(const std::vector<uint8_t>& value)
157 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
158
169 static auto to_base64(const std::vector<uint8_t>& value)
170 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
171
180 static auto from_base64(const std::string& base64_str)
181 -> std::tuple<std::vector<uint8_t>, std::optional<std::string>>;
182
193 static auto replace(std::string& source,
194 const std::string& token,
195 const std::string& target) -> std::optional<std::string>;
196
207 static auto replace2(const std::string& source,
208 const std::string& token,
209 const std::string& target)
210 -> std::tuple<std::optional<std::string>, std::optional<std::string>>;
211
212 private:
214 enum class endian_types
215 {
216 little,
217 big,
218 unknown
219 };
220
222 enum class encoding_types
223 {
224 utf8,
225 utf16,
226 utf32
227 };
228
234 static auto get_code_page_name(int code_page) -> std::string;
235
242 static auto get_encoding_name(encoding_types encoding,
243 endian_types endian = endian_types::little) -> std::string;
244
250 static auto get_wchar_encoding(endian_types endian = endian_types::little) -> std::string;
251
264 template <typename FromType, typename ToType>
265 static auto convert(const FromType& value,
266 const std::string& from_encoding,
267 const std::string& to_encoding)
268 -> std::tuple<std::optional<ToType>, std::optional<std::string>>;
269
275 static auto detect_endian(const std::u16string& value) -> endian_types;
276
282 static auto detect_endian(const std::u32string& value) -> endian_types;
283
289 static auto has_utf8_bom(const std::string& value) -> bool;
290
296 static auto remove_utf8_bom(const std::string& value) -> std::string;
297
303 static auto add_utf8_bom(const std::string& value) -> std::string;
304
310 static auto base64_encode(const std::vector<uint8_t>& data) -> std::string;
311
320 static auto base64_decode(const std::string& base64_str)
321 -> std::tuple<std::vector<uint8_t>, std::optional<std::string>>;
322 };
323} // namespace utility_module
Provides utilities for string encoding conversion, Base64 encoding/decoding, and substring operations...
static auto base64_encode(const std::vector< uint8_t > &data) -> std::string
Encodes a byte array into a Base64 string.
static auto has_utf8_bom(const std::string &value) -> bool
Checks if a string has a UTF-8 BOM (Byte Order Mark).
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 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 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 get_system_code_page() -> int
Retrieves the system code page used for conversions.
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 remove_utf8_bom(const std::string &value) -> std::string
Removes a leading UTF-8 BOM from a string, if present.
encoding_types
Supported encoding types for Unicode conversion.
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 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 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 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 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 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_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 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 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.
endian_types
Possible endianness values for UTF-16 or UTF-32 data.
constexpr std::string_view to_string(test_priority priority)
Converts a test_priority value to its string representation.
Definition test_type.h:51