CommonLib
Loading...
Searching...
No Matches
encoding.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include "expr/encoding_expr.h"
6#include "expr/string_expr.h"
8
9namespace hedgedev::csl::ut::encoding
10{
11 ///
12 /// Converts a string to a different encoding format.
13 ///
14 /// \tparam T_result The string type to convert to.
15 /// \tparam T_str The string type to convert from.
16 ///
17 /// \param in_str The string to convert.
18 /// \param in_src_encoding The encoding to convert from, inferred from the input string type.
19 /// \param in_dst_encoding The encoding to convert to, inferred from the return type.
20 ///
21 /// \returns If successful, the input string in the destination format.
22 /// Otherwise, an empty string in the destination format.
23 ///
24 template <expr::basic_string T_result, expr::any_string T_str>
26
27 ///
28 /// Converts a string to a different encoding format.
29 ///
30 /// \tparam dst_encoding The encoding to convert to.
31 /// \tparam T_str The string type to convert from.
32 /// \tparam T_result The string type to convert to, inferred from the encoding type.
33 ///
34 /// \param in_str The string to convert.
35 ///
36 /// \returns If successful, the input string in the destination format.
37 /// Otherwise, an empty string in the destination format.
38 ///
40 inline T_result convert(const T_str& in_str);
41
42 ///
43 /// Gets the byte order mark of an encoding type.
44 ///
45 /// \param in_encoding The encoding type.
46 ///
47 /// \returns An vector of bytes containing the byte order mark for the
48 /// specified encoding.
49 ///
51
52 ///
53 /// Gets the length of the byte order mark of an encoding type.
54 ///
55 /// \param in_encoding The encoding type.
56 ///
57 /// \returns The length of the byte order mark.
58 ///
60
61 ///
62 /// Gets the length of the byte order mark of an encoding type.
63 ///
64 /// \tparam encoding The encoding type.
65 ///
66 /// \returns The length of the byte order mark.
67 ///
68 template <encoding_type encoding>
69 inline constexpr size_t get_bom_length();
70
71 ///
72 /// Converts multiple strings to share the same encoding format.
73 ///
74 /// \tparam T_args The string types.
75 /// \tparam T_result The string type to convert to inferred from the string with the
76 /// largest character size.
77 ///
78 /// \param in_args The strings to convert.
79 ///
80 /// \returns An array of strings in the same destination format.
81 /// If a string failed to convert, an empty string will be put in
82 /// its place.
83 ///
84 template <expr::any_string... T_args, typename T_result = expr::pi_string_t<T_args...>>
85 inline std::array<T_result, sizeof...(T_args)> precedent_convert(const T_args&... in_args);
86
87 ///
88 /// Tries to convert a string to a different encoding format.
89 ///
90 /// \tparam T_result The string type to convert to.
91 /// \tparam T_str The string type to convert from.
92 ///
93 /// \param in_str The string to convert.
94 /// \param out_result The output string to set.
95 /// \param in_src_encoding The encoding to convert from, inferred from the input string type.
96 /// \param in_dst_encoding The encoding to convert to, inferred from the return type.
97 ///
98 /// \returns `true` if the conversion was successful. Otherwise, `false`.
99 ///
100 template <expr::basic_string T_result, expr::any_string T_str>
101 inline bool try_convert(const T_str& in_str, T_result& out_result, encoding_type in_dst_encoding = expr::get_char_encoding_type<T_result>(), encoding_type in_src_encoding = expr::get_char_encoding_type<T_str>());
102
103 ///
104 /// Tries to convert a string to a different encoding format.
105 ///
106 /// \tparam dst_encoding The encoding to convert to.
107 /// \tparam T_str The string type to convert from.
108 /// \tparam T_result The string type to convert to, inferred from the encoding type.
109 ///
110 /// \param in_str The string to convert.
111 /// \param out_result The output string to set.
112 ///
113 /// \returns `true` if the conversion was successful. Otherwise, `false`.
114 ///
116 inline bool try_convert(const T_str& in_str, T_result& out_result);
117}
118
119#include "encoding.inl"
120
121__CMNLIB_INTERNAL_MAKE_NAMESPACE_ALIAS(hedgedev::csl::ut, encoding, enc);
Definition string_expr.h:92
Definition string_expr.h:116
Definition string_expr.h:122
Definition string_expr.h:110
Definition string_expr.h:104
Definition string_expr.h:98
Definition container_expr.h:21
Definition string_expr.h:74
Definition string_expr.h:38
Definition string_expr.h:56
Definition string_expr.h:62
Definition string_expr.h:50
Definition string_expr.h:44
Definition container_expr.h:15
Definition this_process_win32.inl:13
std::filesystem::path get_executable_root()
Definition this_process.inl:7
bool is_neighbour(const std::filesystem::path &in_path)
Definition this_process.inl:12
Definition encoding.h:10
constexpr size_t get_bom_length()
Definition encoding.inl:73
constexpr size_t get_bom_length(encoding_type in_encoding)
Definition encoding.inl:53
std::array< T_result, sizeof...(T_args)> precedent_convert(const T_args &... in_args)
Definition encoding.inl:79
bool try_convert(const T_str &in_str, T_result &out_result, encoding_type in_dst_encoding=expr::get_char_encoding_type< T_result >(), encoding_type in_src_encoding=expr::get_char_encoding_type< T_str >())
Definition encoding.inl:126
std::vector< uint8_t > get_bom(encoding_type in_encoding)
Definition encoding.inl:30
T_result convert(const T_str &in_str, encoding_type in_dst_encoding=expr::get_char_encoding_type< T_result >(), encoding_type in_src_encoding=expr::get_char_encoding_type< T_str >())
Definition encoding.inl:15
encoding_type
Definition encoding_type.h:6
@ utf32_be
Definition encoding_type.h:12
@ utf16_be
Definition encoding_type.h:10
@ unknown
Definition encoding_type.h:7
@ utf32_le
Definition encoding_type.h:11
@ latin1
Definition encoding_type.h:13
@ utf8
Definition encoding_type.h:8
@ utf16_le
Definition encoding_type.h:9
typename get_encoding_char_type< encoding >::type get_encoding_char_type_t
Definition encoding_expr.h:35
constexpr bool is_char_encoding_type_v
Definition encoding_expr.h:74
constexpr bool is_all_same_v
Definition expr.h:38
constexpr bool is_same_underlying_char_type_v
Definition string_expr.h:128
constexpr encoding::encoding_type get_char_encoding_type()
Definition encoding_expr.inl:9
constexpr size_t get_string_type_precedence()
Definition string_expr.inl:23
constexpr encoding::encoding_type get_native_encoding_type(encoding::encoding_type in_encoding)
Definition encoding_expr.inl:43
constexpr T_result create_inferred_string(const T_str &in_str)
Definition string_expr.inl:6
constexpr size_t get_encoding_char_size(encoding::encoding_type in_encoding)
Definition encoding_expr.inl:71
constexpr bool is_char_encoding_type(encoding::encoding_type in_encoding)
Definition encoding_expr.inl:92
constexpr bool is_supported_template_v
Definition expr.h:46
Definition filesystem.h:6
std::error_code truncate_files_by_age(const std::filesystem::path &in_path, std::string_view in_extension, size_t in_max=10)
Definition filesystem.inl:46
bool is_neighbour(const std::filesystem::path &in_left, const std::filesystem::path &in_right)
Definition filesystem.inl:7
std::error_code truncate_files(const std::filesystem::path &in_path, std::string_view in_extension, size_t in_max, T &&in_compare_func)
Definition filesystem.inl:13
Definition string.h:11
expr::inferred_string_t< T_str > trim_start(const T_str &in_str, std::optional< std::vector< T_trim_chars > > in_trim_chars={})
Definition string.inl:326
expr::inferred_string_t< T > format(const T in_str,...)
Definition string.inl:82
expr::inferred_string_t< T > lower(const T &in_str)
Definition string.inl:288
expr::inferred_string_t< T_str > trim(const T_str &in_str, std::optional< std::vector< T_trim_chars > > in_trim_chars={})
Definition string.inl:365
bool is_null_or_empty(const T &in_str)
Definition string.inl:160
T_result hyperlink(const T_str &in_str, const T_url &in_url)
Definition string.inl:140
expr::inferred_string_t< T_str > remove_xml_tags(const T_str &in_str)
Definition string.inl:247
bool is_null_or_whitespace(const T &in_str)
Definition string.inl:172
expr::inferred_string_t< T_str > trim_end(const T_str &in_str, std::optional< std::vector< T_trim_chars > > in_trim_chars={})
Definition string.inl:345
expr::inferred_string_t< T_str > wrap(const T_str &in_str, size_t in_max_width)
Definition string.inl:505
bool compare(const T_left &in_left, const T_right &in_right, bool in_case_sensitive=true)
Definition string.inl:18
T_result join(const T_delimiter &in_delimiter, const T_args &... in_args)
Definition string.inl:214
std::vector< T_result > split(const T_str &in_str, const T_delimiter &in_delimiter)
Definition string.inl:257
expr::inferred_string_t< T > upper(const T &in_str)
Definition string.inl:307
bool try_parse(const T_str &in_str, T_result &out_result)
Definition string.inl:447
expr::inferred_string_t< T_str > escape(const T_str &in_str, T_str_char in_search_char, T_str_char in_escape_char=T_str_char('\\'))
Definition string.inl:66
bool contains(const T_str &in_str, const T_substr &in_substr, bool in_case_sensitive=true)
Definition string.inl:35
T_result pad(const T_str &in_str, const T_pad_str &in_pad_str)
Definition string.inl:222
T_result hex(T_value in_value, size_t in_max_length=sizeof(size_t) *2, bool in_prefix=true)
Definition string.inl:420
T_result parse(const T_str &in_str)
Definition string.inl:237
size_t get_width(const T &in_str)
Definition string.inl:126
expr::inferred_string_t< T > truncate(const T &in_str, size_t in_max_length, bool in_truncate_end=true, bool in_ellipsis=true)
Definition string.inl:371
expr::inferred_string_t< T_strings > join(const T_delimiter &in_delimiter, const std::vector< T_strings > &in_strings)
Definition string.inl:186
Definition expr.h:7
Definition string_expr.h:19
char type
Definition encoding_expr.h:11
Definition string_expr.h:65