Nemo  2.4.0
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
43 {}

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.

271  {
272  string out;
273  char c;
274  bool closed = false;
275 
276  out += start_c;
277 // cout << "start block : "<<start_c<<endl;
278  while (IN.get(c) && IN.good() && !IN.eof() ) {
279 
280  if(c == end_c) {
281 
282  out += c;
283  closed = true;
284  break;
285 
286  } else if(c == start_c) {//nested blocks
287 
288  out += __get_block(IN, start_c, end_c);
289 
290  } else out += c;
291 
292  } //__end_while__
293 // cout<<out<<endl;
294 
295  if(!closed) fatal("missing closing character '%c' in %s.\n", end_c, IN.str().c_str());
296 
297  // cout << "close block : "<< c << endl;
298 
299  return out;
300  }
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:270
void fatal(const char *str,...)
Definition: output.cc:99

References fatal().

◆ dble2str()

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

Writes a floating-point value into a string.

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

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.

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

References error(), and fatal().

◆ int2str()

◆ isanumber()

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

Check whether the string is a number.

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

Referenced by FHPedigreeReader::FHread(), ParamsParser::getMacroSepParamChar(), ParamsParser::rbernoul(), ParamsParser::rep(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), ParamsParser::rpoiss(), ParamsParser::runif(), ParamsParser::seq(), Param::set(), 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.

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

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.

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

◆ 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
356  {
357  string s(str);
358  size_t first, last;
359 
360  first = s.find_first_of(o, 0);
361  if(first != 0) {
362  if(!allowMissing) {
363  error("tstring::removeEnclosingChar:: string \"%s\" not starting with \"%c\"\n.", s.c_str(), o);
364  return s;
365  }
366  } else
367  s.erase(s.begin());
368 
369  last = s.find_last_of(c);
370  if(last != s.length()-1 || last == string::npos) {
371  if(!allowMissing) {
372  error("tstring::removeEnclosingChar:: string \"%s\" not ending with \"%c\"\n.", s.c_str(), c);
373  return s;
374  }
375  } else
376  s.erase(s.end()-1);
377 
378  return s;
379  }

References error().

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

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

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

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.

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

Referenced by ParamsParser::rep().

+ Here is the caller graph for this function:

◆ removeLeadingWhiteSpace()

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

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
394  {
395  string out(str);
396  size_t pos = out.find_first_of(target, 0);
397 
398  while(pos != string::npos) {
399  out[pos] = replacement;
400  pos = out.find_first_of(target, pos);
401  }
402 
403  return out;
404  }

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
128  {
129  vector<string> tokens;
130  string out(str);
131  size_t pos = 0;
132 
133  while(out.find(delim) != string::npos) {
134  pos = out.find_first_of(delim);
135  tokens.push_back( removeEnclosingChar(out.substr(0, pos), ' ', ' ', true) );
136  out = out.substr(pos+1);
137  if(splitOnce) break;
138  }
139 
140  tokens.push_back( removeEnclosingChar(out, ' ', ' ', true) );
141 
142  return tokens;
143  }

Referenced by ParamsParser::diag_matrix(), ParamsParser::matrix(), Param::parseTemporalArgument(), ParamsParser::rep(), ParamsParser::sym_matrix(), and ParamsParser::tempseq().

+ 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
155  {\"")
156  {
157  if(str.find_first_of(encloser) == string::npos) return split(str, delim);
158 
159  string::size_type open, delim_pos, next_index = 0;
160  string pair, block, tail(str);
161  vector<string> tokens;
162 
163  while (tail.find(delim) != string::npos) {
164 
165  delim_pos = tail.find_first_of(delim);
166 
167  //check if delim is within any enclosed block
168  open = tail.find_first_of(encloser);
169 
170  //read and remove blocks
171  while (open < delim_pos) {
172 
173  block = getBlock(tail.substr(open, string::npos), tail.at(open));
174 
175  next_index = open + block.size();
176 
177  pair += tail.substr(0, next_index);
178 
179  tail = tail.substr(next_index);
180 
181  delim_pos = tail.find_first_of(delim);
182 
183  open = tail.find_first_of(encloser);
184  }
185 
186  pair += tail.substr(0, delim_pos);
187 
188  pair = removeEnclosingChar(pair, ' ', ' ', true);
189 
190  tokens.push_back(pair);
191  pair.clear();
192 
193  if(delim_pos != string::npos) tail = tail.substr(delim_pos + 1);
194  else break;
195  }
196 
197  if(tail.size() != 0) { //check for trailing space characters before adding relevant characters
198  if(removeEnclosingChar(tail, ' ', ' ', true).size() != 0)
199  tokens.push_back( removeEnclosingChar(tail, ' ', ' ', true) );
200  }
201 
202 
203  return tokens;
204  }
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:127
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:212

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.

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

Referenced by ParamsParser::rbernoul(), ParamsParser::rexp(), ParamsParser::rgamma(), ParamsParser::rlognorm(), ParamsParser::rnorm(), 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.

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

Referenced by ParamsParser::rep(), 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.

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

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

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

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

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.

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

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.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR