Ocean
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_STRING_H
9#define META_OCEAN_BASE_STRING_H
10
11#include "ocean/base/Base.h"
12
13#include <sstream>
14#include <string>
15
16namespace Ocean
17{
18
19/**
20 * This class provides string convert functionalities.
21 * @ingroup base
22 */
23class OCEAN_BASE_EXPORT String
24{
25 public:
26
27 /**
28 * Converts a value to a string with 8bit character.
29 * @param value The value to convert
30 * @return Converted string
31 */
32 static std::string toAString(const char value);
33
34 /**
35 * Converts a value to a string with 8bit character.
36 * @param value The value to convert
37 * @return Converted string
38 */
39 static std::string toAString(const unsigned char value);
40
41 /**
42 * Converts a value to a string with 8bit character using a fixed notation.
43 * @param value The value to convert, with range (-infinity, infinity)
44 * @param precision Optional floating-point precision field, with range [1, infinity)
45 * @return Converted string
46 */
47 static std::string toAString(const double value, const unsigned int precision = 5u);
48
49 /**
50 * Converts a value to a string with 8bit character using a fixed notation.
51 * @param value The value to convert, with range (-infinity, infinity)
52 * @param precision Optional floating-point precision field, with range [1, infinity)
53 * @return Converted string
54 */
55 static std::string toAString(const float value, const unsigned int precision = 5u);
56
57 /**
58 * Converts a value to a string with 8bit character using a scientific notation.
59 * @param value The value to convert, with range (-infinity, infinity)
60 * @param precision Optional floating-point precision field, with range [1, infinity)
61 * @return Converted string
62 */
63 static std::string toAStringScientific(const double value, const unsigned int precision = 5u);
64
65 /**
66 * Converts a value to a string with 8bit character using a scientific notation.
67 * @param value The value to convert, with range (-infinity, infinity)
68 * @param precision Optional floating-point precision field, with range [1, infinity)
69 * @return Converted string
70 */
71 static std::string toAStringScientific(const float value, const unsigned int precision = 5u);
72
73 /**
74 * Converts a value to a string with 8bit character.
75 * @param value The value to convert
76 * @return Converted string
77 */
78 static std::string toAString(const int value);
79
80 /**
81 * Converts a value to a string with 8bit character.
82 * @param value The value to convert
83 * @return Converted string
84 */
85 static std::string toAString(const unsigned int value);
86
87 /**
88 * Converts a value to a string with 8bit character.
89 * @param value The value to convert
90 * @return Converted string
91 */
92 static std::string toAString(const long value);
93
94 /**
95 * Converts a value to a string with 8bit character.
96 * @param value The value to convert
97 * @return Converted string
98 */
99 static std::string toAString(const long long value);
100
101 /**
102 * Converts a value to a string with 8bit character.
103 * @param value The value to convert
104 * @return Converted string
105 */
106 static std::string toAString(const unsigned long long value);
107
108 /**
109 * Converts a value to a string with 8bit character.
110 * @param value The value to convert
111 * @return Converted string
112 */
113 static std::string toAString(const long unsigned int value);
114
115 /**
116 * Converts a value to a string with 8bit character.
117 * @param value The value to convert
118 * @return Converted string
119 */
120 static std::string toAString(const short value);
121
122 /**
123 * Converts a value to a string with 8bit character.
124 * @param value The value to convert
125 * @return Converted string
126 */
127 static std::string toAString(const unsigned short value);
128
129 /**
130 * Converts a value to a string with a minimal number of characters.
131 * Zero (prefix) are used to fill the missing part.
132 * @param value The value to be converted
133 * @param minimalChars Number of minimal characters to be created
134 * @return Converted string
135 */
136 static std::string toAString(const unsigned int value, const unsigned int minimalChars);
137
138 /**
139 * Converts a value to a string with 8bit character as hexadecimal notation.
140 * @param value The value to convert
141 * @param upperCases True, to use upper case characters; False, to use lower case characters
142 * @return Converted string
143 */
144 static std::string toAStringHex(const char value, const bool upperCases = true);
145
146 /**
147 * Converts a value to a string with 8bit character as hexadecimal notation.
148 * @param value The value to convert
149 * @param upperCases True, to use upper case characters; False, to use lower case characters
150 * @return Converted string
151 */
152 static std::string toAStringHex(const unsigned char value, const bool upperCases = true);
153
154 /**
155 * Converts a value to a string with 8bit character as hexadecimal notation.
156 * @param value The value to convert
157 * @param upperCases True, to use upper case characters; False, to use lower case characters
158 * @return Converted string
159 */
160 static std::string toAStringHex(const double value, const bool upperCases = true);
161
162 /**
163 * Converts a value to a string with 8bit character as hexadecimal notation.
164 * @param value The value to convert
165 * @param upperCases True, to use upper case characters; False, to use lower case characters
166 * @return Converted string
167 */
168 static std::string toAStringHex(const float value, const bool upperCases = true);
169
170 /**
171 * Converts a value to a string with 8bit character as hexadecimal notation.
172 * @param value The value to convert
173 * @param upperCases True, to use upper case characters; False, to use lower case characters
174 * @return Converted string
175 */
176 static std::string toAStringHex(const int value, const bool upperCases = true);
177
178 /**
179 * Converts a value to a string with 8bit character as hexadecimal notation.
180 * @param value The value to convert
181 * @param upperCases True, to use upper case characters; False, to use lower case characters
182 * @return Converted string
183 */
184 static std::string toAStringHex(const unsigned int value, const bool upperCases = true);
185
186 /**
187 * Converts a value to a string with 8bit character as hexadecimal notation.
188 * @param value The value to convert
189 * @param upperCases True, to use upper case characters; False, to use lower case characters
190 * @return Converted string
191 */
192 static std::string toAStringHex(const long long value, const bool upperCases = true);
193
194 /**
195 * Converts a value to a string with 8bit character as hexadecimal notation.
196 * @param value The value to convert
197 * @param upperCases True, to use upper case characters; False, to use lower case characters
198 * @return Converted string
199 */
200 static std::string toAStringHex(const unsigned long long value, const bool upperCases = true);
201
202 /**
203 * Converts a value to a string with 8bit character as hexadecimal notation.
204 * @param value The value to convert
205 * @param upperCases True, to use upper case characters; False, to use lower case characters
206 * @return Converted string
207 */
208 static std::string toAStringHex(const long unsigned int value, const bool upperCases = true);
209
210 /**
211 * Converts a value to a string with 8bit character as hexadecimal notation.
212 * @param value The value to convert
213 * @param upperCases True, to use upper case characters; False, to use lower case characters
214 * @return Converted string
215 */
216 static std::string toAStringHex(const short value, const bool upperCases = true);
217
218 /**
219 * Converts a value to a string with 8bit character as hexadecimal notation.
220 * @param value The value to convert
221 * @param upperCases True, to use upper case characters; False, to use lower case characters
222 * @return Converted string
223 */
224 static std::string toAStringHex(const unsigned short value, const bool upperCases = true);
225
226 /**
227 * Converts memory to a string with hexadecimal notation.
228 * @param data The memory to convert, can be nullptr if size == 0
229 * @param size The size of the memory in bytes, with range [0, infinity)
230 * @param upperCases True, to use upper case characters; False, to use lower case characters
231 * @return Converted string
232 */
233 static std::string toAStringHex(const uint8_t* data, const size_t size, const bool upperCases = true);
234
235 /**
236 * Converts memory to a string with hexadecimal notation in a reverse order.
237 * @param data The memory to convert, can be nullptr if size == 0
238 * @param size The size of the memory in bytes, with range [0, infinity)
239 * @param upperCases True, to use upper case characters; False, to use lower case characters
240 * @return Converted string
241 */
242 static std::string toAStringHexReverse(const uint8_t* data, const size_t size, const bool upperCases = true);
243
244 /**
245 * Converts a string to a string with lower characters only.
246 * @param value String value to convert, only containing ASCII characters
247 * @return Converted string.
248 */
249 static std::string toLower(const std::string& value);
250
251 /**
252 * Converts a string to a string with upper characters only.
253 * @param value String value to convert, only containing ASCII characters
254 * @return Converted string.
255 */
256 static std::string toUpper(const std::string& value);
257
258 /**
259 * Trims the front of a string.
260 * @param value String to be trimmed at the front
261 * @param character Trimming character
262 * @return Trimmed string
263 */
264 static std::string trimFront(const std::string& value, const char character = ' ');
265
266 /**
267 * Trims the back of a string.
268 * @param value String to be trimmed at the back
269 * @param character Trimming character
270 * @return Trimmed string
271 */
272 static std::string trimBack(const std::string& value, const char character = ' ');
273
274 /**
275 * Trims the front and the back of a string.
276 * @param value String to be trimmed
277 * @param character Trimming character
278 * @return Trimmed string
279 */
280 static std::string trim(const std::string& value, const char character = ' ');
281
282 /**
283 * Replaces a sub-string inside a string with a different string.
284 * @param value The string in which the replacement will take place, can be empty
285 * @param oldString The sub-string to be replaced, empty to replace nothing
286 * @param newString The sub-string which will be placed at the location of the old string, can be empty
287 * @param onlyFirstOccurrence True, to stop after the first replacement; False, to continue with the remaining string
288 * @return The resulting string with replaced sub-string(s)
289 */
290 static std::string replace(std::string&& value, const std::string& oldString, const std::string& newString, const bool onlyFirstOccurrence);
291
292 /**
293 * Replaces characters with a specified value by a new character.
294 * @param value The string in which the character will be replaced
295 * @param oldCharacter The character which will be replaced
296 * @param newCharacter The character which will be set at the position of the old characters
297 * @return The resulting string which replaced characters
298 * @see exchangeCharacters(), removeCharacters().
299 */
300 static std::string replaceCharacters(const std::string& value, const char oldCharacter, const char newCharacter);
301
302 /**
303 * Exchanges two characters within a string.
304 * @param value The string in which the character will be exchanged
305 * @param character0 One of both characters to exchange
306 * @param character1 The second character to exchange
307 * @return The resulting string with exchanged characters
308 * @see replaceCharacters(), removeCharacters().
309 */
310 static std::string exchangeCharacters(const std::string& value, const char character0, const char character1);
311
312 /**
313 * Removes characters with a specified value from a string.
314 * @param value The string from which the characters will be removed
315 * @param character The character which will be removed
316 * @return The resulting string without the specified character
317 * @see replaceCharacters(), exchangeCharacters().
318 */
319 static std::string removeCharacters(const std::string& value, const char character);
320
321 /**
322 * Inserts a specified character into a string in regular intervals.
323 * The specified character will neither be added to the front of the string nor to the end of the string.<br>
324 * This function can be used e.g., to add (thousands) separators to integer values.
325 * @param value The string to which the character will be inserted
326 * @param character The character to be inserted
327 * @param interval The number of string characters that will stay together (between each block the specified character will be inserted), with range [0, infinity)
328 * @param startAtFront True, to start at the front of the string; False, to start at the back of the string
329 * @return The modified string
330 * @tparam TChar The data type of the character of the string
331 */
332 template <typename TChar>
333 static std::basic_string<TChar> insertCharacter(const std::basic_string<TChar>& value, const TChar& character, size_t interval, bool startAtFront);
334
335 /**
336 * Returns whether a given string stores a boolean value.
337 * A boolean value must be 'true' or 'false'.<br>
338 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
339 * @param stringValue String to be checked
340 * @param matchCase True, if the sting must match the lower case, otherwise any case is accepted
341 * @param acceptInteger True, if '1' and '0' will be accepted as 'true' and 'false'
342 * @param value Optional the value is returned
343 * @return True, if so
344 */
345 static bool isBoolean(const std::string& stringValue, const bool matchCase = true, const bool acceptInteger = false, bool* value = nullptr);
346
347 /**
348 * Returns whether a given string stores a 32bit integer value.
349 * An integer value may have a prefix sign ('+' or '-') followed by digits.<br>
350 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
351 * @param stringValue String to be checked
352 * @param value Optional the value is returned
353 * @return True, if so
354 */
355 static bool isInteger32(const std::string& stringValue, int32_t* value = nullptr);
356
357 /**
358 * Returns whether a given string stores an unsigned 64bit integer value.
359 * An integer value may have a prefix sign ('+') followed by digits.<br>
360 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
361 * @param stringValue String to be checked
362 * @param value Optional the value is returned
363 * @return True, if so
364 */
365 static bool isUnsignedInteger64(const std::string& stringValue, uint64_t* value = nullptr);
366
367 /**
368 * Returns whether a given string stores an hexadecimal value with up to 64 bits.
369 * The hexadecimal value may have a prefix "0x" and may contain up to 16 characters of digits or letters ['a', 'f'] or ['A' - 'F'].
370 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
371 * @param hexString The hexadecimal value given as string, must be valid
372 * @param length The number of characters the given string contains, with range [1, 18]
373 * @param needPrefix True, to accept hexadecimal values only if they start with "0x"; False, to accept also hexadecimal values with the prefix
374 * @param value Optional resulting decimal value of the given hexadecimal value; nullptr, if the value is not of interest
375 * @return True, if so
376 */
377 static bool isHexValue64(const char* hexString, size_t length, const bool needPrefix = false, uint64_t* value = nullptr);
378
379 /**
380 * Returns whether a given string stores a number value.
381 * The string may start with a sign, may hold one dot, and may end with an exponent, the sign of the exponent is optional but at least one digit must follow it.
382 * <pre>
383 * value acceptInteger = false acceptInteger = true
384 * "10.4" accepted accepted
385 * "-9.0" accepted accepted
386 * "+10.2" accepted accepted
387 * "1." accepted accepted
388 * ".5" accepted accepted
389 * "-.5" accepted accepted
390 * "1e5" accepted accepted
391 * "1E5" accepted accepted
392 * "1e+5" accepted accepted
393 * ".5e3" accepted accepted
394 * "-10.23e+10" accepted accepted
395 * "10.5E-3" accepted accepted
396 * "5" rejected accepted
397 * "-5" rejected accepted
398 * "+5" rejected accepted
399 * "1.2.3" rejected rejected
400 * "1..2" rejected rejected
401 * "1e5.5" rejected rejected
402 * "1e5e5" rejected rejected
403 * "1e" rejected rejected
404 * "1e+" rejected rejected
405 * "0x10" rejected rejected
406 * "1,5" rejected rejected
407 * " 5" rejected rejected
408 * "5 " rejected rejected
409 * "" rejected rejected
410 * </pre>
411 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
412 * @param stringValue String to be checked
413 * @param acceptInteger True, if also strict integers will be accepted
414 * @param value Optional the value is returned
415 * @return True, if so
416 */
417 static bool isNumber(const std::string& stringValue, const bool acceptInteger = false, double* value = nullptr);
418
419 /**
420 * Converts a value to a string with 8bit character.
421 * @param value The value to convert
422 * @return Converted string
423 */
424 static std::string toAString(const wchar_t value);
425
426 /**
427 * Converts a value to a string with 8bit character.
428 * @param value The value to convert, may be nullptr
429 * @return Converted string
430 */
431 static std::string toAString(const char* value);
432
433 /**
434 * Converts a value to a string with 8bit character.
435 * @param value The value to convert, may be nullptr
436 * @return Converted string
437 */
438 static std::string toAString(const wchar_t* value);
439
440 /**
441 * Converts a value to a string with 8bit character.
442 * @param value The value to convert
443 * @return Converted string
444 */
445 static std::string toAString(const std::string& value);
446
447 /**
448 * Converts a value to a string with 8bit character.
449 * @param value The value to convert
450 * @return Converted string
451 */
452 static std::string toAString(const std::wstring& value);
453
454 /**
455 * Converts a value to a wstring.
456 * @param value The value to convert
457 * @return Converted string
458 */
459 static std::wstring toWString(const char value);
460
461 /**
462 * Converts a value to a wstring.
463 * @param value The value to convert, may be nullptr
464 * @return Converted string
465 */
466 static std::wstring toWString(const char* value);
467
468 /**
469 * Converts a value to a wstring.
470 * @param value The value to convert, may be nullptr
471 * @return Converted string
472 */
473 static std::wstring toWString(const wchar_t* value);
474
475 /**
476 * Converts a value to a wstring.
477 * @param value The value to convert, may be nullptr
478 * @return Converted string
479 */
480 static std::wstring toWString(const unsigned short* value);
481
482 /**
483 * Converts a value to a wstring.
484 * @param value The value to convert
485 * @return Converted string
486 */
487 static std::wstring toWString(const unsigned char value);
488
489 /**
490 * Converts a value to a wstring using a fixed notation.
491 * @param value The value to convert, with range (-infinity, infinity)
492 * @param precision Optional floating-point precision field, with range [1, infinity)
493 * @return Converted string
494 */
495 static std::wstring toWString(const double value, const unsigned int precision = 5u);
496
497 /**
498 * Converts a value to a wstring using a fixed notation.
499 * @param value The value to convert, with range (-infinity, infinity)
500 * @param precision Optional floating-point precision field, with range [1, infinity)
501 * @return Converted string
502 */
503 static std::wstring toWString(const float value, const unsigned int precision = 5u);
504
505 /**
506 * Converts a value to a wstring using a scientific notation.
507 * @param value The value to convert, with range (-infinity, infinity)
508 * @param precision Optional floating-point precision field, with range [1, infinity)
509 * @return Converted string
510 */
511 static std::wstring toWStringScientific(const double value, const unsigned int precision = 5u);
512
513 /**
514 * Converts a value to a wstring using a scientific notation.
515 * @param value The value to convert, with range (-infinity, infinity)
516 * @param precision Optional floating-point precision field, with range [1, infinity)
517 * @return Converted string
518 */
519 static std::wstring toWStringScientific(const float value, const unsigned int precision = 5u);
520
521 /**
522 * Converts a value to a wstring.
523 * @param value The value to convert
524 * @return Converted string
525 */
526 static std::wstring toWString(const int value);
527
528 /**
529 * Converts a value to a wstring.
530 * @param value The value to convert
531 * @return Converted string
532 */
533 static std::wstring toWString(const unsigned int value);
534
535 /**
536 * Converts a value to a wstring.
537 * @param value The value to convert
538 * @return Converted string
539 */
540 static std::wstring toWString(const long value);
541
542 /**
543 * Converts a value to a wstring.
544 * @param value The value to convert
545 * @return Converted string
546 */
547 static std::wstring toWString(const long long value);
548
549 /**
550 * Converts a value to a wstring.
551 * @param value The value to convert
552 * @return Converted string
553 */
554 static std::wstring toWString(const unsigned long long value);
555
556 /**
557 * Converts a value to a wstring.
558 * @param value The value to convert
559 * @return Converted string
560 */
561 static std::wstring toWString(const short value);
562
563 /**
564 * Converts a value to a wstring.
565 * @param value The value to convert
566 * @return Converted string
567 */
568 static std::wstring toWString(const unsigned short value);
569
570 /**
571 * Converts a value to a wstring.
572 * @param value The value to convert
573 * @return Converted string
574 */
575 static std::wstring toWString(const long unsigned int value);
576
577 /**
578 * Converts a value to a wstring.
579 * @param value The value to convert
580 * @return Converted string
581 */
582 static std::wstring toWString(const wchar_t value);
583
584 /**
585 * Converts a value to a wstring with a minimal number of characters.
586 * Zero (prefix) are used to fill the missing part.
587 * @param value The value to be converted
588 * @param minimalChars Number of minimal characters to be created
589 * @return Converted string
590 */
591 static std::wstring toWString(const unsigned int value, const unsigned int minimalChars);
592
593 /**
594 * Converts a value to a wstring.
595 * @param value The value to convert
596 * @return Converted string
597 */
598 static std::wstring toWString(const std::string& value);
599
600 /**
601 * Converts a value to a wstring.
602 * @param value The value to convert
603 * @return Converted string
604 */
605 static std::wstring toWString(const std::wstring& value);
606
607 /**
608 * Trims the front of a string.
609 * @param value String to be trimmed at the front
610 * @param character Trimming character
611 * @return Trimmed string
612 */
613 static std::wstring trimFront(const std::wstring& value, const wchar_t character = L' ');
614
615 /**
616 * Trims the back of a string.
617 * @param value String to be trimmed at the back
618 * @param character Trimming character
619 * @return Trimmed string
620 */
621 static std::wstring trimBack(const std::wstring& value, const wchar_t character = L' ');
622
623 /**
624 * Trims the front and the back of a string.
625 * @param value String to be trimmed
626 * @param character Trimming character
627 * @return Trimmed string
628 */
629 static std::wstring trim(const std::wstring& value, const wchar_t character = L' ');
630
631 /**
632 * Returns the specified text with all whitespace characters removed from the left and right side.
633 * Characters considered whitespace are:
634 * - ' ' (0x20) space (SPC)
635 * - '\\t' (0x09) horizontal tab (TAB)
636 * - '\\n' (0x0a) newline (LF)
637 * - '\\v' (0x0b) vertical tab (VT)
638 * - '\\f' (0x0c) feed (FF)
639 * - '\\r' (0x0d) carriage return (CR)
640 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
641 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
642 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
643 * @param text The text string to be trimmed
644 * @return Trimmed text string
645 * @see trimWhitespaceString
646 */
647 static std::string trimWhitespace(const std::string& text);
648
649 /**
650 * Returns the specified text with all whitespace characters removed from the left and right side.
651 ** Characters considered whitespace are:
652 * - ' ' (0x20) space (SPC)
653 * - '\\t' (0x09) horizontal tab (TAB)
654 * - '\\n' (0x0a) newline (LF)
655 * - '\\v' (0x0b) vertical tab (VT)
656 * - '\\f' (0x0c) feed (FF)
657 * - '\\r' (0x0d) carriage return (CR)
658 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
659 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
660 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
661 * @param text The text string to be trimmed
662 * @return Trimmed text string
663 * @see trimWhitespaceString
664 */
665 static std::wstring trimWhitespace(const std::wstring& text);
666
667 /**
668 * Replaces characters with a specified value by a new character.
669 * @param value The string in which the character will be replaced
670 * @param oldCharacter The character which will be replaced
671 * @param newCharacter The character which will be set at the position of the old characters
672 * @return The resulting string which replaced characters
673 * @see exchangeCharacters(), removeCharacters().
674 */
675 static std::wstring replaceCharacters(const std::wstring& value, const wchar_t oldCharacter, const wchar_t newCharacter);
676
677 /**
678 * Exchanges two characters within a string.
679 * @param value The string in which the character will be exchanged
680 * @param character0 One of both characters to exchange
681 * @param character1 The second character to exchange
682 * @return The resulting string with exchanged characters
683 * @see replaceCharacters(), removeCharacters().
684 */
685 static std::wstring exchangeCharacters(const std::wstring& value, const wchar_t character0, const wchar_t character1);
686
687 /**
688 * Removes characters with a specified value from a string.
689 * @param value The string from which the characters will be removed
690 * @param character The character which will be removed
691 * @return The resulting string without the specified character
692 * @see replaceCharacters(), exchangeCharacters().
693 */
694 static std::wstring removeCharacters(const std::wstring& value, const wchar_t character);
695
696 private:
697
698 /**
699 * Converts a value of an arbitrary primitive type into a string (supported types are: ordinal types, floating point types, bool, string, char arrays)
700 * @param value The value that is to be converted
701 * @return Text representation of value
702 * @tparam TChar Data type of the characters of the C++ String
703 * @tparam T Type of value
704 */
705 template <typename TChar, typename T>
706 static std::basic_string<TChar> toString(const T& value);
707
708 /**
709 * Returns the specified text with all whitespace characters removed from the left and right side.
710 * Characters considered whitespace are:
711 * - ' ' (0x20) space (SPC)
712 * - '\\t' (0x09) horizontal tab (TAB)
713 * - '\\n' (0x0a) newline (LF)
714 * - '\\v' (0x0b) vertical tab (VT)
715 * - '\\f' (0x0c) feed (FF)
716 * - '\\r' (0x0d) carriage return (CR)
717 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
718 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
719 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
720 * @param text The text string to be trimmed
721 * @tparam TChar Character data type of specified string, either use char or wchar_t
722 * @return Trimmed text string
723 */
724 template <typename TChar>
725 static std::basic_string<TChar> trimWhitespaceString(const std::basic_string<TChar>& text);
726};
727
728template <>
729inline std::string String::toString(const bool& value)
730{
731 return value ? std::string("true") : std::string("false");
732}
733
734template <>
735inline std::wstring String::toString(const bool& value)
736{
737 return value ? std::wstring(L"true") : std::wstring(L"false");
738}
739
740template <typename TChar>
741std::basic_string<TChar> String::insertCharacter(const std::basic_string<TChar>& value, const TChar& character, size_t interval, bool startAtFront)
742{
743 ocean_assert(interval != 0);
744
745 std::basic_string<TChar> result(value);
746
747 if (startAtFront)
748 {
749 size_t position = interval;
750
751 while (position < result.length())
752 {
753 result.insert(position, 1, character);
754 position += interval + 1;
755 }
756 }
757 else
758 {
759 std::ptrdiff_t position = std::ptrdiff_t(result.length()) - std::ptrdiff_t(interval);
760
761 while (position >= 1)
762 {
763 result.insert(position, 1, character);
764 position -= std::ptrdiff_t(interval);
765 }
766 }
767
768 return result;
769}
770
771template <typename TChar, typename T>
772std::basic_string<TChar> String::toString(const T& value)
773{
774 std::basic_ostringstream<TChar> stream;
775 stream << value;
776
777 return stream.str();
778}
779
780}
781
782#endif // META_OCEAN_BASE_STRING_H
This class provides string convert functionalities.
Definition String.h:24
static std::string trimBack(const std::string &value, const char character=' ')
Trims the back of a string.
static std::string removeCharacters(const std::string &value, const char character)
Removes characters with a specified value from a string.
static std::wstring toWStringScientific(const double value, const unsigned int precision=5u)
Converts a value to a wstring using a scientific notation.
static std::wstring toWString(const std::wstring &value)
Converts a value to a wstring.
static std::basic_string< TChar > insertCharacter(const std::basic_string< TChar > &value, const TChar &character, size_t interval, bool startAtFront)
Inserts a specified character into a string in regular intervals.
Definition String.h:741
static std::string toAStringHex(const long unsigned int value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAStringScientific(const double value, const unsigned int precision=5u)
Converts a value to a string with 8bit character using a scientific notation.
static std::string toAString(const long long value)
Converts a value to a string with 8bit character.
static std::string trimFront(const std::string &value, const char character=' ')
Trims the front of a string.
static std::string toAString(const char value)
Converts a value to a string with 8bit character.
static std::string toAStringHex(const double value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::wstring toWString(const unsigned int value, const unsigned int minimalChars)
Converts a value to a wstring with a minimal number of characters.
static std::wstring toWString(const short value)
Converts a value to a wstring.
static bool isBoolean(const std::string &stringValue, const bool matchCase=true, const bool acceptInteger=false, bool *value=nullptr)
Returns whether a given string stores a boolean value.
static std::string toLower(const std::string &value)
Converts a string to a string with lower characters only.
static std::wstring toWString(const double value, const unsigned int precision=5u)
Converts a value to a wstring using a fixed notation.
static std::string trimWhitespace(const std::string &text)
Returns the specified text with all whitespace characters removed from the left and right side.
static std::string toAString(const char *value)
Converts a value to a string with 8bit character.
static std::string replaceCharacters(const std::string &value, const char oldCharacter, const char newCharacter)
Replaces characters with a specified value by a new character.
static std::string toAString(const std::string &value)
Converts a value to a string with 8bit character.
static std::wstring toWStringScientific(const float value, const unsigned int precision=5u)
Converts a value to a wstring using a scientific notation.
static bool isInteger32(const std::string &stringValue, int32_t *value=nullptr)
Returns whether a given string stores a 32bit integer value.
static std::wstring trim(const std::wstring &value, const wchar_t character=L' ')
Trims the front and the back of a string.
static bool isNumber(const std::string &stringValue, const bool acceptInteger=false, double *value=nullptr)
Returns whether a given string stores a number value.
static std::string toAString(const unsigned short value)
Converts a value to a string with 8bit character.
static std::wstring replaceCharacters(const std::wstring &value, const wchar_t oldCharacter, const wchar_t newCharacter)
Replaces characters with a specified value by a new character.
static std::string trim(const std::string &value, const char character=' ')
Trims the front and the back of a string.
static std::wstring toWString(const unsigned short *value)
Converts a value to a wstring.
static std::string toAStringHex(const char value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAStringHex(const unsigned long long value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::wstring toWString(const unsigned char value)
Converts a value to a wstring.
static std::basic_string< TChar > toString(const T &value)
Converts a value of an arbitrary primitive type into a string (supported types are: ordinal types,...
Definition String.h:772
static std::string toAString(const wchar_t value)
Converts a value to a string with 8bit character.
static bool isUnsignedInteger64(const std::string &stringValue, uint64_t *value=nullptr)
Returns whether a given string stores an unsigned 64bit integer value.
static std::string toAString(const float value, const unsigned int precision=5u)
Converts a value to a string with 8bit character using a fixed notation.
static std::string toAStringHex(const long long value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAString(const unsigned int value)
Converts a value to a string with 8bit character.
static std::wstring toWString(const long value)
Converts a value to a wstring.
static std::string toAStringHex(const short value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAString(const unsigned long long value)
Converts a value to a string with 8bit character.
static std::wstring toWString(const unsigned int value)
Converts a value to a wstring.
static std::wstring toWString(const unsigned short value)
Converts a value to a wstring.
static std::string toAString(const unsigned int value, const unsigned int minimalChars)
Converts a value to a string with a minimal number of characters.
static std::string toAString(const short value)
Converts a value to a string with 8bit character.
static std::wstring toWString(const wchar_t value)
Converts a value to a wstring.
static std::wstring toWString(const char *value)
Converts a value to a wstring.
static std::string toAStringHex(const int value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string replace(std::string &&value, const std::string &oldString, const std::string &newString, const bool onlyFirstOccurrence)
Replaces a sub-string inside a string with a different string.
static std::string toAStringHex(const uint8_t *data, const size_t size, const bool upperCases=true)
Converts memory to a string with hexadecimal notation.
static std::wstring trimBack(const std::wstring &value, const wchar_t character=L' ')
Trims the back of a string.
static std::wstring removeCharacters(const std::wstring &value, const wchar_t character)
Removes characters with a specified value from a string.
static std::string toAStringHex(const float value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toUpper(const std::string &value)
Converts a string to a string with upper characters only.
static std::string toAStringHex(const unsigned char value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAString(const double value, const unsigned int precision=5u)
Converts a value to a string with 8bit character using a fixed notation.
static std::string toAString(const long value)
Converts a value to a string with 8bit character.
static std::string toAString(const wchar_t *value)
Converts a value to a string with 8bit character.
static std::basic_string< TChar > trimWhitespaceString(const std::basic_string< TChar > &text)
Returns the specified text with all whitespace characters removed from the left and right side.
static std::wstring toWString(const long long value)
Converts a value to a wstring.
static std::string toAString(const unsigned char value)
Converts a value to a string with 8bit character.
static std::string toAStringHex(const unsigned short value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAStringHexReverse(const uint8_t *data, const size_t size, const bool upperCases=true)
Converts memory to a string with hexadecimal notation in a reverse order.
static std::string exchangeCharacters(const std::string &value, const char character0, const char character1)
Exchanges two characters within a string.
static std::string toAStringHex(const unsigned int value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::wstring toWString(const long unsigned int value)
Converts a value to a wstring.
static bool isHexValue64(const char *hexString, size_t length, const bool needPrefix=false, uint64_t *value=nullptr)
Returns whether a given string stores an hexadecimal value with up to 64 bits.
static std::wstring toWString(const float value, const unsigned int precision=5u)
Converts a value to a wstring using a fixed notation.
static std::wstring toWString(const wchar_t *value)
Converts a value to a wstring.
static std::wstring toWString(const char value)
Converts a value to a wstring.
static std::string toAString(const long unsigned int value)
Converts a value to a string with 8bit character.
static std::string toAString(const int value)
Converts a value to a string with 8bit character.
static std::wstring toWString(const std::string &value)
Converts a value to a wstring.
static std::wstring exchangeCharacters(const std::wstring &value, const wchar_t character0, const wchar_t character1)
Exchanges two characters within a string.
static std::wstring toWString(const int value)
Converts a value to a wstring.
static std::wstring trimWhitespace(const std::wstring &text)
Returns the specified text with all whitespace characters removed from the left and right side.
static std::string toAStringScientific(const float value, const unsigned int precision=5u)
Converts a value to a string with 8bit character using a scientific notation.
static std::string toAString(const std::wstring &value)
Converts a value to a string with 8bit character.
static std::wstring toWString(const unsigned long long value)
Converts a value to a wstring.
static std::wstring trimFront(const std::wstring &value, const wchar_t character=L' ')
Trims the front of a string.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15