50 G_EXCEPTION_CLASS( Overflow ,
tx(
"string conversion error: over/underflow") ) ;
51 G_EXCEPTION_CLASS( InvalidFormat,
tx(
"string conversion error: invalid format") ) ;
52 G_EXCEPTION_CLASS( NotEmpty,
tx(
"internal error: string container not empty") ) ;
53 G_EXCEPTION( InvalidEol ,
tx(
"invalid end-of-line specifier") ) ;
68 std::size_t * pos_p =
nullptr ) ;
73 static void replace( std::string & s ,
char from ,
char to ) ;
85 static std::string
replaced(
const std::string & s ,
char from ,
char to ) ;
88 static void removeAll( std::string & ,
char ) ;
91 static std::string
removedAll(
const std::string & ,
char ) ;
185 static
std::
string fromULong(
unsigned long , const Hex & ) ;
214 static
float toFloat( const
std::
string & s ) ;
281 template <typename T> static T
toUnsigned( const
char * p , const
char * end ,
282 bool & overflow ,
bool & invalid ) noexcept ;
287 template <typename T> static T
toUnsigned( const
char * &p , const
char * end ,
288 bool & overflow ) noexcept ;
417 static std::istream &
readLine( std::istream & stream , std::string & result ,
418 string_view eol = {} ,
bool pre_erase_result = true , std::size_t limit = 0U ) ;
426 static std::istream &
readLine( std::istream & stream , std::string & result ,
427 Eol ,
bool pre_erase_result =
true , std::size_t limit = 0U ) ;
446 char sep ,
char escape =
'\0' ,
447 bool remove_escapes =
true ) ;
463 static std::string
dequote(
const std::string & ,
char qq =
'\"' ,
char esc =
'\\' ,
470 static std::string
join( string_view sep ,
const StringArray & strings ) ;
473 static std::string
join( string_view sep , string_view s1 , string_view s2 ,
474 string_view s3 = {} , string_view s4 = {} , string_view s5 = {} ,
475 string_view s6 = {} , string_view s7 = {} , string_view s8 = {} ,
476 string_view s9 = {} ) ;
480 static std::string
join( string_view sep ,
const StringMap & , string_view eq ,
481 string_view
tail = {} ) ;
487 static std::string
head( string_view in , std::size_t pos , string_view default_ = {} ) ;
493 static std::string
head( string_view , string_view sep ,
bool default_empty =
true ) ;
499 static string_view
headView( string_view in , std::size_t pos , string_view default_ = {} )
noexcept ;
502 static string_view
headView( string_view in , string_view sep ,
bool default_empty =
true ) noexcept ;
505 static
std::
string tail( string_view in ,
std::
size_t pos , string_view default_ = {} ) ;
511 static std::string
tail( string_view in , string_view sep ,
bool default_empty =
true ) ;
517 static string_view
tailView( string_view in , std::size_t pos , string_view default_ = {} )
noexcept ;
520 static string_view
tailView( string_view in , string_view sep ,
bool default_empty =
true ) noexcept ;
523 static
bool match( string_view , string_view ) noexcept ;
526 static
bool iless( string_view , string_view ) noexcept ;
531 static
bool imatch(
char ,
char ) noexcept ;
534 static
bool imatch( string_view , string_view ) noexcept ;
538 static
std::
size_t ifind( string_view s , string_view key ) ;
542 static
std::
size_t ifindat( string_view s , string_view key ,
std::
size_t pos ) ;
547 static
bool tailMatch( const
std::
string & in , string_view ending ) noexcept ;
550 static
bool headMatch( const
std::
string & in , string_view
head ) noexcept ;
553 static string_view
ws() noexcept ;
556 static string_view
alnum() noexcept ;
559 static string_view
alnum_() noexcept ;
568 static
bool isPositive( string_view ) noexcept ;
571 static
bool isNegative( string_view ) noexcept ;
574 static
std::
string unique( const
std::
string & s ,
char c ,
char r ) ;
578 static
std::
string unique( const
std::
string & s ,
char c ) ;
585 static errno_t
strncpy_s(
char * dst ,
std::
size_t n_dst , const
char * src ,
std::
size_t count ) noexcept ;
600 return std::to_string( i ) ;
606 return std::to_string( l ) ;
612 return std::to_string( s ) ;
618 return std::to_string( ui ) ;
624 return std::to_string( ul ) ;
630 return std::to_string( us ) ;
636 std::array <char,
sizeof(u)*2U> buffer ;
643 std::array <char,
sizeof(u)*2U> buffer ;
650 if( p ==
nullptr || end ==
nullptr || p == end )
656 T result = toUnsigned<T>( p , end , overflow ) ;
665 static_assert( std::is_integral<T>::value ,
"" ) ;
667 for( ; p != end ; p++ )
670 if( *p ==
'0' ) n = 0 ;
671 else if( *p ==
'1' ) n = 1 ;
672 else if( *p ==
'2' ) n = 2 ;
673 else if( *p ==
'3' ) n = 3 ;
674 else if( *p ==
'4' ) n = 4 ;
675 else if( *p ==
'5' ) n = 5 ;
676 else if( *p ==
'6' ) n = 6 ;
677 else if( *p ==
'7' ) n = 7 ;
678 else if( *p ==
'8' ) n = 8 ;
679 else if( *p ==
'9' ) n = 9 ;
683 result = result * 10 ;
A static class which provides string helper functions.
static std::string fromShort(short s)
Converts short 's' to a string.
static bool isPrintableAscii(string_view s) noexcept
Returns true if every character is a 7-bit, non-control character (ie.
static std::size_t ifind(string_view s, string_view key)
Returns the position of the key in 's' using a seven-bit case-insensitive search.
static string_view headView(string_view in, std::size_t pos, string_view default_={}) noexcept
Like head() but returning a view into the input string.
static bool match(string_view, string_view) noexcept
Returns true if the two strings are the same.
static bool replace(std::string &s, string_view from, string_view to, std::size_t *pos_p=nullptr)
A string_view overload.
static bool isUShort(string_view s) noexcept
Returns true if the string can be converted into an unsigned short without throwing an exception.
static string_view tailView(string_view in, std::size_t pos, string_view default_={}) noexcept
Like tail() but returning a view into the input string.
static constexpr std::size_t truncate
A special value for the G::Str::strncpy_s() 'count' parameter.
static unsigned int toUInt(string_view s)
Converts string 's' to an unsigned int.
static string_view alnum() noexcept
Returns a string of seven-bit alphanumeric characters, ie A-Z, a-z and 0-9.
static void toLower(std::string &s)
Replaces all seven-bit upper-case characters in string 's' by lower-case characters.
static bool isNumeric(string_view s, bool allow_minus_sign=false) noexcept
Returns true if every character is a decimal digit.
static short toShort(string_view s)
Converts string 's' to a short.
static StringArray keys(const StringMap &string_map)
Extracts the keys from a map of strings.
static float toFloat(const std::string &s)
Converts string 's' to a float.
static std::istream & readLine(std::istream &stream, std::string &result, string_view eol={}, bool pre_erase_result=true, std::size_t limit=0U)
Reads a line from the stream using the given line terminator, which may be multi-character.
static int toInt(string_view s)
Converts string 's' to an int.
static bool isHex(string_view s) noexcept
Returns true if every character is a hexadecimal digit.
static std::string positive()
Returns a default positive string. See isPositive().
static unsigned int replaceAll(std::string &s, string_view from, string_view to)
Does a global replace on string 's', replacing all occurrences of sub-string 'from' with 'to'.
static bool iless(string_view, string_view) noexcept
Returns true if the first string is lexicographically less than the first, after seven-bit lower-case...
static unsigned short toUShort(string_view s, Limited)
Converts string 's' to an unsigned short.
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
static bool tailMatch(const std::string &in, string_view ending) noexcept
Returns true if the string has the given ending (or the given ending is empty).
static std::size_t ifindat(string_view s, string_view key, std::size_t pos)
Returns the position of the key in 's' at of after position 'pos' using a seven-bit case-insensitive ...
static bool isPrintable(string_view s) noexcept
Returns true if every character is a non-control character (ie.
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 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 toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
static string_view trimmedView(string_view s, string_view ws) noexcept
Returns a trim()med view of the input view.
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 long toLong(string_view s)
Converts string 's' to a long.
static string_view alnum_() noexcept
Returns alnum() with an additional trailing underscore character.
static string_view fromULongToHex(unsigned long, char *out) noexcept
Low-level conversion from an unsigned value to a lower-case hex string with no leading zeros.
static string_view meta() noexcept
Returns a list of shell meta-characters with a tilde as the first character.
static std::string join(string_view sep, const StringArray &strings)
Concatenates an array of strings with separators.
static bool isULong(string_view s) noexcept
Returns true if the string can be converted into an unsigned long without throwing an exception.
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string. See also only().
static std::string fromInt(int i)
Converts int 'i' to a string.
static std::string lower(string_view)
Returns a copy of 's' in which all seven-bit upper-case characters have been replaced by lower-case c...
static std::string readLineFrom(std::istream &stream, string_view eol={})
Reads a line from the stream using the given line terminator.
static std::string negative()
Returns a default negative string. See isNegative().
static std::string only(string_view allow_chars, string_view s)
Returns the 's' with all occurrences of the characters not appearing in the first string deleted.
static bool isUInt(string_view s) noexcept
Returns true if the string can be converted into an unsigned integer without throwing an exception.
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 removedAll(const std::string &, char)
Removes all occurrences of the character from the string and returns the result.
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 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 bool isPositive(string_view) noexcept
Returns true if the string has a positive meaning, such as "1", "true", "yes".
static bool isSimple(string_view s) noexcept
Returns true if every character is alphanumeric or "-" or "_".
static bool isNegative(string_view) noexcept
Returns true if the string has a negative meaning, such as "0", "false", "no".
static std::string upper(string_view)
Returns a copy of 's' in which all seven-bit lower-case characters have been replaced by upper-case c...
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
static void unescape(std::string &s, char c_escape, string_view specials_in, string_view specials_out)
Unescapes the string by replacing e-e with e, e-special-in with special-out, and e-other with other.
static bool isInt(string_view s) noexcept
Returns true if the string can be converted into an integer without throwing an exception.
static void toUpper(std::string &s)
Replaces all seven-bit lower-case characters in string 's' by upper-case characters.
static unsigned long toULong(string_view s, Limited)
Converts string 's' to an unsigned long.
static std::string fromDouble(double d)
Converts double 'd' to a string.
static std::string tail(string_view in, std::size_t pos, string_view default_={})
Returns the last part of the string after the given position.
static std::string fromULongLong(unsigned long long, const Hex &)
Converts an unsigned value to a lower-case hex string with no leading zeros.
static std::string head(string_view in, std::size_t pos, string_view default_={})
Returns the first part of the string up to just before the given position.
static void splitIntoFields(string_view in, StringArray &out, char sep, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
static string_view fromULongLongToHex(unsigned long long, char *out) noexcept
Low-level conversion from an unsigned value to a lower-case hex string with no leading zeros.
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 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 toBool(string_view s)
Converts string 's' to a bool.
static bool headMatch(const std::string &in, string_view head) noexcept
Returns true if the string has the given start (or head is empty).
static string_view trimLeftView(string_view, string_view ws, std::size_t limit=0U) noexcept
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
static string_view ws() noexcept
Returns a string of standard whitespace characters.
static void escape(std::string &s, char c_escape, string_view specials_in, string_view specials_out)
Prefixes each occurrence of one of the special-in characters with the escape character and its corres...
static string_view trimRightView(string_view sv, string_view ws, std::size_t limit=0U) noexcept
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
static std::string escaped(string_view, char c_escape, string_view specials_in, string_view specials_out)
Returns the escape()d string.
static std::string & trim(std::string &s, string_view ws)
Trims both ends of s, taking off any of the 'ws' characters.
A class like c++17's std::string_view.
std::vector< std::string > StringArray
A std::vector of std::strings.
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
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.