Jpp
JString.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSTRING__
2 #define __JLANG__JSTRING__
3 
4 #include <string>
5 #include <sstream>
6 #include <limits>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 
11 #include "JLang/JStringFacet.hh"
12 #include "JLang/JException.hh"
13 #include "JLang/JLangToolkit.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JLANG {}
21 namespace JPP { using namespace JLANG; }
22 
23 namespace JLANG {
24 
25  /**
26  * Wrapper class around STL string class.
27  */
28  class JString :
29  public std::string
30  {
31  public:
32 
34  using std::string::assign;
35 
36 
37  /**
38  * Default constructor.
39  */
40  JString() :
41  std::string()
42  {}
43 
44 
45  /**
46  * Constructor.
47  *
48  * \param buffer string
49  */
50  explicit JString(const std::string& buffer) :
51  std::string(buffer)
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param c char
59  */
60  explicit JString(const char c):
61  std::string(1,c)
62  {}
63 
64 
65  /**
66  * Constructor.
67  *
68  * \param buffer string
69  * \param length length
70  */
71  explicit JString(const char* buffer, const std::string::size_type length) :
72  std::string(buffer, length)
73  {}
74 
75 
76  /**
77  * Constructor.
78  *
79  * \param value value
80  */
81  template<class T>
82  JString(const T& value) :
83  std::string(valueOf(value))
84  {}
85 
86 
87  /**
88  * Constructor.
89  * This constructor compiles (see below) the input string.
90  *
91  * \param buffer input string
92  * \param facet facet
93  */
94  JString(const JString& buffer, const JStringFacet& facet) :
95  std::string(buffer)
96  {
97  compile(facet);
98  }
99 
100 
101  /**
102  * Constructor.
103  *
104  * Composes a string with the same text that would be printed if format was used on printf.
105  *
106  * \param format format
107  */
108  JString(const char* format, ...)
109  {
110  using namespace std;
111 
112  va_list args;
113 
114  for (int size = 1024; ; size <<= 1) {
115 
116  char buffer[size];
117 
118  va_start(args, format);
119 
120  const int length = vsnprintf(buffer, size, format, args);
121 
122  if (length < size) {
123 
124  this->assign(buffer, buffer + length);
125 
126  break;
127  }
128  }
129 
130  va_end(args);
131  }
132 
133 
134  /**
135  * Compile token with given facet.
136  * This method uses the given facet to parse the input string.
137  * The result is then compatible with the definition of a token and may be empty.
138  *
139  * \param facet facet
140  * \result this string
141  */
142  JString& compile(const JStringFacet& facet)
143  {
144  using namespace std;
145 
146  istringstream is(*this);
147 
148  ios_base::iostate state;
149 
150  facet.get(is, istreambuf_iterator<char>(), is, state, *this);
151 
152  return *this;
153  }
154 
155 
156 
157  /**
158  * Test if this string starts with the specified prefix.
159  *
160  * \param prefix prefix
161  * \return true if this string starts with prefix; else false
162  */
163  bool startsWith(const std::string& prefix) const
164  {
165  return this->size() >= prefix.size() && this->substr(0, prefix.size()) == prefix;
166  }
167 
168 
169  /**
170  * Test if this string ends with the specified suffix.
171  *
172  * \param suffix suffix
173  * \return true if this string ends with suffix; else false
174  */
175  bool endsWith(const std::string& suffix) const
176  {
177  return this->size() >= suffix.size() && this->substr(this->size() - suffix.size()) == suffix;
178  }
179 
180 
181  /**
182  * Replace characters.
183  *
184  * \param target target character
185  * \param replacement replacement character
186  * \param max maximum number of replacements
187  * \return this string
188  */
189  JString& replace(const char target,
190  const char replacement,
191  const std::size_t max = std::numeric_limits<std::size_t>::max())
192  {
193  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = find(target,i), --n) {
194  (*this)[i] = replacement;
195  }
196 
197  return *this;
198  }
199 
200 
201  /**
202  * Replace character sequences.
203  *
204  * \param target target string
205  * \param replacement replacement string
206  * \param max maximum number of replacements
207  * \return this string
208  */
209  JString& replace(const std::string& target,
210  const std::string& replacement,
211  const std::size_t max = std::numeric_limits<std::size_t>::max())
212  {
213  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
214  replace(this->begin() + i, this->begin() + i + target.length(), replacement);
215  }
216 
217  return *this;
218  }
219 
220 
221  /**
222  * Replace character sequence.
223  *
224  * \param target target string
225  * \param value value
226  * \param max maximum number of replacements
227  * \return this string
228  */
229  template<class T>
230  JString& replace(const std::string& target,
231  const T& value,
232  const std::size_t max = std::numeric_limits<std::size_t>::max())
233  {
234  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
235  replace(this->begin() + i, this->begin() + i + target.length(), JString(value));
236  }
237 
238  return *this;
239  }
240 
241 
242  /**
243  * Trim string.\n
244  * Returns the modified string, with leading and trailing white spaces omitted.
245  *
246  * \return this string
247  */
249  {
250  *this = JLANG::trim(*this);
251 
252  return *this;
253  }
254 
255 
256  /**
257  * Trim string.\n
258  * Returns the modified string, with leading and trailing target characters omitted.
259  *
260  * \param c strip character
261  * \return this string
262  */
263  JString& trim(const char c)
264  {
265  *this = JLANG::trim(*this, c);
266 
267  return *this;
268  }
269 
270 
271  /**
272  * Trim string.\n
273  * Returns the modified string, with leading and trailing target characters omitted.
274  *
275  * \param target character(s) to strip
276  * \return this string
277  */
278  JString trim(const std::string& target)
279  {
280  *this = JLANG::trim(*this, target);
281 
282  return *this;
283  }
284 
285 
286  /**
287  * Convert all character to upper case.
288  *
289  * \return this string
290  */
292  {
293  for (iterator i = begin(); i != end(); ++i) {
294  *i = toupper(*i);
295  }
296 
297  return *this;
298  }
299 
300 
301  /**
302  * Convert all character to lower case.
303  *
304  * \return this string
305  */
307  {
308  for (iterator i = begin(); i != end(); ++i) {
309  *i = tolower(*i);
310  }
311 
312  return *this;
313  }
314 
315 
316  /**
317  * Convert enumeration type to string.
318  *
319  * \param input value
320  * \return string
321  */
322  static JString valueOf(const int input)
323  {
324  std::ostringstream os;
325 
326  if (os << input)
327  return JString(os.str());
328  else
329  throw JException("JString::valueOf()");
330  }
331 
332 
333  /**
334  * Convert value to string.
335  *
336  * \param input value
337  * \return string
338  */
339  template<class T>
340  static JString valueOf(const T& input)
341  {
342  std::ostringstream os;
343 
344  if (os << input)
345  return JString(os.str());
346  else
347  throw JException("JString::valueOf()");
348  }
349 
350 
351  /**
352  * Convert string to value.
353  *
354  * \param input string
355  * \return value
356  */
357  template<class T>
358  static const T& toValue(const JString& input)
359  {
360  static T value;
361 
362  std::istringstream is(input);
363 
364  if (is >> value)
365  return value;
366  else
367  throw JException("JString::toValue<T>()");
368  }
369 
370 
371  /**
372  * Assign (part of) string to value.
373  *
374  * \param output value
375  * \return remaining string
376  */
377  template<class T>
378  JString& assign(T& output)
379  {
380  using namespace std;
381 
382  istringstream is(*this);
383 
384  is >> output;
385 
386  if (!is) {
387  throw JException("JString::assign()");
388  }
389 
390  this->assign(istreambuf_iterator<char>(is),
391  istreambuf_iterator<char>());
392 
393  return *this;
394  }
395 
396 
397  /**
398  * Read string from input stream.
399  *
400  * \param in input stream
401  * \param object string
402  * \return input stream
403  */
404  friend inline std::istream& operator>>(std::istream& in, JString& object)
405  {
406  using namespace std;
407 
408  istream::sentry sentry(in); // skips white spaces
409 
410  if (sentry) {
411 
412  const locale& loc = in.getloc();
413 
414  if (has_facet<JStringFacet>(loc)) {
415 
416  ios_base::iostate state;
417 
418  use_facet<JStringFacet>(loc).get(in, istreambuf_iterator<char>(), in, state, object);
419 
420  if (state != ios_base::goodbit && state != ios_base::eofbit) {
421  in.setstate(state);
422  }
423 
424  } else {
425 
426  in >> static_cast<string&>(object);
427  }
428  }
429 
430  return in;
431  }
432 
433 
434  /**
435  * Write string to output stream.
436  *
437  * \param out output stream
438  * \param object string
439  * \return output stream
440  */
441  friend inline std::ostream& operator<<(std::ostream& out, const JString& object)
442  {
443  using namespace std;
444 
445  const locale& loc = out.getloc();
446 
447  if (has_facet<JStringFacet>(loc)) {
448 
449  use_facet<JStringFacet>(loc).put(out, out, out.fill(), object);
450 
451  } else {
452 
453  out << static_cast<const string&>(object);
454  }
455 
456  return out;
457  }
458  };
459 
460 
461  /**
462  * Read string from input stream until end of line.
463  *
464  * \param in input stream
465  * \param object string
466  * \return input stream
467  */
468  inline std::istream& getline(std::istream& in, JString& object)
469  {
470  using namespace std;
471 
472  istream::sentry sentry(in, true); // do not skip white spaces
473 
474  if (sentry) {
475 
476  const locale& loc = in.getloc();
477 
478  if (has_facet<JStringFacet>(loc)) {
479 
480  use_facet<JStringFacet>(loc).getline(in, object);
481 
482  } else {
483 
484  getline(in, static_cast<string&>(object));
485  }
486  }
487 
488  return in;
489  }
490 }
491 
492 #endif
JException.hh
std::iterator
Definition: JSTDTypes.hh:18
JLANG::JString::operator<<
friend std::ostream & operator<<(std::ostream &out, const JString &object)
Write string to output stream.
Definition: JString.hh:441
JLANG::JString::trim
JString & trim()
Trim string.
Definition: JString.hh:248
JLANG::JString::JString
JString()
Default constructor.
Definition: JString.hh:40
JLANG::JString::replace
JString & replace(const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace characters.
Definition: JString.hh:189
JLANG::assign
JAssignSequence< typename JContainer_t::value_type > assign(JContainer_t &out)
Helper method to assign sequence of Comma Separated Values to output container.
Definition: JCSV.hh:129
JTOOLS::n
const int n
Definition: JPolint.hh:628
JLANG::JString::toUpper
JString & toUpper()
Convert all character to upper case.
Definition: JString.hh:291
JLANG::JString::trim
JString & trim(const char c)
Trim string.
Definition: JString.hh:263
JLANG::JString::assign
JString & assign(T &output)
Assign (part of) string to value.
Definition: JString.hh:378
loc
char * loc(char *orig)
JLANG::JString::JString
JString(const JString &buffer, const JStringFacet &facet)
Constructor.
Definition: JString.hh:94
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JString::JString
JString(const char *buffer, const std::string::size_type length)
Constructor.
Definition: JString.hh:71
JLANG::JStringFacet::get
istreambuf_iterator get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, std::string &buffer) const
Get string.
Definition: JStringFacet.hh:70
JLANG::JString::endsWith
bool endsWith(const std::string &suffix) const
Test if this string ends with the specified suffix.
Definition: JString.hh:175
JLANG::JString::startsWith
bool startsWith(const std::string &prefix) const
Test if this string starts with the specified prefix.
Definition: JString.hh:163
JLANG::JString::replace
JString & replace(const std::string &target, const std::string &replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace character sequences.
Definition: JString.hh:209
JLANG::JString
Wrapper class around STL string class.
Definition: JString.hh:28
JLANG::JString::replace
JString & replace(const std::string &target, const T &value, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace character sequence.
Definition: JString.hh:230
JLANG::JString::JString
JString(const char *format,...)
Constructor.
Definition: JString.hh:108
JLANG::JString::trim
JString trim(const std::string &target)
Trim string.
Definition: JString.hh:278
JLANG::replace
std::string replace(const std::string &input, const std::string &target, const std::string &replacement)
Replace tokens in string.
Definition: JLangToolkit.hh:124
JLANG::JString::JString
JString(const std::string &buffer)
Constructor.
Definition: JString.hh:50
JLANG::JString::valueOf
static JString valueOf(const int input)
Convert enumeration type to string.
Definition: JString.hh:322
JLANG::JString::valueOf
static JString valueOf(const T &input)
Convert value to string.
Definition: JString.hh:340
JLANG::JString::toLower
JString & toLower()
Convert all character to lower case.
Definition: JString.hh:306
JStringFacet.hh
JLANG::JString::JString
JString(const char c)
Constructor.
Definition: JString.hh:60
std
Definition: jaanetDictionary.h:36
JLANG::JString::operator>>
friend std::istream & operator>>(std::istream &in, JString &object)
Read string from input stream.
Definition: JString.hh:404
JLANG::JString::toValue
static const T & toValue(const JString &input)
Convert string to value.
Definition: JString.hh:358
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JLANG::trim
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JLANG::JStringFacet
Facet class to specify parsing of a JLANG::JString object.
Definition: JStringFacet.hh:26
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JLANG::JString::compile
JString & compile(const JStringFacet &facet)
Compile token with given facet.
Definition: JString.hh:142
JLANG::JException
General exception.
Definition: JException.hh:23
JLANG::JString::JString
JString(const T &value)
Constructor.
Definition: JString.hh:82
JLangToolkit.hh