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] ;
99 std::string
G::Str::dequote(
const std::string & s ,
char qq ,
char esc ,
const std::string & ws )
102 result.reserve( s.size() ) ;
103 bool in_quote = false ;
104 bool escaped = false ;
105 for( std::string::const_iterator p = s.begin() ; p != s.end() ; ++p )
107 if( *p == esc && !escaped )
113 bool is_space = ws.find(*p) != std::string::npos ;
114 if( *p == qq && !escaped && !in_quote )
118 else if( *p == qq && !escaped )
122 else if( ws.find(*p) != std::string::npos && in_quote )
124 result.append( 1U , esc ) ;
125 result.append( 1U , *p ) ;
129 result.append( 1U , *p ) ;
139 unescape( s ,
'\\' ,
"rnt0" ,
"\r\n\t" ) ;
142 void G::Str::unescape( std::string & s ,
char c_escape ,
const char * specials_in ,
const char * specials_out )
144 G_ASSERT( specials_in !=
nullptr && specials_out !=
nullptr && (std::strlen(specials_in)-std::strlen(specials_out)) <= 1U ) ;
145 bool escaped = false ;
146 std::string::iterator out = s.begin() ;
147 for( std::string::iterator in = s.begin() ; in != s.end() ; ++in )
149 const char * specials_in_p = std::strchr( specials_in , *in ) ;
150 const char * specials_out_p = specials_in_p ? (specials_out+(specials_in_p-specials_in)) : nullptr ;
151 if( escaped && specials_out_p )
152 *out++ = *specials_out_p , escaped = false ;
153 else if( escaped && *in == c_escape )
154 *out++ = c_escape , escaped = false ;
156 *out++ = *in , escaped = false ;
157 else if( *in == c_escape )
160 *out++ = *in , escaped = false ;
162 if( out != s.end() ) s.erase( out , s.end() ) ;
167 std::string s( s_in ) ;
172 bool G::Str::replace( std::string & s ,
const std::string & from ,
const std::string & to , size_type * pos_p )
174 if( from.length() == 0 )
177 size_type pos = pos_p ==
nullptr ? 0 : *pos_p ;
178 if( pos >= s.length() )
181 pos = s.find( from , pos ) ;
182 if( pos == std::string::npos )
188 s.replace( pos , from.length() , to ) ;
189 if( pos_p !=
nullptr )
190 *pos_p = pos + to.length() ;
195 unsigned int G::Str::replaceAll( std::string & s ,
const std::string & from ,
const std::string & to )
197 unsigned int count = 0U ;
198 for( size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
205 if( s.find(from) != std::string::npos )
207 unsigned int count = 0U ;
208 for( size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
220 const std::string::iterator end = s.end() ;
221 s.erase( std::remove_if( s.begin() , end , std::bind1st(std::equal_to<char>(),c) ) , end ) ;
226 size_type n = s.find_first_not_of( ws ) ;
227 if( limit != 0U && ( n == std::string::npos || n > limit ) )
228 n = limit >= s.length() ? std::string::npos : limit ;
229 if( n == std::string::npos )
237 size_type n = s.find_last_not_of( ws ) ;
238 if( limit != 0U && ( n == std::string::npos || s.length() > (limit+n+1U) ) )
239 n = limit >= s.length() ? std::string::npos : (s.length()-limit-1U) ;
240 if( n == std::string::npos )
242 else if( (n+1U) != s.length() )
248 trimLeft(s,ws) ; trimRight(s,ws) ;
253 std::string s( s_in ) ;
260 struct IsDigit : std::unary_function<char,bool>
262 bool operator()(
char c )
const {
return !! isdigit( c ) ; }
268 const std::string::const_iterator end = s.end() ;
269 std::string::const_iterator p = s.begin() ;
270 if( allow_minus_sign && p != end && *p ==
'-' ) ++p ;
271 return std::find_if( p , end , std::not1(IsDigit()) ) == end ;
276 struct IsPrintableAscii : std::unary_function<char,bool>
278 bool operator()(
char c )
const {
return c >= 0x20 && c < 0x7f ; }
284 const std::string::const_iterator end = s.end() ;
285 return std::find_if( s.begin() , end , std::not1(IsPrintableAscii()) ) == end ;
290 bool overflow = false ;
291 bool invalid = false ;
292 toIntImp( s , overflow , invalid ) ;
293 return !overflow && !invalid ;
298 bool overflow = false ;
299 bool invalid = false ;
300 toUShortImp( s , overflow , invalid ) ;
301 return !overflow && !invalid ;
306 bool overflow = false ;
307 bool invalid = false ;
308 toUIntImp( s , overflow , invalid ) ;
309 return !overflow && !invalid ;
314 bool overflow = false ;
315 bool invalid = false ;
316 toULongImp( s , overflow , invalid ) ;
317 return !overflow && !invalid ;
322 return b ?
"true" :
"false" ;
327 std::ostringstream ss ;
328 ss << std::setprecision(16) << d ;
334 std::ostringstream ss ;
341 std::ostringstream ss ;
348 std::ostringstream ss ;
355 std::ostringstream ss ;
362 std::ostringstream ss ;
369 std::ostringstream ss ;
376 std::string str = lower( s ) ;
381 else if( str ==
"false" )
387 throw InvalidFormat(
"expected true/false" , s ) ;
394 char * end = nullptr ;
395 double result = ::strtod( s.c_str(), &end ) ;
397 if( end == 0 || end[0] !=
'\0' )
398 throw InvalidFormat(
"expected floating point number" , s ) ;
400 if( result == HUGE_VAL || result == -(HUGE_VAL) )
401 throw Overflow( s ) ;
408 bool overflow = false ;
409 bool invalid = false ;
410 int result = toIntImp( s , overflow , invalid ) ;
412 throw InvalidFormat(
"expected integer" , s ) ;
414 throw Overflow( s ) ;
418 int G::Str::toIntImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
420 long long_val = toLongImp( s , overflow , invalid ) ;
421 int result =
static_cast<int>( long_val ) ;
422 if( result != long_val )
429 bool overflow = false ;
430 bool invalid = false ;
431 long result = toLongImp( s , overflow , invalid ) ;
433 throw InvalidFormat(
"expected long integer" , s ) ;
435 throw Overflow( s ) ;
439 long G::Str::toLongImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
441 char * end = nullptr ;
442 long result = ::strtol( s.c_str(), &end, 10 ) ;
443 if( end == 0 || end[0] !=
'\0' )
445 if( result == LONG_MAX || result == LONG_MIN )
452 bool overflow = false ;
453 bool invalid = false ;
454 short result = toShortImp( s , overflow , invalid ) ;
456 throw InvalidFormat(
"expected short integer" , s ) ;
458 throw Overflow( s ) ;
462 short G::Str::toShortImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
464 long long_val = toLongImp( s , overflow , invalid ) ;
465 short result =
static_cast<short>( long_val ) ;
466 if( result != long_val )
473 return !s1.empty() && isUInt(s1) ? toUInt(s1) : toUInt(s2) ;
478 bool overflow = false ;
479 bool invalid = false ;
480 unsigned int result = toUIntImp( s , overflow , invalid ) ;
482 throw InvalidFormat(
"expected unsigned integer" , s ) ;
490 bool overflow = false ;
491 bool invalid = false ;
492 unsigned int result = toUIntImp( s , overflow , invalid ) ;
494 throw InvalidFormat(
"expected unsigned integer" , s ) ;
496 throw Overflow( s ) ;
500 unsigned int G::Str::toUIntImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
502 unsigned long ulong_val = toULongImp( s , overflow , invalid ) ;
503 unsigned int result =
static_cast<unsigned int>( ulong_val ) ;
504 if( result != ulong_val )
511 bool overflow = false ;
512 bool invalid = false ;
513 unsigned long result = toULongImp( s , overflow , invalid ) ;
515 throw InvalidFormat(
"expected unsigned long integer" , s ) ;
523 bool overflow = false ;
524 bool invalid = false ;
525 unsigned long result = toULongImp( s , overflow , invalid ) ;
527 throw InvalidFormat(
"expected unsigned long integer" , s ) ;
529 throw Overflow( s ) ;
533 unsigned long G::Str::toULongImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
543 char * end = nullptr ;
544 unsigned long result = ::strtoul( s.c_str() , &end , 10 ) ;
545 if( end == 0 || end[0] !=
'\0' )
547 if( result == ULONG_MAX )
555 bool overflow = false ;
556 bool invalid = false ;
557 unsigned short result = toUShortImp( s , overflow , invalid ) ;
559 throw InvalidFormat(
"expected unsigned short integer" , s ) ;
567 bool overflow = false ;
568 bool invalid = false ;
569 unsigned short result = toUShortImp( s , overflow , invalid ) ;
571 throw InvalidFormat(
"expected unsigned short integer" , s ) ;
573 throw Overflow( s ) ;
577 unsigned short G::Str::toUShortImp(
const std::string & s ,
bool & overflow ,
bool & invalid )
579 unsigned long ulong_val = toULongImp( s , overflow , invalid ) ;
580 unsigned short result =
static_cast<unsigned short>( ulong_val ) ;
581 if( result != ulong_val )
588 struct ToLower : std::unary_function<char,char>
590 char operator()(
char c ) {
return static_cast<char>( tolower(c) ) ; }
596 std::transform( s.begin() , s.end() , s.begin() , ToLower() ) ;
601 std::string out = in ;
608 struct ToUpper : std::unary_function<char,char>
610 char operator()(
char c ) {
return static_cast<char>( toupper(c) ) ; }
616 std::transform( s.begin() , s.end() , s.begin() , ToUpper() ) ;
621 std::string out = in ;
628 template <
typename T
char =
char ,
typename Tu
char =
unsigned char>
629 struct PrintableAppender : std::unary_function<Tchar,void>
635 PrintableAppender( std::string & s_ , Tchar escape_ ,
bool eight_bit_ ) :
638 escape_out(static_cast<char>(escape_)) ,
639 eight_bit(eight_bit_)
642 void operator()( Tchar c )
644 const Tuchar uc =
static_cast<Tuchar
>(c) ;
647 s.append( 2U , escape_out ) ;
649 else if( !eight_bit && uc >= 0x20U && uc < 0x7FU && uc != 0xFFU )
651 s.append( 1U , static_cast<char>(c) ) ;
653 else if( eight_bit && ( ( uc >= 0x20U && uc < 0x7FU ) || uc >= 0xA0 ) && uc != 0xFFU )
655 s.append( 1U , static_cast<char>(c) ) ;
659 s.append( 1U , escape_out ) ;
660 if( static_cast<char>(c) ==
'\n' )
662 s.append( 1U ,
'n' ) ;
664 else if( static_cast<char>(c) ==
'\r' )
666 s.append( 1U ,
'r' ) ;
668 else if( static_cast<char>(c) ==
'\t' )
670 s.append( 1U ,
't' ) ;
674 s.append( 1U ,
'0' ) ;
678 s.append( 1U ,
'x' ) ;
679 const char *
const map =
"0123456789abcdef" ;
680 unsigned long n = uc ;
681 if(
sizeof(Tchar) == 1 )
684 s.append( 1U , map[(n/16UL)%16UL] ) ;
685 s.append( 1U , map[n%16UL] ) ;
690 s.append( 1U , map[(n/4096UL)%16UL] ) ;
691 s.append( 1U , map[(n/256UL)%16UL] ) ;
692 s.append( 1U , map[(n/16UL)%16UL] ) ;
693 s.append( 1U , map[n%16UL] ) ;
704 result.reserve( in.length() + 1U ) ;
705 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
true) ) ;
712 result.reserve( in.length() + 1U ) ;
713 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
false) ) ;
720 PrintableAppender<char,unsigned char> append_printable( result , escape ,
false ) ;
721 append_printable( c ) ;
728 result.reserve( in.length() * 3U ) ;
729 std::for_each( in.begin() , in.end() , PrintableAppender<wchar_t,unsigned long>(result,escape,
false) ) ;
736 readLineFrom( stream , eol.empty() ? std::string(1U,
'\n') : eol , result , true ) ;
740 void G::Str::readLineFrom( std::istream & stream ,
const std::string & eol , std::string & line ,
bool pre_erase )
742 G_ASSERT( eol.length() != 0U ) ;
748 if( eol.length() == 2U && eol[0] != eol[1] && line.length() == 1U )
756 const char c = line[0] ;
758 std::getline( stream , line , eol[1] ) ;
759 const std::string::size_type line_length = line.length() ;
760 bool complete = line_length > 0U && line[line_length-1U] == eol[0] ;
763 line.resize( line_length - 1U ) ;
764 line.insert( 0U , &c , 1U ) ;
768 line.insert( 0U , &c , 1U ) ;
771 line.append( 1U , eol[1] ) ;
772 readLineFromImp( stream , eol , line ) ;
778 readLineFromImp( stream , eol , line ) ;
782 void G::Str::readLineFromImp( std::istream & stream ,
const std::string & eol , std::string & line )
784 const size_type limit = line.max_size() ;
785 const size_type eol_length = eol.length() ;
786 const char eol_final = eol.at( eol_length - 1U ) ;
787 size_type line_length = line.length() ;
789 bool changed = false ;
799 stream.clear( ( stream.rdstate() & ~
std::ios_base::failbit ) | std::ios_base::eofbit ) ;
803 if( line_length == limit )
805 stream.setstate( std::ios_base::failbit ) ;
809 line.append( 1U , c ) ;
813 if( line_length >= eol_length && c == eol_final )
815 const size_type offset = line_length - eol_length ;
816 if( line.find(eol,offset) == offset )
824 stream.setstate( std::ios_base::failbit ) ;
827 std::string
G::Str::wrap( std::string text ,
const std::string & prefix_1 ,
828 const std::string & prefix_2 , size_type width )
830 std::string ws(
" \t\n" ) ;
831 std::ostringstream ss ;
832 for(
bool first_line =
true ; text.length() ; first_line = false )
834 const size_type prefix_length =
835 first_line ? prefix_1.length() : prefix_2.length() ;
836 size_type w = (width > prefix_length) ? (width-prefix_length) : width ;
838 const size_type pos_nl = text.find_first_of(
"\n") ;
839 if( pos_nl != std::string::npos && pos_nl != 0U && pos_nl < w )
844 std::string line = text ;
845 if( text.length() > w )
847 line = text.substr( 0U , w ) ;
848 if( text.find_first_of(ws,w) != w )
850 const size_type white_space = line.find_last_of( ws ) ;
851 const size_type black_space = line.find_first_not_of( ws ) ;
852 if( white_space != std::string::npos &&
853 black_space != std::string::npos &&
854 (white_space+1U) != black_space )
856 line = line.substr( 0U , white_space ) ;
861 if( line.length() != 0U )
863 ss << ( first_line ? prefix_1 : prefix_2 ) << line << std::endl ;
866 text = text.length() == line.length() ?
867 std::string() : text.substr(line.length()) ;
869 const size_type black_space = text.find_first_not_of( ws ) ;
870 if( black_space != 0U && black_space != std::string::npos )
872 unsigned int newlines = 0U ;
873 for( size_type pos = 0U ; pos < black_space ; ++pos )
875 if( text.at(pos) ==
'\n' )
879 ss << prefix_2 << std::endl ;
883 text = text.substr( black_space ) ;
891 template <
typename T>
892 void splitIntoTokens_(
const std::string & in , T & out ,
const std::string & ws )
894 typedef G::Str::size_type size_type ;
895 for( size_type p = 0U ; p != std::string::npos ; )
897 p = in.find_first_not_of( ws , p ) ;
898 if( p != std::string::npos )
900 size_type end = in.find_first_of( ws , p ) ;
901 size_type len = end == std::string::npos ? end : (end-p) ;
902 out.push_back( in.substr(p,len) ) ;
910 splitIntoTokens_( in , out , ws ) ;
915 splitIntoTokens_( in , out , ws ) ;
921 template <
typename T>
922 void splitIntoFields_(
const std::string & in , T & out ,
const std::string & ws )
930 if( pos >= in.length() )
break ;
931 pos = in.find_first_of( ws , pos ) ;
932 if( pos == std::string::npos ) break ;
933 out.push_back( in.substr(start,pos-start) ) ;
937 out.push_back( in.substr(start,pos-start) ) ;
940 template <
typename T>
941 void splitIntoFields_(
const std::string & in_in , T & out ,
const std::string & ws ,
942 char escape ,
bool remove_escapes )
944 std::string ews( ws ) ;
945 if( escape !=
'\0' ) ews.append( 1U , escape ) ;
948 std::string in = in_in ;
953 if( pos >= in.length() )
break ;
954 pos = in.find_first_of( ews , pos ) ;
955 if( pos == std::string::npos ) break ;
956 if( in.at(pos) == escape )
959 in.erase( pos , 1U ) ;
966 out.push_back( in.substr(start,pos-start) ) ;
971 out.push_back( in.substr(start,pos-start) ) ;
976 char escape ,
bool remove_escapes )
978 splitIntoFields_( in , out , ws , escape , remove_escapes ) ;
983 splitIntoFields_( in , out , ws ) ;
989 template <
typename T>
990 struct Joiner : std::unary_function<const T&,void>
995 Joiner( T & result_ ,
const T & sep_ ,
bool & first_ ) :
1002 void operator()(
const T & s )
1004 if( !first ) result.append( sep ) ;
1005 result.append( s ) ;
1012 const std::string & pre_in ,
const std::string & post )
1014 std::string pre = pre_in.empty() ? std::string(
"=") : pre_in ;
1015 std::string result ;
1017 Joiner<std::string> joiner( result , sep , first ) ;
1018 for( StringMap::const_iterator p = map.begin() ; p != map.end() ; ++p )
1019 joiner( (*p).first + pre + (*p).second + post ) ;
1025 std::string result ;
1027 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
1031 std::string
G::Str::join(
const std::string & sep ,
const std::set<std::string> & strings )
1033 std::string result ;
1035 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
1039 std::string
G::Str::join(
const std::string & sep ,
const std::string & s1 ,
const std::string & s2 ,
1040 const std::string & s3 ,
const std::string & s4 ,
const std::string & s5 ,
const std::string & s6 ,
1041 const std::string & s7 ,
const std::string & s8 ,
const std::string & s9 )
1043 std::string result ;
1044 joinImp( sep , result , s1 ) ;
1045 joinImp( sep , result , s2 ) ;
1046 joinImp( sep , result , s3 ) ;
1047 joinImp( sep , result , s4 ) ;
1048 joinImp( sep , result , s5 ) ;
1049 joinImp( sep , result , s6 ) ;
1050 joinImp( sep , result , s7 ) ;
1051 joinImp( sep , result , s8 ) ;
1052 joinImp( sep , result , s9 ) ;
1056 void G::Str::joinImp(
const std::string & sep , std::string & result ,
const std::string & s )
1058 if( !result.empty() && !s.empty() )
1059 result.append( sep ) ;
1060 result.append( s ) ;
1065 template <
typename T>
1066 struct Firster : std::unary_function<const T&,const typename T::first_type&>
1068 const typename T::first_type & operator()(
const T & pair ) {
return pair.first ; }
1073 std::set<std::string> result ;
1074 std::transform( map.begin() , map.end() , std::inserter(result,result.end()) , Firster<StringMap::value_type>() ) ;
1080 return std::string(
" \t\n\r") ;
1085 return std::string(
"~<>[]*$|?\\(){}\"`'&;=") ;
1088 std::string
G::Str::head(
const std::string & in , std::string::size_type pos ,
const std::string & default_ )
1091 pos == std::string::npos ?
1093 ( pos == 0U ? std::string() : ( pos >= in.length() ? in : in.substr(0U,pos) ) ) ;
1096 std::string
G::Str::head(
const std::string & in ,
const std::string & sep ,
bool default_empty )
1098 size_t pos = sep.empty() ? std::string::npos : in.find( sep ) ;
1099 return head( in , pos , default_empty ? std::string() : in ) ;
1102 std::string
G::Str::tail(
const std::string & in , std::string::size_type pos ,
const std::string & default_ )
1105 pos == std::string::npos ?
1107 ( (pos+1U) >= in.length() ? std::string() : in.substr(pos+1U) ) ;
1110 std::string
G::Str::tail(
const std::string & in ,
const std::string & sep ,
bool default_empty )
1112 size_t pos = sep.empty() ? std::string::npos : in.find(sep) ;
1113 if( pos != std::string::npos ) pos += (sep.length()-1U) ;
1114 return tail( in , pos , default_empty ? std::string() : in ) ;
1121 ( in.length() >= tail.length() && 0 == in.compare(in.length()-tail.length(),tail.length(),tail) ) ;
1128 const std::string & m_tail ;
1129 TailMatch(
const std::string & s ) : m_tail(s) {}
1130 bool operator()(
const std::string & s )
const {
return G::Str::tailMatch(s,m_tail) ; }
1135 TailMatch matcher( tail ) ;
1136 return std::find_if( in.begin() , in.end() , matcher ) != in.end() ;
1143 ( in.length() >= head.length() && 0 == in.compare(0U,head.length(),head) ) ;
1150 const std::string & m_head ;
1151 HeadMatch(
const std::string & s ) : m_head(s) {}
1152 bool operator()(
const std::string & s )
const {
return G::Str::headMatch(s,m_head) ; }
1157 HeadMatch matcher( head ) ;
1158 return std::find_if( in.begin() , in.end() , matcher ) != in.end() ;
1163 G::StringArray::const_iterator
const end = in.end() ;
1164 for( G::StringArray::const_iterator p = in.begin() ; p != end ; ++p )
1166 if( headMatch( *p , head ) )
1167 return (*p).substr( head.length() ) ;
1169 return std::string() ;
1184 std::string s = trimmed( lower(s_in) , ws() ) ;
1185 return !s.empty() && ( s ==
"y" || s ==
"yes" || s ==
"t" || s ==
"true" || s ==
"1" || s ==
"on" ) ;
1190 std::string s = trimmed( lower(s_in) , ws() ) ;
1191 return !s.empty() && ( s ==
"n" || s ==
"no" || s ==
"f" || s ==
"false" || s ==
"0" || s ==
"off" ) ;
1201 StringArray::const_iterator
const end = a.end() ;
1202 for( StringArray::const_iterator p = a.begin() ; p != end ; ++p )
1212 bool icompare(
char c1 ,
char c2 )
1214 if( c1 >=
'A' && c1 <=
'Z' ) c1 +=
'\x20' ;
1215 if( c2 >=
'A' && c2 <=
'Z' ) c2 +=
'\x20' ;
1222 return a.size() == b.size() && std::equal( a.begin() , a.end() , b.begin() , icompare ) ;
1227 StringArray::const_iterator
const end = a.end() ;
1228 for( StringArray::const_iterator p = a.begin() ; p != end ; ++p )
1236 std::string::size_type
G::Str::ifind(
const std::string & s ,
const std::string & key , std::string::size_type pos )
1238 if( s.empty() || key.empty() || pos > s.length() )
return std::string::npos ;
1239 std::string::const_iterator p = std::search( s.begin()+pos , s.end() , key.begin() , key.end() , icompare ) ;
1240 return p == s.end() ? std::string::npos : std::distance(s.begin(),p) ;
1245 template <
typename T,
typename V>
1246 T unique_imp( T in , T end , V repeat , V replacement )
1252 T in_next = in ; ++in_next ;
1253 if( *in == repeat && *in_next == repeat )
1255 while( *in == repeat )
1257 *out++ = replacement ;
1270 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 size_type ifind(const std::string &s, const std::string &key, size_type pos=0U)
Returns the position of the key in 's' using a Latin-1 case-insensitive search.
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 std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
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 string has the given ending (or ending is empty).
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
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 std::string dequote(const std::string &, char qq= '\"' , char esc = '\\' , const std::string & ws = Str::ws() )
Dequotes a string by removing unescaped quotes and escaping quoted whitespace, so "qq-aaa-esc-qq-bbb-...
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring Latin-1 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'.
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 headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
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 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 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' characters replaced by one 'r' character.
static double toDouble(const std::string &s)
Converts string 's' to a double.
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 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 bool match(const std::string &, const std::string &)
Returns true if the two strings are the same.
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 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 with separators.
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 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 void splitIntoFields(const std::string &in, StringArray &out, const std::string &separators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.
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 with a tilde as the first character.
static std::string ws()
Returns a string of standard whitespace characters.
static std::string toPrintableAscii(char c, char escape= '\\')
Returns a 7-bit printable representation of the given input character.
static void toUpper(std::string &s)
Replaces all Latin-1 lowercase characters in string 's' by uppercase characters.