E-MailRelay
gstr.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gstr.h
19///
20
21#ifndef G_STR_H
22#define G_STR_H
23
24#include "gdef.h"
25#include "gexception.h"
26#include "gstrings.h"
27#include "gstringview.h"
28#include <string>
29#include <sstream>
30#include <iostream>
31#include <list>
32#include <vector>
33#include <set>
34#include <limits>
35#include <type_traits>
36
37namespace G
38{
39 class Str ;
40}
41
42//| \class G::Str
43/// A static class which provides string helper functions.
44///
45class G::Str
46{
47public:
48 G_EXCEPTION_CLASS( Overflow , "string conversion error: over/underflow" ) ;
49 G_EXCEPTION_CLASS( InvalidFormat, "string conversion error: invalid format" ) ;
50 G_EXCEPTION_CLASS( NotEmpty, "internal error: string container not empty" ) ;
51
52 struct Limited /// Overload discrimiator for G::Str::toUWhatever() requesting a range-limited result.
53 {} ;
54
55 struct Hex /// Overload discrimiator for G::Str::toUWhatever() indicating hexadecimal strings.
56 {} ;
57
58 static bool replace( std::string & s , const std::string & from , const std::string & to ,
59 std::size_t * pos_p = nullptr ) ;
60 ///< Replaces 'from' with 'to', starting at offset '*pos_p'.
61 ///< Returns true if a substitution was made, and adjusts
62 ///< '*pos_p' by to.length().
63
64 static void replace( std::string & s , char from , char to ) ;
65 ///< Replaces all 'from' characters with 'to'.
66
67 static void replace( StringArray & , char from , char to ) ;
68 ///< Replaces 'from' characters with 'to' in all the strings in the array.
69
70 static unsigned int replaceAll( std::string & s , const std::string & from , const std::string & to ) ;
71 ///< Does a global replace on string 's', replacing all occurrences
72 ///< of sub-string 'from' with 'to'. Returns the number of substitutions
73 ///< made. Consider using in a while loop if 'from' is more than one
74 ///< character.
75
76 static unsigned int replaceAll( std::string & s , const char * from , const char * to ) ;
77 ///< A c-string overload, provided for performance reasons.
78
79 static std::string replaced( const std::string & s , char from , char to ) ;
80 ///< Returns the string 's' with all occurrences of 'from' replaced by 'to'.
81
82 static void removeAll( std::string & , char ) ;
83 ///< Removes all occurrences of the character from the string. See also only().
84
85 static std::string & trimLeft( std::string & s , string_view ws , std::size_t limit = 0U ) ;
86 ///< Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
87 ///< Returns s.
88
89 static std::string & trimRight( std::string & s , string_view ws , std::size_t limit = 0U ) ;
90 ///< Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
91 ///< Returns s.
92
93 static std::string & trim( std::string & s , string_view ws ) ;
94 ///< Trims both ends of s, taking off any of the 'ws' characters.
95 ///< Returns s.
96
97 static std::string trimmed( const std::string & s , string_view ws ) ;
98 ///< Returns a trim()med version of s.
99
100 static std::string trimmed( std::string && s , string_view ws ) ;
101 ///< Returns a trim()med version of s.
102
103 static bool isNumeric( const std::string & s , bool allow_minus_sign = false ) ;
104 ///< Returns true if every character is a decimal digit.
105 ///< Empty strings return true.
106
107 static bool isHex( const std::string & s ) ;
108 ///< Returns true if every character is a hexadecimal digit.
109 ///< Empty strings return true.
110
111 static bool isPrintableAscii( const std::string & s ) ;
112 ///< Returns true if every character is a 7-bit, non-control
113 ///< character (ie. 0x20<=c<0x7f). Empty strings return true.
114
115 static bool isUShort( const std::string & s ) ;
116 ///< Returns true if the string can be converted into
117 ///< an unsigned short without throwing an exception.
118
119 static bool isUInt( const std::string & s ) ;
120 ///< Returns true if the string can be converted into
121 ///< an unsigned integer without throwing an exception.
122
123 static bool isULong( const std::string & s ) ;
124 ///< Returns true if the string can be converted into
125 ///< an unsigned long without throwing an exception.
126
127 static bool isInt( const std::string & s ) ;
128 ///< Returns true if the string can be converted into
129 ///< an integer without throwing an exception.
130
131 static std::string fromBool( bool b ) ;
132 ///< Converts boolean 'b' to a string.
133
134 static std::string fromDouble( double d ) ;
135 ///< Converts double 'd' to a string.
136
137 static std::string fromInt( int i ) ;
138 ///< Converts int 'i' to a string.
139
140 static std::string fromLong( long l ) ;
141 ///< Converts long 'l' to a string.
142
143 static std::string fromShort( short s ) ;
144 ///< Converts short 's' to a string.
145
146 static std::string fromUInt( unsigned int ui ) ;
147 ///< Converts unsigned int 'ui' to a string.
148
149 static std::string fromULong( unsigned long ul ) ;
150 ///< Converts unsigned long 'ul' to a string.
151
152 static std::string fromUShort( unsigned short us ) ;
153 ///< Converts unsigned short 'us' to a string.
154
155 static bool toBool( const std::string & s ) ;
156 ///< Converts string 's' to a bool.
157 ///<
158 ///< Exception: InvalidFormat
159
160 static double toDouble( const std::string & s ) ;
161 ///< Converts string 's' to a double.
162 ///<
163 ///< Exception: Overflow
164 ///< Exception: InvalidFormat
165
166 static int toInt( const std::string & s ) ;
167 ///< Converts string 's' to an int.
168 ///<
169 ///< Exception: Overflow
170 ///< Exception: InvalidFormat
171
172 static long toLong( const std::string & s ) ;
173 ///< Converts string 's' to a long.
174 ///<
175 ///< Exception: Overflow
176 ///< Exception: InvalidFormat
177
178 static short toShort( const std::string & s ) ;
179 ///< Converts string 's' to a short.
180 ///<
181 ///< Exception: Overflow
182 ///< Exception: InvalidFormat
183
184 static unsigned int toUInt( const std::string & s ) ;
185 ///< Converts string 's' to an unsigned int.
186 ///<
187 ///< Exception: Overflow
188 ///< Exception: InvalidFormat
189
190 static unsigned int toUInt( const std::string & s , Limited ) ;
191 ///< Converts string 's' to an unsigned int.
192 ///<
193 ///< Very large numeric strings are limited to the maximum
194 ///< value of the numeric type, without an Overflow exception.
195 ///<
196 ///< Exception: InvalidFormat
197
198 static unsigned int toUInt( const std::string & s1 , const std::string & s2 ) ;
199 ///< Overload that converts the first string if it can be converted
200 ///< without throwing, or otherwise the second string.
201
202 static unsigned long toULong( const std::string & s , Limited ) ;
203 ///< Converts string 's' to an unsigned long.
204 ///<
205 ///< Very large numeric strings are limited to the maximum value
206 ///< of the numeric type, without an Overflow exception.
207 ///<
208 ///< Exception: InvalidFormat
209
210 static unsigned long toULong( const std::string & s , Hex ) ;
211 ///< An overload for hexadecimal strings. To avoid exceptions
212 ///< use isHex() and check the string length.
213
214 static unsigned long toULong( const std::string & s , Hex , Limited ) ;
215 ///< An overload for hexadecimal strings where overflow
216 ///< results in the return of the maximum value. To avoid
217 ///< exceptions use isHex().
218
219 template <typename T> static T toUnsigned( const char * p , const char * end ,
220 bool & overflow , bool & invalid ) noexcept ;
221 ///< Low-level conversion from an unsigned decimal string to a number.
222 ///< All characters in the range are used; any character
223 ///< not 0..9 yields an 'invalid' result.
224
225 template <typename T> static T toUnsigned( const char * &p , const char * end ,
226 bool & overflow ) noexcept ;
227 ///< Low-level conversion from an unsigned decimal string to a number.
228 ///< Consumes charaters until the first invalid character.
229
230 static unsigned long toULong( const std::string & s ) ;
231 ///< Converts string 's' to an unsigned long.
232 ///<
233 ///< Exception: Overflow
234 ///< Exception: InvalidFormat
235
236 static unsigned long toULong( const std::string & s1 , const std::string & s2 ) ;
237 ///< Overload that converts the first string if it can be converted
238 ///< without throwing, or otherwise the second string.
239
240 static unsigned short toUShort( const std::string & s , Limited ) ;
241 ///< Converts string 's' to an unsigned short.
242 ///<
243 ///< Very large numeric strings are limited to the maximum value
244 ///< of the numeric type, without an Overflow exception.
245 ///<
246 ///< Exception: InvalidFormat
247
248 static unsigned short toUShort( const std::string & s ) ;
249 ///< Converts string 's' to an unsigned short.
250 ///<
251 ///< Exception: Overflow
252 ///< Exception: InvalidFormat
253
254 static void toUpper( std::string & s ) ;
255 ///< Replaces all Latin-1 lower-case characters in string 's' by
256 ///< upper-case characters.
257
258 static void toLower( std::string & s ) ;
259 ///< Replaces all Latin-1 upper-case characters in string 's' by
260 ///< lower-case characters.
261
262 static std::string upper( const std::string & s ) ;
263 ///< Returns a copy of 's' in which all Latin-1 lower-case characters
264 ///< have been replaced by upper-case characters.
265
266 static std::string lower( const std::string & s ) ;
267 ///< Returns a copy of 's' in which all Latin-1 upper-case characters
268 ///< have been replaced by lower-case characters.
269
270 static std::string toPrintableAscii( const std::string & in , char escape = '\\' ) ;
271 ///< Returns a 7-bit printable representation of the given input string.
272
273 static std::string toPrintableAscii( const std::wstring & in , wchar_t escape = L'\\' ) ;
274 ///< Returns a 7-bit printable representation of the given wide input string.
275
276 static std::string printable( const std::string & in , char escape = '\\' ) ;
277 ///< Returns a printable representation of the given input string, using
278 ///< chacter code ranges 0x20 to 0x7e and 0xa0 to 0xfe inclusive.
279 ///< Typically used to prevent escape sequences getting into log files.
280
281 static std::string printable( std::string && in , char escape = '\\' ) ;
282 ///< Returns a printable representation of the given input string, using
283 ///< chacter code ranges 0x20 to 0x7e and 0xa0 to 0xfe inclusive.
284 ///< Typically used to prevent escape sequences getting into log files.
285
286 static std::string only( const std::string & allow_chars , const std::string & s ) ;
287 ///< Returns the 's' with all occurrences of the characters not appearing in
288 ///< the fist string deleted.
289
290 static void escape( std::string & s , char c_escape , const std::string & specials_in ,
291 const std::string & specials_out ) ;
292 ///< Prefixes each occurrence of one of the special-in characters with
293 ///< the escape character and its corresponding special-out character.
294 ///<
295 ///< If the specials-in string contains the nul character it must be
296 ///< at the end, otherwise the two specials strings must be the same
297 ///< length. The specials-out string cannot contain the nul character.
298 ///<
299 ///< The specials-in string should normally include the escape character
300 ///< itself, otherwise unescaping will not recover the original.
301
302 static void escape( std::string & s , char c_escape , const char * specials_in ,
303 const char * specials_out ) ;
304 ///< Overload for c-style 'special' strings.
305
306 static void escape( std::string & s ) ;
307 ///< Overload for 'normal' backslash escaping of whitespace.
308
309 static std::string escaped( const std::string & , char c_escape , const std::string & specials_in ,
310 const std::string & specials_out ) ;
311 ///< Returns the escape()d string.
312
313 static std::string escaped( const std::string & , char c_escape , const char * specials_in ,
314 const char * specials_out ) ;
315 ///< Returns the escape()d string.
316
317 static std::string escaped( const std::string & ) ;
318 ///< Returns the escape()d string.
319
320 static void unescape( std::string & s , char c_escape , const char * specials_in , const char * specials_out ) ;
321 ///< Unescapes the string by replacing e-e with e, e-special-in with
322 ///< special-out, and e-other with other.
323 ///<
324 ///< If the specials-out string includes the nul character then it must be at
325 ///< the end, otherwise the two specials strings must be the same length.
326
327 static void unescape( std::string & s ) ;
328 ///< Overload for 'normal' unescaping where the string has backslash escaping
329 ///< of whitespace.
330
331 static std::string unescaped( const std::string & s ) ;
332 ///< Returns the unescape()d version of s.
333
334 static string_view meta() ;
335 ///< Returns a list of shell meta-characters with a tilde as the
336 ///< first character. Does not contain the nul character. This is
337 ///< typically used with escape().
338
339 static std::string readLineFrom( std::istream & stream , const std::string & eol = std::string() ) ;
340 ///< Reads a line from the stream using the given line terminator.
341 ///< The line terminator is not part of the returned string.
342 ///< The terminator defaults to the newline.
343 ///<
344 ///< Note that alternatives in the standard library such as
345 ///< std::istream::getline() or std::getline(stream,string)
346 ///< in the standard "string" header are limited to a single
347 ///< character as the terminator.
348 ///<
349 ///< The stream's fail bit is set if (1) an empty string was
350 ///< returned because the stream was already at eof or (2)
351 ///< the string overflowed. Therefore, ignoring overflow, if
352 ///< the stream ends in an incomplete line that line fragment
353 ///< is returned with the stream's eof flag set but the fail bit
354 ///< reset and the next attempted read will return an empty string
355 ///< with the fail bit set. If the stream ends with a complete
356 ///< line then the last line is returned with eof and fail
357 ///< bits reset and the next attempted read will return
358 ///< an empty string with eof and fail bits set.
359 ///<
360 ///< Boolean tests on a stream are equivalent to using fail(),
361 ///< and fail() tests for failbit or badbit, so to process
362 ///< even incomplete lines at the end we can use a read
363 ///< loop like "while(s.good()){read(s);if(s)...}".
364
365 static void readLineFrom( std::istream & stream , const std::string & eol , std::string & result ,
366 bool pre_erase_result = true ) ;
367 ///< An overload which avoids string copying.
368
369 static void splitIntoTokens( const std::string & in , StringArray & out , string_view ws , char esc = '\0' ) ;
370 ///< Splits the string into 'ws'-delimited tokens. The behaviour is like
371 ///< strtok() in that adjacent delimiters count as one and leading and
372 ///< trailing delimiters are ignored. The output array is _not_ cleared
373 ///< first; new tokens are appended to the output list. If the escape
374 ///< character is supplied then it can be used to escape whitespace
375 ///< characters, preventing a split, with those escape characters being
376 ///< consumed in the process. For shell-like tokenising use dequote()
377 ///< before splitIntoTokens(), and revert the non-breaking spaces
378 ///< afterwards.
379
380 static StringArray splitIntoTokens( const std::string & in , string_view ws = Str::ws() , char esc = '\0' ) ;
381 ///< Overload that returns by value.
382
383 static void splitIntoFields( const std::string & in , StringArray & out ,
384 string_view ws , char escape = '\0' ,
385 bool remove_escapes = true ) ;
386 ///< Splits the string into fields. Duplicated, leading and trailing
387 ///< separator characters are all significant. The output array is
388 ///< cleared first.
389 ///<
390 ///< If a non-null escape character is given then escaped separators
391 ///< do not result in a split. If the 'remove-escapes' parameter is
392 ///< true then all unescaped escapes are removed from the output; this
393 ///< is generally the preferred behaviour for nested escaping. If the
394 ///< 'remove-escapes' parameter is false then escapes are not removed;
395 ///< unescaped escapes are used to prevent splitting but they remain
396 ///< in the output.
397
398 static StringArray splitIntoFields( const std::string & in , string_view ws = Str::ws() ) ;
399 ///< Overload that returns by value.
400
401 static std::string dequote( const std::string & , char qq = '\"' , char esc = '\\' ,
402 string_view ws = Str::ws() , string_view nbws = Str::ws() ) ;
403 ///< Dequotes a string by removing unescaped quotes and escaping
404 ///< quoted whitespace, so "qq-aaa-esc-qq-bbb-ws-ccc-qq" becomes
405 ///< "aaa-qq-bbb-esc-ws-ccc". Escaped whitespace characters within
406 ///< quotes can optionally be converted to non-breaking equivalents.
407
408 static std::string join( const std::string & sep , const StringArray & strings ) ;
409 ///< Concatenates an array of strings with separators.
410
411 static std::string join( const std::string & sep , const std::set<std::string> & strings ) ;
412 ///< Concatenates a set of strings with separators.
413
414 static std::string join( const std::string & sep , const std::string & s1 , const std::string & s2 ,
415 const std::string & s3 = std::string() , const std::string & s4 = std::string() , const std::string & s5 = std::string() ,
416 const std::string & s6 = std::string() , const std::string & s7 = std::string() , const std::string & s8 = std::string() ,
417 const std::string & s9 = std::string() ) ;
418 ///< Concatenates a small number of strings with separators.
419 ///< In this overload empty strings are ignored.
420
421 static std::string join( const std::string & sep , const StringMap & ,
422 const std::string & eq = std::string(1U,'=') , const std::string & tail = std::string() ) ;
423 ///< Concatenates entries in a map, where an entry is "<key><eq><value><tail>".
424
425 static std::set<std::string> keySet( const StringMap & string_map ) ;
426 ///< Extracts the keys from a map of strings.
427
428 static StringArray keys( const StringMap & string_map ) ;
429 ///< Extracts the keys from a map of strings.
430
431 static std::string head( const std::string & in , std::size_t pos ,
432 const std::string & default_ = std::string() ) ;
433 ///< Returns the first part of the string up to just before the given position.
434 ///< The character at pos is not returned. Returns the supplied default
435 ///< if pos is npos. Returns the whole string if pos is one-or-more
436 ///< off the end.
437
438 static std::string head( const std::string & in , const std::string & sep , bool default_empty = true ) ;
439 ///< Overload taking a separator string, and with the default
440 ///< as either the input string or the empty string. If the
441 ///< separator occurs more than once in the input then only the
442 ///< first occurrence is relevant.
443
444 static std::string tail( const std::string & in , std::size_t pos ,
445 const std::string & default_ = std::string() ) ;
446 ///< Returns the last part of the string after the given position.
447 ///< The character at pos is not returned. Returns the supplied default
448 ///< if pos is npos. Returns the empty string if pos is one-or-more
449 ///< off the end.
450
451 static std::string tail( const std::string & in , const std::string & sep , bool default_empty = true ) ;
452 ///< Overload taking a separator string, and with the default
453 ///< as either the input string or the empty string. If the
454 ///< separator occurs more than once in the input then only the
455 ///< first occurrence is relevant.
456
457 static bool match( const std::string & , const std::string & ) ;
458 ///< Returns true if the two strings are the same.
459
460 static bool match( const StringArray & , const std::string & ) ;
461 ///< Returns true if any string in the array matches the given string.
462
463 static bool iless( const std::string & , const std::string & ) ;
464 ///< Returns true if the first string is lexicographically less
465 ///< than the first, after Latin-1 lower-case letters have been
466 ///< folded to upper-case.
467
468 static bool imatch( char , char ) ;
469 ///< Returns true if the two characters are the same, ignoring Latin-1 case.
470
471 static bool imatch( const std::string & , const std::string & ) ;
472 ///< Returns true if the two strings are the same, ignoring Latin-1 case.
473 ///< The locale is ignored.
474
475 static bool imatch( const StringArray & , const std::string & ) ;
476 ///< Returns true if any string in the array matches the given string, ignoring
477 ///< Latin-1 case. The locale is ignored.
478
479 static std::size_t ifind( const std::string & s , const std::string & key ,
480 std::size_t pos = 0U ) ;
481 ///< Returns the position of the key in 's' using a Latin-1 case-insensitive
482 ///< search. Returns std::string::npos if not found. The locale is ignored.
483
484 static bool tailMatch( const std::string & in , const std::string & ending ) ;
485 ///< Returns true if the string has the given ending (or the given ending is empty).
486
487 static bool tailMatch( const StringArray & in , const std::string & ending ) ;
488 ///< Returns true if any string in the array has the given ending
489 ///< (or the given ending is empty).
490
491 static bool headMatch( const std::string & in , const std::string & head ) ;
492 ///< Returns true if the string has the given start (or head is empty).
493
494 static bool headMatch( const std::string & in , const char * head ) ;
495 ///< A c-string overload.
496
497 static bool headMatch( const StringArray & in , const std::string & head ) ;
498 ///< Returns true if any string in the array has the given start
499 ///< (or head is empty).
500
501 static std::string headMatchResidue( const StringArray & in , const std::string & head ) ;
502 ///< Returns the unmatched part of the first string in the array that has
503 ///< the given start. Returns the empty string if nothing matches or if
504 ///< the first match is an exact match.
505
506 static string_view ws() ;
507 ///< Returns a string of standard whitespace characters.
508
509 static string_view alnum() ;
510 ///< Returns a string of seven-bit alphanumeric characters, ie A-Z, a-z and 0-9.
511
512 static std::string positive() ;
513 ///< Returns a default positive string. See isPositive().
514
515 static std::string negative() ;
516 ///< Returns a default negative string. See isNegative().
517
518 static bool isPositive( const std::string & ) ;
519 ///< Returns true if the string has a positive meaning, such as "1", "true", "yes".
520
521 static bool isNegative( const std::string & ) ;
522 ///< Returns true if the string has a negative meaning, such as "0", "false", "no".
523
524 static std::string unique( const std::string & s , char c , char r ) ;
525 ///< Returns a string with repeated 'c' characters replaced by
526 ///< one 'r' character. Single 'c' characters are not replaced.
527
528 static std::string unique( const std::string & s , char c ) ;
529 ///< An overload that replaces repeated 'c' characters by
530 ///< one 'c' character.
531
532 static StringArray::iterator keepMatch( StringArray::iterator begin , StringArray::iterator end ,
533 const StringArray & match_list , bool ignore_case = false ) ;
534 ///< Removes items in the begin/end list that do not match any of the
535 ///< elements in the match-list (whitelist), but keeps everything (by
536 ///< returning 'end') if the match-list is empty. Returns an iterator
537 ///< for erase().
538
539 static StringArray::iterator removeMatch( StringArray::iterator begin , StringArray::iterator end ,
540 const StringArray & match_list , bool ignore_case = false ) ;
541 ///< Removes items in the begin/end list that match one of the elements
542 ///< in the match-list (blocklist). (Removes nothing if the match-list is
543 ///< empty.) Returns an iterator for erase().
544
545 static constexpr std::size_t truncate = (~(static_cast<std::size_t>(0U))) ;
546 ///< A special value for the G::Str::strncpy_s() 'count' parameter.
547
548 static errno_t strncpy_s( char * dst , std::size_t n_dst , const char * src , std::size_t count ) noexcept ;
549 ///< Does the same as windows strncpy_s(). Copies count characters
550 ///< from src to dst and adds a terminator character, but fails
551 ///< if dst is too small. If the count is 'truncate' then as
552 ///< much of src is copied as will fit in dst, allowing for the
553 ///< terminator character. Returns zero on success or on
554 ///< truncation (unlike windows strncpy_s()).
555
556public:
557 Str() = delete ;
558} ;
559
560inline
561std::string G::Str::fromInt( int i )
562{
563 return std::to_string( i ) ;
564}
565
566inline
567std::string G::Str::fromLong( long l )
568{
569 return std::to_string( l ) ;
570}
571
572inline
573std::string G::Str::fromShort( short s )
574{
575 return std::to_string( s ) ;
576}
577
578inline
579std::string G::Str::fromUInt( unsigned int ui )
580{
581 return std::to_string( ui ) ;
582}
583
584inline
585std::string G::Str::fromULong( unsigned long ul )
586{
587 return std::to_string( ul ) ;
588}
589
590inline
591std::string G::Str::fromUShort( unsigned short us )
592{
593 return std::to_string( us ) ;
594}
595
596template <typename T>
597T G::Str::toUnsigned( const char * p , const char * end , bool & overflow , bool & invalid ) noexcept
598{
599 if( p == nullptr || end == nullptr || p == end )
600 {
601 invalid = true ;
602 overflow = false ;
603 return 0UL ;
604 }
605 T result = toUnsigned<T>( p , end , overflow ) ;
606 if( p != end )
607 invalid = true ;
608 return result ;
609}
610
611template <typename T>
612T G::Str::toUnsigned( const char * &p , const char * end , bool & overflow ) noexcept
613{
614 static_assert( std::is_integral<T>::value , "" ) ;
615 T result = 0 ;
616 for( ; p != end ; p++ )
617 {
618 T n = 0 ;
619 if( *p == '0' ) n = 0 ;
620 else if( *p == '1' ) n = 1 ;
621 else if( *p == '2' ) n = 2 ;
622 else if( *p == '3' ) n = 3 ;
623 else if( *p == '4' ) n = 4 ;
624 else if( *p == '5' ) n = 5 ;
625 else if( *p == '6' ) n = 6 ;
626 else if( *p == '7' ) n = 7 ;
627 else if( *p == '8' ) n = 8 ;
628 else if( *p == '9' ) n = 9 ;
629 else break ;
630
631 auto old = result ;
632 result = result * 10 ;
633 result += n ;
634 if( result < old )
635 overflow = true ;
636 }
637 return result ;
638}
639
640#endif
A static class which provides string helper functions.
Definition: gstr.h:46
static std::string fromShort(short s)
Converts short 's' to a string.
Definition: gstr.h:573
static void unescape(std::string &s, char c_escape, const char *specials_in, const char *specials_out)
Unescapes the string by replacing e-e with e, e-special-in with special-out, and e-other with other.
Definition: gstr.cpp:218
static string_view alnum()
Returns a string of seven-bit alphanumeric characters, ie A-Z, a-z and 0-9.
Definition: gstr.cpp:1261
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1362
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1356
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1195
static bool imatch(char, char)
Returns true if the two characters are the same, ignoring Latin-1 case.
Definition: gstr.cpp:1414
static constexpr std::size_t truncate
A special value for the G::Str::strncpy_s() 'count' parameter.
Definition: gstr.h:545
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
Definition: gstr.cpp:405
static bool toBool(const std::string &s)
Converts string 's' to a bool.
Definition: gstr.cpp:474
static void toLower(std::string &s)
Replaces all Latin-1 upper-case characters in string 's' by lower-case characters.
Definition: gstr.cpp:735
static string_view ws()
Returns a string of standard whitespace characters.
Definition: gstr.cpp:1255
static std::set< std::string > keySet(const StringMap &string_map)
Extracts the keys from a map of strings.
Definition: gstr.cpp:1238
static StringArray keys(const StringMap &string_map)
Extracts the keys from a map of strings.
Definition: gstr.cpp:1246
static std::string positive()
Returns a default positive string. See isPositive().
Definition: gstr.cpp:1346
static std::string tail(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:1287
static void splitIntoFields(const std::string &in, StringArray &out, string_view ws, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:1146
static std::string unescaped(const std::string &s)
Returns the unescape()d version of s.
Definition: gstr.cpp:242
static std::string fromULong(unsigned long ul)
Converts unsigned long 'ul' to a string.
Definition: gstr.h:585
static bool isPrintableAscii(const std::string &s)
Returns true if every character is a 7-bit, non-control character (ie.
Definition: gstr.cpp:420
static T toUnsigned(const char *p, const char *end, bool &overflow, bool &invalid) noexcept
Low-level conversion from an unsigned decimal string to a number.
Definition: gstr.h:597
static double toDouble(const std::string &s)
Converts string 's' to a double.
Definition: gstr.cpp:487
static std::string replaced(const std::string &s, char from, char to)
Returns the string 's' with all occurrences of 'from' replaced by 'to'.
Definition: gstr.cpp:311
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
Definition: gstr.cpp:462
static std::string & trimLeft(std::string &s, string_view ws, std::size_t limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
Definition: gstr.cpp:335
static void splitIntoTokens(const std::string &in, StringArray &out, string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:1073
static std::string escaped(const std::string &, char c_escape, const std::string &specials_in, const std::string &specials_out)
Returns the escape()d string.
Definition: gstr.cpp:101
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
Definition: gstr.cpp:905
static unsigned short toUShort(const std::string &s, Limited)
Converts string 's' to an unsigned short.
Definition: gstr.cpp:700
static std::string unique(const std::string &s, char c, char r)
Returns a string with repeated 'c' characters replaced by one 'r' character.
Definition: gstr.cpp:1472
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the string has the given ending (or the given ending is empty).
Definition: gstr.cpp:1302
static bool isULong(const std::string &s)
Returns true if the string can be converted into an unsigned long without throwing an exception.
Definition: gstr.cpp:453
static long toLong(const std::string &s)
Converts string 's' to a long.
Definition: gstr.cpp:529
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string. See also only().
Definition: gstr.cpp:318
static StringArray::iterator removeMatch(StringArray::iterator begin, StringArray::iterator end, const StringArray &match_list, bool ignore_case=false)
Removes items in the begin/end list that match one of the elements in the match-list (blocklist).
Definition: gstr.cpp:1508
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lower-case characters have been replaced by upper-case cha...
Definition: gstr.cpp:754
static std::string fromInt(int i)
Converts int 'i' to a string.
Definition: gstr.h:561
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1351
static bool match(const std::string &, const std::string &)
Returns true if the two strings are the same.
Definition: gstr.cpp:1368
static string_view meta()
Returns a list of shell meta-characters with a tilde as the first character.
Definition: gstr.cpp:1267
static std::string & trimRight(std::string &s, string_view ws, std::size_t limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
Definition: gstr.cpp:347
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
Definition: gstr.h:579
static std::string dequote(const std::string &, char qq='\"' , char esc = '\\' , string_view ws = Str::ws() , string_view nbws = Str::ws() )
Dequotes a string by removing unescaped quotes and escaping quoted whitespace, so "qq-aaa-esc-qq-bbb-...
Definition: gstr.cpp:167
static bool isHex(const std::string &s)
Returns true if every character is a hexadecimal digit.
Definition: gstr.cpp:414
static std::string printable(const std::string &in, char escape='\\')
Returns a printable representation of the given input string, using chacter code ranges 0x20 to 0x7e ...
Definition: gstr.cpp:885
static std::string only(const std::string &allow_chars, const std::string &s)
Returns the 's' with all occurrences of the characters not appearing in the fist string deleted.
Definition: gstr.cpp:323
static bool headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
Definition: gstr.cpp:1322
static std::string headMatchResidue(const StringArray &in, const std::string &head)
Returns the unmatched part of the first string in the array that has the given start.
Definition: gstr.cpp:1335
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
Definition: gstr.h:591
static void toUpper(std::string &s)
Replaces all Latin-1 lower-case characters in string 's' by upper-case characters.
Definition: gstr.cpp:748
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurrences of sub-string 'from' with 'to'.
Definition: gstr.cpp:287
static int toInt(const std::string &s)
Converts string 's' to an int.
Definition: gstr.cpp:507
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:604
static unsigned long toULong(const std::string &s, Limited)
Converts string 's' to an unsigned long.
Definition: gstr.cpp:626
static std::string fromDouble(double d)
Converts double 'd' to a string.
Definition: gstr.cpp:467
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception.
Definition: gstr.cpp:444
static errno_t strncpy_s(char *dst, std::size_t n_dst, const char *src, std::size_t count) noexcept
Does the same as windows strncpy_s().
Definition: gstr.cpp:1528
static std::string head(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:1273
static std::string trimmed(const std::string &s, string_view ws)
Returns a trim()med version of s.
Definition: gstr.cpp:364
static std::string fromLong(long l)
Converts long 'l' to a string.
Definition: gstr.h:567
static bool isInt(const std::string &s)
Returns true if the string can be converted into an integer without throwing an exception.
Definition: gstr.cpp:426
static StringArray::iterator keepMatch(StringArray::iterator begin, StringArray::iterator end, const StringArray &match_list, bool ignore_case=false)
Removes items in the begin/end list that do not match any of the elements in the match-list (whitelis...
Definition: gstr.cpp:1498
static bool replace(std::string &s, const std::string &from, const std::string &to, std::size_t *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
Definition: gstr.cpp:264
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 upper-case characters have been replaced by lower-case cha...
Definition: gstr.cpp:741
static bool iless(const std::string &, const std::string &)
Returns true if the first string is lexicographically less than the first, after Latin-1 lower-case l...
Definition: gstr.cpp:1402
static void escape(std::string &s, char c_escape, const std::string &specials_in, const std::string &specials_out)
Prefixes each occurrence of one of the special-in characters with the escape character and its corres...
Definition: gstr.cpp:158
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
Definition: gstr.cpp:924
static bool isUShort(const std::string &s)
Returns true if the string can be converted into an unsigned short without throwing an exception.
Definition: gstr.cpp:435
static std::string & trim(std::string &s, string_view ws)
Trims both ends of s, taking off any of the 'ws' characters.
Definition: gstr.cpp:359
static short toShort(const std::string &s)
Converts string 's' to a short.
Definition: gstr.cpp:564
static std::size_t ifind(const std::string &s, const std::string &key, std::size_t pos=0U)
Returns the position of the key in 's' using a Latin-1 case-insensitive search.
Definition: gstr.cpp:1442
A class template like c++17's std::basic_string_view.
Definition: gstringview.h:73
Low-level classes.
Definition: galign.h:28
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:32
Overload discrimiator for G::Str::toUWhatever() indicating hexadecimal strings.
Definition: gstr.h:56
Overload discrimiator for G::Str::toUWhatever() requesting a range-limited result.
Definition: gstr.h:53