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") )
67 static bool replace( std::string & s , std::string_view from , std::string_view to ,
68 std::size_t * pos_p =
nullptr ) ;
73 static void replace( std::string & s ,
char from ,
char to ) ;
79 static unsigned int replaceAll( std::string & s , std::string_view from , std::string_view 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 ) ;
95 static std::string &
trimLeft( std::string & s , std::string_view
ws , std::size_t limit = 0U ) ;
99 static std::string_view
trimLeftView( std::string_view , std::string_view
ws , std::size_t limit = 0U ) noexcept ;
103 static std::string &
trimRight( std::string & s , std::string_view
ws , std::size_t limit = 0U ) ;
107 static std::string_view
trimRightView( std::string_view sv , std::string_view
ws , std::size_t limit = 0U ) noexcept ;
111 static std::string &
trim( std::string & s , std::string_view
ws ) ;
115 static std::string
trimmed(
const std::string & s , std::string_view
ws ) ;
118 static std::string_view
trimmedView( std::string_view s , std::string_view
ws ) noexcept ;
121 static bool isNumeric( std::string_view s ,
bool allow_minus_sign =
false ) noexcept ;
181 static
std::
string fromULong(
unsigned long , const Hex & ) ;
210 static
float toFloat( const
std::
string & s ) ;
277 template <typename T> static T
toUnsigned( const
char * p , const
char * end ,
278 bool & overflow ,
bool & invalid ) noexcept ;
283 template <typename T> static T
toUnsigned( const
char * &p , const
char * end ,
284 bool & overflow ) noexcept ;
413 static std::istream &
readLine( std::istream & stream , std::string & result ,
414 std::string_view eol = {} ,
bool pre_erase_result = true , std::size_t limit = 0U ) ;
422 static std::istream &
readLine( std::istream & stream , std::string & result ,
423 Eol ,
bool pre_erase_result =
true , std::size_t limit = 0U ) ;
442 char sep ,
char escape =
'\0' ,
443 bool remove_escapes =
true ) ;
459 static std::string
dequote(
const std::string & ,
char qq =
'\"' ,
char esc =
'\\' ,
466 static std::string
join( std::string_view sep ,
const StringArray & strings ) ;
469 static std::string
join( std::string_view sep , std::string_view s1 , std::string_view s2 ,
470 std::string_view s3 = {} , std::string_view s4 = {} , std::string_view s5 = {} ,
471 std::string_view s6 = {} , std::string_view s7 = {} , std::string_view s8 = {} ,
472 std::string_view s9 = {} ) ;
476 static std::string
join( std::string_view sep ,
const StringMap & , std::string_view eq ,
477 std::string_view
tail = {} ) ;
483 static std::string
head( std::string_view in , std::size_t pos , std::string_view default_ = {} ) ;
489 static std::string
head( std::string_view , std::string_view sep ,
bool default_empty =
true ) ;
495 static std::string_view
headView( std::string_view in , std::size_t pos , std::string_view default_ = {} )
noexcept ;
498 static std::string_view
headView( std::string_view in , std::string_view sep ,
bool default_empty =
true ) noexcept ;
501 static
std::
string tail(
std::string_view in ,
std::
size_t pos ,
std::string_view default_ = {} ) ;
507 static std::string
tail( std::string_view in , std::string_view sep ,
bool default_empty =
true ) ;
513 static std::string_view
tailView( std::string_view in , std::size_t pos , std::string_view default_ = {} )
noexcept ;
516 static std::string_view
tailView( std::string_view in , std::string_view sep ,
bool default_empty =
true ) noexcept ;
519 static
bool match(
std::string_view ,
std::string_view ) noexcept ;
522 static
bool iless(
std::string_view ,
std::string_view ) noexcept ;
527 static
bool imatch(
char ,
char ) noexcept ;
530 static
bool imatch(
std::string_view ,
std::string_view ) noexcept ;
534 static
std::
size_t ifind(
std::string_view s ,
std::string_view key ) ;
538 static
std::
size_t ifindat(
std::string_view s ,
std::string_view key ,
std::
size_t pos ) ;
543 static
bool tailMatch(
std::string_view in ,
std::string_view ending ) noexcept ;
549 static
std::string_view
ws() noexcept ;
552 static
std::string_view
alnum() noexcept ;
555 static
std::string_view
alnum_() noexcept ;
570 static
std::
string unique( const
std::
string & s ,
char c ,
char r ) ;
574 static
std::
string unique( const
std::
string & s ,
char c ) ;
581 static errno_t
strncpy_s(
char * dst ,
std::
size_t n_dst , const
char * src ,
std::
size_t count ) noexcept ;
596 return std::to_string( i ) ;
602 return std::to_string( l ) ;
608 return std::to_string( s ) ;
614 return std::to_string( ui ) ;
620 return std::to_string( ul ) ;
626 return std::to_string( us ) ;
632 std::array <char,
sizeof(u)*2U> buffer ;
639 std::array <char,
sizeof(u)*2U> buffer ;
646 if( p ==
nullptr || end ==
nullptr || p == end )
652 T result = toUnsigned<T>( p , end , overflow ) ;
661 static_assert( std::is_integral<T>::value ,
"" ) ;
663 for( ; p != end ; p++ )
666 if( *p ==
'0' ) n = 0 ;
667 else if( *p ==
'1' ) n = 1 ;
668 else if( *p ==
'2' ) n = 2 ;
669 else if( *p ==
'3' ) n = 3 ;
670 else if( *p ==
'4' ) n = 4 ;
671 else if( *p ==
'5' ) n = 5 ;
672 else if( *p ==
'6' ) n = 6 ;
673 else if( *p ==
'7' ) n = 7 ;
674 else if( *p ==
'8' ) n = 8 ;
675 else if( *p ==
'9' ) n = 9 ;
679 result = result * 10 ;
A static class which provides string helper functions.
static std::string & trimRight(std::string &s, std::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 fromShort(short s)
Converts short 's' to a string.
static std::string lower(std::string_view)
Returns a copy of 's' in which all seven-bit upper-case characters have been replaced by lower-case c...
static bool isUInt(std::string_view s) noexcept
Returns true if the string can be converted into an unsigned integer without throwing an exception.
static bool isPrintable(std::string_view s) noexcept
Returns true if every character is 0x20 or above but not 0x7f.
static std::string_view trimLeftView(std::string_view, std::string_view ws, std::size_t limit=0U) noexcept
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
static short toShort(std::string_view s)
Converts string 's' to a short.
static void splitIntoTokens(const std::string &in, StringArray &out, std::string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
static constexpr std::size_t truncate
A special value for the G::Str::strncpy_s() 'count' parameter.
static std::string_view trimmedView(std::string_view s, std::string_view ws) noexcept
Returns a trim()med view of the input view.
static bool tailMatch(std::string_view in, std::string_view ending) noexcept
Returns true if the string has the given ending (or the given ending is empty).
static int toInt(std::string_view s)
Converts string 's' to an int.
static void toLower(std::string &s)
Replaces all seven-bit upper-case characters in string 's' by lower-case characters.
static void escape(std::string &s, char c_escape, std::string_view specials_in, std::string_view specials_out)
Prefixes each occurrence of one of the special-in characters with the escape character and its corres...
static std::string join(std::string_view sep, const StringArray &strings)
Concatenates an array of strings with separators.
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::string positive()
Returns a default positive string. See isPositive().
static std::string_view trimRightView(std::string_view sv, std::string_view ws, std::size_t limit=0U) noexcept
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
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 isSimple(std::string_view s) noexcept
Returns true if every character is alphanumeric or "-" or "_".
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::size_t ifindat(std::string_view s, std::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 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 bool isPositive(std::string_view) noexcept
Returns true if the string has a positive meaning, such as "1", "true", "yes".
static bool headMatch(std::string_view in, std::string_view head) noexcept
Returns true if the string has the given start (or head is empty).
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
static std::istream & readLine(std::istream &stream, std::string &result, std::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 std::string & trim(std::string &s, std::string_view ws)
Trims both ends of s, taking off any of the 'ws' characters.
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 isInt(std::string_view s) noexcept
Returns true if the string can be converted into an integer without throwing an exception.
static void splitIntoFields(std::string_view in, StringArray &out, char sep, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
static std::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 std::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 void removeAll(std::string &, char)
Removes all occurrences of the character from the string. See also only().
static std::string_view alnum_() noexcept
Returns alnum() with an additional trailing underscore character.
static std::string fromInt(int i)
Converts int 'i' to a string.
static bool isNumeric(std::string_view s, bool allow_minus_sign=false) noexcept
Returns true if every character is a decimal digit.
static std::string negative()
Returns a default negative string. See isNegative().
static bool isUShort(std::string_view s) noexcept
Returns true if the string can be converted into an unsigned short without throwing an exception.
static unsigned short toUShort(std::string_view s, Limited)
Converts string 's' to an unsigned short.
static bool isULong(std::string_view s) noexcept
Returns true if the string can be converted into an unsigned long without throwing an exception.
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 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 upper(std::string_view)
Returns a copy of 's' in which all seven-bit lower-case characters have been replaced by upper-case c...
static std::size_t ifind(std::string_view s, std::string_view key)
Returns the position of the key in 's' using a seven-bit case-insensitive search.
static std::string tail(std::string_view in, std::size_t pos, std::string_view default_={})
Returns the last part of the string after the given position.
static std::string & trimLeft(std::string &s, std::string_view ws, std::size_t limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
static bool isHex(std::string_view s) noexcept
Returns true if every character is a hexadecimal digit.
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
static std::string readLineFrom(std::istream &stream, std::string_view eol={})
Reads a line from the stream using the given line terminator.
static void toUpper(std::string &s)
Replaces all seven-bit lower-case characters in string 's' by upper-case characters.
static std::string_view alnum() noexcept
Returns a string of seven-bit alphanumeric characters, ie A-Z, a-z and 0-9.
static std::string_view tailView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like tail() but returning a view into the input string.
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
static long toLong(std::string_view s)
Converts string 's' to a long.
static std::string fromDouble(double d)
Converts double 'd' to a string.
static std::string fromULongLong(unsigned long long, const Hex &)
Converts an unsigned value to a lower-case hex string with no leading zeros.
static unsigned int replaceAll(std::string &s, std::string_view from, std::string_view to)
Does a global replace on string 's', replacing all occurrences of sub-string 'from' with 'to'.
static std::string dequote(const std::string &, char qq='\"' , char esc = '\\' , std::string_view ws = Str::ws() , std::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_view ws() noexcept
Returns a string of standard whitespace characters.
static bool replace(std::string &s, std::string_view from, std::string_view to, std::size_t *pos_p=nullptr)
A std::string_view overload.
static bool match(std::string_view, std::string_view) noexcept
Returns true if the two strings are the same.
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 unsigned long toULong(std::string_view s, Limited)
Converts string 's' to an unsigned long.
static std::string_view headView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like head() but returning a view into the input string.
static std::string head(std::string_view in, std::size_t pos, std::string_view default_={})
Returns the first part of the string up to just before the given position.
static bool iless(std::string_view, std::string_view) noexcept
Returns true if the first string is lexicographically less than the first, after seven-bit lower-case...
static std::string fromLong(long l)
Converts long 'l' to a string.
static bool isNegative(std::string_view) noexcept
Returns true if the string has a negative meaning, such as "0", "false", "no".
static bool toBool(std::string_view s)
Converts string 's' to a bool.
static std::string_view meta() noexcept
Returns a list of shell meta-characters with a tilde as the first character.
static void unescape(std::string &s, char c_escape, std::string_view specials_in, std::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 isPrintableAscii(std::string_view s) noexcept
Returns true if every character is between 0x20 and 0x7e inclusive.
static std::string trimmed(const std::string &s, std::string_view ws)
Returns a trim()med version of s.
static std::string escaped(std::string_view, char c_escape, std::string_view specials_in, std::string_view specials_out)
Returns the escape()d string.
static std::string only(std::string_view allow_chars, std::string_view s)
Returns the 's' with all occurrences of the characters not appearing in the first string deleted.
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) noexcept
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.