E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gstr.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 // ===
17 ///
18 /// \file gstr.h
19 ///
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 #include <set>
33
34 namespace G
35 {
36 class Str ;
37 }
38
39 /// \class G::Str
40 /// A static class which provides string helper functions.
41 ///
42 class G::Str
43 {
44 public:
45 G_EXCEPTION_CLASS( Overflow , "string conversion error: over/underflow" ) ;
46 G_EXCEPTION_CLASS( InvalidFormat, "string conversion error: invalid format" ) ;
47 G_EXCEPTION_CLASS( NotEmpty, "internal error: string container not empty" ) ;
48 typedef std::string::size_type size_type ;
49
50 struct Limited /// Overload discrimiator for G::Str::toUWhatever()
51 {} ;
52
53 static bool replace( std::string & s , const std::string & from , const std::string & to ,
54 size_type * pos_p = nullptr ) ;
55 ///< Replaces 'from' with 'to', starting at offset '*pos_p'.
56 ///< Returns true if a substitution was made, and adjusts
57 ///< '*pos_p' by to.length().
58
59 static unsigned int replaceAll( std::string & s , const std::string & from , const std::string & to ) ;
60 ///< Does a global replace on string 's', replacing all occurances
61 ///< of sub-string 'from' with 'to'. Returns the number of substitutions
62 ///< made. Consider using in a while loop if 'from' is more than one
63 ///< character.
64
65 static unsigned int replaceAll( std::string & s , const char * from , const char * to ) ;
66 ///< A c-string overload, provided for performance reasons.
67
68 static void removeAll( std::string & , char ) ;
69 ///< Removes all occurrences of the character from the string.
70
71 static void trimLeft( std::string & s , const std::string & ws , size_type limit = 0U ) ;
72 ///< Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
73
74 static void trimRight( std::string & s , const std::string & ws , size_type limit = 0U ) ;
75 ///< Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
76
77 static void trim( std::string & s , const std::string & ws ) ;
78 ///< Trims both ends of s, taking off any of the 'ws' characters.
79
80 static std::string trimmed( const std::string & s , const std::string & ws ) ;
81 ///< Returns a trim()med version of s.
82
83 static bool isNumeric( const std::string & s , bool allow_minus_sign = false ) ;
84 ///< Returns true if every character is a decimal digit.
85 ///< Empty strings return true.
86
87 static bool isPrintableAscii( const std::string & s ) ;
88 ///< Returns true if every character is a 7-bit, non-control
89 ///< character (ie. 0x20<=c<0x7f). Empty strings return true.
90
91 static bool isUShort( const std::string & s ) ;
92 ///< Returns true if the string can be converted into
93 ///< an unsigned short without throwing an exception.
94
95 static bool isUInt( const std::string & s ) ;
96 ///< Returns true if the string can be converted into
97 ///< an unsigned integer without throwing an exception.
98
99 static bool isULong( const std::string & s ) ;
100 ///< Returns true if the string can be converted into
101 ///< an unsigned long without throwing an exception.
102
103 static bool isInt( const std::string & s ) ;
104 ///< Returns true if the string can be converted into
105 ///< an integer without throwing an exception.
106
107 static std::string fromBool( bool b ) ;
108 ///< Converts boolean 'b' to a string.
109
110 static std::string fromDouble( double d ) ;
111 ///< Converts double 'd' to a string.
112
113 static std::string fromInt( int i ) ;
114 ///< Converts int 'i' to a string.
115
116 static std::string fromLong( long l ) ;
117 ///< Converts long 'l' to a string.
118
119 static std::string fromShort( short s ) ;
120 ///< Converts short 's' to a string.
121
122 static std::string fromUInt( unsigned int ui ) ;
123 ///< Converts unsigned int 'ui' to a string.
124
125 static std::string fromULong( unsigned long ul ) ;
126 ///< Converts unsigned long 'ul' to a string.
127
128 static std::string fromUShort( unsigned short us ) ;
129 ///< Converts unsigned short 'us' to a string.
130
131 static bool toBool( const std::string & s ) ;
132 ///< Converts string 's' to a bool.
133 ///<
134 ///< Exception: InvalidFormat
135
136 static double toDouble( const std::string & s ) ;
137 ///< Converts string 's' to a double.
138 ///<
139 ///< Exception: Overflow
140 ///< Exception: InvalidFormat
141
142 static int toInt( const std::string & s ) ;
143 ///< Converts string 's' to an int.
144 ///<
145 ///< Exception: Overflow
146 ///< Exception: InvalidFormat
147
148 static long toLong( const std::string & s ) ;
149 ///< Converts string 's' to a long.
150 ///<
151 ///< Exception: Overflow
152 ///< Exception: InvalidFormat
153
154 static short toShort( const std::string & s ) ;
155 ///< Converts string 's' to a short.
156 ///<
157 ///< Exception: Overflow
158 ///< Exception: InvalidFormat
159
160 static unsigned int toUInt( const std::string & s ) ;
161 ///< Converts string 's' to an unsigned int.
162 ///<
163 ///< Exception: Overflow
164 ///< Exception: InvalidFormat
165
166 static unsigned int toUInt( const std::string & s , Limited ) ;
167 ///< Converts string 's' to an unsigned int.
168 ///<
169 ///< Very large numeric strings are limited to the maximum
170 ///< value of the numeric type, without an Overflow exception.
171 ///<
172 ///< Exception: InvalidFormat
173
174 static unsigned int toUInt( const std::string & s1 , const std::string & s2 ) ;
175 ///< Overload that converts the first string if it can be converted
176 ///< without throwing, or otherwise the second string.
177
178 static unsigned long toULong( const std::string & s , Limited ) ;
179 ///< Converts string 's' to an unsigned long.
180 ///<
181 ///< Very large numeric strings are limited to the maximum value
182 ///< of the numeric type, without an Overflow exception.
183 ///<
184 ///< Exception: InvalidFormat
185
186 static unsigned long toULong( const std::string & s ) ;
187 ///< Converts string 's' to an unsigned long.
188 ///<
189 ///< Exception: Overflow
190 ///< Exception: InvalidFormat
191
192 static unsigned short toUShort( const std::string & s , Limited ) ;
193 ///< Converts string 's' to an unsigned short.
194 ///<
195 ///< Very large numeric strings are limited to the maximum value
196 ///< of the numeric type, without an Overflow exception.
197 ///<
198 ///< Exception: InvalidFormat
199
200 static unsigned short toUShort( const std::string & s ) ;
201 ///< Converts string 's' to an unsigned short.
202 ///<
203 ///< Exception: Overflow
204 ///< Exception: InvalidFormat
205
206 static void toUpper( std::string & s ) ;
207 ///< Replaces all Latin-1 lowercase characters in string 's' by
208 ///< uppercase characters.
209
210 static void toLower( std::string & s ) ;
211 ///< Replaces all Latin-1 uppercase characters in string 's' by
212 ///< lowercase characters.
213
214 static std::string upper( const std::string & s ) ;
215 ///< Returns a copy of 's' in which all Latin-1 lowercase characters
216 ///< have been replaced by uppercase characters.
217
218 static std::string lower( const std::string & s ) ;
219 ///< Returns a copy of 's' in which all Latin-1 uppercase characters
220 ///< have been replaced by lowercase characters.
221
222 static std::string toPrintableAscii( char c , char escape = '\\' ) ;
223 ///< Returns a 7-bit printable representation of the given input character.
224
225 static std::string toPrintableAscii( const std::string & in , char escape = '\\' ) ;
226 ///< Returns a 7-bit printable representation of the given input string.
227
228 static std::string toPrintableAscii( const std::wstring & in , wchar_t escape = L'\\' ) ;
229 ///< Returns a 7-bit printable representation of the given wide input string.
230
231 static std::string printable( const std::string & in , char escape = '\\' ) ;
232 ///< Returns a printable represention of the given input string.
233 ///< Typically used to prevent escape sequences getting into log files.
234
235 static void escape( std::string & s , char c_escape , const std::string & specials_in ,
236 const std::string & specials_out ) ;
237 ///< Prefixes each occurrence of one of the special-in characters with
238 ///< the escape character and its corresponding special-out character.
239 ///<
240 ///< If the specials-in string contains the nul character it must be
241 ///< at the end, otherwise the two specials strings must be the same
242 ///< length. The specials-out string cannot contain the nul character.
243 ///<
244 ///< The specials-in string should normally include the escape character
245 ///< itself, otherwise unescaping will not recover the original.
246
247 static void escape( std::string & s , char c_escape , const char * specials_in ,
248 const char * specials_out ) ;
249 ///< Overload for c-style 'special' strings.
250
251 static void escape( std::string & s ) ;
252 ///< Overload for 'normal' backslash escaping of whitespace.
253
254 static std::string escaped( const std::string & , char c_escape , const std::string & specials_in ,
255 const std::string & specials_out ) ;
256 ///< Returns the escape()d string.
257
258 static std::string escaped( const std::string & , char c_escape , const char * specials_in ,
259 const char * specials_out ) ;
260 ///< Returns the escape()d string.
261
262 static std::string escaped( const std::string & ) ;
263 ///< Returns the escape()d string.
264
265 static void unescape( std::string & s , char c_escape , const char * specials_in , const char * specials_out ) ;
266 ///< Unescapes the string by replacing e-e with e, e-special-in with
267 ///< special-out, and e-other with other.
268 ///<
269 ///< If the specials-out string includes the nul character then it must be at
270 ///< the end, otherwise the two specials strings must be the same length.
271
272 static void unescape( std::string & s ) ;
273 ///< Overload for "normal" unescaping where the string has backslash escaping
274 ///< of whitespace.
275
276 static std::string unescaped( const std::string & s ) ;
277 ///< Returns the unescape()d version of s.
278
279 static std::string meta() ;
280 ///< Returns a list of shell meta-characters with a tilde as the
281 ///< first character. Does not contain the nul character. This is
282 ///< typically used with escape().
283
284 static std::string readLineFrom( std::istream & stream , const std::string & eol = std::string() ) ;
285 ///< Reads a line from the stream using the given line terminator.
286 ///< The line terminator is not part of the returned string.
287 ///< The terminator defaults to the newline.
288 ///<
289 ///< Note that alternatives in the standard library such as
290 ///< std::istream::getline() or std::getline(stream,string)
291 ///< in the standard "string" header are limited to a single
292 ///< character as the terminator.
293 ///<
294 ///< The stream's fail bit is set if (1) an empty string was
295 ///< returned because the stream was already at eof or (2)
296 ///< the string overflowed. Therefore, ignoring overflow, if
297 ///< the stream ends in an incomplete line that line fragment
298 ///< is returned with the stream's eof flag set but the fail bit
299 ///< reset and the next attempted read will return an empty string
300 ///< with the fail bit set. If the stream ends with a complete
301 ///< line then the last line is returned with eof and fail
302 ///< bits reset and the next attempted read will return
303 ///< an empty string with eof and fail bits set.
304 ///<
305 ///< Boolean tests on a stream are equivalent to using fail(),
306 ///< and fail() tests for failbit or badbit, so to process
307 ///< even incomplete lines at the end we can use a read
308 ///< loop like "while(s.good()){read(s);if(s)...}".
309
310 static void readLineFrom( std::istream & stream , const std::string & eol , std::string & result ,
311 bool pre_erase_result = true ) ;
312 ///< An overload which avoids string copying.
313
314 static std::string wrap( std::string text ,
315 const std::string & prefix_first_line , const std::string & prefix_subsequent_lines ,
316 size_type width = 70U ) ;
317 ///< Does word-wrapping. The return value is a string with
318 ///< embedded newlines.
319
320 static void splitIntoTokens( const std::string & in , StringArray & out , const std::string & ws ) ;
321 ///< Splits the string into 'ws'-delimited tokens. The behaviour is like
322 ///< strtok() in that adjacent delimiters count as one and leading and
323 ///< trailing delimiters are ignored. The output array is cleared first.
324
325 static StringArray splitIntoTokens( const std::string & in , const std::string & ws = Str::ws() ) ;
326 ///< Overload that returns by value.
327
328 static void splitIntoFields( const std::string & in , StringArray & out ,
329 const std::string & separators , char escape = '\0' ,
330 bool remove_escapes = true ) ;
331 ///< Splits the string into fields. Duplicated, leading and trailing
332 ///< separator characters are all significant. Ths output array is
333 ///< cleared first.
334 ///<
335 ///< If a non-null escape character is given then escaped separators
336 ///< do not result in a split. If the 'remove-escapes' parameter is
337 ///< true then all unescaped escapes are removed from the output; this
338 ///< is generally the preferred behaviour for nested escaping. If the
339 ///< 'remove-escapes' parameter is false then escapes are not removed;
340 ///< unescaped escapes are used to prevent splitting but they remain
341 ///< in the output.
342
343 static StringArray splitIntoFields( const std::string & in , const std::string & ws = Str::ws() ) ;
344 ///< Overload that returns by value.
345
346 static std::string dequote( const std::string & , char qq = '\"' , char esc = '\\' , const std::string & ws = Str::ws() ) ;
347 ///< Dequotes a string by removing unescaped quotes and escaping
348 ///< quoted whitespace, so "qq-aaa-esc-qq-bbb-ws-ccc-qq" becomes
349 ///< "aaa-qq-bbb-esc-ws-ccc".
350
351 static std::string join( const std::string & sep , const StringArray & strings ) ;
352 ///< Concatenates an array of strings with separators.
353
354 static std::string join( const std::string & sep , const std::set<std::string> & strings ) ;
355 ///< Concatenates a set of strings with separators.
356
357 static std::string join( const std::string & sep , const std::string & s1 , const std::string & s2 ,
358 const std::string & s3 = std::string() , const std::string & s4 = std::string() , const std::string & s5 = std::string() ,
359 const std::string & s6 = std::string() , const std::string & s7 = std::string() , const std::string & s8 = std::string() ,
360 const std::string & s9 = std::string() ) ;
361 ///< Concatenates a small number of strings with separators.
362 ///< In this overload empty strings are ignored.
363
364 static std::string join( const std::string & sep , const StringMap & ,
365 const std::string & eq = std::string(1U,'=') , const std::string & tail = std::string() ) ;
366 ///< Contatenates entries in a map, where an entry is "<key><eq><value><tail>".
367
368 static std::set<std::string> keySet( const StringMap & string_map ) ;
369 ///< Extracts the keys from a map of strings.
370
371 static std::string head( const std::string & in , size_type pos ,
372 const std::string & default_ = std::string() ) ;
373 ///< Returns the first part of the string up to just before the given position.
374 ///< The character at pos is not returned. Returns the supplied default
375 ///< if pos is npos. Returns the whole string if pos is one-or-more
376 ///< off the end.
377
378 static std::string head( const std::string & in , const std::string & sep , bool default_empty = true ) ;
379 ///< Overload taking a separator string, and with the default
380 ///< as either the input string or the empty string. If the
381 ///< separator occurs more than once in the input then only the
382 ///< first occurence is relevant.
383
384 static std::string tail( const std::string & in , size_type pos ,
385 const std::string & default_ = std::string() ) ;
386 ///< Returns the last part of the string after the given position.
387 ///< The character at pos is not returned. Returns the supplied default
388 ///< if pos is npos. Returns the empty string if pos is one-or-more
389 ///< off the end.
390
391 static std::string tail( const std::string & in , const std::string & sep , bool default_empty = true ) ;
392 ///< Overload taking a separator string, and with the default
393 ///< as either the input string or the empty string. If the
394 ///< separator occurs more than once in the input then only the
395 ///< first occurence is relevant.
396
397 static bool match( const std::string & , const std::string & ) ;
398 ///< Returns true if the two strings are the same.
399
400 static bool match( const StringArray & , const std::string & ) ;
401 ///< Returns true if any string in the array matches the given string.
402
403 static bool imatch( const std::string & , const std::string & ) ;
404 ///< Returns true if the two strings are the same, ignoring Latin-1 case.
405 ///< The locale is ignored.
406
407 static bool imatch( const StringArray & , const std::string & ) ;
408 ///< Returns true if any string in the array matches the given string, ignoring
409 ///< Latin-1 case. The locale is ignored.
410
411 static bool tailMatch( const std::string & in , const std::string & ending ) ;
412 ///< Returns true if the string has the given ending (or ending is empty).
413
414 static bool tailMatch( const StringArray & in , const std::string & ending ) ;
415 ///< Returns true if any string in the array has the given ending
416 ///< (or ending is empty).
417
418 static bool headMatch( const std::string & in , const std::string & head ) ;
419 ///< Returns true if the string has the given start (or head is empty).
420
421 static bool headMatch( const StringArray & in , const std::string & head ) ;
422 ///< Returns true if any string in the array has the given start
423 ///< (or head is empty).
424
425 static std::string headMatchResidue( const StringArray & in , const std::string & head ) ;
426 ///< Returns the unmatched part of the first string in the array that has
427 ///< the given start. Returns the empty string if nothing matches or if
428 ///< the first match is an exact match.
429
430 static std::string ws() ;
431 ///< Returns a string of standard whitespace characters.
432
433 static std::string positive() ;
434 ///< Returns a default positive string. See isPositive().
435
436 static std::string negative() ;
437 ///< Returns a default negative string. See isNegative().
438
439 static bool isPositive( const std::string & ) ;
440 ///< Returns true if the string has a positive meaning, such as "1", "true", "yes".
441
442 static bool isNegative( const std::string & ) ;
443 ///< Returns true if the string has a negative meaning, such as "0", "false", "no".
444
445 static size_type ifind( const std::string & s , const std::string & key ,
446 size_type pos = 0U ) ;
447 ///< Returns the position of the key in 's' using a Latin-1 case-insensitive
448 ///< search. The locale is ignored.
449
450 static std::string unique( std::string s , char c = ' ' , char r = ' ' ) ;
451 ///< Returns a string with repeated 'c' characters replaced by
452 ///< one 'r' character. Single 'c' characters are not replaced.
453
454 private:
455 Str() ; // not implemented
456 static void readLineFromImp( std::istream & , const std::string & , std::string & ) ;
457 static unsigned short toUShortImp( const std::string & s , bool & overflow , bool & invalid ) ;
458 static unsigned long toULongImp( const std::string & s , bool & overflow , bool & invalid ) ;
459 static unsigned int toUIntImp( const std::string & s , bool & overflow , bool & invalid ) ;
460 static short toShortImp( const std::string & s , bool & overflow , bool & invalid ) ;
461 static long toLongImp( const std::string & s , bool & overflow , bool & invalid ) ;
462 static int toIntImp( const std::string & s , bool & overflow , bool & invalid ) ;
463 static void escapeImp( std::string & , char , const char * , const char * , bool ) ;
464 static void joinImp( const std::string & , std::string & , const std::string & ) ;
465 } ;
466
467 #endif
G::Str::toULong
static unsigned long toULong(const std::string &s, Limited)
Converts string 's' to an unsigned long.
Definition: gstr.cpp:509
G::Str::positive
static std::string positive()
Returns a default positive string. See isPositive().
Definition: gstr.cpp:1172
G::Str::fromBool
static std::string fromBool(bool b)
Converts boolean 'b' to a string.
Definition: gstr.cpp:320
G::Str::ifind
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.
Definition: gstr.cpp:1236
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:701
gstrings.h
G::Str::toBool
static bool toBool(const std::string &s)
Converts string 's' to a bool.
Definition: gstr.cpp:374
G::Str::tail
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.
Definition: gstr.cpp:1102
G::Str::toInt
static int toInt(const std::string &s)
Converts string 's' to an int.
Definition: gstr.cpp:406
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:827
G::Str::tailMatch
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the string has the given ending (or ending is empty).
Definition: gstr.cpp:1117
G::Str::head
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.
Definition: gstr.cpp:1088
G::Str::isPositive
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1182
G::Str::fromDouble
static std::string fromDouble(double d)
Converts double 'd' to a string.
Definition: gstr.cpp:325
G::Str::dequote
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-...
Definition: gstr.cpp:99
G::Str::imatch
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring Latin-1 case.
Definition: gstr.cpp:1220
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
G::Str::replace
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'.
Definition: gstr.cpp:172
G::Str::splitIntoTokens
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:908
G::Str::toShort
static short toShort(const std::string &s)
Converts string 's' to a short.
Definition: gstr.cpp:450
G::Str::headMatch
static bool headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
Definition: gstr.cpp:1139
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:266
G::Str::escape
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...
Definition: gstr.cpp:66
G::Str::fromInt
static std::string fromInt(int i)
Converts int 'i' to a string.
Definition: gstr.cpp:332
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:246
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:224
G::Str::fromLong
static std::string fromLong(long l)
Converts long 'l' to a string.
Definition: gstr.cpp:339
G::Str::lower
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...
Definition: gstr.cpp:599
G::Str::toUInt
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:488
G::Str::escaped
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.
Definition: gstr.cpp:41
G::Str::toLower
static void toLower(std::string &s)
Replaces all Latin-1 uppercase characters in string 's' by lowercase characters.
Definition: gstr.cpp:594
G::Str::fromUInt
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
Definition: gstr.cpp:353
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 occurances of sub-string 'from' with 'to'...
Definition: gstr.cpp:195
G
Low-level classes.
Definition: garg.h:29
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:235
G::Str::isInt
static bool isInt(const std::string &s)
Returns true if the string can be converted into an integer without throwing an exception.
Definition: gstr.cpp:288
G::Str::unique
static std::string unique(std::string s, char c= ' ', char r= ' ')
Returns a string with repeated 'c' characters replaced by one 'r' character.
Definition: gstr.cpp:1268
G::Str::toDouble
static double toDouble(const std::string &s)
Converts string 's' to a double.
Definition: gstr.cpp:392
G::Str::headMatchResidue
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.
Definition: gstr.cpp:1161
G::Str::fromShort
static std::string fromShort(short s)
Converts short 's' to a string.
Definition: gstr.cpp:346
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:296
G::Str::upper
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...
Definition: gstr.cpp:619
G::Str::fromUShort
static std::string fromUShort(unsigned short us)
Converts unsigned short 'us' to a string.
Definition: gstr.cpp:367
G::Str::match
static bool match(const std::string &, const std::string &)
Returns true if the two strings are the same.
Definition: gstr.cpp:1194
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:251
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:733
G::Str::Limited
Overload discrimiator for G::Str::toUWhatever()
Definition: gstr.h:50
G::Str::isNegative
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1188
G::Str::unescape
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...
Definition: gstr.cpp:142
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:282
G::StringMap
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:34
G::Str::join
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1023
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:427
G::Str::toUShort
static unsigned short toUShort(const std::string &s, Limited)
Converts string 's' to an unsigned short.
Definition: gstr.cpp:553
G::Str::negative
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1177
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:304
G::Str::removeAll
static void removeAll(std::string &, char)
Removes all occurrences of the character from the string.
Definition: gstr.cpp:218
G::Str::keySet
static std::set< std::string > keySet(const StringMap &string_map)
Extracts the keys from a map of strings.
Definition: gstr.cpp:1071
G::Str::splitIntoFields
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.
Definition: gstr.cpp:975
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:312
G::Str::fromULong
static std::string fromULong(unsigned long ul)
Converts unsigned long 'ul' to a string.
Definition: gstr.cpp:360
G::Str::unescaped
static std::string unescaped(const std::string &s)
Returns the unescape()d version of s.
Definition: gstr.cpp:165
G::Str::meta
static std::string meta()
Returns a list of shell meta-characters with a tilde as the first character.
Definition: gstr.cpp:1083
G::Str::ws
static std::string ws()
Returns a string of standard whitespace characters.
Definition: gstr.cpp:1078
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:717
G::Str::toUpper
static void toUpper(std::string &s)
Replaces all Latin-1 lowercase characters in string 's' by uppercase characters.
Definition: gstr.cpp:614