36 std::string s( s_in ) ;
41 std::string
G::Str::escaped(
const std::string & s_in ,
char c_escape ,
const std::string & specials_in ,
const std::string & specials_out )
43 std::string s( s_in ) ;
44 escape( s , c_escape , specials_in , specials_out ) ;
48 std::string
G::Str::escaped(
const std::string & s_in ,
char c_escape ,
const char * specials_in ,
const char * specials_out )
50 std::string s( s_in ) ;
51 escape( s , c_escape , specials_in , specials_out ) ;
57 escapeImp( s ,
'\\' ,
"\\\r\n\t" ,
"\\rnt0" ,
true ) ;
60 void G::Str::escape( std::string & s ,
char c_escape ,
const char * specials_in ,
const char * specials_out )
62 bool with_nul = std::strlen(specials_in) != std::strlen(specials_out) ;
63 escapeImp( s , c_escape , specials_in , specials_out , with_nul ) ;
66 void G::Str::escape( std::string & s ,
char c_escape ,
const std::string & specials_in ,
const std::string & specials_out )
68 G_ASSERT( specials_in.length() == specials_out.length() ) ;
69 bool with_nul = !specials_in.empty() && specials_in.at(specials_in.length()-1U) ==
'\0' ;
70 escapeImp( s , c_escape , specials_in.c_str() , specials_out.c_str() , with_nul ) ;
73 void G::Str::escapeImp( std::string & s ,
char c_escape ,
const char * specials_in ,
const char * specials_out ,
bool with_nul )
75 G_ASSERT( specials_in !=
nullptr && specials_out !=
nullptr && (std::strlen(specials_out)-std::strlen(specials_in)) <= 1U ) ;
80 pos = s.find_first_of( specials_in , pos ) ;
81 if( pos != std::string::npos )
84 pos = s.find(
'\0' , pos ) ;
85 if( pos == std::string::npos )
88 G_ASSERT( std::strchr(specials_in,c_in) !=
nullptr ) ;
89 const size_t special_index = std::strchr(specials_in,c_in) - specials_in ;
90 G_ASSERT( special_index < std::strlen(specials_out) ) ;
92 s.insert( pos , 1U , c_escape ) ;
94 s.at(pos) = specials_out[special_index] ;
101 unescape( s ,
'\\' ,
"rnt0" ,
"\r\n\t" ) ;
104 void G::Str::unescape( std::string & s ,
char c_escape ,
const char * specials_in ,
const char * specials_out )
106 G_ASSERT( specials_in !=
nullptr && specials_out !=
nullptr && (std::strlen(specials_in)-std::strlen(specials_out)) <= 1U ) ;
107 bool escaped = false ;
108 std::string::iterator out = s.begin() ;
109 for( std::string::iterator in = s.begin() ; in != s.end() ; ++in )
111 const char * specials_in_p = std::strchr( specials_in , *in ) ;
112 const char * specials_out_p = specials_in_p ? (specials_out+(specials_in_p-specials_in)) : nullptr ;
113 if( escaped && specials_out_p )
114 *out++ = *specials_out_p , escaped = false ;
115 else if( escaped && *in == c_escape )
116 *out++ = c_escape , escaped = false ;
118 *out++ = *in , escaped = false ;
119 else if( *in == c_escape )
122 *out++ = *in , escaped = false ;
124 if( out != s.end() ) s.erase( out , s.end() ) ;
129 std::string s( s_in ) ;
134 bool G::Str::replace( std::string & s ,
const std::string & from ,
const std::string & to , size_type * pos_p )
136 if( from.length() == 0 )
139 size_type pos = pos_p ==
nullptr ? 0 : *pos_p ;
140 if( pos >= s.length() )
143 pos = s.find( from , pos ) ;
144 if( pos == std::string::npos )
150 s.replace( pos , from.length() , to ) ;
151 if( pos_p !=
nullptr )
152 *pos_p = pos + to.length() ;
157 unsigned int G::Str::replaceAll( std::string & s ,
const std::string & from ,
const std::string & to )
159 unsigned int count = 0U ;
160 for( size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
167 if( s.find(from) != std::string::npos )
169 unsigned int count = 0U ;
170 for( size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
182 const std::string::iterator end = s.end() ;
183 s.erase( std::remove_if( s.begin() , end , std::bind1st(std::equal_to<char>(),c) ) , end ) ;
188 size_type n = s.find_first_not_of( ws ) ;
189 if( limit != 0U && ( n == std::string::npos || n > limit ) )
190 n = limit >= s.length() ? std::string::npos : limit ;
191 if( n == std::string::npos )
199 size_type n = s.find_last_not_of( ws ) ;
200 if( limit != 0U && ( n == std::string::npos || s.length() > (limit+n+1U) ) )
201 n = limit >= s.length() ? std::string::npos : (s.length()-limit-1U) ;
202 if( n == std::string::npos )
204 else if( (n+1U) != s.length() )
210 trimLeft(s,ws) ; trimRight(s,ws) ;
215 std::string s( s_in ) ;
222 struct IsDigit : std::unary_function<char,bool>
224 bool operator()(
char c )
const {
return !! isdigit( c ) ; }
230 const std::string::const_iterator end = s.end() ;
231 std::string::const_iterator p = s.begin() ;
232 if( allow_minus_sign && p != end && *p ==
'-' ) ++p ;
233 return std::find_if( p , end , std::not1(IsDigit()) ) == end ;
238 struct IsPrintableAscii : std::unary_function<char,bool>
240 bool operator()(
char c )
const {
return c >= 0x20 && c < 0x7f ; }
246 const std::string::const_iterator end = s.end() ;
247 return std::find_if( s.begin() , end , std::not1(IsPrintableAscii()) ) == end ;
252 bool overflow = false ;
253 bool invalid = false ;
254 toIntImp( s , overflow , invalid ) ;
255 return !overflow && !invalid ;
260 bool overflow = false ;
261 bool invalid = false ;
262 toUShortImp( s , overflow , invalid ) ;
263 return !overflow && !invalid ;
268 bool overflow = false ;
269 bool invalid = false ;
270 toUIntImp( s , overflow , invalid ) ;
271 return !overflow && !invalid ;
276 bool overflow = false ;
277 bool invalid = false ;
278 toULongImp( s , overflow , invalid ) ;
279 return !overflow && !invalid ;
284 return b ?
"true" :
"false" ;
289 std::ostringstream ss ;
290 ss << std::setprecision(16) << d ;
296 std::ostringstream ss ;
303 std::ostringstream ss ;
310 std::ostringstream ss ;
317 std::ostringstream ss ;
324 std::ostringstream ss ;
331 std::ostringstream ss ;
338 std::string str = lower( s ) ;
343 else if( str ==
"false" )
349 throw InvalidFormat(
"expected true/false" , s ) ;
356 char * end = nullptr ;
357 double result = ::strtod( s.c_str(), &end ) ;
359 if( end == 0 || end[0] !=
'\0' )
360 throw InvalidFormat(
"expected floating point number" , s ) ;
362 if( result == HUGE_VAL || result == -(HUGE_VAL) )
363 throw Overflow( s ) ;
370 bool overflow = false ;
371 bool invalid = false ;
372 int result = toIntImp( s , overflow , invalid ) ;
374 throw InvalidFormat(
"expected integer" , s ) ;
376 throw Overflow( s ) ;
380 int G::Str::toIntImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
382 long long_val = toLongImp( s , overflow , invalid ) ;
383 int result =
static_cast<int>( long_val ) ;
384 if( result != long_val )
391 bool overflow = false ;
392 bool invalid = false ;
393 long result = toLongImp( s , overflow , invalid ) ;
395 throw InvalidFormat(
"expected long integer" , s ) ;
397 throw Overflow( s ) ;
401 long G::Str::toLongImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
403 char * end = nullptr ;
404 long result = ::strtol( s.c_str(), &end, 10 ) ;
405 if( end == 0 || end[0] !=
'\0' )
407 if( result == LONG_MAX || result == LONG_MIN )
414 bool overflow = false ;
415 bool invalid = false ;
416 short result = toShortImp( s , overflow , invalid ) ;
418 throw InvalidFormat(
"expected short integer" , s ) ;
420 throw Overflow( s ) ;
424 short G::Str::toShortImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
426 long long_val = toLongImp( s , overflow , invalid ) ;
427 short result =
static_cast<short>( long_val ) ;
428 if( result != long_val )
435 return !s1.empty() && isUInt(s1) ? toUInt(s1) : toUInt(s2) ;
440 bool overflow = false ;
441 bool invalid = false ;
442 unsigned int result = toUIntImp( s , overflow , invalid ) ;
444 throw InvalidFormat(
"expected unsigned integer" , s ) ;
452 bool overflow = false ;
453 bool invalid = false ;
454 unsigned int result = toUIntImp( s , overflow , invalid ) ;
456 throw InvalidFormat(
"expected unsigned integer" , s ) ;
458 throw Overflow( s ) ;
462 unsigned int G::Str::toUIntImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
464 unsigned long ulong_val = toULongImp( s , overflow , invalid ) ;
465 unsigned int result =
static_cast<unsigned int>( ulong_val ) ;
466 if( result != ulong_val )
473 bool overflow = false ;
474 bool invalid = false ;
475 unsigned long result = toULongImp( s , overflow , invalid ) ;
477 throw InvalidFormat(
"expected unsigned long integer" , s ) ;
485 bool overflow = false ;
486 bool invalid = false ;
487 unsigned long result = toULongImp( s , overflow , invalid ) ;
489 throw InvalidFormat(
"expected unsigned long integer" , s ) ;
491 throw Overflow( s ) ;
495 unsigned long G::Str::toULongImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
505 char * end = nullptr ;
506 unsigned long result = ::strtoul( s.c_str() , &end , 10 ) ;
507 if( end == 0 || end[0] !=
'\0' )
509 if( result == ULONG_MAX )
517 bool overflow = false ;
518 bool invalid = false ;
519 unsigned short result = toUShortImp( s , overflow , invalid ) ;
521 throw InvalidFormat(
"expected unsigned short integer" , s ) ;
529 bool overflow = false ;
530 bool invalid = false ;
531 unsigned short result = toUShortImp( s , overflow , invalid ) ;
533 throw InvalidFormat(
"expected unsigned short integer" , s ) ;
535 throw Overflow( s ) ;
539 unsigned short G::Str::toUShortImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
541 unsigned long ulong_val = toULongImp( s , overflow , invalid ) ;
542 unsigned short result =
static_cast<unsigned short>( ulong_val ) ;
543 if( result != ulong_val )
550 struct ToLower : std::unary_function<char,char>
552 char operator()(
char c ) {
return static_cast<char>( tolower(c) ) ; }
558 std::transform( s.begin() , s.end() , s.begin() , ToLower() ) ;
563 std::string out = in ;
570 struct ToUpper : std::unary_function<char,char>
572 char operator()(
char c ) {
return static_cast<char>( toupper(c) ) ; }
578 std::transform( s.begin() , s.end() , s.begin() , ToUpper() ) ;
583 std::string out = in ;
590 template <
typename T
char =
char ,
typename Tu
char =
unsigned char>
591 struct PrintableAppender : std::unary_function<Tchar,void>
597 PrintableAppender( std::string & s_ , Tchar escape_ ,
bool eight_bit_ ) :
600 escape_out(static_cast<char>(escape_)) ,
601 eight_bit(eight_bit_)
604 void operator()( Tchar c )
606 const Tuchar uc =
static_cast<Tuchar
>(c) ;
609 s.append( 2U , escape_out ) ;
611 else if( !eight_bit && uc >= 0x20U && uc < 0x7FU && uc != 0xFFU )
613 s.append( 1U , static_cast<char>(c) ) ;
615 else if( eight_bit && ( ( uc >= 0x20U && uc < 0x7FU ) || uc >= 0xA0 ) && uc != 0xFFU )
617 s.append( 1U , static_cast<char>(c) ) ;
621 s.append( 1U , escape_out ) ;
622 if( static_cast<char>(c) ==
'\n' )
624 s.append( 1U ,
'n' ) ;
626 else if( static_cast<char>(c) ==
'\r' )
628 s.append( 1U ,
'r' ) ;
630 else if( static_cast<char>(c) ==
'\t' )
632 s.append( 1U ,
't' ) ;
636 s.append( 1U ,
'0' ) ;
640 s.append( 1U ,
'x' ) ;
641 const char *
const map =
"0123456789abcdef" ;
642 unsigned long n = uc ;
643 if(
sizeof(Tchar) == 1 )
646 s.append( 1U , map[(n/16UL)%16UL] ) ;
647 s.append( 1U , map[n%16UL] ) ;
652 s.append( 1U , map[(n/4096UL)%16UL] ) ;
653 s.append( 1U , map[(n/256UL)%16UL] ) ;
654 s.append( 1U , map[(n/16UL)%16UL] ) ;
655 s.append( 1U , map[n%16UL] ) ;
666 result.reserve( in.length() + 1U ) ;
667 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
true) ) ;
674 result.reserve( in.length() + 1U ) ;
675 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
false) ) ;
682 PrintableAppender<char,unsigned char> append_printable( result , escape ,
false ) ;
683 append_printable( c ) ;
690 result.reserve( in.length() * 3U ) ;
691 std::for_each( in.begin() , in.end() , PrintableAppender<wchar_t,unsigned long>(result,escape,
false) ) ;
698 readLineFrom( stream , eol.empty() ? std::string(1U,
'\n') : eol , result , true ) ;
702 void G::Str::readLineFrom( std::istream & stream ,
const std::string & eol , std::string & line ,
bool pre_erase )
704 G_ASSERT( eol.length() != 0U ) ;
710 if( eol.length() == 2U && eol[0] != eol[1] && line.length() == 1U )
718 const char c = line[0] ;
720 std::getline( stream , line , eol[1] ) ;
721 const std::string::size_type line_length = line.length() ;
722 bool complete = line_length > 0U && line[line_length-1U] == eol[0] ;
725 line.resize( line_length - 1U ) ;
726 line.insert( 0U , &c , 1U ) ;
730 line.insert( 0U , &c , 1U ) ;
733 line.append( 1U , eol[1] ) ;
734 readLineFromImp( stream , eol , line ) ;
740 readLineFromImp( stream , eol , line ) ;
744 void G::Str::readLineFromImp( std::istream & stream ,
const std::string & eol , std::string & line )
746 const size_type limit = line.max_size() ;
747 const size_type eol_length = eol.length() ;
748 const char eol_final = eol.at( eol_length - 1U ) ;
749 size_type line_length = line.length() ;
751 bool changed = false ;
759 stream.clear( ( stream.rdstate() & ~std::ios_base::failbit ) | std::ios_base::eofbit ) ;
763 if( line_length == limit )
765 stream.setstate( std::ios_base::failbit ) ;
769 line.append( 1U , c ) ;
773 if( line_length >= eol_length && c == eol_final )
775 const size_type offset = line_length - eol_length ;
776 if( line.find(eol,offset) == offset )
784 stream.setstate( std::ios_base::failbit ) ;
787 std::string
G::Str::wrap( std::string text ,
const std::string & prefix_1 ,
788 const std::string & prefix_2 , size_type width )
790 std::string ws(
" \t\n" ) ;
791 std::ostringstream ss ;
792 for(
bool first_line =
true ; text.length() ; first_line = false )
794 const size_type prefix_length =
795 first_line ? prefix_1.length() : prefix_2.length() ;
796 size_type w = (width > prefix_length) ? (width-prefix_length) : width ;
798 const size_type pos_nl = text.find_first_of(
"\n") ;
799 if( pos_nl != std::string::npos && pos_nl != 0U && pos_nl < w )
804 std::string line = text ;
805 if( text.length() > w )
807 line = text.substr( 0U , w ) ;
808 if( text.find_first_of(ws,w) != w )
810 const size_type white_space = line.find_last_of( ws ) ;
811 const size_type black_space = line.find_first_not_of( ws ) ;
812 if( white_space != std::string::npos &&
813 black_space != std::string::npos &&
814 (white_space+1U) != black_space )
816 line = line.substr( 0U , white_space ) ;
821 if( line.length() != 0U )
823 ss << ( first_line ? prefix_1 : prefix_2 ) << line << std::endl ;
826 text = text.length() == line.length() ?
827 std::string() : text.substr(line.length()) ;
829 const size_type black_space = text.find_first_not_of( ws ) ;
830 if( black_space != 0U && black_space != std::string::npos )
832 unsigned int newlines = 0U ;
833 for( size_type pos = 0U ; pos < black_space ; ++pos )
835 if( text.at(pos) ==
'\n' )
839 ss << prefix_2 << std::endl ;
843 text = text.substr( black_space ) ;
851 template <
typename T>
852 void splitIntoTokens_(
const std::string & in , T & out ,
const std::string & ws )
854 typedef G::Str::size_type size_type ;
855 for( size_type p = 0U ; p != std::string::npos ; )
857 p = in.find_first_not_of( ws , p ) ;
858 if( p != std::string::npos )
860 size_type end = in.find_first_of( ws , p ) ;
861 size_type len = end == std::string::npos ? end : (end-p) ;
862 out.push_back( in.substr(p,len) ) ;
870 splitIntoTokens_( in , out , ws ) ;
875 splitIntoTokens_( in , out , ws ) ;
881 template <
typename T>
882 void splitIntoFields_(
const std::string & in_in , T & out ,
const std::string & ws ,
883 char escape ,
bool discard_bogus )
885 typedef G::Str::size_type size_type ;
886 std::string ews( ws ) ;
888 ews.append( 1U , escape ) ;
892 std::string in = in_in ;
893 size_type start = 0U ;
894 size_type last_pos = in.length() - 1U ;
898 if( pos >= in.length() )
break ;
899 pos = in.find_first_of( ews , pos ) ;
900 if( pos == std::string::npos ) break ;
901 if( in.at(pos) == escape )
903 const bool valid = pos != last_pos && in.find(ews,pos+1U) == (pos+1U) ;
904 if( valid || discard_bogus )
905 in.erase( pos , 1U ) ;
912 out.push_back( in.substr(start,pos-start) ) ;
917 out.push_back( in.substr(start,pos-start) ) ;
922 char escape ,
bool discard_bogus )
924 splitIntoFields_( in , out , ws , escape , discard_bogus ) ;
930 splitIntoTokens( in , a , ws ) ;
931 return std::find( a.begin() , a.end() , s ) != a.end() ;
937 splitIntoTokens( in , a , ws ) ;
938 for( StringArray::iterator p = a.begin() ; p != a.end() ; ++p )
941 return std::string() ;
942 if( (*p).find(s) == 0U )
943 return (*p).substr( s.size() ) ;
945 return std::string() ;
950 template <
typename T>
951 struct Joiner : std::unary_function<const T&,void>
956 Joiner( T & result_ ,
const T & sep_ ,
bool & first_ ) :
963 void operator()(
const T & s )
965 if( !first ) result.append( sep ) ;
976 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
980 std::string
G::Str::join(
const std::string & sep ,
const std::set<std::string> & strings )
984 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
988 std::string
G::Str::join(
const std::string & sep ,
const std::string & s1 ,
const std::string & s2 ,
989 const std::string & s3 ,
const std::string & s4 ,
const std::string & s5 ,
const std::string & s6 ,
990 const std::string & s7 ,
const std::string & s8 ,
const std::string & s9 )
993 joinImp( sep , result , s1 ) ;
994 joinImp( sep , result , s2 ) ;
995 joinImp( sep , result , s3 ) ;
996 joinImp( sep , result , s4 ) ;
997 joinImp( sep , result , s5 ) ;
998 joinImp( sep , result , s6 ) ;
999 joinImp( sep , result , s7 ) ;
1000 joinImp( sep , result , s8 ) ;
1001 joinImp( sep , result , s9 ) ;
1005 void G::Str::joinImp(
const std::string & sep , std::string & result ,
const std::string & s )
1007 if( !result.empty() && !s.empty() )
1008 result.append( sep ) ;
1009 result.append( s ) ;
1014 template <
typename T>
1015 struct Firster : std::unary_function<const T&,const typename T::first_type&>
1017 const typename T::first_type & operator()(
const T & pair ) {
return pair.first ; }
1022 std::set<std::string> result ;
1023 std::transform( map.begin() , map.end() , std::inserter(result,result.end()) , Firster<StringMap::value_type>() ) ;
1029 return std::string(
" \t\n\r") ;
1034 return std::string(
"~<>[]*$|?\\(){}\"`'&;=") ;
1037 std::string
G::Str::head(
const std::string & in , std::string::size_type pos ,
const std::string & default_ )
1040 pos == std::string::npos ?
1042 ( pos == 0U ? std::string() : ( (pos+1U) > in.length() ? in : in.substr(0U,pos) ) ) ;
1045 std::string
G::Str::head(
const std::string & in ,
const std::string & sep ,
bool default_empty )
1047 size_t pos = sep.empty() ? std::string::npos : in.find( sep ) ;
1048 return head( in , pos , default_empty ? std::string() : in ) ;
1051 std::string
G::Str::tail(
const std::string & in , std::string::size_type pos ,
const std::string & default_ )
1054 pos == std::string::npos ?
1056 ( (pos+1U) >= in.length() ? std::string() : in.substr(pos+1U) ) ;
1059 std::string
G::Str::tail(
const std::string & in ,
const std::string & sep ,
bool default_empty )
1061 size_t pos = sep.empty() ? std::string::npos : in.find(sep) ;
1062 if( pos != std::string::npos ) pos += (sep.length()-1U) ;
1063 return tail( in , pos , default_empty ? std::string() : in ) ;
1070 ( in.length() >= ending.length() &&
1071 in.substr( in.length() - ending.length() ) == ending ) ;
1087 std::string s = trimmed( lower(s_in) , ws() ) ;
1088 return !s.empty() && ( s ==
"y" || s ==
"yes" || s ==
"t" || s ==
"true" || s ==
"1" || s ==
"on" ) ;
1093 std::string s = trimmed( lower(s_in) , ws() ) ;
1094 return !s.empty() && ( s ==
"n" || s ==
"no" || s ==
"f" || s ==
"false" || s ==
"0" || s ==
"off" ) ;
1099 bool icompare(
char c1 ,
char c2 )
1101 if( c1 >=
'A' && c1 <=
'Z' ) c1 +=
'\x20' ;
1102 if( c2 >=
'A' && c2 <=
'Z' ) c2 +=
'\x20' ;
1109 return a.size() == b.size() && std::equal( a.begin() , a.end() , b.begin() , icompare ) ;
1112 std::string::size_type
G::Str::ifind(
const std::string & s ,
const std::string & key , std::string::size_type pos )
1114 if( s.empty() || key.empty() || pos > s.length() )
return std::string::npos ;
1115 std::string::const_iterator p = std::search( s.begin()+pos , s.end() , key.begin() , key.end() , icompare ) ;
1116 return p == s.end() ? std::string::npos : std::distance(s.begin(),p) ;
1121 template <
typename T,
typename V>
1122 T unique_imp( T in , T end , V repeat , V replacement )
1128 T in_next = in ; ++in_next ;
1129 if( *in == repeat && *in_next == repeat )
1131 while( *in == repeat )
1133 *out++ = replacement ;
1146 s.erase( unique_imp( s.begin() , s.end() , c , r ) , s.end() ) ;
static unsigned long toULong(const std::string &s, Limited)
Converts string 's' to an unsigned long.
static std::string positive()
Returns a default positive string. See isPositive().
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
static bool toBool(const std::string &s)
Converts string 's' to a bool.
static int toInt(const std::string &s)
Converts string 's' to an int.
static std::string wrap(std::string text, const std::string &prefix_first_line, const std::string &prefix_subsequent_lines, size_type width=70U)
Does word-wrapping.
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the given string has the given ending.
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
static std::string fromDouble(double d)
Converts double 'd' to a string.
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring case.
std::vector< std::string > StringArray
A std::vector of std::strings.
static bool replace(std::string &s, const std::string &from, const std::string &to, size_type *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
std::string::size_type size_type
A std::size_t type.
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
static short toShort(const std::string &s)
Converts string 's' to a short.
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
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 fromInt(int i)
Converts int 'i' to a string.
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
static void trimLeft(std::string &s, const std::string &ws, size_type limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
static std::string fromLong(long l)
Converts long 'l' to a string.
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 uppercase characters have been replaced by lowercase chara...
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
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 void toLower(std::string &s)
Replaces all Latin-1 uppercase characters in string 's' by lowercase characters.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
static std::string head(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
static void trimRight(std::string &s, const std::string &ws, size_type limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
static bool isInt(const std::string &s)
Returns true if the string can be converted into an integer without throwing an exception.
static std::string unique(std::string s, char c= ' ', char r= ' ')
Returns a string with repeated 'c' charaters replaced by one 'r' character.
static double toDouble(const std::string &s)
Converts string 's' to a double.
static bool splitMatch(const std::string &in, const std::string &s, const std::string &ws=Str::ws())
Returns true if any of the split parts of 'in' are equal to 's'.
static std::string fromShort(short s)
Converts short 's' to a string.
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 upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lowercase characters have been replaced by uppercase chara...
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
Overload discrimiator for G::Str::toUWhatever()
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
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 std::string splitMatchTail(const std::string &in, const std::string &s, const std::string &ws=Str::ws())
Splits the input string into parts and looks for a part that starts with 's' and returns the trailing...
static bool isPrintableAscii(const std::string &s)
Returns true if every character is a 7-bit, non-control character (ie.
std::map< std::string, std::string > StringMap
A std::map of std::strings.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings.
static long toLong(const std::string &s)
Converts string 's' to a long.
static unsigned short toUShort(const std::string &s, Limited)
Converts string 's' to an unsigned short.
static std::string negative()
Returns a default negative string. See isNegative().
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
static std::string::size_type ifind(const std::string &s, const std::string &key, std::string::size_type pos=0U)
Does a case-insensitive std::string::find().
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string.
static std::set< std::string > keySet(const StringMap &string_map)
Extracts the keys from a map of strings.
static bool isULong(const std::string &s)
Returns true if the string can be converted into an unsigned long without throwing an exception...
static std::string fromULong(unsigned long ul)
Converts unsigned long 'ul' to a string.
static std::string unescaped(const std::string &s)
Returns the unescape()d version of s.
static std::string meta()
Returns a list of shell meta-characters, typically used with escape().
static std::string ws()
A convenience function returning standard whitespace characters.
static std::string toPrintableAscii(char c, char escape= '\\')
Returns a 7-bit printable representation of the given input character.
static void splitIntoFields(const std::string &in, StringArray &out, const std::string &seperators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.
static void toUpper(std::string &s)
Replaces all Latin-1 lowercase characters in string 's' by uppercase characters.