-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.h
More file actions
executable file
·415 lines (363 loc) · 18 KB
/
strings.h
File metadata and controls
executable file
·415 lines (363 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*********************************************************************************/
#ifndef RA_STRING_H
#define RA_STRING_H
#include <stdint.h>
#include <string>
#include <vector>
#include <stdio.h>
#include "rapidassist/config.h"
namespace ra { namespace strings {
typedef std::vector<std::string> StringVector;
/// <summary>The required amount of precision to get a lossless conversion from float to string.</summary>
extern const int FLOAT_TOSTRING_LOSSLESS_PRECISION;
/// <summary>The required amount of precision to get a lossless conversion from double to string.</summary>
extern const int DOUBLE_TOSTRING_LOSSLESS_PRECISION;
/// <summary>The default epsilon value for converting a float to string with a minimal lossy conversion.</summary>
extern const float FLOAT_TOSTRING_LOSSY_EPSILON;
/// <summary>The default epsilon value for converting a double to string with a minimal lossy conversion.</summary>
extern const double DOUBLE_TOSTRING_LOSSY_EPSILON;
/// <summary>
/// Defines if a string value is a numeric value.
/// A numeric value can be positive or negative.
/// A numeric value can be an integer or a floating point value.
/// </summary>
/// <param name="value">The string value to validate.</param>
/// <returns>True when value is numeric. False otherwise.</returns>
bool IsNumeric(const char * value);
/// <summary>
/// Replace an occurance of a string by another.
/// </summary>
/// <param name="str">The given string that need to be searched.</param>
/// <param name="old_value">The old value to replace.</param>
/// <param name="new_value">The new value to replace.</param>
/// <returns>Returns the number of token that was replaced.</returns>
int Replace(std::string & str, const std::string & old_value, const std::string & new_value);
/// <summary>
/// Converts the given value to string.
/// </summary>
/// <param name="value">The boolean value.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToString(const bool & value);
/// <summary>
/// Converts the given value to string.
/// </summary>
/// <param name="value">The numeric value.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToString(const int8_t & value);
std::string ToString(const uint8_t & value);
std::string ToString(const int16_t & value);
std::string ToString(const uint16_t & value);
std::string ToString(const int32_t & value);
std::string ToString(const uint32_t & value);
std::string ToString(const int64_t & value);
std::string ToString(const uint64_t & value);
#ifdef __APPLE__
std::string ToString(const size_t & value);
#endif
/// <summary>
/// Converts the given value to string. The conversion to string is lossless. That is no data is lost if the string is converted back to floating point.
/// </summary>
/// <remarks>
/// The string conversion uses as much digits as required so that converting the value to string and back to the original type matches the original value (bitwise compare).
/// The function assumes that most use of 32 bit floating point values probably target a practical range of -1 million to +1 million, and requires a precision of 9.
/// The function assumes that most use of 64 bit floating point values probably target a practical range of -100 billion to +100 billion, and requires a precision of 17.
/// However, this creates artifacts. For instance, converting 5.3f to string returns "5.30000019".
/// If you need digits precision, use ToStringFormatted(value, digits) instead.
/// </remarks>
/// <param name="value">The numeric value.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToStringLossless(const float & value);
std::string ToStringLossless(const double & value);
/// <summary>
/// Converts the given value to string.
/// The conversion to string is lossy. That is data might be lost if the string is converted back to floating point.
/// However, the result string is the shortest string representation without changing the given value more than epsilon.
/// The appropriate number of digits after the decimal point is automatically detected.
/// </summary>
/// <remarks>
/// This method of conversion is usually prefered as it creates a more human-friendly (readable) value.
/// For instance, the value 5.3f which converts to "5.30000019" when lossless has a more meaningful value if read as "5.3".
/// This is what is intended by this function.
/// </remarks>
/// <param name="value">The numeric value.</param>
/// <param name="epsilon">The amount of acceptable data loss while converting in order to get a smaller value more readable.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToStringLossy(const float & value, const float & epsilon);
std::string ToStringLossy(const double & value, const double & epsilon);
/// <summary>
/// Converts the given value to string with a fixed number of digits after the decimal point.
/// The conversion to string is lossy. That is data might be lost if the string is converted back to floating point.
/// </summary>
/// <remarks>
/// The function is usefull when handling values that have 1/10th or 1/100th increments.
/// For instance, currency.
/// </remarks>
/// <param name="value">The numeric value.</param>
/// <param name="digits">The number of digits after the decimal point.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToStringFormatted(const float & value, int digits);
std::string ToStringFormatted(const double & value, int digits);
/// <summary>
/// Converts the given value to string.
/// </summary>
/// <remarks>
/// Note that implementation of this method could change which would affect the quality or the returned value of this function.
/// To get a constant conversion, one should use ToStringLossless(), ToStringLossy() or ToStringFormatted().
/// </remarks>
/// <param name="value">The numeric value.</param>
/// <returns>Converts the given value to string.</returns>
std::string ToString(const float & value);
std::string ToString(const double & value);
/// <summary>
/// Parse the given string as a boolean value.
/// </summary>
/// <param name="str">The input string which contains a boolean string representation.</param>
/// <returns>Returns true when the given string is logically 'true'. Returns false otherwise.</returns>
bool ParseBoolean(const std::string & str);
/// <summary>
/// Parse the given string into the given numeric variable.
/// </summary>
/// <param name="str">The input string which contains a numeric value.</param>
/// <param name="value">The output numeric value.</param>
/// <returns>Returns true when the parsing is successful.</returns>
bool Parse(const std::string& str, int8_t & value);
bool Parse(const std::string& str, uint8_t & value);
bool Parse(const std::string& str, int16_t & value);
bool Parse(const std::string& str, uint16_t & value);
bool Parse(const std::string& str, int32_t & value);
bool Parse(const std::string& str, uint32_t & value);
bool Parse(const std::string& str, int64_t & value);
bool Parse(const std::string& str, uint64_t & value);
bool Parse(const std::string& str, float & value);
bool Parse(const std::string& str, double & value);
bool Parse(const std::string& str, bool & value);
/// <summary>
/// Capitalize the first character of the given string.
/// </summary>
/// <param name="value">The string value to uppercase the first character.</param>
/// <returns>Returns the given string with the first character capitalized.</returns>
std::string CapitalizeFirstCharacter(const std::string & value);
/// <summary>
/// Upper case all characters of the given string.
/// </summary>
/// <param name="value">The string value to uppercase.</param>
/// <returns>Returns the given string uppercased.</returns>
std::string Uppercase(const std::string & value);
/// <summary>
/// Lower case all characters of the given string.
/// </summary>
/// <param name="value">The string value to lowercase.</param>
/// <returns>Returns the given string lowercased.</returns>
std::string Lowercase(const std::string & value);
/// <summary>
/// Removes LF, CR or CRLF characters from the given string (unix and windows).
/// </summary>
/// <param name="buffer">The given buffer to modify.</param>
void RemoveEol(char * buffer);
/// <summary>
/// Removes LF, CR or CRLF characters from the given string (unix and windows).
/// </summary>
/// <param name="value">The given string to modify.</param>
void RemoveEol(std::string& value);
/// <summary>
/// Splits an input string into multiple string based on the given splitting character.
/// </summary>
/// <param name="text">The input text to split.</param>
/// <param name="split_characters">The splitting character.</param>
/// <returns>Returns a list of string.</returns>
StringVector Split(const std::string & text, char split_characters);
/// <summary>
/// Splits an input string into multiple string based on the given splitting character.
/// </summary>
/// <param name="text">The input text to split.</param>
/// <param name="split_pattern">The splitting pattern.</param>
/// <returns>Returns a list of string.</returns>
StringVector Split(const std::string & text, const char * split_pattern);
/// <summary>
/// Splits an input string into multiple string based on the given split character and store the result in values.
/// </summary>
/// <param name="values">The output list of string.</param>
/// <param name="text">The input text to split.</param>
/// <param name="split_characters">The splitting character.</param>
void Split(StringVector & values, const std::string & text, char split_character);
/// <summary>
/// Splits an input string into multiple string based on the given split pattern and store the result in values parameter.
/// </summary>
/// <param name="values">The output list of string.</param>
/// <param name="text">The input text to split.</param>
/// <param name="split_pattern">The splitting pattern.</param>
void Split(StringVector & values, const std::string & text, const char * split_pattern);
/// <summary>
/// Join a list of strings into a single string separating each element by separator.
/// </summary>
/// <param name="values">The list of elements to join.</param>
/// <param name="separator">The string separator.</param>
/// <returns>Returns a list of strings merged into a single string separating each element by separator.</returns>
std::string Join(const StringVector & values, const char * separator);
/// <summary>
/// Removes leading and trailing space characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <returns>Returns the trimmed string.</returns>
std::string Trim(const std::string & str);
/// <summary>
/// Removes specified leading and trailing characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <param name="c">The character to remove.</param>
/// <returns>Returns the trimmed string.</returns>
std::string Trim(const std::string & str, const char c);
/// <summary>
/// Removes trailing space characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <returns>Returns the trimmed string.</returns>
std::string TrimRight(const std::string & str);
/// <summary>
/// Removes leading space characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <returns>Returns the trimmed string.</returns>
std::string TrimLeft(const std::string & str);
/// <summary>
/// Remove specified trailing characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <param name="c">The character to remove.</param>
/// <returns>Returns the trimmed string.</returns>
std::string TrimRight(const std::string & str, const char c);
/// <summary>
/// Remove specified leading characters from a string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <param name="c">The character to remove.</param>
/// <returns>Returns the trimmed string.</returns>
std::string TrimLeft(const std::string & str, const char c);
/// <summary>
/// Reverse order each character of the given string.
/// </summary>
/// <param name="str">The string to trim.</param>
/// <returns>Returns a mirror copy of the given string.</returns>
std::string Reverse(const std::string & str);
/// <summary>
/// Format a string.
/// </summary>
/// <param name="format">The format of the string. Same as printf() format.</param>
/// <returns>Returns a formatted string with the given parameters inserted.</returns>
std::string Format(const char * format, ...);
} //namespace strings
} //namespace ra
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const void * value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const std::string & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const char * value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const int8_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const uint8_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const int16_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const uint16_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const int32_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const uint32_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const int64_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const uint64_t & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const float & value);
/// <summary>
/// Streams a value to an existing string.
/// </summary>
/// <param name="str">The destination string.</param>
/// <param name="value">The value to append to the given string.</param>
/// <returns>Returns the given string.</returns>
std::string& operator<<(std::string& str, const double & value);
#endif //RA_STRING_H