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

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.

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

References fatal().

◆ dble2str()

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

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

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

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

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

◆ isNA()

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

Check whether the string is NA | NaN.

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

Referenced by FHPedigreeReader::FHread().

◆ removeChar()

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

Removes a given character from a string.

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

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

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

◆ removeFirstCharOf()

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

Removes the first of a character found in a string.

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

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

◆ removeLastCharOf()

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

Removes the last of a character found in a string.

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

Referenced by ParamsParser::rep().

◆ removeLeadingWhiteSpace()

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

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

◆ replaceChar()

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

Referenced by Param::parseArgument().

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

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

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

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

◆ str2dble()

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

Converts a string into a double.

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

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

◆ str2int()

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

Converts a string into an integer.

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

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

◆ str2uint()

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

Converts a string into an unsigned integer.

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

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

◆ str2ulong()

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

Converts a string into an unsigned integer.

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

Referenced by FHPedigreeReader::FHread().

◆ ulong2str()

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

Writes an integer value into a string.

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

Referenced by SimRunner::run().


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

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR