- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gstr.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20
21 #ifndef G_STR_H
22 #define G_STR_H
23
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gstrings.h"
27 #include <string>
28 #include <sstream>
29 #include <iostream>
30 #include <list>
31 #include <vector>
32
34 namespace G
35 {
36 class Str ;
37 }
38
42 class G::Str
43 {
44 public:
45 G_EXCEPTION_CLASS( Overflow , "conversion error: over/underflow" ) ;
46 G_EXCEPTION_CLASS( InvalidFormat, "conversion error: invalid format" ) ;
47 typedef std::string::size_type size_type ;
48
49 static bool replace( std::string & s ,
50 const std::string & from , const std::string & to ,
51 size_type * pos_p = NULL ) ;
55
56 static unsigned int replaceAll( std::string & s , const std::string & from , const std::string & to ) ;
61
62 static unsigned int replaceAll( std::string & s , const char * from , const char * to ) ;
64
65 static void removeAll( std::string & , char ) ;
67
68 static void trimLeft( std::string & s , const std::string & ws , size_type limit = 0U ) ;
70
71 static void trimRight( std::string & s , const std::string & ws , size_type limit = 0U ) ;
73
74 static void trim( std::string & s , const std::string & ws ) ;
76
77 static std::string trimmed( const std::string & s , const std::string & ws ) ;
79
80 static bool isNumeric( const std::string & s , bool allow_minus_sign = false ) ;
83
84 static bool isPrintableAscii( const std::string & s ) ;
87
88 static bool isUShort( const std::string & s ) ;
91
92 static bool isUInt( const std::string & s ) ;
95
96 static bool isULong( const std::string & s ) ;
99
100 static std::string fromBool( bool b ) ;
102
103 static std::string fromDouble( double d ) ;
105
106 static std::string fromInt( int i ) ;
108
109 static std::string fromLong( long l ) ;
111
112 static std::string fromShort( short s ) ;
114
115 static std::string fromUInt( unsigned int ui ) ;
117
118 static std::string fromULong( unsigned long ul ) ;
120
121 static std::string fromUShort( unsigned short us ) ;
123
124 static bool toBool( const std::string & s ) ;
128
129 static double toDouble( const std::string & s ) ;
134
135 static int toInt( const std::string & s ) ;
140
141 static long toLong( const std::string & s ) ;
146
147 static short toShort( const std::string & s ) ;
152
153 static unsigned int toUInt( const std::string & s , bool limited = false ) ;
162
163 static unsigned long toULong( const std::string & s , bool limited = false ) ;
172
173 static unsigned short toUShort( const std::string & s , bool limited = false ) ;
182
183 static void toUpper( std::string & s ) ;
186
187 static void toLower( std::string & s ) ;
190
191 static std::string upper( const std::string & s ) ;
194
195 static std::string lower( const std::string & s ) ;
198
199 static std::string toPrintableAscii( char c , char escape = '\\' ) ;
201
202 static std::string toPrintableAscii( const std::string & in , char escape = '\\' ) ;
204
205 static std::string toPrintableAscii( const std::wstring & in , wchar_t escape = L'\\' ) ;
207
208 static std::string printable( const std::string & in , char escape = '\\' ) ;
211
212 static void escape( std::string & , const std::string & specials , char escape = '\\' ) ;
215
216 static std::string escaped( const std::string & , const std::string & specials , char escape = '\\' ) ;
219
220 static std::string readLineFrom( std::istream & stream , const std::string & eol = std::string() ) ;
244
245 static void readLineFrom( std::istream & stream , const std::string & eol , std::string & result ,
246 bool pre_erase_result = true ) ;
248
249 static std::string wrap( std::string text ,
250 const std::string & prefix_first_line , const std::string & prefix_subsequent_lines ,
251 size_type width = 70U ) ;
254
255 static void splitIntoTokens( const std::string & in , Strings & out , const std::string & ws ) ;
260
261 static void splitIntoTokens( const std::string & in , StringArray & out , const std::string & ws ) ;
263
264 static void splitIntoFields( const std::string & in , Strings & out ,
265 const std::string & seperators , char escape = '\0' ,
266 bool discard_bogus_escapes = true ) ;
278
279 static void splitIntoFields( const std::string & in , StringArray & out ,
280 const std::string & seperators , char escape = '\0' , bool discard_bogus_escapes = true ) ;
282
283 static std::string join( const Strings & strings , const std::string & sep ) ;
285
286 static std::string join( const StringArray & strings , const std::string & sep ) ;
288
289 static Strings keys( const StringMap & string_map ) ;
291
292 static std::string head( const std::string & in , std::string::size_type pos ,
293 const std::string & default_ = std::string() ) ;
297
298 static std::string tail( const std::string & in , std::string::size_type pos ,
299 const std::string & default_ = std::string() ) ;
303
304 static bool tailMatch( const std::string & in , const std::string & ending ) ;
306
307 static std::string ws() ;
309
310 private:
311 static void readLineFromImp( std::istream & , const std::string & , std::string & ) ;
312 Str() ; // not implemented
313 } ;
314
315 #endif
G::Str::fromBool
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
Definition: gstr.cpp:224
G::Str::size_type
std::string::size_type size_type
Definition: gstr.h:45
G::Str::printable
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:507
gstrings.h
G::Str::toBool
static bool toBool(const std::string &s)
Converts string 's' to a bool.
Definition: gstr.cpp:278
G::Str::toUInt
static unsigned int toUInt(const std::string &s, bool limited=false)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:346
G::Str::toInt
static int toInt(const std::string &s)
Converts string 's' to an int.
Definition: gstr.cpp:310
G::Strings
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
G::Str::wrap
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.
Definition: gstr.cpp:633
G::Str::tailMatch
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the given string has the given ending.
Definition: gstr.cpp:850
G::Str::splitIntoFields
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.
Definition: gstr.cpp:765
G::Str::escape
static void escape(std::string &, const std::string &specials, char escape= '\\')
Prefixes each occurrence of one of the special characters with the escape character.
Definition: gstr.cpp:40
G::Str::fromDouble
static std::string fromDouble(double d)
Converts double 'd' to a string.
Definition: gstr.cpp:229
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:44
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
G::Str::splitIntoTokens
static void splitIntoTokens(const std::string &in, Strings &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:714
G::Str::toShort
static short toShort(const std::string &s)
Converts string 's' to a short.
Definition: gstr.cpp:335
G::Str::isNumeric
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
Definition: gstr.cpp:152
G::Str::fromInt
static std::string fromInt(int i)
Converts int 'i' to a string.
Definition: gstr.cpp:236
G::Str::tail
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.
Definition: gstr.cpp:842
G::Str::trim
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
Definition: gstr.cpp:133
G::Str::trimLeft
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.
Definition: gstr.cpp:111
G::Str::fromLong
static std::string fromLong(long l)
Converts long 'l' to a string.
Definition: gstr.cpp:243
G::Str::lower
static std::string lower(const std::string &s)
Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters...
Definition: gstr.cpp:408
G::Str::toLower
static void toLower(std::string &s)
Replaces all uppercase characters in string 's' by lowercase characters.
Definition: gstr.cpp:404
G::Str::keys
static Strings keys(const StringMap &string_map)
Extracts the keys from a map of strings.
Definition: gstr.cpp:822
G::Str::fromUInt
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
Definition: gstr.cpp:257
G::Str::replace
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'.
Definition: gstr.cpp:55
G::Str::replaceAll
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'...
Definition: gstr.cpp:78
G
Low-level classes.
G::Str::head
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.
Definition: gstr.cpp:834
G::Str::trimRight
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.
Definition: gstr.cpp:122
G::Str::toDouble
static double toDouble(const std::string &s)
Converts string 's' to a double.
Definition: gstr.cpp:296
G::Str::fromShort
static std::string fromShort(short s)
Converts short 's' to a string.
Definition: gstr.cpp:250
G::Str::isUShort
static bool isUShort(const std::string &s)
Returns true if the string can be converted into an unsigned short without throwing an exception...
Definition: gstr.cpp:173
G::Str::upper
static std::string upper(const std::string &s)
Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters...
Definition: gstr.cpp:426
G::Str::fromUShort
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
Definition: gstr.cpp:271
G::Str::escaped
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.
Definition: gstr.cpp:33
G::Str::trimmed
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
Definition: gstr.cpp:138
G::Str::readLineFrom
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
Definition: gstr.cpp:536
gexception.h
G::Str::isPrintableAscii
static bool isPrintableAscii(const std::string &s)
Returns true if every character is a 7-bit, non-control character (ie.
Definition: gstr.cpp:167
G::StringMap
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:49
G::Str
A static class which provides string helper functions.
Definition: gstr.h:42
G::Str::toLong
static long toLong(const std::string &s)
Converts string 's' to a long.
Definition: gstr.cpp:321
G::Str::toUShort
static unsigned short toUShort(const std::string &s, bool limited=false)
Converts string 's' to an unsigned short.
Definition: gstr.cpp:381
G::Str::toULong
static unsigned long toULong(const std::string &s, bool limited=false)
Converts string 's' to an unsigned long.
Definition: gstr.cpp:362
G::Str::isUInt
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Definition: gstr.cpp:190
G::Str::removeAll
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string.
Definition: gstr.cpp:105
G::Str::isULong
static bool isULong(const std::string &s)
Returns true if the string can be converted into an unsigned long without throwing an exception...
Definition: gstr.cpp:207
G::Str::fromULong
static std::string fromULong(unsigned long ul)
Converts unsigned long 'ul' to a string.
Definition: gstr.cpp:264
G::Str::join
static std::string join(const Strings &strings, const std::string &sep)
Concatenates a set of strings.
Definition: gstr.cpp:799
G::Str::ws
static std::string ws()
A convenience function returning standard whitespace characters.
Definition: gstr.cpp:829
G::Str::toPrintableAscii
static std::string toPrintableAscii(char c, char escape= '\\')
Returns a 7-bit printable representation of the given input character.
Definition: gstr.cpp:521
G_EXCEPTION_CLASS
#define G_EXCEPTION_CLASS(class_name, description)
Definition: gexception.h:87
G::Str::toUpper
static void toUpper(std::string &s)
Replaces all lowercase characters in string 's' by uppercase characters.
Definition: gstr.cpp:422