Fixposition SDK 0.0.0-heads/main-0-g6592994
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: String utilities
12 *
13 * @page FPSDK_COMMON_STRING String utilities
14 *
15 * **API**: fpsdk_common/string.hpp and fpsdk::common::string
16 *
17 */
18#ifndef __FPSDK_COMMON_STRING_HPP__
19#define __FPSDK_COMMON_STRING_HPP__
20
21/* LIBC/STL */
22#include <cinttypes>
23#include <cstdarg>
24#include <cstdint>
25#include <functional>
26#include <string>
27#include <vector>
28
29/* EXTERNAL */
30
31/* PACKAGE */
32
33namespace fpsdk {
34namespace common {
35/**
36 * @brief String utilities
37 */
38namespace string {
39/* ****************************************************************************************************************** */
40
41/**
42 * @brief Helper macro for marking functions as taking printf() style formatting strings
43 *
44 * This helps the compiler to check that the number and types of the variable arguments match the format string.
45 */
46#ifndef PRINTF_ATTR
47# define PRINTF_ATTR(n) __attribute__((format(printf, n, n + 1)))
48#endif
49
50/**
51 * @brief Format string
52 *
53 * @param[in] fmt printf() style format string
54 * @param[in] ... Arguments to the format string
55 *
56 * @returns the formatted string
57 */
58std::string Sprintf(const char* const fmt, ...) PRINTF_ATTR(1);
59
60/**
61 * @brief Format string
62 *
63 * @param[in] fmt printf() style format string
64 * @param[in] args Arguments list to the format string
65 *
66 * @returns the formatted string
67 */
68std::string Vsprintf(const char* fmt, va_list args);
69
70/**
71 * @brief Format time
72 *
73 * @param[in] fmt Format, see strftime(3), can be NULL for a default "yyyy-mm-dd hh:mm:ss" format
74 * @param[in] ts Posix timestamp [s] (time_t), or 0 for "now"
75 * @param[in] utc Format as UTC (true) or localtime (false, default)
76 *
77 * @returns a string with the formatted time
78 */
79std::string Strftime(const char* const fmt, const int64_t ts = 0, const bool utc = false);
80
81/**
82 * @brief Search and replace
83 *
84 * @param[in,out] str The string to search and replace in
85 * @param[in] search The search term
86 * @param[in] replace The replacement
87 * @param[in] max Maximum number of replacements to do (or <= 0 for unlimited)
88 *
89 * @returns the number of matches
90 */
91int StrReplace(std::string& str, const std::string& search, const std::string& replace, const int max = 0);
92
93/**
94 * @brief Trim string left
95 *
96 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the left
97 */
98void StrTrimLeft(std::string& str);
99
100/**
101 * @brief Trim string right
102 *
103 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the right
104 */
105void StrTrimRight(std::string& str);
106
107/**
108 * @brief Trim string left and right
109 *
110 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the left and the right
111 */
112void StrTrim(std::string& str);
113
114/**
115 * @brief Split string
116 *
117 * Splits a string using a separator string. All parts, including empty ones, are returned. An empty separator leads to
118 * only one part, that is equal to the input string, unless that is empty, in which case an empty vector is returned.
119 *
120 * Examples:
121 *
122 * "foo,bar,baz" separated by "," --> [ "foo", "bar", "baz" ]
123 * "foo,bar,baz" separated by "," (max=2) --> [ "foo", "bar,baz" ]
124 * "foo,,baz,,," separated by "," --> [ "foo", "", "baz", "", "", "" ]
125 * "foo,bar,baz" separated by "" --> [ "foo,bar,baz" ]
126 * "" separated by "something" --> [ ]
127 * "" separated by "" --> [ ]
128 *
129 * @param[in] str The string
130 * @param[in] sep The separator, empty string is allowed
131 * @param[in] maxNum The maximum number of parts, or 0 (default) for as many as necessary
132 *
133 * @returns a vector with all parts
134 */
135std::vector<std::string> StrSplit(const std::string& str, const std::string& sep, const int maxNum = 0);
136
137/**
138 * @brief Join strings
139 *
140 * @param[in] strs List of strings to join
141 * @param[in] sep Separator
142 *
143 * @returns a string of all given parts separated by the given separator
144 */
145std::string StrJoin(const std::vector<std::string>& strs, const std::string& sep);
146
147/**
148 * @brief Map strings
149 *
150 * @param[in] strs List of strings to map
151 * @param[in] map Map function
152 *
153 * @returns the mapped list of strings
154 */
155std::vector<std::string> StrMap(
156 const std::vector<std::string>& strs, std::function<std::string(const std::string&)> map);
157
158/**
159 * @brief Remove duplicates
160 *
161 * @param[in] strs List of strings
162 */
163void MakeUnique(std::vector<std::string>& strs);
164
165/**
166 * @brief Format hexdump of data
167 *
168 * @param[in] data The data
169 *
170 * @returns one or more lines (strings) with a hexdump of the data
171 */
172std::vector<std::string> HexDump(const std::vector<uint8_t> data);
173
174/**
175 * @brief Format hexdump of data
176 *
177 * @param[in] data The data
178 * @param[in] size The size of the data
179 *
180 * @returns one or more lines (strings) with a hexdump of the data
181 */
182std::vector<std::string> HexDump(const uint8_t* data, const std::size_t size);
183
184/**
185 * @brief Format value to "bits string"
186 *
187 * @param[in] value The value
188 * @param[in] size The number of bits to format (clamped to 1-64)
189 *
190 * @returns a "0b...." string with the bits formatted as '0' and '1'
191 */
192std::string StrFormatBits(const uint64_t value, const std::size_t size = 64);
193
194/**
195 * @brief Check if one string starts with another string
196 *
197 * @param[in] str String to check
198 * @param[in] prefix Prefix to check, length must be >= length of str
199 *
200 * @returns true if string starts with the prefix, false otherwise (no match or empty string(s))
201 */
202bool StrStartsWith(const std::string& str, const std::string& prefix);
203
204/**
205 * @brief Check if one string ends with another string
206 *
207 * @param[in] str String to check
208 * @param[in] suffix Prefix to check, length must be >= length of str
209 *
210 * @returns true if string ends with the suffix, false otherwise (no match or empty string(s))
211 */
212bool StrEndsWith(const std::string& str, const std::string& suffix);
213
214/**
215 * @brief Check if one string is contained within another string string
216 *
217 * @param[in] str String to check
218 * @param[in] sub String to find
219 *
220 * @returns true if string contains the substriong, false otherwise(no match or empty string(s))
221 */
222bool StrContains(const std::string& str, const std::string& sub);
223
224/**
225 * @brief Convert string to value (int8_t)
226 *
227 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
228 * target value type result in a failure (return false). The output value is only modified on success.
229 *
230 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT8_MIN..INT8_MAX
231 * @param[out] value The value
232 *
233 * @returns true if the string could be converted, false otherwise
234 */
235bool StrToValue(const std::string& str, int8_t& value);
236
237/**
238 * @brief Convert string to value (uint8_t)
239 *
240 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
241 * target value type result in a failure (return false). The output value is only modified on success.
242 *
243 * @param[in] str The string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT8_MAX
244 * @param[out] value The value
245 *
246 * @returns true if the string could be converted, false otherwise
247 */
248bool StrToValue(const std::string& str, uint8_t& value);
249
250/**
251 * @brief Convert string to value (int16_t)
252 *
253 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
254 * target value type result in a failure (return false). The output value is only modified on success.
255 *
256 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT16_MIN..INT16_MAX
257 * @param[out] value The value
258 *
259 * @returns true if the string could be converted, false otherwise
260 */
261bool StrToValue(const std::string& str, int16_t& value);
262
263/**
264 * @brief Convert string to value (uint16_t)
265 *
266 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
267 * target value type result in a failure (return false). The output value is only modified on success.
268 *
269 * @param[in] str The string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT16_MAX
270 * @param[out] value The value
271 *
272 * @returns true if the string could be converted, false otherwise
273 */
274bool StrToValue(const std::string& str, uint16_t& value);
275
276/**
277 * @brief Convert string to value (int32_t)
278 *
279 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
280 * target value type result in a failure (return false). The output value is only modified on success.
281 *
282 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT32_MIN..INT32_MAX
283 * @param[out] value The value
284 *
285 * @returns true if the string could be converted, false otherwise
286 */
287bool StrToValue(const std::string& str, int32_t& value);
288
289/**
290 * @brief Convert string to value (uint32_t)
291 *
292 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
293 * target value type result in a failure (return false). The output value is only modified on success.
294 *
295 * @param[in] str The string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT32_MAX
296 * @param[out] value The value
297 *
298 * @returns true if the string could be converted, false otherwise
299 */
300bool StrToValue(const std::string& str, uint32_t& value);
301
302/**
303 * @brief Convert string to value (int64_t, long)
304 *
305 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
306 * target value type result in a failure (return false). The output value is only modified on success.
307 *
308 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: (INT64_MIN+1)..(INT64_MAX-1)
309 * @param[out] value The value
310 *
311 * @returns true if the string could be converted, false otherwise
312 */
313bool StrToValue(const std::string& str, int64_t& value);
314
315/**
316 * @brief Convert string to value (uint64_t, unsigned long)
317 *
318 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
319 * target value type result in a failure (return false). The output value is only modified on success.
320 *
321 * @param[in] str The string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..(INT64_MAX-1)
322 * @param[out] value The value
323 *
324 * @returns true if the string could be converted, false otherwise
325 */
326bool StrToValue(const std::string& str, uint64_t& value);
327
328/**
329 * @brief Convert string to value (float)
330 *
331 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
332 * target value type result in a failure (return false). The output value is only modified on success.
333 *
334 * @param[in] str The string, anything %f understands (but not infinite or NaN)
335 * @param[out] value The value
336 *
337 * @returns true if the string could be converted, false otherwise
338 */
339bool StrToValue(const std::string& str, float& value);
340
341/**
342 * @brief Convert string to value (double)
343 *
344 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
345 * target value type result in a failure (return false). The output value is only modified on success.
346 *
347 * @param[in] str The string, anything %f understands (but not infinite or NaN)
348 * @param[out] value The value
349 *
350 * @returns true if the string could be converted, false otherwise
351 */
352bool StrToValue(const std::string& str, double& value);
353
354/**
355 * @brief Convert string to value (bool)
356 *
357 * The following strings (case insensitive) are considered true resp. false:
358 * - true: "true", "yes", "1"
359 * - false: "false", "no", "0"
360 *
361 * @note White-space or other spurious characters in the string result in a failure (return false). The output value is
362 * only modified on success.
363 *
364 * @param[in] str The string, anything %f understands (but not infinite or NaN)
365 * @param[out] value The value
366 *
367 * @returns true if the string could be converted, false otherwise
368 */
369bool StrToValue(const std::string& str, bool& value);
370
371/**
372 * @brief Convert string to all upper case
373 *
374 * @param[in] str The string
375 *
376 * @returns the string converted to all upper case
377 */
378std::string StrToUpper(const std::string& str);
379
380/**
381 * @brief Convert string to all lower case
382 *
383 * @param[in] str The string
384 *
385 * @returns the string converted to all lower case
386 */
387std::string StrToLower(const std::string& str);
388
389/**
390 * @brief Stringify glibc error
391 *
392 * This works much like strerror(3), but is a bit more detailed adding the errnum value and name to the description. For
393 * example: StrError(EAGAIN) returns "Resource temporarily unavailable (11, EAGAIN)".
394 *
395 * @param[in] errnum The error number, see errno(3)
396 *
397 * @returns a string describing the error
398 */
399std::string StrError(const int errnum);
400
401/**
402 * @brief Stringify value (bool)
403 *
404 * @param[in] value The value
405 *
406 * @returns a stringification of the value
407 */
408constexpr const char* ToStr(const bool value)
409{
410 return value ? "true" : "false";
411}
412
413/**
414 * @brief Convert string to buffer
415 *
416 * @param[in] str The string
417 *
418 * @returns a buffer with the string's data
419 */
420std::vector<uint8_t> StrToBuf(const std::string& str);
421
422/**
423 * @brief Convert buffer to string
424 *
425 * @param[in] buf The buffer
426 *
427 * @returns a string with the buffer's data
428 */
429std::string BufToStr(const std::vector<uint8_t>& buf);
430
431/**
432 * @brief Encode to base64
433 *
434 * @param[in] buf The data to encode
435 *
436 * @returns the base64 encoded data
437 */
438std::string Base64Enc(const std::vector<uint8_t>& buf);
439
440/**
441 * @brief Decode from base64
442 *
443 * @param[in] str The base64 encoded data
444 *
445 * @returns the decoded data
446 */
447std::vector<uint8_t> Base64Dec(const std::string& str);
448
449/* ****************************************************************************************************************** */
450} // namespace string
451} // namespace common
452} // namespace fpsdk
453#endif // __FPSDK_COMMON_STRING_HPP__
String utilities.
Definition string.hpp:38
std::string Vsprintf(const char *fmt, va_list args)
Format string.
std::string Strftime(const char *const fmt, const int64_t ts=0, const bool utc=false)
Format time.
void StrTrimLeft(std::string &str)
Trim string left.
std::vector< std::string > StrSplit(const std::string &str, const std::string &sep, const int maxNum=0)
Split string.
std::string StrToUpper(const std::string &str)
Convert string to all upper case.
std::vector< std::string > HexDump(const std::vector< uint8_t > data)
Format hexdump of data.
std::vector< uint8_t > StrToBuf(const std::string &str)
Convert string to buffer.
void StrTrimRight(std::string &str)
Trim string right.
std::vector< std::string > StrMap(const std::vector< std::string > &strs, std::function< std::string(const std::string &)> map)
Map strings.
void StrTrim(std::string &str)
Trim string left and right.
constexpr const char * ToStr(const bool value)
Stringify value (bool)
Definition string.hpp:408
std::string StrToLower(const std::string &str)
Convert string to all lower case.
std::vector< uint8_t > Base64Dec(const std::string &str)
Decode from base64.
bool StrToValue(const std::string &str, int8_t &value)
Convert string to value (int8_t)
bool StrEndsWith(const std::string &str, const std::string &suffix)
Check if one string ends with another string.
std::string StrFormatBits(const uint64_t value, const std::size_t size=64)
Format value to "bits string".
bool StrContains(const std::string &str, const std::string &sub)
Check if one string is contained within another string string.
std::string Sprintf(const char *const fmt,...) PRINTF_ATTR(1)
Format string.
bool StrStartsWith(const std::string &str, const std::string &prefix)
Check if one string starts with another string.
std::string StrJoin(const std::vector< std::string > &strs, const std::string &sep)
Join strings.
std::string StrError(const int errnum)
Stringify glibc error.
int StrReplace(std::string &str, const std::string &search, const std::string &replace, const int max=0)
Search and replace.
std::string BufToStr(const std::vector< uint8_t > &buf)
Convert buffer to string.
void MakeUnique(std::vector< std::string > &strs)
Remove duplicates.
std::string Base64Enc(const std::vector< uint8_t > &buf)
Encode to base64.
Fixposition SDK: Common library.
Definition doc.hpp:21
Fixposition SDK.
#define PRINTF_ATTR(n)
Helper macro for marking functions as taking printf() style formatting strings.
Definition string.hpp:47