NOMAD Source  Version 4.0.0 Beta
utils.hpp File Reference

Utility functions (headers) More...

#include <list>
#include "../Util/defines.hpp"
#include "../nomad_nsbegin.hpp"
#include "../nomad_nsend.hpp"
Include dependency graph for utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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 ()
 

Detailed Description

Utility functions (headers)

Author
Sebastien Le Digabel, modified by Viviane Rochon Montplaisir
Date
March 2017
See also
utils.cpp

Definition in file utils.hpp.

Function Documentation

◆ atoi() [1/2]

bool NOMAD::atoi ( const char  c,
int &  i 
)

Convert a character into an integer.

Parameters
cThe character – IN.
iThe integer – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 200 of file utils.cpp.

201 {
202  std::string s = "-";
203  s[0] = c;
204  return NOMAD::atoi(s,i);
205 }

◆ atoi() [2/2]

bool NOMAD::atoi ( const std::string &  s,
int &  i 
)

Convert a string into an integer.

Parameters
sThe string – IN.
iThe integer – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 119 of file utils.cpp.

120 {
121  i = -1;
122  if ( s.empty() )
123  {
124  return false;
125  }
126 
127  size_t n = s.size();
128 
129  if ( s[0] == '-' )
130  {
131  if ( n > 1 && s[1] == '-' )
132  return false;
133  std::string ss = s;
134  ss.erase(ss.begin());
135  if ( NOMAD::atoi ( ss , i ) )
136  {
137  i = -i;
138  return true;
139  }
140  return false;
141  }
142 
143  std::string ts = s;
144  NOMAD::toupper(ts);
145  if ( ts == "INF" || ts == "+INF" )
146  {
147  i = NOMAD::P_INF_INT;
148  return true;
149  }
150  if ( ts == "-INF" )
151  {
152  i = NOMAD::M_INF_INT;
153  return true;
154  }
155 
156  for ( size_t k = 0 ; k < n ; ++k )
157  {
158  if ( !isdigit(s[k]) )
159  {
160  return false;
161  }
162  }
163  i = std::atoi(s.c_str());
164  return true;
165 }

◆ atost()

bool NOMAD::atost ( const std::string &  s,
size_t &  st 
)

Convert a string into a size_t.

Parameters
sThe string – IN.
stThe size_t – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 170 of file utils.cpp.

171 {
172  st = NOMAD::INF_SIZE_T;
173 
174  if ( s.empty() )
175  {
176  return false;
177  }
178 
179  std::string ts = s;
180  NOMAD::toupper(ts);
181  if ( ts == "INF" || ts == "+INF" )
182  {
183  st = NOMAD::INF_SIZE_T;
184  return true;
185  }
186 
187  int i;
188  bool success = NOMAD::atoi(s,i);
189  if ( success )
190  {
191  if ( i < 0 )
192  throw NOMAD::Exception(__FILE__, __LINE__, "Invalid value for size_t. Value must be >0");
193  st = static_cast<size_t>(i);
194  }
195 
196  return success;
197 }

◆ boolToString()

std::string NOMAD::boolToString ( bool  boolean)

Convert a bool to "true" or "false".

Definition at line 344 of file utils.cpp.

345 {
346  return (boolean) ? "true" : "false";
347 }

◆ enumStr()

std::string enumStr ( SuccessType  success)

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.

379 {
380  // These values non modifiable for now.
381  const size_t nbDigitsBeforePoint = NOMAD::NB_DIGITS_BEFORE_POINT;
382  const size_t intWidth = NOMAD::INT_DISPLAY_WIDTH;
383 
384  if (0 == prec)
385  {
386  // Integer.
387  width = intWidth;
388  }
389  else
390  {
391  // Double
392  width = nbDigitsBeforePoint + 1 + prec;
393  size_t pointPos = s.find(".");
394  spacePadding = 1 + prec; // Including one space for the decimal point
395  if (pointPos != std::string::npos)
396  {
397  // Compute padding as needed.
398  // Ex. if precision is 6 but the string number is "27.03" -> pad to "27.03 ".
399  spacePadding -= (s.size() - pointPos);
400  if (spacePadding >= width)
401  {
402  spacePadding = 0;
403  }
404  }
405  }
406 }

◆ getThreadNum()

int NOMAD::getThreadNum ( )

Definition at line 533 of file utils.cpp.

534 {
535  int threadNum = 0;
536 #ifdef _OPENMP
537  threadNum = omp_get_thread_num();
538 #endif // _OPENMP
539  return threadNum;
540 }

◆ itos() [1/2]

std::string NOMAD::itos ( const int  i)

Transform an integer into a string.

Parameters
iThe integer – IN.
Returns
The string.

Definition at line 63 of file utils.cpp.

64 {
65  std::ostringstream oss;
66  oss << i;
67  return oss.str();
68 }

◆ itos() [2/2]

std::string NOMAD::itos ( const size_t  i)

Transform a unsigned long (size_t) into a string.

Parameters
iThe unsigned long – IN.
Returns
The string.

Definition at line 73 of file utils.cpp.

74 {
75  std::ostringstream oss;
76  oss << i;
77  return oss.str();
78 }

◆ 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.

410 {
411  // NOTE: format found here is used in StatsInfo, for display of OBJ, CONS_H
412  // and H_MAX (or any Double). It is not used for values of type ArrayOfDouble
413  // or its derived classes.
414 
415  format = "";
416  tag = s;
417  const std::string formatLetters = "eEfgGdi";
418  const std::string allLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
419  bool doSeparate = false;
420 
421  if ('%' == s[0])
422  {
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)
428  {
429  cutIndex += 1;
430  }
431  if (std::string::npos != cutIndex)
432  {
433  std::string tempFormat = s.substr(0, cutIndex);
434  doSeparate = validFormat(tempFormat);
435  if (doSeparate)
436  {
437  format = tempFormat;
438  tag = s.substr(cutIndex, s.length() - cutIndex);
439  }
440  }
441  }
442 
443  return doSeparate;
444 }

◆ 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.

321 {
322  bool ret = false;
323  std::string s = string;
324  NOMAD::toupper(s);
325 
326  if ( s == "Y" || s == "YES" || s == "1" || s == "TRUE" )
327  {
328  ret = true;
329  }
330  else if ( s == "N" || s == "NO" || s == "0" || s == "FALSE" )
331  {
332  ret = false;
333  }
334  else
335  {
336  throw NOMAD::Exception(__FILE__, __LINE__, "Unrecognized string for bool: " + s);
337  }
338 
339  return ret;
340 }

◆ 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
sThe string – IN.
iThe first index iOUT.
jThe second index jOUT.
check_orderA boolean indicating if i and j are to be compared – INoptional (default = true).
Returns
A boolean equal to true if the conversion was possible.

Definition at line 240 of file utils.cpp.

244 {
245  if ( s.empty() )
246  return false;
247 
248 // For now we accept only range i-j and -j
249 // if ( s == "*" )
250 // {
251 // if ( !n )
252 // return false;
253 // i = 0;
254 // j = *n-1;
255 // return true;
256 // }
257 
258  if ( s[0] == '-' )
259  {
260 
261  size_t ns = s.size();
262  if ( ns > 1 && s[1] == '-' )
263  return false;
264 
265  std::string ss = s;
266  ss.erase ( ss.begin() );
267 
268  if ( NOMAD::stringToIndexRange ( ss , i , j , false ) )
269  {
270  i = -i;
271  return true;
272  }
273  return false;
274  }
275 
276  std::istringstream in (s);
277  std::string s1;
278 
279  getline ( in , s1 , '-' );
280 
281  if (in.fail())
282  return false;
283 
284  size_t k , n1 = s1.size();
285 
286  if ( n1 >= s.size() - 1 )
287  {
288  for ( k = 0 ; k < n1 ; ++k )
289  if (!isdigit(s1[k]))
290  return false;
291  if ( ! atost ( s1 , i ) )
292  return false;
293  if ( n1 == s.size() )
294  {
295  j = i;
296  return true;
297  }
298  return false;
299  }
300 
301  std::string s2;
302  getline (in, s2);
303 
304  if (in.fail())
305  return false;
306 
307  size_t n2 = s2.size();
308  for ( k = 0 ; k < n2 ; ++k )
309  if ( !isdigit(s2[k]) )
310  return false;
311 
312  if ( ! atost ( s1, i ) || ! atost ( s2 , j ) )
313  return false;
314 
315  return !check_order || i <= j;
316 }

◆ toupper() [1/2]

void NOMAD::toupper ( std::list< std::string > &  ls)

Put a list of strings into upper cases.

Parameters
lsThe list of strings – IN/OUT.

Definition at line 106 of file utils.cpp.

107 {
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 )
111  {
112  NOMAD::toupper(*it);
113  }
114 }

◆ toupper() [2/2]

void NOMAD::toupper ( std::string &  s)

Put a string into upper cases.

Parameters
sThe string – IN/OUT.

Definition at line 84 of file utils.cpp.

85 {
86  // Warning: strings like "é" do not get converted.
87  // Even when using locale.
88  // This is a widechar issue, not investigating in it right now.
89  // There is no easy way to show a clear warning, so do not do
90  // anything about it - just file it is as a known issue.
91 
92 // So 98
93 // size_t ns = s.size();
94 // for ( size_t i = 0 ; i < ns ; ++i )
95 // {
96 // s[i] = std::toupper(s[i]);
97 // }
98 
99 // modern obfuscated C++
100  for_each(s.begin(), s.end(), [](char& in){ in = std::toupper(in); });
101 }

◆ validFormat()

bool NOMAD::validFormat ( std::string &  s)

Definition at line 464 of file utils.cpp.

465 {
466  const std::string formatLetters = "eEfgGdi";
467  bool isValid = true;
468 
469  // Verify size
470  if (s.length() < 2)
471  {
472  isValid = false;
473  }
474 
475  else
476  {
477  // Append f if last char is a digit.
478  if (std::isdigit(s[s.length()-1]))
479  {
480  s = s + "f";
481  }
482  size_t indexFormatLetter = s.find_first_of(formatLetters, 1);
483 
484  // Verify we do have a format letter
485  if (std::string::npos == indexFormatLetter)
486  {
487  isValid = false;
488  }
489  // Verify first character is '%'
490  else if (s[0] != '%')
491  {
492  isValid = false;
493  }
494  // Verify last character is a formatting character
495  else if (indexFormatLetter < s.length()-1)
496  {
497  isValid = false;
498  }
499  // Verify in between we have a decimal number
500  else
501  {
502  bool pointEncountered = false;
503  for (size_t i = 1; i < indexFormatLetter; i++)
504  {
505  if (std::isdigit(s[i]))
506  {
507  // ok
508  }
509  else if (s[i] == '.')
510  {
511  if (!pointEncountered)
512  {
513  pointEncountered = true;
514  }
515  else
516  {
517  // more than one point
518  isValid = false;
519  }
520  }
521  else
522  {
523  isValid = false;
524  }
525  }
526  }
527  }
528 
529  return isValid;
530 }
M_INF_INT
const int M_INF_INT
minus infinity for int
Definition: defines.hpp:128
validFormat
bool validFormat(std::string &s)
Definition: utils.cpp:464
atoi
bool atoi(const std::string &s, int &i)
Convert a string into an integer.
Definition: utils.cpp:119
toupper
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:84
stringToIndexRange
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.
Definition: utils.cpp:240
INT_DISPLAY_WIDTH
const int INT_DISPLAY_WIDTH
Definition: defines.hpp:139
atost
bool atost(const std::string &s, size_t &st)
Convert a string into a size_t.
Definition: utils.cpp:170
INF_SIZE_T
const size_t INF_SIZE_T
The infinity for size_t.
Definition: defines.hpp:129
NB_DIGITS_BEFORE_POINT
const int NB_DIGITS_BEFORE_POINT
Definition: defines.hpp:138
P_INF_INT
const int P_INF_INT
plus infinity for int
Definition: defines.hpp:127