Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
tstring Class Reference

A global class to handle string conversions and operations. More...

#include <tstring.h>

Static Public Member Functions

static unsigned int str2uint (const string &str)
 Converts a string into an unsigned integer. More...
 
static unsigned long str2ulong (const string &str)
 Converts a string into an unsigned integer. More...
 
static int str2int (const string &str)
 Converts a string into an integer. More...
 
static double str2dble (const string &str)
 Converts a string into a double. More...
 
static string int2str (const int i)
 Writes an integer value into a string. More...
 
static string ulong2str (const unsigned long i)
 Writes an integer value into a string. More...
 
static string dble2str (const double d)
 Writes a floating-point value into a string. More...
 
static vector< string > split (const string &str, const char delim, bool splitOnce=false)
 splits a string into substrings (tokens) delimited by a single character. More...
 
static vector< string > splitExcludeEnclosedDelimiters (const string &str, const char delim=',', const string &encloser="([{\"")
 Splits a string into substrings (tokens) delimited by a single character. More...
 
static string getBlock (const string &str, const char start_c, const char end_c='\0', bool includeEnclosing=true)
 Reads a substring delimited by two enclosing character from a string. More...
 
static string __get_block (istringstream &IN, const char start_c, const char end_c)
 Internal function used by getBlock to recursively read nested blocks. More...
 
static string removeChar (const string &str, const char c)
 Removes a given character from a string. More...
 
static string removeFirstCharOf (const string &str, const char c)
 Removes the first of a character found in a string. More...
 
static string removeLastCharOf (const string &str, const char c)
 Removes the last of a character found in a string. More...
 
static string removeEnclosingChar (const string &str, const char o, const char c, bool allowMissing=false)
 Removes characters enclosing a string. More...
 
static string removeLeadingWhiteSpace (const string &str)
 
static string replaceChar (const string &str, const char target, const char replacement)
 
static bool isanumber (const string &str)
 Check whether the string is a number. More...
 
static bool isNA (const string &str)
 Check whether the string is NA | NaN. More...
 

Private Member Functions

 tstring ()
 

Detailed Description

A global class to handle string conversions and operations.

Allows to parse the parameters argument strings into various tokens and to remove encolsing characters from a string.

Constructor & Destructor Documentation

◆ tstring()

tstring::tstring ( )
inlineprivate
42 {}

Member Function Documentation

◆ __get_block()

static string tstring::__get_block ( istringstream &  IN,
const char  start_c,
const char  end_c 
)
inlinestatic

Internal function used by getBlock to recursively read nested blocks.

270  {
271  string out;
272  char c;
273  bool closed = false;
274 
275  out += start_c;
276 // cout << "start block : "<<start_c<<endl;
277  while (IN.get(c) && IN.good() && !IN.eof() ) {
278 
279  if(c == end_c) {
280 
281  out += c;
282  closed = true;
283  break;
284 
285  } else if(c == start_c) {//nested blocks
286 
287  out += __get_block(IN, start_c, end_c);
288 
289  } else out += c;
290 
291  } //__end_while__
292 // cout<<out<<endl;
293 
294  if(!closed) fatal("missing closing character '%c' in %s.\n", end_c, IN.str().c_str());
295 
296  // cout << "close block : "<< c << endl;
297 
298  return out;
299  }
static string __get_block(istringstream &IN, const char start_c, const char end_c)
Internal function used by getBlock to recursively read nested blocks.
Definition: tstring.h:269
void fatal(const char *str,...)
Definition: output.cc:98

References fatal().

◆ dble2str()

static string tstring::dble2str ( const double  d)
inlinestatic

Writes a floating-point value into a string.

114  {
115  ostringstream OUT;
116  OUT << d;
117  return OUT.str();
118  }

Referenced by TTQFreqExtractor::FHwrite(), TTProtoWithMap::recordRandomMap(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::rpoiss(), ParamsParser::runif(), ParamsParser::seq(), and TTProtoWithMap::setRecombinationMapRandom().

+ Here is the caller graph for this function:

◆ getBlock()

static string tstring::getBlock ( const string &  str,
const char  start_c,
const char  end_c = '\0',
bool  includeEnclosing = true 
)
inlinestatic

Reads a substring delimited by two enclosing character from a string.

May or may not retain the enclosing characters.

213  {
214  string out;
215  char c, end = 0;
216  bool closed = false;
217 
218  istringstream IN( removeEnclosingChar(str, ' ', ' ', true) );
219 
220  IN.get(c);
221 
222  if(end_c == 0) {
223  switch (c) {
224  case '(':
225  end = ')';
226  break;
227  case '{':
228  end = '}';
229  break;
230  case '[':
231  end = ']';
232  break;
233  case '"':
234  end = '"';
235  break;
236  default:
237  error("tstring::getBlock: unknown start of block \'%c\'\n", c);
238  break;
239  }
240  } else end = end_c;
241 
242  out += c;
243 
244  while (IN.get(c) && IN.good() && !IN.eof() ) {
245 
246  if(c == end) {
247 
248  out += c;
249  closed = true;
250  break;
251 
252  } else if(c == start_c) {//nested blocks
253 
254  out += __get_block(IN, start_c, end);
255 
256  } else out += c;
257 
258  } //__end_while__
259 
260 
261  if(!closed) fatal("missing closing character '%c' in %s.\n", end, str.c_str());
262 
263  return out;
264  }
static string removeEnclosingChar(const string &str, const char o, const char c, bool allowMissing=false)
Removes characters enclosing a string.
Definition: tstring.h:354
int error(const char *str,...)
Definition: output.cc:77

References error(), and fatal().

◆ int2str()

◆ isanumber()

static bool tstring::isanumber ( const string &  str)
inlinestatic

Check whether the string is a number.

409  {
410  unsigned int i = 0;
411 
412  while(i < str.size()) {
413  if(!isdigit(str[i]))
414  if(str[i] != '.' && str[i] != 'e' && str[i] != '-' && str[i] != '+')
415  return false;
416  i++;
417  }
418 
419  return true;
420  }

Referenced by ParamsParser::ceiling(), FHPedigreeReader::FHread(), ParamsParser::floor(), ParamsParser::getMacroSepParamChar(), ParamsParser::rbernoul(), ParamsParser::rep(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::round(), ParamsParser::rpoiss(), ParamsParser::runif(), ParamsParser::sample(), ParamsParser::seq(), Param::set(), ParamsParser::sort(), and ParamsParser::sym_matrix().

+ Here is the caller graph for this function:

◆ isNA()

static bool tstring::isNA ( const string &  str)
inlinestatic

Check whether the string is NA | NaN.

425  {
426  if("NA" == str) return true;
427 
428  if("na" == str) return true;
429 
430  if("NaN" == str) return true;
431 
432  if("NAN" == str) return true;
433 
434  if("nan" == str) return true;
435 
436  return false;
437  }

Referenced by FHPedigreeReader::FHread().

+ Here is the caller graph for this function:

◆ removeChar()

static string tstring::removeChar ( const string &  str,
const char  c 
)
inlinestatic

Removes a given character from a string.

305  {
306  string s(str);
307 
308  for (unsigned int i = 0; i < s.size(); i++) {
309  if (s[i] == c) {
310  s.erase(s.begin() + i);
311  i--;
312  }
313  }
314 
315  return s;
316  }

◆ removeEnclosingChar()

static string tstring::removeEnclosingChar ( const string &  str,
const char  o,
const char  c,
bool  allowMissing = false 
)
inlinestatic

Removes characters enclosing a string.

Can be used to remove parenthesis/brackets or trailing whitespace characters.

Parameters
strthe string
othe opening (front) character
cthe closing (back) character
allowMissingif trus, allows to ignore a missing enclosing character
355  {
356  string s(str);
357  size_t first, last;
358 
359  first = s.find_first_of(o, 0);
360  if(first != 0) {
361  if(!allowMissing) {
362  error("tstring::removeEnclosingChar:: string \"%s\" not starting with \"%c\"\n.", s.c_str(), o);
363  return s;
364  }
365  } else
366  s.erase(s.begin());
367 
368  last = s.find_last_of(c);
369  if(last != s.length()-1 || last == string::npos) {
370  if(!allowMissing) {
371  error("tstring::removeEnclosingChar:: string \"%s\" not ending with \"%c\"\n.", s.c_str(), c);
372  return s;
373  }
374  } else
375  s.erase(s.end()-1);
376 
377  return s;
378  }

References error().

Referenced by ParamsParser::concat(), ParamsParser::getMacroSepParamChar(), Param::parseArgument(), ParamsParser::rep(), and LCE_BreedAssortativeMating::setParameters().

+ Here is the caller graph for this function:

◆ removeFirstCharOf()

static string tstring::removeFirstCharOf ( const string &  str,
const char  c 
)
inlinestatic

Removes the first of a character found in a string.

322  {
323  string out(str);
324  size_t pos;
325 
326  if( (pos = out.find_first_of(c)) != string::npos )
327  out.erase(out.begin() + pos);
328 
329  return out;
330  }

Referenced by Param::getArgumentFromFile(), and Param::parseTemporalArgument().

+ Here is the caller graph for this function:

◆ removeLastCharOf()

static string tstring::removeLastCharOf ( const string &  str,
const char  c 
)
inlinestatic

Removes the last of a character found in a string.

336  {
337  string out(str);
338  size_t pos;
339 
340  if( (pos = out.find_last_of(c)) != string::npos )
341  out.erase(out.begin() + pos);
342 
343  return out;
344  }

Referenced by ParamsParser::rep().

+ Here is the caller graph for this function:

◆ removeLeadingWhiteSpace()

static string tstring::removeLeadingWhiteSpace ( const string &  str)
inlinestatic
383  {
384  string out(str);
385 
386  while(isspace(out[0]))
387  out.erase(out.begin());
388 
389  return out;
390  }

Referenced by Param::parseArgument(), and Param::parseTemporalArgument().

+ Here is the caller graph for this function:

◆ replaceChar()

static string tstring::replaceChar ( const string &  str,
const char  target,
const char  replacement 
)
inlinestatic
393  {
394  string out(str);
395  size_t pos = out.find_first_of(target, 0);
396 
397  while(pos != string::npos) {
398  out[pos] = replacement;
399  pos = out.find_first_of(target, pos);
400  }
401 
402  return out;
403  }

Referenced by ParamManager::build_records(), and Param::parseArgument().

+ Here is the caller graph for this function:

◆ split()

static vector<string> tstring::split ( const string &  str,
const char  delim,
bool  splitOnce = false 
)
inlinestatic

splits a string into substrings (tokens) delimited by a single character.

Parameters
strthe input string
delimthe token delimiter
splitOnceif true, return the first token only
127  {
128  vector<string> tokens;
129  string out(str);
130  size_t pos = 0;
131 
132  while(out.find(delim) != string::npos) {
133  pos = out.find_first_of(delim);
134  tokens.push_back( removeEnclosingChar(out.substr(0, pos), ' ', ' ', true) );
135  out = out.substr(pos+1);
136  if(splitOnce) break;
137  }
138 
139  tokens.push_back( removeEnclosingChar(out, ' ', ' ', true) );
140 
141  return tokens;
142  }

Referenced by Param::parseTemporalArgument(), and ParamsParser::rep().

+ Here is the caller graph for this function:

◆ splitExcludeEnclosedDelimiters()

static vector<string> tstring::splitExcludeEnclosedDelimiters ( const string &  str,
const char  delim = ',',
const string &  encloser = "([{\"" 
)
inlinestatic

Splits a string into substrings (tokens) delimited by a single character.

The tokens may contain the delimiter character if it is enclosed. For instance: ‘(@g0 {{0,1,0,10}}, @g10 {{1,0,2,2}})’ --> [0] = ‘@g0 {{0,1,0,10}}’ and [1] = ‘@g10 {{1,0,2,2}}’

Parameters
strthe input string
delimthe token delimiter, is a comma by default
encloserthe character that starts an enclosed string
154  {\"")
155  {
156  if(str.find_first_of(encloser) == string::npos) return split(str, delim);
157 
158  string::size_type open, delim_pos, next_index = 0;
159  string pair, block, tail(str);
160  vector<string> tokens;
161 
162  while (tail.find(delim) != string::npos) {
163 
164  delim_pos = tail.find_first_of(delim);
165 
166  //check if delim is within any enclosed block
167  open = tail.find_first_of(encloser);
168 
169  //read and remove blocks
170  while (open < delim_pos) {
171 
172  block = getBlock(tail.substr(open, string::npos), tail.at(open));
173 
174  next_index = open + block.size();
175 
176  pair += tail.substr(0, next_index);
177 
178  tail = tail.substr(next_index);
179 
180  delim_pos = tail.find_first_of(delim);
181 
182  open = tail.find_first_of(encloser);
183  }
184 
185  pair += tail.substr(0, delim_pos);
186 
187  pair = removeEnclosingChar(pair, ' ', ' ', true);
188 
189  tokens.push_back(pair);
190  pair.clear();
191 
192  if(delim_pos != string::npos) tail = tail.substr(delim_pos + 1);
193  else break;
194  }
195 
196  if(tail.size() != 0) { //check for trailing space characters before adding relevant characters
197  if(removeEnclosingChar(tail, ' ', ' ', true).size() != 0)
198  tokens.push_back( removeEnclosingChar(tail, ' ', ' ', true) );
199  }
200 
201 
202  return tokens;
203  }
static vector< string > split(const string &str, const char delim, bool splitOnce=false)
splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:126
static string getBlock(const string &str, const char start_c, const char end_c='\0', bool includeEnclosing=true)
Reads a substring delimited by two enclosing character from a string.
Definition: tstring.h:211

Referenced by ParamManager::build_records(), ParamsParser::getMacroArgs(), ParamsParser::getMacroParamValue(), ParamsParser::getMacroSepParamChar(), Param::parseArgument(), ParamsParser::rbernoul(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::rpoiss(), and ParamsParser::runif().

+ Here is the caller graph for this function:

◆ str2dble()

static double tstring::str2dble ( const string &  str)
inlinestatic

Converts a string into a double.

83  {
84  istringstream IN(str);
85  double d;
86  IN >> d;
87  return d;
88  }

Referenced by ParamsParser::ceiling(), ParamsParser::floor(), ParamsParser::rbernoul(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::round(), ParamsParser::rpoiss(), ParamsParser::runif(), ParamsParser::seq(), Param::set(), and ParamsParser::sym_matrix().

+ Here is the caller graph for this function:

◆ str2int()

static int tstring::str2int ( const string &  str)
inlinestatic

Converts a string into an integer.

72  {
73  istringstream IN(str);
74  int i;
75  IN >> i;
76  return i;
77  }

Referenced by ParamsParser::rep(), ParamsParser::round(), Param::set(), LCE_StatServiceNotifier::setOccurence(), LCE_Cross::setParameters(), and ParamsParser::sym_matrix().

+ Here is the caller graph for this function:

◆ str2uint()

static unsigned int tstring::str2uint ( const string &  str)
inlinestatic

Converts a string into an unsigned integer.

50  {
51  istringstream IN(str);
52  unsigned int i;
53  IN >> i;
54  return i;
55  }

Referenced by ParamsParser::diag_matrix(), ParamsParser::matrix(), Param::parseTemporalArgument(), ParamsParser::rbernoul(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::rpoiss(), ParamsParser::runif(), and ParamsParser::sample().

+ Here is the caller graph for this function:

◆ str2ulong()

static unsigned long tstring::str2ulong ( const string &  str)
inlinestatic

Converts a string into an unsigned integer.

61  {
62  istringstream IN(str);
63  unsigned long i;
64  IN >> i;
65  return i;
66  }

Referenced by FHPedigreeReader::FHread().

+ Here is the caller graph for this function:

◆ ulong2str()

static string tstring::ulong2str ( const unsigned long  i)
inlinestatic

Writes an integer value into a string.

104  {
105  ostringstream OUT;
106  OUT << i;
107  return OUT.str();
108  }

Referenced by SimRunner::run().

+ Here is the caller graph for this function:

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

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR