Diana Software
Functions
QStringHandler Namespace Reference

general purpose string manipulation functions More...

Functions

std::string & SwallowSpaces (std::string &s)
 remove spaces and tabs from the beginning of s More...
 
std::string & RSwallowSpaces (std::string &s)
 remove spaces and tabs from the end of s More...
 
std::string & DoubleSwallowSpaces (std::string &s)
 remove spaces and tabs from both the beginning and the end of s More...
 
std::string RemoveFromString (const std::string &text, const char *chars)
 Remove some characters from the string. More...
 
std::string ToLower (const std::string &text)
 Convert to lower case. More...
 
std::string ToUpper (const std::string &text)
 Convert to upper case. More...
 
void Escape (std::string &text, char toBeEscaped)
 escape text by adding backslash before each instance of toBeEscaped character More...
 
void UnEscape (std::string &text, char escaped)
 unescape text by removing backslash preceding each instance of escaped character More...
 
void Replace (std::string &text, char oldChar, char newChar)
 replace oldChar with newChar in text. Escaped characters are not replaced. More...
 
size_t Split (std::string source, std::list< std::string > &splitted, char separtator)
 split source into a list of substrings separated by separator More...
 
bool StringToInt (std::string in, int &out, bool strictCheck)
 convert string to integer More...
 
bool StringToLong (std::string in, Long64_t &out, bool strictCheck)
 convert string to Long64_t More...
 
bool StringToInt (const std::string &in, int &out)
 wrapper for StringToInt(in, out, true) More...
 
bool StringToBool (std::string in, bool &out)
 convert string to bool More...
 
bool StringToDouble (std::string in, double &out, bool strictCheck)
 convert string to double More...
 
bool StringToDouble (const std::string &in, double &out)
 wrapper for StringToDouble(in, out, true) More...
 
bool StringToChar (const std::string &in, char &out)
 convert string to signed char (i.e. 8-bit int) More...
 
std::string IntToString (int val, int width=0, char pad='0')
 convert int value to string More...
 
std::string LongToString (Long64_t val, int width=0, char pad='0')
 convert long64 value to string More...
 
std::string BoolToString (bool val)
 convert bool value to string ("true" or "false") More...
 
std::string DoubleToString (double val, int nDigits=4, bool scientific=true)
 convert double value to string More...
 
std::string IntToHexString (int value)
 convert int value to string using hex representation More...
 
std::string Resize (const std::string &s, size_t len)
 resize a string to len, adding spaces if necessary More...
 
std::string TimeFormat (const char *fmt="%b %d %Y %H:%M:%S")
 Get the current time formated as a string. More...
 
std::string TimeFormat (const char *fmt, time_t t0)
 Convert an time struct to a formated time string. More...
 
template<class T >
std::string SqlString (const Diana::QBaseType< T > &baseVal, bool modeSelect)
 build string valid for sql select or insert query starting from a QBaseType variable More...
 
std::string SqlString (const Diana::QBool &boolVal, bool modeSelect)
 build string valid for sql select or insert query starting from a QBool More...
 
std::string SqlString (const std::vector< int > &vecVal, bool modeSelect, bool writeEmptyBra)
 build string valid for sql select or insert query starting from a std::vector<int> More...
 
std::string SqlString (const Diana::QString &strVal, const std::string &selectName="")
 build string valid for sql select or insert query starting from a QString More...
 
std::string RandomString (UInt_t length, Bool_t caseSensitive=true, Bool_t startLetter=false)
 Return a random string of lenght. More...
 
template<class ForwardIterator >
std::string Join (const std::string &joiner, ForwardIterator begin, ForwardIterator end)
 Join a list of things into a string. More...
 

Detailed Description

general purpose string manipulation functions

Author
Alessandro Razeto
Sergio Di Domizio

Function Documentation

◆ BoolToString()

std::string QStringHandler::BoolToString ( bool  val)

convert bool value to string ("true" or "false")

Definition at line 245 of file QStringHandler.cc.

◆ DoubleSwallowSpaces()

std::string& QStringHandler::DoubleSwallowSpaces ( std::string &  s)
inline

remove spaces and tabs from both the beginning and the end of s

Definition at line 36 of file QStringHandler.hh.

References RSwallowSpaces(), and SwallowSpaces().

Referenced by QDLLLoader::Init(), Split(), StringToBool(), StringToDouble(), StringToInt(), and StringToLong().

◆ DoubleToString()

std::string QStringHandler::DoubleToString ( double  val,
int  nDigits = 4,
bool  scientific = true 
)

convert double value to string

Parameters
nDigitstotal number of significant digits (defaults to 4)
scientificif true scientific notation

If scientific is true the number is converted into scientific format with 1 digit before the decimal point and (nDigits - 1) digits after the decimal point. The converted value also contains the exponential part, composed by the letter e followed by an optional sign and three digits.

If scientific is false the number is converted into float field format (i.e. human-readable notation, e.g. 34.49), and with a total number of nDigits significant digits.

Definition at line 251 of file QStringHandler.cc.

◆ Escape()

void QStringHandler::Escape ( std::string &  text,
char  toBeEscaped 
)

escape text by adding backslash before each instance of toBeEscaped character

Definition at line 41 of file QStringHandler.cc.

◆ IntToHexString()

std::string QStringHandler::IntToHexString ( int  value)

convert int value to string using hex representation

The returned string is prefixed with 0x

Definition at line 268 of file QStringHandler.cc.

◆ IntToString()

std::string QStringHandler::IntToString ( int  val,
int  width = 0,
char  pad = '0' 
)

convert int value to string

Definition at line 227 of file QStringHandler.cc.

Referenced by QChannelRunDataHandle::FillFromDB().

◆ Join()

template<class ForwardIterator >
std::string QStringHandler::Join ( const std::string &  joiner,
ForwardIterator  begin,
ForwardIterator  end 
)

Join a list of things into a string.

Join a list of things together with a string. Join each element between begin and end with the string in joiner. Return as a string.

Definition at line 281 of file QStringHandler.hh.

◆ LongToString()

std::string QStringHandler::LongToString ( Long64_t  val,
int  width = 0,
char  pad = '0' 
)

convert long64 value to string

Definition at line 236 of file QStringHandler.cc.

◆ RandomString()

std::string QStringHandler::RandomString ( UInt_t  length,
Bool_t  caseSensitive = true,
Bool_t  startLetter = false 
)

Return a random string of lenght.

Return a random string of a given length. In the case of case sensitive: chooses characters from 0-9a-zA-Z, in the case of case insensitive: 0-9a-z.

By default, start with any of 0-9a-zA-Z. Sometimes you want to start with letter only, so if startLetter=true, the first character will be a letter.

Note on randomness: You can make strings of arbitrary length and probability of any given string is (1./62)**length or (1./36)**length (case insensitive), which has a p~1e-9 after 5 or 6 characters. BUT the random number generator takes a UInt_t as a seed, which has a range of 4e9, but the seed only comes from TTimeStamp::GetNanoSec(), which significantly limits the seeds.

Definition at line 368 of file QStringHandler.cc.

Referenced by QFileWriter::BuildFileName().

◆ RemoveFromString()

std::string QStringHandler::RemoveFromString ( const std::string &  text,
const char *  chars 
)

Remove some characters from the string.

Definition at line 19 of file QStringHandler.cc.

◆ Replace()

void QStringHandler::Replace ( std::string &  text,
char  oldChar,
char  newChar 
)

replace oldChar with newChar in text. Escaped characters are not replaced.

Definition at line 72 of file QStringHandler.cc.

◆ Resize()

std::string QStringHandler::Resize ( const std::string &  s,
size_t  len 
)

◆ RSwallowSpaces()

std::string& QStringHandler::RSwallowSpaces ( std::string &  s)
inline

remove spaces and tabs from the end of s

Definition at line 32 of file QStringHandler.hh.

Referenced by DoubleSwallowSpaces(), and QModuleFactory::Init().

◆ Split()

size_t QStringHandler::Split ( std::string  source,
std::list< std::string > &  splitted,
char  separtator 
)

split source into a list of substrings separated by separator

Parameters
sourcestring to be splitted
splittedlist filled with splitted substrings
separatorcharacter used to split source
Returns
number of elements contained in splitted

Escaped separators (i.e. preceded by backslash) are not considered.
DoubleSwallowSpaces() is executed on each substring before adding it to splitted list (empty substring are not added).

Definition at line 89 of file QStringHandler.cc.

References DoubleSwallowSpaces().

Referenced by main().

◆ SqlString() [1/4]

template<class T >
std::string QStringHandler::SqlString ( const Diana::QBaseType< T > &  baseVal,
bool  modeSelect 
)

build string valid for sql select or insert query starting from a QBaseType variable

Parameters
modeSelectif true, a select-like string will be built, otherwise a insert-like string will be built

The insert-like string is the input value converted to string or "NULL". The select-like string is the same as insert-like string, but preceded by "=" (if the input value is defined) or "IS " (if the input value is NULL)

Definition at line 200 of file QStringHandler.hh.

◆ SqlString() [2/4]

std::string QStringHandler::SqlString ( const Diana::QBool &  boolVal,
bool  modeSelect 
)

build string valid for sql select or insert query starting from a QBool

Parameters
modeSelectif true, a select-like string will be built, otherwise a insert-like string will be built

The insert-like string is the input value converted to string or "NULL". The select-like string is the same as insert-like string, but preceded by "=" (if the input value is defined) or "IS " (if the input value is NULL)

Definition at line 303 of file QStringHandler.cc.

◆ SqlString() [3/4]

std::string QStringHandler::SqlString ( const Diana::QString &  strVal,
const std::string &  selectName = "" 
)

build string valid for sql select or insert query starting from a QString

Parameters
selectNameif empty, this method behaves as the same method for QBaseType with modeSelect = false; if not empty, this method beheves as the same method for QBaseType input and modeSelect = true, but also includes in the return string the name of the db field to be selected.

Definition at line 344 of file QStringHandler.cc.

◆ SqlString() [4/4]

std::string QStringHandler::SqlString ( const std::vector< int > &  vecVal,
bool  modeSelect,
bool  writeEmptyBra 
)

build string valid for sql select or insert query starting from a std::vector<int>

Parameters
modeSelectif true, a select-like string will be built, otherwise a insert-like string will be built

The insert-like string is the input value converted to string or "NULL" (if useEmpty is false) or "{}" (if useEmpty is true). The select-like string is the same as insert-like string, but preceded by "=" (if the input value is defined or useEmpty is true) or "IS " (if the input value is NULL)

Definition at line 318 of file QStringHandler.cc.

◆ StringToBool()

bool QStringHandler::StringToBool ( std::string  in,
bool &  out 
)

convert string to bool

Returns
true unless in is invalid (see detailed description)

Allowed values for true are: "true", "t", "TRUE", "T", "1"
Allowed values for false are: "false", "f", "FALSE", "F", "0"
Empty characters at the beginning and/or at the end of input string s are allowed.

Definition at line 165 of file QStringHandler.cc.

References DoubleSwallowSpaces().

◆ StringToChar()

bool QStringHandler::StringToChar ( const std::string &  in,
char &  out 
)

convert string to signed char (i.e. 8-bit int)

Returns
true on success

Convert in to int using StringToInt(). If converted value is negative or is bigger than 256, return false. Otherwise, cast int value to char and return true.

Definition at line 211 of file QStringHandler.cc.

References StringToInt().

◆ StringToDouble() [1/2]

bool QStringHandler::StringToDouble ( const std::string &  in,
double &  out 
)
inline

wrapper for StringToDouble(in, out, true)

Definition at line 130 of file QStringHandler.hh.

References StringToDouble().

◆ StringToDouble() [2/2]

bool QStringHandler::StringToDouble ( std::string  in,
double &  out,
bool  strictCheck 
)

convert string to double

Does the same things as StringToInt() apart for the following:

  • strtod() is used instead of strtol() ...
  • it is not possible to use input string formatted as hex ...

Definition at line 193 of file QStringHandler.cc.

References DoubleSwallowSpaces().

Referenced by StringToDouble().

◆ StringToInt() [1/2]

bool QStringHandler::StringToInt ( const std::string &  in,
int &  out 
)
inline

wrapper for StringToInt(in, out, true)

Definition at line 106 of file QStringHandler.hh.

References StringToInt().

◆ StringToInt() [2/2]

bool QStringHandler::StringToInt ( std::string  in,
int &  out,
bool  strictCheck 
)

convert string to integer

Parameters
incan be formatted as decimal or hex (must start with "0x")
strictCheckdetermines the return value if only a part of input string can be converted
Returns
true unless conversion fails (see detailed description)

This function calls DoubleSwallowSpaces() on in, therefore spaces at the beginning or at the end of input string are allowed.
It then calls strtol on input string.

  • if the conversion fails at the first character of input string, false is returned.
  • If the whole string is converted, true is returned
  • In case of partial conversion, (!strictCheck) is returned

Definition at line 128 of file QStringHandler.cc.

References DoubleSwallowSpaces().

Referenced by StringToChar(), and StringToInt().

◆ StringToLong()

bool QStringHandler::StringToLong ( std::string  in,
Long64_t &  out,
bool  strictCheck 
)

convert string to Long64_t

Behave exactly as StringToInt() but use strtoll instead of strtol for conversion.

Definition at line 146 of file QStringHandler.cc.

References DoubleSwallowSpaces().

◆ SwallowSpaces()

std::string& QStringHandler::SwallowSpaces ( std::string &  s)
inline

remove spaces and tabs from the beginning of s

Definition at line 28 of file QStringHandler.hh.

Referenced by DoubleSwallowSpaces(), and QModuleFactory::Init().

◆ TimeFormat() [1/2]

std::string QStringHandler::TimeFormat ( const char *  fmt,
time_t  t0 
)

Convert an time struct to a formated time string.

Definition at line 294 of file QStringHandler.cc.

References t0.

◆ TimeFormat() [2/2]

std::string QStringHandler::TimeFormat ( const char *  fmt = "%b %d %Y %H:%M:%S")

Get the current time formated as a string.

Definition at line 289 of file QStringHandler.cc.

◆ ToLower()

std::string QStringHandler::ToLower ( const std::string &  text)

Convert to lower case.

Definition at line 27 of file QStringHandler.cc.

◆ ToUpper()

std::string QStringHandler::ToUpper ( const std::string &  text)

Convert to upper case.

Definition at line 34 of file QStringHandler.cc.

◆ UnEscape()

void QStringHandler::UnEscape ( std::string &  text,
char  escaped 
)

unescape text by removing backslash preceding each instance of escaped character

Definition at line 55 of file QStringHandler.cc.