Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
 JString (const std::string &buffer)
 Constructor.
 
 JString (const char c)
 Constructor.
 
 JString (const char *buffer, const std::string::size_type length)
 Constructor.
 
template<class T >
 JString (const T &value)
 Constructor.
 
template<class ... Args>
 JString (const char *format, const Args &...args)
 Constructor.
 
 JString (const JString &buffer, const JStringFacet &facet)
 Constructor.
 
JStringcompile (const JStringFacet &facet)
 Compile token with given facet.
 
bool startsWith (const std::string &prefix) const
 Test if this string starts with the specified prefix.
 
bool endsWith (const std::string &suffix) const
 Test if this string ends with the specified suffix.
 
JStringreplace (const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
 Replace characters.
 
JStringreplace (const std::string &target, const std::string &replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
 Replace character sequences.
 
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.
 
JStringtrim ()
 Trim string.
 
JStringtrim (const char c)
 Trim string.
 
JString trim (const std::string &target)
 Trim string.
 
JStringtoUpper ()
 Convert all character to upper case.
 
JStringtoLower ()
 Convert all character to lower case.
 
template<class T >
JStringassign (T &output)
 Assign (part of) string to value.
 

Static Public Member Functions

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

Private Member Functions

template<class T , class ... Args>
void __set__ (const T &value, const Args &...args)
 Recursive method for formatting string.
 
void __set__ () const
 Termination method for formatting string.
 

Friends

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

Detailed Description

Wrapper class around STL string class.

Definition at line 27 of file JString.hh.

Constructor & Destructor Documentation

◆ JString() [1/7]

JLANG::JString::JString ( )
inline

Default constructor.

Definition at line 39 of file JString.hh.

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

◆ JString() [2/7]

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

Constructor.

Parameters
bufferstring

Definition at line 49 of file JString.hh.

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

◆ JString() [3/7]

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

Constructor.

Parameters
cchar

Definition at line 59 of file JString.hh.

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

◆ JString() [4/7]

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

Constructor.

Parameters
bufferstring
lengthlength

Definition at line 70 of file JString.hh.

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

◆ JString() [5/7]

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

Constructor.

Parameters
valuevalue

Definition at line 81 of file JString.hh.

81 :
82 std::string(valueOf(value))
83 {}
static JString valueOf(const int input)
Convert enumeration type to string.
Definition JString.hh:303

◆ JString() [6/7]

template<class ... Args>
JLANG::JString::JString ( const char * format,
const Args &... args )
inline

Constructor.


Each '' in the format string will be replaced by the corresponding argument.

Parameters
formatformat
argsvalues

Definition at line 94 of file JString.hh.

94 :
95 std::string(format)
96 {
97 __set__(args...);
98 }
void __set__() const
Termination method for formatting string.
Definition JString.hh:466

◆ JString() [7/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 108 of file JString.hh.

108 :
109 std::string(buffer)
110 {
111 compile(facet);
112 }
JString & compile(const JStringFacet &facet)
Compile token with given facet.
Definition JString.hh:123

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 123 of file JString.hh.

124 {
125 using namespace std;
126
127 istringstream is(*this);
128
129 ios_base::iostate state;
130
131 facet.get(is, istreambuf_iterator<char>(), is, state, *this);
132
133 return *this;
134 }

◆ 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 144 of file JString.hh.

145 {
146 return this->size() >= prefix.size() && this->substr(0, prefix.size()) == prefix;
147 }

◆ 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 156 of file JString.hh.

157 {
158 return this->size() >= suffix.size() && this->substr(this->size() - suffix.size()) == suffix;
159 }

◆ 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 170 of file JString.hh.

173 {
174 for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = find(target,i), --n) {
175 (*this)[i] = replacement;
176 }
177
178 return *this;
179 }
const int n
Definition JPolint.hh:791

◆ 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 190 of file JString.hh.

193 {
194 for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
195 replace(this->begin() + i, this->begin() + i + target.length(), replacement);
196 }
197
198 return *this;
199 }
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:170

◆ 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 211 of file JString.hh.

214 {
215 for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
216 replace(this->begin() + i, this->begin() + i + target.length(), JString(value));
217 }
218
219 return *this;
220 }
JString()
Default constructor.
Definition JString.hh:39

◆ 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 229 of file JString.hh.

230 {
231 *this = JLANG::trim(*this);
232
233 return *this;
234 }
std::string trim(const std::string &buffer)
Trim string.

◆ 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 244 of file JString.hh.

245 {
246 *this = JLANG::trim(*this, c);
247
248 return *this;
249 }

◆ 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 259 of file JString.hh.

260 {
261 *this = JLANG::trim(*this, target);
262
263 return *this;
264 }

◆ toUpper()

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

Convert all character to upper case.

Returns
this string

Definition at line 272 of file JString.hh.

273 {
274 for (iterator i = begin(); i != end(); ++i) {
275 *i = toupper(*i);
276 }
277
278 return *this;
279 }

◆ toLower()

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

Convert all character to lower case.

Returns
this string

Definition at line 287 of file JString.hh.

288 {
289 for (iterator i = begin(); i != end(); ++i) {
290 *i = tolower(*i);
291 }
292
293 return *this;
294 }

◆ valueOf() [1/2]

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

Convert enumeration type to string.

Parameters
inputvalue
Returns
string

Definition at line 303 of file JString.hh.

304 {
305 std::ostringstream os;
306
307 if (os << input)
308 return JString(os.str());
309 else
310 THROW(JException, "JString::valueOf()");
311 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

◆ 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 321 of file JString.hh.

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

◆ 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 339 of file JString.hh.

340 {
341 static T value;
342
343 std::istringstream is(input);
344
345 if (is >> value)
346 return value;
347 else
348 THROW(JException, "JString::toValue<T>()");
349 }

◆ 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 359 of file JString.hh.

360 {
361 using namespace std;
362
363 istringstream is(*this);
364
365 is >> output;
366
367 if (!is) {
368 THROW(JException, "JString::assign()");
369 }
370
371 this->assign(istreambuf_iterator<char>(is),
372 istreambuf_iterator<char>());
373
374 return *this;
375 }
JString & assign(T &output)
Assign (part of) string to value.
Definition JString.hh:359

◆ __set__() [1/2]

template<class T , class ... Args>
void JLANG::JString::__set__ ( const T & value,
const Args &... args )
inlineprivate

Recursive method for formatting string.

Parameters
valuenext value
argsremaining values

Definition at line 448 of file JString.hh.

449 {
450 using namespace std;
451
452 const size_t pos = this->find('%');
453
454 if (pos != string::npos) {
455
456 replace(pos, 1, JString::valueOf(value));
457
458 __set__(args...);
459 }
460 }

◆ __set__() [2/2]

void JLANG::JString::__set__ ( ) const
inlineprivate

Termination method for formatting string.

Definition at line 466 of file JString.hh.

467 {}

Friends And Related Symbol 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 385 of file JString.hh.

386 {
387 using namespace std;
388
389 istream::sentry sentry(in); // skips white spaces
390
391 if (sentry) {
392
393 const locale& loc = in.getloc();
394
395 if (has_facet<JStringFacet>(loc)) {
396
397 ios_base::iostate state;
398
399 use_facet<JStringFacet>(loc).get(in, istreambuf_iterator<char>(), in, state, object);
400
401 if (state != ios_base::goodbit && state != ios_base::eofbit) {
402 in.setstate(state);
403 }
404
405 } else {
406
407 in >> static_cast<string&>(object);
408 }
409 }
410
411 return in;
412 }
char * loc(char *orig)

◆ 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 422 of file JString.hh.

423 {
424 using namespace std;
425
426 const locale& loc = out.getloc();
427
428 if (has_facet<JStringFacet>(loc)) {
429
430 use_facet<JStringFacet>(loc).put(out, out, out.fill(), object);
431
432 } else {
433
434 out << static_cast<const string&>(object);
435 }
436
437 return out;
438 }

The documentation for this class was generated from the following file: