Utility functions (headers)
More...
Go to the source code of this file.
|
std::string | itos (const int i) |
| Transform an integer into a string. More...
|
|
std::string | itos (const size_t i) |
| Transform a unsigned long (size_t) into a string. More...
|
|
void | toupper (std::string &s) |
| Put a string into upper cases. More...
|
|
void | toupper (std::list< std::string > &ls) |
| Put a list of strings into upper cases. More...
|
|
bool | atoi (const std::string &s, int &i) |
| Convert a string into an integer. More...
|
|
bool | atoi (const char c, int &i) |
| Convert a character into an integer. More...
|
|
bool | atost (const std::string &s, size_t &st) |
| Convert a string into a size_t. More...
|
|
bool | stringToIndexRange (const std::string &s, size_t &i, size_t &j, bool check_order=true) |
| Convert a string with format "i-j" into two size_t i and j. More...
|
|
std::string | enumStr (SuccessType success) |
| Convert a success type to a string. More...
|
|
bool | stringToBool (const std::string &string) |
| Convert a string in {"YES","NO","Y","N","0","1","TRUE","FALSE"} to a boolean. More...
|
|
std::string | boolToString (bool boolean) |
| Convert a bool to "true" or "false". More...
|
|
std::size_t | nbDecimals (const std::string &s) |
| Return the number of decimals of a string representing a double. More...
|
|
void | getFormat (const std::string &s, const size_t prec, size_t &width, size_t &spacePadding) |
|
bool | separateFormat (const std::string &s, std::string &format, std::string &tag) |
|
bool | validFormat (std::string &s) |
|
int | getThreadNum () |
|
Utility functions (headers)
- Author
- Sebastien Le Digabel, modified by Viviane Rochon Montplaisir
- Date
- March 2017
- See also
- utils.cpp
Definition in file utils.hpp.
◆ atoi() [1/2]
bool NOMAD::atoi |
( |
const char |
c, |
|
|
int & |
i |
|
) |
| |
Convert a character into an integer.
- Parameters
-
c | The character – IN. |
i | The integer – OUT. |
- Returns
- A boolean equal to
true
if the conversion was possible.
Definition at line 200 of file utils.cpp.
◆ atoi() [2/2]
bool NOMAD::atoi |
( |
const std::string & |
s, |
|
|
int & |
i |
|
) |
| |
Convert a string into an integer.
- Parameters
-
s | The string – IN. |
i | The integer – OUT. |
- Returns
- A boolean equal to
true
if the conversion was possible.
Definition at line 119 of file utils.cpp.
131 if ( n > 1 && s[1] ==
'-' )
134 ss.erase(ss.begin());
145 if ( ts ==
"INF" || ts ==
"+INF" )
156 for (
size_t k = 0 ; k < n ; ++k )
158 if ( !isdigit(s[k]) )
◆ atost()
bool NOMAD::atost |
( |
const std::string & |
s, |
|
|
size_t & |
st |
|
) |
| |
Convert a string into a size_t.
- Parameters
-
s | The string – IN. |
st | The size_t – OUT. |
- Returns
- A boolean equal to
true
if the conversion was possible.
Definition at line 170 of file utils.cpp.
181 if ( ts ==
"INF" || ts ==
"+INF" )
192 throw NOMAD::Exception(__FILE__, __LINE__,
"Invalid value for size_t. Value must be >0");
193 st =
static_cast<size_t>(i);
◆ boolToString()
std::string NOMAD::boolToString |
( |
bool |
boolean | ) |
|
Convert a bool to "true" or "false".
Definition at line 344 of file utils.cpp.
346 return (
boolean) ?
"true" :
"false";
◆ enumStr()
Convert a success type to a string.
◆ getFormat()
void NOMAD::getFormat |
( |
const std::string & |
s, |
|
|
const size_t |
prec, |
|
|
size_t & |
width, |
|
|
size_t & |
spacePadding |
|
) |
| |
Given a string s with precision prec after the decimal point, return the suggested width and the string padding needed for alignment.
Definition at line 378 of file utils.cpp.
392 width = nbDigitsBeforePoint + 1 + prec;
393 size_t pointPos = s.find(
".");
394 spacePadding = 1 + prec;
395 if (pointPos != std::string::npos)
399 spacePadding -= (s.size() - pointPos);
400 if (spacePadding >= width)
◆ getThreadNum()
int NOMAD::getThreadNum |
( |
| ) |
|
Definition at line 533 of file utils.cpp.
537 threadNum = omp_get_thread_num();
◆ itos() [1/2]
std::string NOMAD::itos |
( |
const int |
i | ) |
|
Transform an integer into a string.
- Parameters
-
- Returns
- The string.
Definition at line 63 of file utils.cpp.
65 std::ostringstream oss;
◆ itos() [2/2]
std::string NOMAD::itos |
( |
const size_t |
i | ) |
|
Transform a unsigned long (size_t) into a string.
- Parameters
-
- Returns
- The string.
Definition at line 73 of file utils.cpp.
75 std::ostringstream oss;
◆ nbDecimals()
std::size_t nbDecimals |
( |
const std::string & |
s | ) |
|
Return the number of decimals of a string representing a double.
◆ separateFormat()
bool NOMAD::separateFormat |
( |
const std::string & |
s, |
|
|
std::string & |
format, |
|
|
std::string & |
tag |
|
) |
| |
Given a string starting with '', separate the actual formatting from the tag. Ex. "%5.2fOBJ" -> "%5.2f", "OBJ" Ex. "%dBBO" -> "%d", "BBO" Ex. "%12BBE" -> "%12f", "BBE" "TIME" returns false. "%4.2" returns false.
- Returns
true
if a valid format was found, false
otherwise.
Definition at line 409 of file utils.cpp.
417 const std::string formatLetters =
"eEfgGdi";
418 const std::string allLetters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
419 bool doSeparate =
false;
423 std::size_t firstLetter = s.find_first_of(allLetters, 1);
424 std::size_t firstFormatLetter = s.find_first_of(formatLetters, 1);
425 std::size_t cutIndex = firstLetter;
426 if (std::string::npos != firstFormatLetter
427 && firstLetter == firstFormatLetter)
431 if (std::string::npos != cutIndex)
433 std::string tempFormat = s.substr(0, cutIndex);
438 tag = s.substr(cutIndex, s.length() - cutIndex);
◆ stringToBool()
bool NOMAD::stringToBool |
( |
const std::string & |
string | ) |
|
Convert a string in {"YES","NO","Y","N","0","1","TRUE","FALSE"} to a boolean.
Definition at line 320 of file utils.cpp.
323 std::string s = string;
326 if ( s ==
"Y" || s ==
"YES" || s ==
"1" || s ==
"TRUE" )
330 else if ( s ==
"N" || s ==
"NO" || s ==
"0" || s ==
"FALSE" )
336 throw NOMAD::Exception(__FILE__, __LINE__,
"Unrecognized string for bool: " + s);
◆ stringToIndexRange()
bool NOMAD::stringToIndexRange |
( |
const std::string & |
s, |
|
|
size_t & |
i, |
|
|
size_t & |
j, |
|
|
bool |
check_order = true |
|
) |
| |
Convert a string with format "i-j" into two size_t i and j.
- Parameters
-
s | The string – IN. |
i | The first index i – OUT. |
j | The second index j – OUT. |
check_order | A boolean indicating if i and j are to be compared – IN – optional (default = true ). |
- Returns
- A boolean equal to
true
if the conversion was possible.
Definition at line 240 of file utils.cpp.
261 size_t ns = s.size();
262 if ( ns > 1 && s[1] ==
'-' )
266 ss.erase ( ss.begin() );
276 std::istringstream in (s);
279 getline ( in , s1 ,
'-' );
284 size_t k , n1 = s1.size();
286 if ( n1 >= s.size() - 1 )
288 for ( k = 0 ; k < n1 ; ++k )
291 if ( !
atost ( s1 , i ) )
293 if ( n1 == s.size() )
307 size_t n2 = s2.size();
308 for ( k = 0 ; k < n2 ; ++k )
309 if ( !isdigit(s2[k]) )
312 if ( !
atost ( s1, i ) || !
atost ( s2 , j ) )
315 return !check_order || i <= j;
◆ toupper() [1/2]
void NOMAD::toupper |
( |
std::list< std::string > & |
ls | ) |
|
Put a list of strings into upper cases.
- Parameters
-
ls | The list of strings – IN/OUT. |
Definition at line 106 of file utils.cpp.
108 std::list<std::string>::iterator it;
109 std::list<std::string>::const_iterator end = ls.end();
110 for ( it = ls.begin() ; it != end ; ++it )
◆ toupper() [2/2]
void NOMAD::toupper |
( |
std::string & |
s | ) |
|
Put a string into upper cases.
- Parameters
-
Definition at line 84 of file utils.cpp.
100 for_each(s.begin(), s.end(), [](
char& in){ in = std::toupper(in); });
◆ validFormat()
bool NOMAD::validFormat |
( |
std::string & |
s | ) |
|
Definition at line 464 of file utils.cpp.
466 const std::string formatLetters =
"eEfgGdi";
478 if (std::isdigit(s[s.length()-1]))
482 size_t indexFormatLetter = s.find_first_of(formatLetters, 1);
485 if (std::string::npos == indexFormatLetter)
490 else if (s[0] !=
'%')
495 else if (indexFormatLetter < s.length()-1)
502 bool pointEncountered =
false;
503 for (
size_t i = 1; i < indexFormatLetter; i++)
505 if (std::isdigit(s[i]))
509 else if (s[i] ==
'.')
511 if (!pointEncountered)
513 pointEncountered =
true;