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 <vector>
6
7namespace hedgedev::csl::ut::string
8{
9 ///
10 /// Compares two strings.
11 ///
12 /// \param in_rLeft The first string.
13 /// \param in_rRight The second string.
14 /// \param in_isCaseSensitive Determines whether the comparison should be case sensitive.
15 ///
16 /// \returns `true` if the strings are identical. Otherwise, `false`.
17 ///
18 template <expr::AnyString TLeft, expr::AnyString TRight>
19 inline bool Compare(const TLeft& in_rLeft, const TRight& in_rRight, bool in_isCaseSensitive = true);
20
21 ///
22 /// Checks if a string contains a substring.
23 ///
24 /// \param in_rStr The string to check.
25 /// \param in_rSubStr The substring to search for.
26 /// \param in_isCaseSensitive Determines whether the search should be case sensitive.
27 ///
28 /// \returns `true` if the input string contains the specified substring. Otherwise, `false`.
29 ///
30 template <expr::AnyString TString, expr::AnyString TSubString>
31 inline bool Contains(const TString& in_rStr, const TSubString& in_rSubStr, bool in_isCaseSensitive = true);
32
33 ///
34 /// Converts a string to a different encoding format.
35 ///
36 /// \tparam TDst The string type to convert to.
37 /// \tparam TSrc The string type to convert from.
38 ///
39 /// \param in_rStr The string to convert.
40 /// \param out_rDst The output string to set.
41 ///
42 /// \returns If successful, the input string in the destination format.
43 /// Otherwise, an empty string in the destination format.
44 ///
45 template <expr::AnyString TDst, expr::AnyString TSrc>
47
48 ///
49 /// Escapes all instances of a character in a string with an escape sequence.
50 ///
51 /// \tparam TString The input string type.
52 /// \tparam TChar The search and escape character type inferred from the input string type.
53 ///
54 /// \param in_rStr The string to escape.
55 /// \param in_searchChar The character to search for and escape.
56 /// \param in_escapeChar The character to escape with.
57 ///
58 /// \returns The input string where all characters matching the search character have been escaped using the escape character.
59 ///
60 template <expr::AnyString TString, typename TChar = expr::GetCharType_t<TString>>
62
63 ///
64 /// Formats a string.
65 ///
66 /// \tparam TString The input string type.
67 ///
68 /// \param in_str The string to format.
69 ///
70 /// \returns The input string with the formatters filled in.
71 ///
72 template <expr::AnyString TString>
73 inline expr::InferredString<TString> Format(const TString in_str, ...);
74
75 ///
76 /// Gets the width of a string.
77 ///
78 /// \tparam TString The input string type.
79 ///
80 /// \param in_rStr The string to search.
81 ///
82 /// \returns The width of the string in characters determined by the longest line.
83 ///
84 template <expr::AnyString TString>
85 inline size_t GetWidth(const TString& in_rStr);
86
87 ///
88 /// Formats a string to have a HTML hyperlink.
89 ///
90 /// \tparam TString The input string type.
91 ///
92 /// \param in_rStr The string to display.
93 /// \param in_rUrl The URL to use.
94 ///
95 /// \returns The input string formatted with a HTML hyperlink with a URL target.
96 ///
97 template <expr::AnyString TString, expr::AnyString TUrl, typename TOut = expr::PrecedentInferredString<TString, TUrl>>
98 inline TOut Hyperlink(const TString& in_rStr, const TUrl& in_rUrl);
99
100 ///
101 /// Joins a collection of strings together using a delimiter.
102 ///
103 /// \tparam TDelimiter The delimiter string type.
104 /// \tparam TCollection The collection string type.
105 ///
106 /// \param in_pDelimiter The delimiter to join the strings with.
107 /// \param in_rStrings The strings to join.
108 ///
109 /// \returns The input strings joined together, separated by the delimiter.
110 ///
111 template <expr::AnyString TDelimiter, expr::AnyString TCollection>
113
114 ///
115 /// Joins a collection of strings together using a delimiter.
116 ///
117 /// \tparam TDelimiter The delimiter string type.
118 /// \tparam TArgs The string types.
119 ///
120 /// \param in_pDelimiter The delimiter to join the strings with.
121 /// \param in_rArgs The strings to join.
122 ///
123 /// \returns The input strings joined together, separated by the delimiter.
124 ///
125 template <expr::AnyString TDelimiter, expr::AnyString... TArgs, typename TOut = expr::PrecedentInferredString<TDelimiter, TArgs...>>
126 inline TOut Join(const TDelimiter& in_rDelimiter, const TArgs&... in_rArgs);
127
128 ///
129 /// Pads a string with another.
130 ///
131 /// \tparam TString The input string type.
132 /// \tparam TPadString The pad string type.
133 ///
134 /// \param in_rStr The string to pad.
135 /// \param in_rPadStr The string to pad with.
136 ///
137 /// \returns The input string padded on either side with the pad string.
138 ///
139 template <expr::AnyString TString, expr::AnyString TPadString, typename TOut = expr::PrecedentInferredString<TString, TPadString>>
140 inline TOut Pad(const TString& in_rStr, const TPadString& in_rPadStr);
141
142 ///
143 /// Parses a value from a string.
144 ///
145 /// \tparam TOut The value type to parse.
146 /// \tparam TString The input string type.
147 ///
148 /// \param in_rStr The string to parse.
149 ///
150 /// \returns The value that was parsed.
151 ///
152 template <typename TOut, expr::AnyString TString>
153 inline TOut Parse(const TString& in_rStr);
154
155 ///
156 /// Converts multiple strings to share the same encoding format.
157 ///
158 /// \tparam TArgs The string types.
159 /// \tparam TOut The string type to convert to inferred from the string with the largest character size.
160 ///
161 /// \param in_rArgs The strings to convert.
162 ///
163 /// \returns An array of strings in the same destination format.
164 /// If a string failed to convert, an empty string will be put in its place.
165 ///
166 template <expr::AnyString... TArgs, typename TOut = expr::PrecedentInferredString<TArgs...>>
167 inline std::array<TOut, sizeof...(TArgs)> PrecedentConvert(const TArgs&... in_rArgs);
168
169 ///
170 /// Omits XML tags from a string.
171 ///
172 /// \tparam TString The input string type.
173 ///
174 /// \param in_str The string to omit XML tags from.
175 ///
176 /// \returns The input string with XML tags removed.
177 ///
178 template <expr::AnyString TString>
180
181 ///
182 /// Splits a string into a collection using a delimiter.
183 ///
184 /// \tparam TString The input string type.
185 /// \tparam TDelimiter The delimiter string type.
186 ///
187 /// \param in_rStr The string to split.
188 /// \param in_rDelimiter The delimiter to split the string with.
189 ///
190 /// \returns A collection of strings split by the delimiter.
191 ///
192 template <expr::AnyString TString, expr::AnyString TDelimiter, typename TOut = expr::PrecedentInferredString<TString, TDelimiter>>
193 inline std::vector<TOut> Split(const TString& in_rStr, const TDelimiter& in_rDelimiter);
194
195 ///
196 /// Transforms a string to lowercase.
197 ///
198 /// \tparam TString The input string type.
199 ///
200 /// \param in_rStr The string to transform to lowercase.
201 ///
202 /// \returns The input string as lowercase.
203 ///
204 template <expr::AnyString TString>
206
207 ///
208 /// Transforms a string to uppercase.
209 ///
210 /// \tparam TString The input string type.
211 ///
212 /// \param in_rStr The string to transform to uppercase.
213 ///
214 /// \returns The input string as uppercase.
215 ///
216 template <expr::AnyString TString>
218
219 ///
220 /// Removes all leading occurrences of a set of characters from the input string.
221 ///
222 /// \tparam TString The input string type.
223 /// \tparam TChar The trim character type inferred from the input string type.
224 ///
225 /// \param in_rStr The string to trim.
226 /// \param in_trimChars The characters to trim off.
227 ///
228 /// \returns The input string with all occurrences of the specified characters removed from the start.
229 ///
230 template <expr::AnyString TString, typename TChar = expr::GetCharType_t<TString>>
232
233 ///
234 /// Removes all trailing occurrences of a set of characters from the input string.
235 ///
236 /// \tparam TString The input string type.
237 /// \tparam TChar The trim character type inferred from the input string type.
238 ///
239 /// \param in_rStr The string to trim.
240 /// \param in_trimChars The characters to trim off.
241 ///
242 /// \returns The input string with all occurrences of the specified characters removed from the end.
243 ///
244 template <expr::AnyString TString, typename TChar = expr::GetCharType_t<TString>>
246
247 ///
248 /// Removes all leading and trailing occurrences of a set of characters from the input string.
249 ///
250 /// \tparam TString The input string type.
251 /// \tparam TChar The trim character type inferred from the input string type.
252 ///
253 /// \param in_rStr The string to trim.
254 /// \param in_trimChars The characters to trim off.
255 ///
256 /// \returns The input string with all occurrences of the specified characters removed from the start and end.
257 ///
258 template <expr::AnyString TString, typename TChar = expr::GetCharType_t<TString>>
260
261 ///
262 /// Truncates a string.
263 ///
264 /// \tparam TString The input string type.
265 ///
266 /// \param in_rStr The string to truncate.
267 /// \param in_maxLength The maximum length to truncate to.
268 /// \param in_truncateEnd Determines whether to truncate the end of the string, rather than the beginning.
269 /// \param in_ellipsis Determines whether to add an ellipsis to the truncated side of the string.
270 ///
271 /// \returns The input string truncated to the specified length.
272 ///
273 template <expr::AnyString TString>
275
276 ///
277 /// Transforms a value into a hexadecimal string.
278 ///
279 /// \tparam TString The output string type.
280 /// \tparam TValue The input value type.
281 ///
282 /// \param in_value The value to transform.
283 /// \param in_maxLength The maximum number of leading zeroes to display.
284 /// \param in_prefix Determines whether to use the "0x" prefix.
285 ///
286 /// \returns The input value represented as a hexadecimal string.
287 ///
288 template <expr::BasicString TString, typename TValue>
289 inline TString ToHex(TValue in_value, size_t in_maxLength = sizeof(size_t) * 2, bool in_prefix = true);
290
291 ///
292 /// Tries to convert a string to a different encoding format.
293 ///
294 /// \tparam TDst The string type to convert to.
295 /// \tparam TSrc The string type to convert from.
296 ///
297 /// \param in_rSrc The string to convert.
298 /// \param out_rDst The output string to set.
299 ///
300 /// \returns `true` if the conversion was successful. Otherwise, `false`.
301 ///
302 template <expr::AnyString TDst, expr::AnyString TSrc>
303 inline bool TryConvert(const TSrc& in_rSrc, TDst& out_rDst);
304
305 ///
306 /// Tries to parse a value from a string.
307 ///
308 /// \tparam TOut The value type to parse.
309 /// \tparam TString The input string type.
310 ///
311 /// \param in_rStr The string to parse.
312 /// \param out_rValue The output value to set.
313 ///
314 /// \returns `true` if the parse was successful. Otherwise, `false`.
315 ///
316 template <typename TOut, expr::AnyString TString>
317 inline bool TryParse(const TString& in_rStr, TOut& out_rValue);
318
319 ///
320 /// Wraps a string to a fixed character length on each line.
321 ///
322 /// \tparam TString The input string type.
323 ///
324 /// \param in_rStr The string to wrap.
325 /// \param in_maxWidth The maximum width of each line.
326 ///
327 /// \returns The input string with line breaks placed around word
328 /// boundaries to fit within the maximum line width.
329 ///
330 template <expr::AnyString TString>
332}
333
334#include "String.inl"
335
336__CMNLIB_INTERNAL_MAKE_NAMESPACE_ALIAS(hedgedev::csl::ut, string, str);
#define __CMNLIB_INTERNAL_MAKE_NAMESPACE_ALIAS(PARENT, NAME, ALIAS)
Definition CommonLib.h:6
#define ASSERT_RETURN(CONDITION, RETURN_VALUE)
Definition Preprocessor.h:17
Definition StringTypes.h:89
Definition StringTypes.h:83
Definition StringTypes.h:77
Definition StringTypes.h:59
Definition StringTypes.h:65
Definition StringTypes.h:71
bool IsNeighbour(const std::filesystem::path &in_rPath)
Definition ThisProcess.inl:10
std::filesystem::path GetExecutableRoot()
Definition ThisProcess.inl:5
Definition ThisProcess.inl:4
constexpr bool HasFlag(T in_mask, T in_flag)
Definition Expressions.inl:4
constexpr bool IsSameUnderlyingCharType
Definition StringTypes.h:95
std::tuple_element_t< Index, std::tuple< TArgs... > > GetPackType
Definition Expressions.h:9
constexpr bool IsBasicString_v
Definition StringTypes.h:47
constexpr TDst CreateInferredString(const TSrc &in_rStr)
Definition StringTypes.inl:4
constexpr size_t GetStringTypePrecedence()
Definition StringTypes.inl:12
constexpr bool IsBasicStringView_v
Definition StringTypes.h:53
constexpr bool IsAllSame
Definition Expressions.h:15
Definition String.h:8
size_t GetWidth(const TString &in_rStr)
Definition String.inl:133
bool Contains(const TString &in_rStr, const TSubString &in_rSubStr, bool in_isCaseSensitive=true)
Definition String.inl:26
TString ToHex(TValue in_value, size_t in_maxLength=sizeof(size_t) *2, bool in_prefix=true)
Definition String.inl:427
std::vector< TOut > Split(const TString &in_rStr, const TDelimiter &in_rDelimiter)
Definition String.inl:283
std::array< TOut, sizeof...(TArgs)> PrecedentConvert(const TArgs &... in_rArgs)
Definition String.inl:228
expr::InferredString< TString > Escape(const TString &in_rStr, TChar in_searchChar, TChar in_escapeChar=TChar('\\'))
Definition String.inl:74
expr::InferredString< TString > TrimEnd(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:361
TOut Pad(const TString &in_rStr, const TPadString &in_rPadStr)
Definition String.inl:203
TOut Parse(const TString &in_rStr)
Definition String.inl:218
TOut Hyperlink(const TString &in_rStr, const TUrl &in_rUrl)
Definition String.inl:147
expr::InferredString< TCollection > Join(const TDelimiter &in_rDelimiter, const std::vector< TCollection > &in_rStrings)
Definition String.inl:167
bool TryParse(const TString &in_rStr, TOut &out_rValue)
Definition String.inl:516
TOut Join(const TDelimiter &in_rDelimiter, const TArgs &... in_rArgs)
Definition String.inl:195
bool Compare(const TLeft &in_rLeft, const TRight &in_rRight, bool in_isCaseSensitive=true)
Definition String.inl:9
expr::InferredString< TString > Truncate(const TString &in_rStr, size_t in_maxLength, bool in_truncateEnd=true, bool in_ellipsis=true)
Definition String.inl:387
expr::InferredString< TString > RemoveXmlTags(const TString &in_rStr)
Definition String.inl:275
expr::InferredString< TString > Wrap(const TString &in_rStr, size_t in_maxWidth)
Definition String.inl:546
expr::InferredString< TString > ToLower(const TString &in_rStr)
Definition String.inl:314
expr::InferredString< TString > Format(const TString in_str,...)
Definition String.inl:90
expr::InferredString< TString > TrimStart(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:342
bool TryConvert(const TSrc &in_rSrc, TDst &out_rDst)
Definition String.inl:447
expr::InferredString< TString > ToUpper(const TString &in_rStr)
Definition String.inl:328
expr::InferredString< TString > Trim(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:381
Definition Expressions.inl:2
Definition StringTypes.h:11
Definition StringTypes.h:26