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, unsigned long long* value = nullptr);
378
379 /**
380 * Returns whether a given string stores a number value.
381 * Any beginning or ending white space will violate the value conditions, therefore provide a trimmed string value.
382 * @param stringValue String to be checked
383 * @param acceptInteger True, if also strict integers will be accepted
384 * @param value Optional the value is returned
385 * @return True, if so
386 */
387 static bool isNumber(const std::string& stringValue, const bool acceptInteger = false, double* value = nullptr);
388
389 /**
390 * Converts a value to a string with 8bit character.
391 * @param value The value to convert
392 * @return Converted string
393 */
394 static std::string toAString(const wchar_t value);
395
396 /**
397 * Converts a value to a string with 8bit character.
398 * @param value The value to convert, may be nullptr
399 * @return Converted string
400 */
401 static std::string toAString(const char* value);
402
403 /**
404 * Converts a value to a string with 8bit character.
405 * @param value The value to convert, may be nullptr
406 * @return Converted string
407 */
408 static std::string toAString(const wchar_t* value);
409
410 /**
411 * Converts a value to a string with 8bit character.
412 * @param value The value to convert
413 * @return Converted string
414 */
415 static std::string toAString(const std::string& value);
416
417 /**
418 * Converts a value to a string with 8bit character.
419 * @param value The value to convert
420 * @return Converted string
421 */
422 static std::string toAString(const std::wstring& value);
423
424 /**
425 * Converts a value to a wstring.
426 * @param value The value to convert
427 * @return Converted string
428 */
429 static std::wstring toWString(const char value);
430
431 /**
432 * Converts a value to a wstring.
433 * @param value The value to convert, may be nullptr
434 * @return Converted string
435 */
436 static std::wstring toWString(const char* value);
437
438 /**
439 * Converts a value to a wstring.
440 * @param value The value to convert, may be nullptr
441 * @return Converted string
442 */
443 static std::wstring toWString(const wchar_t* value);
444
445 /**
446 * Converts a value to a wstring.
447 * @param value The value to convert, may be nullptr
448 * @return Converted string
449 */
450 static std::wstring toWString(const unsigned short* value);
451
452 /**
453 * Converts a value to a wstring.
454 * @param value The value to convert
455 * @return Converted string
456 */
457 static std::wstring toWString(const unsigned char value);
458
459 /**
460 * Converts a value to a wstring using a fixed notation.
461 * @param value The value to convert, with range (-infinity, infinity)
462 * @param precision Optional floating-point precision field, with range [1, infinity)
463 * @return Converted string
464 */
465 static std::wstring toWString(const double value, const unsigned int precision = 5u);
466
467 /**
468 * Converts a value to a wstring using a fixed notation.
469 * @param value The value to convert, with range (-infinity, infinity)
470 * @param precision Optional floating-point precision field, with range [1, infinity)
471 * @return Converted string
472 */
473 static std::wstring toWString(const float value, const unsigned int precision = 5u);
474
475 /**
476 * Converts a value to a wstring using a scientific notation.
477 * @param value The value to convert, with range (-infinity, infinity)
478 * @param precision Optional floating-point precision field, with range [1, infinity)
479 * @return Converted string
480 */
481 static std::wstring toWStringScientific(const double value, const unsigned int precision = 5u);
482
483 /**
484 * Converts a value to a wstring using a scientific notation.
485 * @param value The value to convert, with range (-infinity, infinity)
486 * @param precision Optional floating-point precision field, with range [1, infinity)
487 * @return Converted string
488 */
489 static std::wstring toWStringScientific(const float value, const unsigned int precision = 5u);
490
491 /**
492 * Converts a value to a wstring.
493 * @param value The value to convert
494 * @return Converted string
495 */
496 static std::wstring toWString(const int value);
497
498 /**
499 * Converts a value to a wstring.
500 * @param value The value to convert
501 * @return Converted string
502 */
503 static std::wstring toWString(const unsigned int value);
504
505 /**
506 * Converts a value to a wstring.
507 * @param value The value to convert
508 * @return Converted string
509 */
510 static std::wstring toWString(const long value);
511
512 /**
513 * Converts a value to a wstring.
514 * @param value The value to convert
515 * @return Converted string
516 */
517 static std::wstring toWString(const long long value);
518
519 /**
520 * Converts a value to a wstring.
521 * @param value The value to convert
522 * @return Converted string
523 */
524 static std::wstring toWString(const unsigned long long value);
525
526 /**
527 * Converts a value to a wstring.
528 * @param value The value to convert
529 * @return Converted string
530 */
531 static std::wstring toWString(const short value);
532
533 /**
534 * Converts a value to a wstring.
535 * @param value The value to convert
536 * @return Converted string
537 */
538 static std::wstring toWString(const unsigned short value);
539
540 /**
541 * Converts a value to a wstring.
542 * @param value The value to convert
543 * @return Converted string
544 */
545 static std::wstring toWString(const long unsigned int value);
546
547 /**
548 * Converts a value to a wstring.
549 * @param value The value to convert
550 * @return Converted string
551 */
552 static std::wstring toWString(const wchar_t value);
553
554 /**
555 * Converts a value to a wstring with a minimal number of characters.
556 * Zero (prefix) are used to fill the missing part.
557 * @param value The value to be converted
558 * @param minimalChars Number of minimal characters to be created
559 * @return Converted string
560 */
561 static std::wstring toWString(const unsigned int value, const unsigned int minimalChars);
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 std::string& 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 std::wstring& value);
576
577 /**
578 * Trims the front of a string.
579 * @param value String to be trimmed at the front
580 * @param character Trimming character
581 * @return Trimmed string
582 */
583 static std::wstring trimFront(const std::wstring& value, const wchar_t character = L' ');
584
585 /**
586 * Trims the back of a string.
587 * @param value String to be trimmed at the back
588 * @param character Trimming character
589 * @return Trimmed string
590 */
591 static std::wstring trimBack(const std::wstring& value, const wchar_t character = L' ');
592
593 /**
594 * Trims the front and the back of a string.
595 * @param value String to be trimmed
596 * @param character Trimming character
597 * @return Trimmed string
598 */
599 static std::wstring trim(const std::wstring& value, const wchar_t character = L' ');
600
601 /**
602 * Returns the specified text with all whitespace characters removed from the left and right side.
603 * Characters considered whitespace are:
604 * - ' ' (0x20) space (SPC)
605 * - '\\t' (0x09) horizontal tab (TAB)
606 * - '\\n' (0x0a) newline (LF)
607 * - '\\v' (0x0b) vertical tab (VT)
608 * - '\\f' (0x0c) feed (FF)
609 * - '\\r' (0x0d) carriage return (CR)
610 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
611 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
612 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
613 * @param text The text string to be trimmed
614 * @return Trimmed text string
615 * @see trimWhitespaceString
616 */
617 static std::string trimWhitespace(const std::string& text);
618
619 /**
620 * Returns the specified text with all whitespace characters removed from the left and right side.
621 ** Characters considered whitespace are:
622 * - ' ' (0x20) space (SPC)
623 * - '\\t' (0x09) horizontal tab (TAB)
624 * - '\\n' (0x0a) newline (LF)
625 * - '\\v' (0x0b) vertical tab (VT)
626 * - '\\f' (0x0c) feed (FF)
627 * - '\\r' (0x0d) carriage return (CR)
628 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
629 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
630 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
631 * @param text The text string to be trimmed
632 * @return Trimmed text string
633 * @see trimWhitespaceString
634 */
635 static std::wstring trimWhitespace(const std::wstring& text);
636
637 /**
638 * Replaces characters with a specified value by a new character.
639 * @param value The string in which the character will be replaced
640 * @param oldCharacter The character which will be replaced
641 * @param newCharacter The character which will be set at the position of the old characters
642 * @return The resulting string which replaced characters
643 * @see exchangeCharacters(), removeCharacters().
644 */
645 static std::wstring replaceCharacters(const std::wstring& value, const wchar_t oldCharacter, const wchar_t newCharacter);
646
647 /**
648 * Exchanges two characters within a string.
649 * @param value The string in which the character will be exchanged
650 * @param character0 One of both characters to exchange
651 * @param character1 The second character to exchange
652 * @return The resulting string with exchanged characters
653 * @see replaceCharacters(), removeCharacters().
654 */
655 static std::wstring exchangeCharacters(const std::wstring& value, const wchar_t character0, const wchar_t character1);
656
657 /**
658 * Removes characters with a specified value from a string.
659 * @param value The string from which the characters will be removed
660 * @param character The character which will be removed
661 * @return The resulting string without the specified character
662 * @see replaceCharacters(), exchangeCharacters().
663 */
664 static std::wstring removeCharacters(const std::wstring& value, const wchar_t character);
665
666 private:
667
668 /**
669 * Converts a value of an arbitrary primitive type into a string (supported types are: ordinal types, floating point types, bool, string, char arrays)
670 * @param value The value that is to be converted
671 * @return Text representation of value
672 * @tparam TChar Data type of the characters of the C++ String
673 * @tparam T Type of value
674 */
675 template <typename TChar, typename T>
676 static std::basic_string<TChar> toString(const T& value);
677
678 /**
679 * Returns the specified text with all whitespace characters removed from the left and right side.
680 * Characters considered whitespace are:
681 * - ' ' (0x20) space (SPC)
682 * - '\\t' (0x09) horizontal tab (TAB)
683 * - '\\n' (0x0a) newline (LF)
684 * - '\\v' (0x0b) vertical tab (VT)
685 * - '\\f' (0x0c) feed (FF)
686 * - '\\r' (0x0d) carriage return (CR)
687 * - '\0' (0x00) the null terminator (NUL), for every null-terminator character at position '< std::string::size()'
688 * Further, the string trimmed string will not contain a null-terminator character in the middle of the string at position '< std::string::size()',<br>
689 * the only null terminator character will be at the end of the string at position 'std::string::size()'.
690 * @param text The text string to be trimmed
691 * @tparam TChar Character data type of specified string, either use char or wchar_t
692 * @return Trimmed text string
693 */
694 template <typename TChar>
695 static std::basic_string<TChar> trimWhitespaceString(const std::basic_string<TChar>& text);
696};
697
698template <>
699inline std::string String::toString(const bool& value)
700{
701 return value ? std::string("true") : std::string("false");
702}
703
704template <>
705inline std::wstring String::toString(const bool& value)
706{
707 return value ? std::wstring(L"true") : std::wstring(L"false");
708}
709
710template <typename TChar>
711std::basic_string<TChar> String::insertCharacter(const std::basic_string<TChar>& value, const TChar& character, size_t interval, bool startAtFront)
712{
713 ocean_assert(interval != 0);
714
715 std::basic_string<TChar> result(value);
716
717 if (startAtFront)
718 {
719 size_t position = interval;
720
721 while (position < result.length())
722 {
723 result.insert(position, 1, character);
724 position += interval + 1;
725 }
726 }
727 else
728 {
729 std::ptrdiff_t position = std::ptrdiff_t(result.length()) - std::ptrdiff_t(interval);
730
731 while (position >= 1)
732 {
733 result.insert(position, 1, character);
734 position -= std::ptrdiff_t(interval);
735 }
736 }
737
738 return result;
739}
740
741template <typename TChar, typename T>
742std::basic_string<TChar> String::toString(const T& value)
743{
744 std::basic_ostringstream<TChar> stream;
745 stream << value;
746
747 return stream.str();
748}
749
750}
751
752#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:711
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 bool isHexValue64(const char *hexString, size_t length, const bool needPrefix=false, unsigned long long *value=nullptr)
Returns whether a given string stores an hexadecimal value with up to 64 bits.
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:742
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 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