Jpp
Public Member Functions | Static Public Member Functions | Friends | List of all members
JLANG::JString Class Reference

Wrapper class around STL string class. More...

#include <JString.hh>

Inheritance diagram for JLANG::JString:

Public Member Functions

 JString ()
 Default constructor. More...
 
 JString (const std::string &buffer)
 Constructor. More...
 
 JString (const char c)
 Constructor. More...
 
 JString (const char *buffer, const std::string::size_type length)
 Constructor. More...
 
template<class T >
 JString (const T &value)
 Constructor. More...
 
 JString (const JString &buffer, const JStringFacet &facet)
 Constructor. More...
 
 JString (const char *format,...)
 Constructor. More...
 
JStringcompile (const JStringFacet &facet)
 Compile token with given facet. More...
 
bool startsWith (const std::string &prefix) const
 Test if this string starts with the specified prefix. More...
 
bool endsWith (const std::string &suffix) const
 Test if this string ends with the specified suffix. More...
 
JStringreplace (const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
 Replace characters. More...
 
JStringreplace (const std::string &target, const std::string &replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
 Replace character sequences. More...
 
template<class T >
JStringreplace (const std::string &target, const T &value, const std::size_t max=std::numeric_limits< std::size_t >::max())
 Replace character sequence. More...
 
JStringtrim ()
 Trim string. More...
 
JStringtrim (const char c)
 Trim string. More...
 
JString trim (const std::string &target)
 Trim string. More...
 
JStringtoUpper ()
 Convert all character to upper case. More...
 
JStringtoLower ()
 Convert all character to lower case. More...
 
template<class T >
JStringassign (T &output)
 Assign (part of) string to value. More...
 

Static Public Member Functions

static JString valueOf (const int input)
 Convert enumeration type to string. More...
 
template<class T >
static JString valueOf (const T &input)
 Convert value to string. More...
 
template<class T >
static const T & toValue (const JString &input)
 Convert string to value. More...
 

Friends

std::istream & operator>> (std::istream &in, JString &object)
 Read string from input stream. More...
 
std::ostream & operator<< (std::ostream &out, const JString &object)
 Write string to output stream. More...
 

Detailed Description

Wrapper class around STL string class.

Definition at line 28 of file JString.hh.

Constructor & Destructor Documentation

◆ JString() [1/7]

JLANG::JString::JString ( )
inline

Default constructor.

Definition at line 40 of file JString.hh.

40  :
41  std::string()
42  {}

◆ JString() [2/7]

JLANG::JString::JString ( const std::string &  buffer)
inlineexplicit

Constructor.

Parameters
bufferstring

Definition at line 50 of file JString.hh.

50  :
51  std::string(buffer)
52  {}

◆ JString() [3/7]

JLANG::JString::JString ( const char  c)
inlineexplicit

Constructor.

Parameters
cchar

Definition at line 60 of file JString.hh.

60  :
61  std::string(1,c)
62  {}

◆ JString() [4/7]

JLANG::JString::JString ( const char *  buffer,
const std::string::size_type  length 
)
inlineexplicit

Constructor.

Parameters
bufferstring
lengthlength

Definition at line 71 of file JString.hh.

71  :
72  std::string(buffer, length)
73  {}

◆ JString() [5/7]

template<class T >
JLANG::JString::JString ( const T &  value)
inline

Constructor.

Parameters
valuevalue

Definition at line 82 of file JString.hh.

82  :
83  std::string(valueOf(value))
84  {}

◆ JString() [6/7]

JLANG::JString::JString ( const JString buffer,
const JStringFacet facet 
)
inline

Constructor.

This constructor compiles (see below) the input string.

Parameters
bufferinput string
facetfacet

Definition at line 94 of file JString.hh.

94  :
95  std::string(buffer)
96  {
97  compile(facet);
98  }

◆ JString() [7/7]

JLANG::JString::JString ( const char *  format,
  ... 
)
inline

Constructor.

Composes a string with the same text that would be printed if format was used on printf.

Parameters
formatformat

Definition at line 108 of file JString.hh.

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  }

Member Function Documentation

◆ compile()

JString& JLANG::JString::compile ( const JStringFacet facet)
inline

Compile token with given facet.

This method uses the given facet to parse the input string. The result is then compatible with the definition of a token and may be empty.

Parameters
facetfacet
Returns
this string

Definition at line 142 of file JString.hh.

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  }

◆ startsWith()

bool JLANG::JString::startsWith ( const std::string &  prefix) const
inline

Test if this string starts with the specified prefix.

Parameters
prefixprefix
Returns
true if this string starts with prefix; else false

Definition at line 163 of file JString.hh.

164  {
165  return this->size() >= prefix.size() && this->substr(0, prefix.size()) == prefix;
166  }

◆ endsWith()

bool JLANG::JString::endsWith ( const std::string &  suffix) const
inline

Test if this string ends with the specified suffix.

Parameters
suffixsuffix
Returns
true if this string ends with suffix; else false

Definition at line 175 of file JString.hh.

176  {
177  return this->size() >= suffix.size() && this->substr(this->size() - suffix.size()) == suffix;
178  }

◆ replace() [1/3]

JString& JLANG::JString::replace ( const char  target,
const char  replacement,
const std::size_t  max = std::numeric_limits<std::size_t>::max() 
)
inline

Replace characters.

Parameters
targettarget character
replacementreplacement character
maxmaximum number of replacements
Returns
this string

Definition at line 189 of file JString.hh.

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  }

◆ replace() [2/3]

JString& JLANG::JString::replace ( const std::string &  target,
const std::string &  replacement,
const std::size_t  max = std::numeric_limits<std::size_t>::max() 
)
inline

Replace character sequences.

Parameters
targettarget string
replacementreplacement string
maxmaximum number of replacements
Returns
this string

Definition at line 209 of file JString.hh.

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  }

◆ replace() [3/3]

template<class T >
JString& JLANG::JString::replace ( const std::string &  target,
const T &  value,
const std::size_t  max = std::numeric_limits<std::size_t>::max() 
)
inline

Replace character sequence.

Parameters
targettarget string
valuevalue
maxmaximum number of replacements
Returns
this string

Definition at line 230 of file JString.hh.

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  }

◆ trim() [1/3]

JString& JLANG::JString::trim ( )
inline

Trim string.


Returns the modified string, with leading and trailing white spaces omitted.

Returns
this string

Definition at line 248 of file JString.hh.

249  {
250  *this = JLANG::trim(*this);
251 
252  return *this;
253  }

◆ trim() [2/3]

JString& JLANG::JString::trim ( const char  c)
inline

Trim string.


Returns the modified string, with leading and trailing target characters omitted.

Parameters
cstrip character
Returns
this string

Definition at line 263 of file JString.hh.

264  {
265  *this = JLANG::trim(*this, c);
266 
267  return *this;
268  }

◆ trim() [3/3]

JString JLANG::JString::trim ( const std::string &  target)
inline

Trim string.


Returns the modified string, with leading and trailing target characters omitted.

Parameters
targetcharacter(s) to strip
Returns
this string

Definition at line 278 of file JString.hh.

279  {
280  *this = JLANG::trim(*this, target);
281 
282  return *this;
283  }

◆ toUpper()

JString& JLANG::JString::toUpper ( )
inline

Convert all character to upper case.

Returns
this string

Definition at line 291 of file JString.hh.

292  {
293  for (iterator i = begin(); i != end(); ++i) {
294  *i = toupper(*i);
295  }
296 
297  return *this;
298  }

◆ toLower()

JString& JLANG::JString::toLower ( )
inline

Convert all character to lower case.

Returns
this string

Definition at line 306 of file JString.hh.

307  {
308  for (iterator i = begin(); i != end(); ++i) {
309  *i = tolower(*i);
310  }
311 
312  return *this;
313  }

◆ valueOf() [1/2]

static JString JLANG::JString::valueOf ( const int  input)
inlinestatic

Convert enumeration type to string.

Parameters
inputvalue
Returns
string

Definition at line 322 of file JString.hh.

323  {
324  std::ostringstream os;
325 
326  if (os << input)
327  return JString(os.str());
328  else
329  throw JException("JString::valueOf()");
330  }

◆ valueOf() [2/2]

template<class T >
static JString JLANG::JString::valueOf ( const T &  input)
inlinestatic

Convert value to string.

Parameters
inputvalue
Returns
string

Definition at line 340 of file JString.hh.

341  {
342  std::ostringstream os;
343 
344  if (os << input)
345  return JString(os.str());
346  else
347  throw JException("JString::valueOf()");
348  }

◆ toValue()

template<class T >
static const T& JLANG::JString::toValue ( const JString input)
inlinestatic

Convert string to value.

Parameters
inputstring
Returns
value

Definition at line 358 of file JString.hh.

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  }

◆ assign()

template<class T >
JString& JLANG::JString::assign ( T &  output)
inline

Assign (part of) string to value.

Parameters
outputvalue
Returns
remaining string

Definition at line 378 of file JString.hh.

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  }

Friends And Related Function Documentation

◆ operator>>

std::istream& operator>> ( std::istream &  in,
JString object 
)
friend

Read string from input stream.

Parameters
ininput stream
objectstring
Returns
input stream

Definition at line 404 of file JString.hh.

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  }

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const JString object 
)
friend

Write string to output stream.

Parameters
outoutput stream
objectstring
Returns
output stream

Definition at line 441 of file JString.hh.

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  }

The documentation for this class was generated from the following file:
std::iterator
Definition: JSTDTypes.hh:18
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
JTOOLS::n
const int n
Definition: JPolint.hh:628
JLANG::JString::assign
JString & assign(T &output)
Assign (part of) string to value.
Definition: JString.hh:378
loc
char * loc(char *orig)
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::valueOf
static JString valueOf(const int input)
Convert enumeration type to string.
Definition: JString.hh:322
std
Definition: jaanetDictionary.h:36
JLANG::trim
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JLANG::JString::compile
JString & compile(const JStringFacet &facet)
Compile token with given facet.
Definition: JString.hh:142
JLANG::JException
General exception.
Definition: JException.hh:40