33 std::string
G::Str::escaped(
const std::string & s_in ,
const std::string & specials ,
char c_escape )
35 std::string s( s_in ) ;
36 escape( s , specials , c_escape ) ;
40 void G::Str::escape( std::string & s ,
const std::string & specials ,
char c_escape )
45 pos = s.find_first_of( specials , pos ) ;
46 if( pos == std::string::npos )
49 s.insert( pos , 1U , c_escape ) ;
57 if( from.length() == 0 )
60 size_type pos = pos_p == NULL ? 0 : *pos_p ;
61 if( pos >= s.length() )
64 pos = s.find( from , pos ) ;
65 if( pos == std::string::npos )
71 s.replace( pos , from.length() , to ) ;
73 *pos_p = pos + to.length() ;
78 unsigned int G::Str::replaceAll( std::string & s ,
const std::string & from ,
const std::string & to )
80 unsigned int count = 0U ;
81 for(
size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
92 if( s.find(from) != std::string::npos )
94 unsigned int count = 0U ;
95 for(
size_type pos = 0U ; replace(s,from,to,&pos) ; count++ )
107 const std::string::iterator end = s.end() ;
108 s.erase( std::remove_if( s.begin() , end , std::bind1st(std::equal_to<char>(),c) ) , end ) ;
113 size_type n = s.find_first_not_of( ws ) ;
114 if( limit != 0U && ( n == std::string::npos || n > limit ) )
115 n = limit >= s.length() ? std::string::npos : limit ;
116 if( n == std::string::npos )
125 if( limit != 0U && ( n == std::string::npos || s.length() > (limit+n+1U) ) )
126 n = limit >= s.length() ? std::string::npos : (s.length()-limit-1U) ;
127 if( n == std::string::npos )
135 trimLeft(s,ws) ; trimRight(s,ws) ;
140 std::string s( s_in ) ;
147 struct IsDigit : std::unary_function<char,bool>
149 bool operator()(
char c )
const {
return !! isdigit( c ) ; }
154 const std::string::const_iterator end = s.end() ;
155 std::string::const_iterator p = s.begin() ;
156 if( allow_minus_sign && p != end && *p ==
'-' ) ++p ;
157 return std::find_if( p , end , std::not1(IsDigit()) ) == end ;
162 struct IsPrintableAscii : std::unary_function<char,bool>
164 bool operator()(
char c )
const {
return c >= 0x20 && c < 0x7f ; }
169 const std::string::const_iterator end = s.end() ;
170 return std::find_if( s.begin() , end , std::not1(IsPrintableAscii()) ) == end ;
177 G_IGNORE_RETURN(
unsigned short) toUShort(s) ;
183 catch( InvalidFormat & )
194 G_IGNORE_RETURN(
unsigned int) toUInt(s) ;
200 catch( InvalidFormat & )
211 G_IGNORE_RETURN(
unsigned long) toULong(s) ;
217 catch( InvalidFormat & )
226 return b ?
"true" :
"false" ;
231 std::ostringstream ss ;
232 ss << std::setprecision(16) << d ;
238 std::ostringstream ss ;
245 std::ostringstream ss ;
252 std::ostringstream ss ;
259 std::ostringstream ss ;
266 std::ostringstream ss ;
273 std::ostringstream ss ;
280 std::string str = lower( s ) ;
285 else if( str ==
"false" )
291 throw InvalidFormat( s ) ;
299 double result = ::strtod( s.c_str(), &end ) ;
301 if( end == 0 || end[0] !=
'\0' )
302 throw InvalidFormat( s ) ;
304 if( result == HUGE_VAL || result == -(HUGE_VAL) )
305 throw Overflow( s ) ;
312 long long_val = toLong( s ) ;
313 int int_val =
static_cast<int>( long_val ) ;
315 if( int_val != long_val )
316 throw Overflow( s ) ;
324 long result = ::strtol( s.c_str(), &end, 0 ) ;
326 if( end == 0 || end[0] !=
'\0' )
327 throw InvalidFormat( s ) ;
329 if( result == LONG_MAX || result == LONG_MIN )
330 throw Overflow( s ) ;
337 long long_val = toLong( s ) ;
338 short short_val =
static_cast<short>( long_val ) ;
340 if( short_val != long_val )
341 throw Overflow( s ) ;
348 unsigned long ulong_val = toULong( s ) ;
349 unsigned int uint_val =
static_cast<unsigned int>( ulong_val ) ;
351 if( uint_val != ulong_val )
354 uint_val = UINT_MAX ;
356 throw Overflow( s ) ;
365 unsigned long result = ::strtoul( s.c_str() , &end , 10 ) ;
367 if( end == 0 || end[0] !=
'\0' )
368 throw InvalidFormat( s ) ;
370 if( result == ULONG_MAX )
375 throw Overflow( s ) ;
383 unsigned long ulong_val = toULong( s ) ;
384 unsigned short ushort_val =
static_cast<unsigned short>( ulong_val ) ;
386 if( ushort_val != ulong_val )
389 ushort_val = USHRT_MAX ;
391 throw Overflow( s ) ;
399 struct ToLower : std::unary_function<char,char>
401 char operator()(
char c ) {
return static_cast<char>( tolower(c) ) ; }
406 std::transform( s.begin() , s.end() , s.begin() , ToLower() ) ;
410 std::string out = in ;
417 struct ToUpper : std::unary_function<char,char>
419 char operator()(
char c ) {
return static_cast<char>( toupper(c) ) ; }
424 std::transform( s.begin() , s.end() , s.begin() , ToUpper() ) ;
428 std::string out = in ;
435 template <
typename T
char =
char ,
typename Tu
char =
unsigned char>
436 struct PrintableAppender : std::unary_function<Tchar,void>
442 PrintableAppender( std::string & s_ , Tchar escape_ ,
bool eight_bit_ ) :
445 escape_out(static_cast<char>(escape_)) ,
446 eight_bit(eight_bit_)
449 void operator()( Tchar c )
451 const Tuchar uc =
static_cast<Tuchar
>(c) ;
454 s.append( 2U , escape_out ) ;
456 else if( !eight_bit && uc >= 0x20U && uc < 0x7FU && uc != 0xFFU )
458 s.append( 1U , static_cast<char>(c) ) ;
460 else if( eight_bit && ( ( uc >= 0x20U && uc < 0x7FU ) || uc >= 0xA0 ) && uc != 0xFFU )
462 s.append( 1U , static_cast<char>(c) ) ;
466 s.append( 1U , escape_out ) ;
467 if( static_cast<char>(c) ==
'\n' )
469 s.append( 1U ,
'n' ) ;
471 else if( static_cast<char>(c) ==
'\r' )
473 s.append( 1U ,
'r' ) ;
475 else if( static_cast<char>(c) ==
'\t' )
477 s.append( 1U ,
't' ) ;
481 s.append( 1U ,
'0' ) ;
485 s.append( 1U ,
'x' ) ;
486 const char *
const map =
"0123456789abcdef" ;
487 unsigned long n = uc ;
488 if(
sizeof(Tchar) == 1 )
491 s.append( 1U , map[(n/16UL)%16UL] ) ;
492 s.append( 1U , map[n%16UL] ) ;
497 s.append( 1U , map[(n/4096UL)%16UL] ) ;
498 s.append( 1U , map[(n/256UL)%16UL] ) ;
499 s.append( 1U , map[(n/16UL)%16UL] ) ;
500 s.append( 1U , map[n%16UL] ) ;
510 result.reserve( in.length() + 1U ) ;
511 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
true) ) ;
517 result.reserve( in.length() + 1U ) ;
518 std::for_each( in.begin() , in.end() , PrintableAppender<char,unsigned char>(result,escape,
false) ) ;
524 PrintableAppender<char,unsigned char> append_printable( result , escape ,
false ) ;
525 append_printable( c ) ;
531 result.reserve( in.length() * 3U ) ;
532 std::for_each( in.begin() , in.end() , PrintableAppender<wchar_t,unsigned long>(result,escape,
false) ) ;
539 readLineFrom( stream , eol.empty() ? std::string(1U,
'\n') : eol , result , true ) ;
543 void G::Str::readLineFrom( std::istream & stream ,
const std::string & eol , std::string & line ,
bool pre_erase )
551 if( eol.length() == 2U && eol[0] != eol[1] && line.length() == 1U )
559 const char c = line[0] ;
561 std::getline( stream , line , eol[1] ) ;
563 bool complete = line_length > 0U && line[line_length-1U] == eol[0] ;
566 line.resize( line_length - 1U ) ;
567 line.insert( 0U , &c , 1U ) ;
571 line.insert( 0U , &c , 1U ) ;
574 line.append( 1U , eol[1] ) ;
575 readLineFromImp( stream , eol , line ) ;
581 readLineFromImp( stream , eol , line ) ;
585 void G::Str::readLineFromImp( std::istream & stream ,
const std::string & eol , std::string & line )
587 const size_type limit = line.max_size() ;
588 const size_type eol_length = eol.length() ;
589 const char eol_final = eol.at( eol_length - 1U ) ;
592 bool changed = false ;
601 stream.clear( ( stream.rdstate() & ~std::ios_base::failbit ) | std::ios_base::eofbit ) ;
605 if( line_length == limit )
607 stream.setstate( std::ios_base::failbit ) ;
611 line.append( 1U , c ) ;
615 if( line_length >= eol_length && c == eol_final )
617 const size_type offset = line_length - eol_length ;
618 if( line.find(eol,offset) == offset )
629 stream.setstate( std::ios_base::failbit ) ;
633 std::string
G::Str::wrap( std::string text ,
const std::string & prefix_1 ,
634 const std::string & prefix_2 ,
size_type width )
636 std::string ws(
" \t\n" ) ;
637 std::ostringstream ss ;
638 for(
bool first_line =
true ; text.length() ; first_line = false )
641 first_line ? prefix_1.length() : prefix_2.length() ;
642 size_type w = (width > prefix_length) ? (width-prefix_length) : width ;
644 const size_type pos_nl = text.find_first_of(
"\n") ;
645 if( pos_nl != std::string::npos && pos_nl != 0U && pos_nl < w )
650 std::string line = text ;
651 if( text.length() > w )
653 line = text.substr( 0U , w ) ;
654 if( text.find_first_of(ws,w) != w )
656 const size_type white_space = line.find_last_of( ws ) ;
657 const size_type black_space = line.find_first_not_of( ws ) ;
658 if( white_space != std::string::npos &&
659 black_space != std::string::npos &&
660 (white_space+1U) != black_space )
662 line = line.substr( 0U , white_space ) ;
667 if( line.length() != 0U )
669 ss << ( first_line ? prefix_1 : prefix_2 ) << line << std::endl ;
672 text = text.length() == line.length() ?
673 std::string() : text.substr(line.length()) ;
675 const size_type black_space = text.find_first_not_of( ws ) ;
676 if( black_space != 0U && black_space != std::string::npos )
678 unsigned int newlines = 0U ;
679 for(
size_type pos = 0U ; pos < black_space ; ++pos )
681 if( text.at(pos) ==
'\n' )
685 ss << prefix_2 << std::endl ;
689 text = text.substr( black_space ) ;
697 template <
typename T>
698 void splitIntoTokens_(
const std::string & in , T & out ,
const std::string & ws )
701 for( size_type p = 0U ; p != std::string::npos ; )
703 p = in.find_first_not_of( ws , p ) ;
704 if( p != std::string::npos )
706 size_type end = in.find_first_of( ws , p ) ;
707 size_type len = end == std::string::npos ? end : (end-p) ;
708 out.push_back( in.substr(p,len) ) ;
716 splitIntoTokens_( in , out , ws ) ;
720 splitIntoTokens_( in , out , ws ) ;
725 template <
typename T>
726 void splitIntoFields_(
const std::string & in_in , T & out ,
const std::string & ws ,
727 char escape ,
bool discard_bogus )
730 std::string all( ws ) ;
732 all.append( 1U , escape ) ;
736 std::string in = in_in ;
737 size_type start = 0U ;
738 size_type last_pos = in.length() - 1U ;
742 if( pos >= in.length() )
break ;
743 pos = in.find_first_of( all , pos ) ;
744 if( pos == std::string::npos ) break ;
745 if( in.at(pos) == escape )
747 const bool valid = pos != last_pos && in.find(all,pos+1U) == (pos+1U) ;
748 if( valid || discard_bogus )
749 in.erase( pos , 1U ) ;
756 out.push_back( in.substr(start,pos-start) ) ;
761 out.push_back( in.substr(start,pos-start) ) ;
766 char escape ,
bool discard_bogus )
768 splitIntoFields_( in , out , ws , escape , discard_bogus ) ;
771 char escape ,
bool discard_bogus )
773 splitIntoFields_( in , out , ws , escape , discard_bogus ) ;
778 template <
typename T>
779 struct Joiner : std::unary_function<const T&,void>
784 Joiner( T & result_ ,
const T & sep_ ,
bool & first_ ) :
791 void operator()(
const T & s )
793 if( !first ) result.append( sep ) ;
803 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
810 std::for_each( strings.begin() , strings.end() , Joiner<std::string>(result,sep,first) ) ;
816 template <
typename T>
817 struct Firster : std::unary_function<const T&,const typename T::first_type&>
819 const typename T::first_type & operator()(
const T & pair ) {
return pair.first ; }
825 std::transform( map.begin() , map.end() , std::back_inserter(result) , Firster<StringMap::value_type>() ) ;
831 return std::string(
" \t\n\r") ;
837 pos == std::string::npos ?
839 ( pos == 0U ? std::string() : ( (pos+1U) >= in.length() ? in : in.substr(0U,pos) ) ) ;
845 pos == std::string::npos ?
847 ( (pos+1U) >= in.length() ? std::string() : in.substr(pos+1U) ) ;
854 ( in.length() >= ending.length() &&
855 in.substr( in.length() - ending.length() ) == ending ) ;
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
std::string::size_type size_type
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 unsigned int toUInt(const std::string &s, bool limited=false)
Converts string 's' to an unsigned int.
static int toInt(const std::string &s)
Converts string 's' to an int.
std::list< std::string > Strings
A std::list of std::strings.
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 void splitIntoFields(const std::string &in, Strings &out, const std::string &seperators, char escape= '\0', bool discard_bogus_escapes=true)
Splits the string into fields.
static void escape(std::string &, const std::string &specials, char escape= '\\')
Prefixes each occurrence of one of the special characters with the escape character.
static std::string fromDouble(double d)
Converts double 'd' to a string.
std::vector< std::string > StringArray
A std::vector of std::strings.
std::string::size_type size_type
A std::size_t type.
static void splitIntoTokens(const std::string &in, Strings &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 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 uppercase characters have been replaced by lowercase characters...
static void toLower(std::string &s)
Replaces all uppercase characters in string 's' by lowercase characters.
static Strings keys(const StringMap &string_map)
Extracts the keys from a map of strings.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
static bool replace(std::string &s, const std::string &from, const std::string &to, size_type *pos_p=NULL)
Replaces 'from' with 'to', starting at offset '*pos_p'.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurences 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 double toDouble(const std::string &s)
Converts string 's' to a double.
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 lowercase characters have been replaced by uppercase characters...
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
static std::string escaped(const std::string &, const std::string &specials, char escape= '\\')
Prefixes each occurrence of one of the special characters with the escape character.
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.
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 long toLong(const std::string &s)
Converts string 's' to a long.
static unsigned short toUShort(const std::string &s, bool limited=false)
Converts string 's' to an unsigned short.
static unsigned long toULong(const std::string &s, bool limited=false)
Converts string 's' to an unsigned long.
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 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 join(const Strings &strings, const std::string &sep)
Concatenates a set of strings.
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 toUpper(std::string &s)
Replaces all lowercase characters in string 's' by uppercase characters.