CommonLib
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5#include <type_traits>
6#include <vector>
7
8#include "expr/string_expr.h"
9
10namespace hedgedev::csl::ut::string
11{
12 ///
13 /// Compares two strings.
14 ///
15 /// \tparam T_left The left string type.
16 /// \tparam T_right The right string type.
17 ///
18 /// \param in_left The first string.
19 /// \param in_right The second string.
20 /// \param in_case_sensitive Determines whether the comparison should be
21 /// case sensitive.
22 ///
23 /// \returns `true` if the strings are identical. Otherwise, `false`.
24 ///
25 template <expr::any_string T_left, expr::any_string T_right>
26 inline bool compare(const T_left& in_left, const T_right& in_right, bool in_case_sensitive = true);
27
28 ///
29 /// Checks if a string contains a substring.
30 ///
31 /// \tparam T_str The input string type.
32 /// \tparam T_substr The substring type.
33 ///
34 /// \param in_str The string to check.
35 /// \param in_substr The substring to search for.
36 /// \param in_case_sensitive Determines whether the search should be case sensitive.
37 ///
38 /// \returns `true` if the input string contains the specified substring.
39 /// Otherwise, `false`.
40 ///
41 template <expr::any_string T_str, expr::any_string T_substr>
42 inline bool contains(const T_str& in_str, const T_substr& in_substr, bool in_case_sensitive = true);
43
44 ///
45 /// Escapes all instances of a character in a string with an escape sequence.
46 ///
47 /// \tparam T_str The input string type.
48 /// \tparam T_str_char The search and escape character type inferred from the input
49 /// string type.
50 ///
51 /// \param in_str The string to escape.
52 /// \param in_search_char The character to search for and escape.
53 /// \param in_escape_char The character to escape with.
54 ///
55 /// \returns The input string where all characters matching the search character
56 /// have been escaped using the escape character.
57 ///
58 template <expr::any_string T_str, typename T_str_char = expr::get_char_type_t<T_str>>
60
61 ///
62 /// Formats a string.
63 ///
64 /// \tparam T_str The input string type.
65 ///
66 /// \param in_str The string to format.
67 ///
68 /// \returns The input string with the formatters filled in.
69 ///
70 template <expr::any_string T>
71 inline expr::inferred_string_t<T> format(const T in_str, ...);
72
73 ///
74 /// Gets the width of a string.
75 ///
76 /// \tparam T_str The input string type.
77 ///
78 /// \param in_str The string to search.
79 ///
80 /// \returns The width of the string in characters determined by the longest line.
81 ///
82 template <expr::any_string T>
83 inline size_t get_width(const T& in_str);
84
85 ///
86 /// Formats a string to have a HTML hyperlink.
87 ///
88 /// \tparam T_str The input string type.
89 /// \tparam T_url The URL string type.
90 /// \tparam T_result The output string type inferred from the precedent
91 /// input strings.
92 ///
93 /// \param in_str The string to display.
94 /// \param in_url The URL to use.
95 ///
96 /// \returns The input string formatted with a HTML hyperlink with a URL target.
97 ///
98 template <expr::any_string T_str, expr::any_string T_url, typename T_result = expr::pi_string_t<T_str, T_url>>
99 inline T_result hyperlink(const T_str& in_str, const T_url& in_url);
100
101 ///
102 /// Checks if a string of any type is null or empty.
103 ///
104 /// \tparam T_str The input string type.
105 ///
106 /// \param in_str The string to check.
107 ///
108 /// \returns `true` if the string is null or empty. Otherwise, `false`.
109 ///
110 template <expr::any_string T>
111 inline bool is_null_or_empty(const T& in_str);
112
113 ///
114 /// Checks if a string of any type is null, empty, or consists only of
115 /// white-space characters.
116 ///
117 /// \tparam T_str The input string type.
118 ///
119 /// \param in_str The string to check.
120 ///
121 /// \returns `true` if the string is null or empty, or if the string consists only
122 /// of white-space characters. Otherwise, `false`.
123 ///
124 template <expr::any_string T>
125 inline bool is_null_or_whitespace(const T& in_str);
126
127 ///
128 /// Joins a collection of strings together using a delimiter.
129 ///
130 /// \tparam T_delimiter The delimiter string type.
131 /// \tparam T_strings The collection string type.
132 ///
133 /// \param in_delimiter The delimiter to join the strings with.
134 /// \param in_strings The strings to join.
135 ///
136 /// \returns The input strings joined together, separated by the delimiter.
137 ///
138 template <expr::any_string T_delimiter, expr::any_string T_strings>
140
141 ///
142 /// Joins a collection of strings together using a delimiter.
143 ///
144 /// \tparam T_delimiter The delimiter string type.
145 /// \tparam T_args The string types.
146 /// \tparam T_result The output string type inferred from the precedent
147 /// input strings.
148 ///
149 /// \param in_delimiter The delimiter to join the strings with.
150 /// \param in_args The strings to join.
151 ///
152 /// \returns The input strings joined together, separated by the delimiter.
153 ///
154 template <expr::any_string T_delimiter, expr::any_string... T_args, typename T_result = expr::pi_string_t<T_delimiter, T_args...>>
155 inline T_result join(const T_delimiter& in_delimiter, const T_args&... in_args);
156
157 ///
158 /// Pads a string with another.
159 ///
160 /// \tparam T_str The input string type.
161 /// \tparam T_pad_str The pad string type.
162 /// \tparam T_result The output string type inferred from the precedent
163 /// input strings.
164 ///
165 /// \param in_str The string to pad.
166 /// \param in_pad_str The string to pad with.
167 ///
168 /// \returns The input string padded on either side with the pad string.
169 ///
170 template <expr::any_string T_str, expr::any_string T_pad_str, typename T_result = expr::pi_string_t<T_str, T_pad_str>>
171 inline T_result pad(const T_str& in_str, const T_pad_str& in_pad_str);
172
173 ///
174 /// Parses a value from a string.
175 ///
176 /// \tparam T_result The value type to parse.
177 /// \tparam T_str The input string type.
178 ///
179 /// \param in_str The string to parse.
180 ///
181 /// \returns The value that was parsed.
182 ///
183 template <typename T_result, expr::any_string T_str>
184 inline T_result parse(const T_str& in_str);
185
186 ///
187 /// Removes XML tags from a string.
188 ///
189 /// \tparam T_str The input string type.
190 ///
191 /// \param in_str The string to omit XML tags from.
192 ///
193 /// \returns The input string with XML tags removed.
194 ///
195 template <expr::any_string T_str>
197
198 ///
199 /// Splits a string into a collection using a delimiter.
200 ///
201 /// \tparam T_str The input string type.
202 /// \tparam T_delimiter The delimiter string type.
203 /// \tparam T_result The output string type inferred from the precedent
204 /// input strings.
205 ///
206 /// \param in_str The string to split.
207 /// \param in_delimiter The delimiter to split the string with.
208 ///
209 /// \returns A collection of strings split by the delimiter.
210 ///
211 template <expr::any_string T_str, expr::any_string T_delimiter, typename T_result = expr::pi_string_t<T_str, T_delimiter>>
212 inline std::vector<T_result> split(const T_str& in_str, const T_delimiter& in_delimiter);
213
214 ///
215 /// Transforms a string to lowercase.
216 ///
217 /// \tparam T_str The input string type.
218 ///
219 /// \param in_str The string to transform to lowercase.
220 ///
221 /// \returns The input string as lowercase.
222 ///
223 template <expr::any_string T>
224 inline expr::inferred_string_t<T> lower(const T& in_str);
225
226 ///
227 /// Transforms a string to uppercase.
228 ///
229 /// \tparam T_str The input string type.
230 ///
231 /// \param in_str The string to transform to uppercase.
232 ///
233 /// \returns The input string as uppercase.
234 ///
235 template <expr::any_string T>
236 inline expr::inferred_string_t<T> upper(const T& in_str);
237
238 ///
239 /// Removes all leading occurrences of a set of characters from the input string.
240 ///
241 /// \tparam T_str The input string type.
242 /// \tparam T_trim_chars The trim character type inferred from the input string type.
243 ///
244 /// \param in_str The string to trim.
245 /// \param in_trim_chars The characters to trim off.
246 ///
247 /// \returns The input string with all occurrences of the specified characters
248 /// removed from the start.
249 ///
250 template <expr::any_string T_str, typename T_trim_chars = expr::get_char_type_t<T_str>>
252
253 ///
254 /// Removes all trailing occurrences of a set of characters from the input string.
255 ///
256 /// \tparam T_str The input string type.
257 /// \tparam T_trim_chars The trim character type inferred from the input string type.
258 ///
259 /// \param in_str The string to trim.
260 /// \param in_trim_chars The characters to trim off.
261 ///
262 /// \returns The input string with all occurrences of the specified characters
263 /// removed from the end.
264 ///
265 template <expr::any_string T_str, typename T_trim_chars = expr::get_char_type_t<T_str>>
267
268 ///
269 /// Removes all leading and trailing occurrences of a set of characters from the
270 /// input string.
271 ///
272 /// \tparam T_str The input string type.
273 /// \tparam T_trim_chars The trim character type inferred from the input string type.
274 ///
275 /// \param in_str The string to trim.
276 /// \param in_trim_chars The characters to trim off.
277 ///
278 /// \returns The input string with all occurrences of the specified characters
279 /// removed from the start and end.
280 ///
281 template <expr::any_string T_str, typename T_trim_chars = expr::get_char_type_t<T_str>>
283
284 ///
285 /// Truncates a string.
286 ///
287 /// \tparam T_str The input string type.
288 ///
289 /// \param in_str The string to truncate.
290 /// \param in_max_length The maximum length to truncate to.
291 /// \param in_truncate_end Determines whether to truncate the end of the string,
292 /// rather than the beginning.
293 /// \param in_ellipsis Determines whether to add an ellipsis to the truncated
294 /// side of the string.
295 ///
296 /// \returns The input string truncated to the specified length.
297 ///
298 template <expr::any_string T>
299 inline expr::inferred_string_t<T> truncate(const T& in_str, size_t in_max_length, bool in_truncate_end = true, bool in_ellipsis = true);
300
301 ///
302 /// Transforms a value into a hexadecimal string.
303 ///
304 /// \tparam T_result The output string type.
305 /// \tparam T_value The input value type.
306 ///
307 /// \param in_value The value to transform.
308 /// \param in_max_length The maximum number of leading zeroes to display.
309 /// \param in_prefix Determines whether to use the "0x" prefix.
310 ///
311 /// \returns The input value represented as a hexadecimal string.
312 ///
313 template <expr::basic_string T_result, typename T_value>
314 inline T_result hex(T_value in_value, size_t in_max_length = sizeof(size_t) * 2, bool in_prefix = true);
315
316 ///
317 /// Tries to parse a value from a string.
318 ///
319 /// \tparam T_result The value type to parse.
320 /// \tparam T_str The input string type.
321 ///
322 /// \param in_str The string to parse.
323 /// \param out_result The output value to set.
324 ///
325 /// \returns `true` if the parse was successful. Otherwise, `false`.
326 ///
327 template <typename T_result, expr::any_string T_str>
328 inline bool try_parse(const T_str& in_str, T_result& out_result);
329
330 ///
331 /// Wraps a string to a fixed character length on each line.
332 ///
333 /// \tparam T_str The input string type.
334 ///
335 /// \param in_str The string to wrap.
336 /// \param in_max_width The maximum width of each line.
337 ///
338 /// \returns The input string with line breaks placed around word boundaries to fit
339 /// within the maximum line width.
340 ///
341 template <expr::any_string T_str>
343}
344
345#include "string.inl"
346
347__CMNLIB_INTERNAL_MAKE_NAMESPACE_ALIAS(hedgedev::csl::ut, string, str);
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 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