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" ) ;
58 static bool replace( std::string & s ,
const std::string & from ,
const std::string & to ,
59 std::size_t * pos_p =
nullptr ) ;
64 static void replace( std::string & s ,
char from ,
char to ) ;
70 static unsigned int replaceAll( std::string & s ,
const std::string & from ,
const std::string & to ) ;
76 static unsigned int replaceAll( std::string & s ,
const char * from ,
const char * to ) ;
79 static std::string
replaced(
const std::string & s ,
char from ,
char to ) ;
82 static void removeAll( std::string & ,
char ) ;
103 static bool isNumeric(
const std::string & s ,
bool allow_minus_sign =
false ) ;
107 static bool isHex(
const std::string & s ) ;
115 static bool isUShort(
const std::string & s ) ;
119 static bool isUInt(
const std::string & s ) ;
123 static bool isULong(
const std::string & s ) ;
127 static bool isInt(
const std::string & s ) ;
131 static std::string
fromBool(
bool b ) ;
137 static std::string
fromInt(
int i ) ;
140 static std::string
fromLong(
long l ) ;
143 static std::string
fromShort(
short s ) ;
146 static std::string
fromUInt(
unsigned int ui ) ;
149 static std::string
fromULong(
unsigned long ul ) ;
152 static std::string
fromUShort(
unsigned short us ) ;
155 static bool toBool(
const std::string & s ) ;
160 static double toDouble(
const std::string & s ) ;
166 static int toInt(
const std::string & s ) ;
172 static long toLong(
const std::string & s ) ;
178 static short toShort(
const std::string & s ) ;
184 static unsigned int toUInt(
const std::string & s ) ;
190 static unsigned int toUInt(
const std::string & s ,
Limited ) ;
198 static unsigned int toUInt(
const std::string & s1 ,
const std::string & s2 ) ;
210 static unsigned long toULong(
const std::string & s ,
Hex ) ;
219 template <
typename T>
static T
toUnsigned(
const char * p ,
const char * end ,
220 bool & overflow ,
bool & invalid ) noexcept ;
225 template <
typename T>
static T
toUnsigned(
const char * &p ,
const char * end ,
226 bool & overflow ) noexcept ;
230 static unsigned long toULong(
const std::string & s ) ;
236 static unsigned long toULong(
const std::string & s1 ,
const std::string & s2 ) ;
248 static unsigned short toUShort(
const std::string & s ) ;
254 static void toUpper( std::string & s ) ;
258 static void toLower( std::string & s ) ;
262 static std::string
upper(
const std::string & s ) ;
266 static std::string
lower(
const std::string & s ) ;
276 static std::string
printable(
const std::string & in ,
char escape =
'\\' ) ;
281 static std::string
printable( std::string && in ,
char escape =
'\\' ) ;
286 static std::string
only(
const std::string & allow_chars ,
const std::string & s ) ;
290 static void escape( std::string & s ,
char c_escape ,
const std::string & specials_in ,
291 const std::string & specials_out ) ;
302 static void escape( std::string & s ,
char c_escape ,
const char * specials_in ,
303 const char * specials_out ) ;
306 static void escape( std::string & s ) ;
309 static std::string
escaped(
const std::string & ,
char c_escape ,
const std::string & specials_in ,
310 const std::string & specials_out ) ;
313 static std::string
escaped(
const std::string & ,
char c_escape ,
const char * specials_in ,
314 const char * specials_out ) ;
317 static std::string
escaped(
const std::string & ) ;
320 static void unescape( std::string & s ,
char c_escape ,
const char * specials_in ,
const char * specials_out ) ;
327 static void unescape( std::string & s ) ;
331 static std::string
unescaped(
const std::string & s ) ;
339 static std::string
readLineFrom( std::istream & stream ,
const std::string & eol = std::string() ) ;
365 static void readLineFrom( std::istream & stream ,
const std::string & eol , std::string & result ,
366 bool pre_erase_result =
true ) ;
385 bool remove_escapes =
true ) ;
401 static std::string
dequote(
const std::string & ,
char qq =
'\"' ,
char esc =
'\\' ,
408 static std::string
join(
const std::string & sep ,
const StringArray & strings ) ;
411 static std::string
join(
const std::string & sep ,
const std::set<std::string> & strings ) ;
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() ) ;
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() ) ;
431 static std::string
head(
const std::string & in , std::size_t pos ,
432 const std::string & default_ = std::string() ) ;
438 static std::string
head(
const std::string & in ,
const std::string & sep ,
bool default_empty =
true ) ;
444 static std::string
tail(
const std::string & in , std::size_t pos ,
445 const std::string & default_ = std::string() ) ;
451 static std::string
tail(
const std::string & in ,
const std::string & sep ,
bool default_empty =
true ) ;
457 static bool match(
const std::string & ,
const std::string & ) ;
463 static bool iless(
const std::string & ,
const std::string & ) ;
468 static bool imatch(
char ,
char ) ;
471 static bool imatch(
const std::string & ,
const std::string & ) ;
479 static std::size_t
ifind(
const std::string & s ,
const std::string & key ,
480 std::size_t pos = 0U ) ;
484 static bool tailMatch(
const std::string & in ,
const std::string & ending ) ;
491 static bool headMatch(
const std::string & in ,
const std::string &
head ) ;
494 static bool headMatch(
const std::string & in ,
const char *
head ) ;
518 static bool isPositive(
const std::string & ) ;
521 static bool isNegative(
const std::string & ) ;
524 static std::string
unique(
const std::string & s ,
char c ,
char r ) ;
528 static std::string
unique(
const std::string & s ,
char c ) ;
532 static StringArray::iterator
keepMatch( StringArray::iterator begin , StringArray::iterator end ,
533 const StringArray & match_list ,
bool ignore_case =
false ) ;
539 static StringArray::iterator
removeMatch( StringArray::iterator begin , StringArray::iterator end ,
540 const StringArray & match_list ,
bool ignore_case =
false ) ;
545 static constexpr std::size_t
truncate = (~(
static_cast<std::size_t
>(0U))) ;
548 static errno_t
strncpy_s(
char * dst , std::size_t n_dst ,
const char * src , std::size_t count ) noexcept ;
563 return std::to_string( i ) ;
569 return std::to_string( l ) ;
575 return std::to_string( s ) ;
581 return std::to_string( ui ) ;
587 return std::to_string( ul ) ;
593 return std::to_string( us ) ;
599 if( p ==
nullptr || end ==
nullptr || p == end )
605 T result = toUnsigned<T>( p , end , overflow ) ;
614 static_assert( std::is_integral<T>::value ,
"" ) ;
616 for( ; p != end ; p++ )
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 ;
632 result = result * 10 ;
A static class which provides string helper functions.
static std::string fromShort(short s)
Converts short 's' to a string.
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.
static string_view alnum()
Returns a string of seven-bit alphanumeric characters, ie A-Z, a-z and 0-9.
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
static bool imatch(char, char)
Returns true if the two characters are the same, ignoring Latin-1 case.
static constexpr std::size_t truncate
A special value for the G::Str::strncpy_s() 'count' parameter.
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
static bool toBool(const std::string &s)
Converts string 's' to a bool.
static void toLower(std::string &s)
Replaces all Latin-1 upper-case characters in string 's' by lower-case characters.
static string_view ws()
Returns a string of standard whitespace characters.
static std::set< std::string > keySet(const StringMap &string_map)
Extracts the keys from a map of strings.
static StringArray keys(const StringMap &string_map)
Extracts the keys from a map of strings.
static std::string positive()
Returns a default positive string. See isPositive().
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.
static void splitIntoFields(const std::string &in, StringArray &out, string_view ws, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
static std::string unescaped(const std::string &s)
Returns the unescape()d version of s.
static std::string fromULong(unsigned long ul)
Converts unsigned long 'ul' to a string.
static bool isPrintableAscii(const std::string &s)
Returns true if every character is a 7-bit, non-control character (ie.
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.
static double toDouble(const std::string &s)
Converts string 's' to a double.
static std::string replaced(const std::string &s, char from, char to)
Returns the string 's' with all occurrences of 'from' replaced by 'to'.
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
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.
static void splitIntoTokens(const std::string &in, StringArray &out, string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
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.
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
static unsigned short toUShort(const std::string &s, Limited)
Converts string 's' to an unsigned short.
static std::string unique(const std::string &s, char c, char r)
Returns a string with repeated 'c' characters replaced by one 'r' character.
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).
static bool isULong(const std::string &s)
Returns true if the string can be converted into an unsigned long without throwing an exception.
static long toLong(const std::string &s)
Converts string 's' to a long.
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string. See also only().
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).
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...
static std::string fromInt(int i)
Converts int 'i' to a string.
static std::string negative()
Returns a default negative string. See isNegative().
static bool match(const std::string &, const std::string &)
Returns true if the two strings are the same.
static string_view meta()
Returns a list of shell meta-characters with a tilde as the first character.
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.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
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-...
static bool isHex(const std::string &s)
Returns true if every character is a hexadecimal digit.
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 ...
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.
static bool headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
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.
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
static void toUpper(std::string &s)
Replaces all Latin-1 lower-case characters in string 's' by upper-case characters.
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'.
static int toInt(const std::string &s)
Converts string 's' to an int.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
static unsigned long toULong(const std::string &s, Limited)
Converts string 's' to an unsigned long.
static std::string fromDouble(double d)
Converts double 'd' to a string.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception.
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().
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.
static std::string trimmed(const std::string &s, string_view ws)
Returns a trim()med version of s.
static std::string fromLong(long l)
Converts long 'l' to a string.
static bool isInt(const std::string &s)
Returns true if the string can be converted into an integer without throwing an exception.
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...
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'.
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...
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...
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...
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
static bool isUShort(const std::string &s)
Returns true if the string can be converted into an unsigned short without throwing an exception.
static std::string & trim(std::string &s, string_view ws)
Trims both ends of s, taking off any of the 'ws' characters.
static short toShort(const std::string &s)
Converts string 's' to a short.
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.
A class template like c++17's std::basic_string_view.
std::vector< std::string > StringArray
A std::vector of std::strings.
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Overload discrimiator for G::Str::toUWhatever() indicating hexadecimal strings.
Overload discrimiator for G::Str::toUWhatever() requesting a range-limited result.