Fixposition SDK 0.0.0-heads/main-0-g7b59b93
Collection of c++ libraries and apps for use with Fixposition products
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 <functional>
25#include <string>
26#include <vector>
27
28/* EXTERNAL */
29
30/* PACKAGE */
31
32namespace fpsdk {
33namespace common {
34/**
35 * @brief String utilities
36 */
37namespace string {
38/* ****************************************************************************************************************** */
39
40/**
41 * @brief Helper macro for marking functions as taking printf() style formatting strings
42 *
43 * This helps the compiler to check that the number and types of the variable arguments match the format string.
44 */
45#ifndef PRINTF_ATTR
46# define PRINTF_ATTR(n) __attribute__((format(printf, n, n + 1)))
47#endif
48
49/**
50 * @brief Format string
51 *
52 * @param[in] fmt printf() style format string
53 * @param[in] ... Arguments to the format string
54 *
55 * @returns the formatted string
56 */
57std::string Sprintf(const char* const fmt, ...) PRINTF_ATTR(1);
58
59/**
60 * @brief Format string
61 *
62 * @param[in] fmt printf() style format string
63 * @param[in] args Arguments list to the format string
64 *
65 * @returns the formatted string
66 */
67std::string Vsprintf(const char* fmt, va_list args);
68
69/**
70 * @brief Format time
71 *
72 * @param[in] fmt Format, see strftime(3), can be NULL for a default "yyyy-mm-dd hh:mm:ss" format
73 * @param[in] ts Posix timestamp [s] (time_t), or 0 for "now"
74 * @param[in] utc Format as UTC (true) or localtime (false, default)
75 *
76 * @returns a string with the formatted time
77 */
78std::string Strftime(const char* const fmt, const int64_t ts = 0, const bool utc = false);
79
80/**
81 * @brief Search and replace
82 *
83 * @param[in,out] str The string to search and replace in
84 * @param[in] search The search term
85 * @param[in] replace The replacement
86 * @param[in] max Maximum number of replacements to do (or <= 0 for unlimited)
87 *
88 * @returns the number of matches
89 */
90int StrReplace(std::string& str, const std::string& search, const std::string& replace, const int max = 0);
91
92/**
93 * @brief Trim string left
94 *
95 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the left
96 */
97void StrTrimLeft(std::string& str);
98
99/**
100 * @brief Trim string right
101 *
102 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the right
103 */
104void StrTrimRight(std::string& str);
105
106/**
107 * @brief Trim string left and right
108 *
109 * @param[in,out] str The string with all whitespace (" ", \\t, \\r, \\n) removed on the left and the right
110 */
111void StrTrim(std::string& str);
112
113/**
114 * @brief Split string
115 *
116 * Splits a string using a separator string. All parts, including empty ones, are returned. An empty separator
117 * leads to only one part, that is equal to the input string.
118 *
119 * Examples:
120 * "foo,bar,baz" separated by "," --> [ "foo", "bar", "baz" ]
121 * "foo,bar,baz" separated by "," (max=2) --> [ "foo", "bar,baz" ]
122 * "foo,,baz,,," separated by "," --> [ "foo", "", "baz", "", "", "" ]
123 *
124 * @param[in] str The string
125 * @param[in] sep The separator, empty string is allowed
126 * @param[in] maxNum The maximum number of parts, or 0 (default) for as many as necessary
127 *
128 * @returns a vector with all parts
129 */
130std::vector<std::string> StrSplit(const std::string& str, const std::string& sep, const int maxNum = 0);
131
132/**
133 * @brief Join strings
134 *
135 * @param[in] strs List of strings to join
136 * @param[in] sep Separator
137 *
138 * @returns a string of all given parts separated by the given separator
139 */
140std::string StrJoin(const std::vector<std::string>& strs, const std::string& sep);
141
142/**
143 * @brief Map strings
144 *
145 * @param[in] strs List of strings to map
146 * @param[in] map Map function
147 *
148 * @returns the mapped list of strings
149 */
150std::vector<std::string> StrMap(
151 const std::vector<std::string>& strs, std::function<std::string(const std::string&)> map);
152
153/**
154 * @brief Remove duplicates
155 *
156 * @param[in] strs List of strings
157 */
158void MakeUnique(std::vector<std::string>& strs);
159
160/**
161 * @brief Format hexdump of data
162 *
163 * @param[in] data The data
164 *
165 * @returns one or more lines (strings) with a hexdump of the data
166 */
167std::vector<std::string> HexDump(const std::vector<uint8_t> data);
168
169/**
170 * @brief Format hexdump of data
171 *
172 * @param[in] data The data
173 * @param[in] size The size of the data
174 *
175 * @returns one or more lines (strings) with a hexdump of the data
176 */
177std::vector<std::string> HexDump(const uint8_t* data, const std::size_t size);
178
179/**
180 * @brief Check if one string starts with another string
181 *
182 * @param[in] str String to check
183 * @param[in] prefix Prefix to check, length must be >= length of str
184 *
185 * @returns true if string starts with the prefix, false otherwise (no match or empty string(s))
186 */
187bool StrStartsWith(const std::string& str, const std::string& prefix);
188
189/**
190 * @brief Check if one string ends with another string
191 *
192 * @param[in] str String to check
193 * @param[in] suffix Prefix to check, length must be >= length of str
194 *
195 * @returns true if string ends with the suffix, false otherwise (no match or empty string(s))
196 */
197bool StrEndsWith(const std::string& str, const std::string& suffix);
198
199/**
200 * @brief Check if one string is contained within another string string
201 *
202 * @param[in] str String to check
203 * @param[in] sub String to find
204 *
205 * @returns true if string contains the substriong, false otherwise(no match or empty string(s))
206 */
207bool StrContains(const std::string& str, const std::string& sub);
208
209/**
210 * @brief Convert string to value (int8_t)
211 *
212 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
213 * target value type result in a failure (return false). The output value is only modified on success.
214 *
215 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT8_MIN..INT8_MAX
216 * @param[out] value The value
217 *
218 * @returns true if the string could be converted, false otherwise
219 */
220bool StrToValue(const std::string& str, int8_t& value);
221
222/**
223 * @brief Convert string to value (uint8_t)
224 *
225 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
226 * target value type result in a failure (return false). The output value is only modified on success.
227 *
228 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: 0..UINT8_MAX
229 * @param[out] value The value
230 *
231 * @returns true if the string could be converted, false otherwise
232 */
233bool StrToValue(const std::string& str, uint8_t& value);
234
235/**
236 * @brief Convert string to value (int16_t)
237 *
238 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
239 * target value type result in a failure (return false). The output value is only modified on success.
240 *
241 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT16_MIN..INT16_MAX
242 * @param[out] value The value
243 *
244 * @returns true if the string could be converted, false otherwise
245 */
246bool StrToValue(const std::string& str, int16_t& value);
247
248/**
249 * @brief Convert string to value (uint16_t)
250 *
251 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
252 * target value type result in a failure (return false). The output value is only modified on success.
253 *
254 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: 0..UINT16_MAX
255 * @param[out] value The value
256 *
257 * @returns true if the string could be converted, false otherwise
258 */
259bool StrToValue(const std::string& str, uint16_t& value);
260
261/**
262 * @brief Convert string to value (int32_t)
263 *
264 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
265 * target value type result in a failure (return false). The output value is only modified on success.
266 *
267 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: INT32_MIN..INT32_MAX
268 * @param[out] value The value
269 *
270 * @returns true if the string could be converted, false otherwise
271 */
272bool StrToValue(const std::string& str, int32_t& value);
273
274/**
275 * @brief Convert string to value (uint32_t)
276 *
277 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
278 * target value type result in a failure (return false). The output value is only modified on success.
279 *
280 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: 0..UINT32_MAX
281 * @param[out] value The value
282 *
283 * @returns true if the string could be converted, false otherwise
284 */
285bool StrToValue(const std::string& str, uint32_t& value);
286
287/**
288 * @brief Convert string to value (int64_t, long)
289 *
290 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
291 * target value type result in a failure (return false). The output value is only modified on success.
292 *
293 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: (INT64_MIN+1)..(INT64_MAX-1)
294 * @param[out] value The value
295 *
296 * @returns true if the string could be converted, false otherwise
297 */
298bool StrToValue(const std::string& str, int64_t& value);
299
300/**
301 * @brief Convert string to value (uint64_t, unsigned long)
302 *
303 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
304 * target value type result in a failure (return false). The output value is only modified on success.
305 *
306 * @param[in] str The string, decimal, hex ("0x...") or octal ("0..."), valid range: 0..(INT64_MAX-1)
307 * @param[out] value The value
308 *
309 * @returns true if the string could be converted, false otherwise
310 */
311bool StrToValue(const std::string& str, uint64_t& value);
312
313/**
314 * @brief Convert string to value (float)
315 *
316 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
317 * target value type result in a failure (return false). The output value is only modified on success.
318 *
319 * @param[in] str The string, anything %f understands (but not infinite or NaN)
320 * @param[out] value The value
321 *
322 * @returns true if the string could be converted, false otherwise
323 */
324bool StrToValue(const std::string& str, float& value);
325
326/**
327 * @brief Convert string to value (double)
328 *
329 * @note White-space or other spurious characters in the string or valid number strings that are out of range of the
330 * target value type result in a failure (return false). The output value is only modified on success.
331 *
332 * @param[in] str The string, anything %f understands (but not infinite or NaN)
333 * @param[out] value The value
334 *
335 * @returns true if the string could be converted, false otherwise
336 */
337bool StrToValue(const std::string& str, double& value);
338
339/**
340 * @brief Convert string to all upper case
341 *
342 * @param[in] str The string
343 *
344 * @returns the string converted to all upper case
345 */
346std::string StrToUpper(const std::string& str);
347
348/**
349 * @brief Convert string to all lower case
350 *
351 * @param[in] str The string
352 *
353 * @returns the string converted to all lower case
354 */
355std::string StrToLower(const std::string& str);
356
357/* ****************************************************************************************************************** */
358} // namespace string
359} // namespace common
360} // namespace fpsdk
361#endif // __FPSDK_COMMON_STRING_HPP__
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.
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.
std::string StrToLower(const std::string &str)
Convert string to all lower case.
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.
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.
int StrReplace(std::string &str, const std::string &search, const std::string &replace, const int max=0)
Search and replace.
void MakeUnique(std::vector< std::string > &strs)
Remove duplicates.
Fixposition SDK.
Definition fpsdk_doc.hpp:20
#define PRINTF_ATTR(n)
Helper macro for marking functions as taking printf() style formatting strings.
Definition string.hpp:46