51 static unsigned int str2uint (
const string& str)
53 istringstream IN(str);
64 istringstream IN(str);
75 istringstream IN(str);
86 istringstream IN(str);
128 static vector<string>
split(
const string& str,
const char delim,
bool splitOnce =
false)
130 vector<string> tokens;
134 while(out.find(delim) != string::npos) {
135 pos = out.find_first_of(delim);
137 out = out.substr(pos+1);
156 const string& encloser =
"([{\"")
158 if(str.find_first_of(encloser) == string::npos)
return split(str, delim);
160 string::size_type open, delim_pos, next_index = 0;
161 string pair, block, tail(str);
162 vector<string> tokens;
164 while (tail.find(delim) != string::npos) {
166 delim_pos = tail.find_first_of(delim);
169 open = tail.find_first_of(encloser);
172 while (open < delim_pos) {
174 block =
getBlock(tail.substr(open, string::npos), tail.at(open));
176 next_index = open + block.size();
178 pair += tail.substr(0, next_index);
180 tail = tail.substr(next_index);
182 delim_pos = tail.find_first_of(delim);
184 open = tail.find_first_of(encloser);
187 pair += tail.substr(0, delim_pos);
191 tokens.push_back(pair);
194 if(delim_pos != string::npos) tail = tail.substr(delim_pos + 1);
209 static string getBlock(
const string& str,
const char start_c,
const char end_c =
'\0',
210 bool includeEnclosing =
true)
235 error(
"tstring::getBlock: unknown start of block \'%c\'\n", c);
242 while (IN.get(c) && IN.good() && !IN.eof() ) {
250 }
else if(c == start_c) {
259 if(!closed)
fatal(
"missing closing character '%c' in %s.\n", end, str.c_str());
267 static string __get_block(istringstream& IN,
const char start_c,
const char end_c)
275 while (IN.get(c) && IN.good() && !IN.eof() ) {
283 }
else if(c == start_c) {
292 if(!closed)
fatal(
"missing closing character '%c' in %s.\n", end_c, IN.str().c_str());
306 for (
unsigned int i = 0; i < s.size(); i++) {
308 s.erase(s.begin() + i);
324 if( (pos = out.find_first_of(c)) != string::npos )
325 out.erase(out.begin() + pos);
338 if( (pos = out.find_last_of(c)) != string::npos )
339 out.erase(out.begin() + pos);
352 static string removeEnclosingChar (
const string& str,
const char o,
const char c,
bool allowMissing =
false)
357 first = s.find_first_of(o, 0);
360 error(
"tstring::removeEnclosingChar:: string \"%s\" not starting with \"%c\"\n.", s.c_str(), o);
366 last = s.find_last_of(c);
367 if(last != s.length()-1 || last == string::npos) {
369 error(
"tstring::removeEnclosingChar:: string \"%s\" not ending with \"%c\"\n.", s.c_str(), c);
385 while(i < str.size()) {
387 if(str[i] !=
'.' && str[i] !=
'e' && str[i] !=
'-' && str[i] !=
'+')
397 static bool isNA(
const string& str)
399 if(
"NA" == str)
return true;
401 if(
"na" == str)
return true;
403 if(
"NaN" == str)
return true;
405 if(
"NAN" == str)
return true;
407 if(
"nan" == str)
return true;
A global class to handle string conversions and operations.
Definition: tstring.h:41
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 double str2dble(const string &str)
Converts a string into a double.
Definition: tstring.h:84
static string removeEnclosingChar(const string &str, const char o, const char c, bool allowMissing=false)
Removes characters enclosing a string.
Definition: tstring.h:352
static int str2int(const string &str)
Converts a string into an integer.
Definition: tstring.h:73
static unsigned long str2ulong(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:62
static vector< string > splitExcludeEnclosedDelimiters(const string &str, const char delim=',', const string &encloser="([{\"")
Splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:155
static bool isNA(const string &str)
Check whether the string is NA | NaN.
Definition: tstring.h:397
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:267
static bool isanumber(const string &str)
Check whether the string is a number.
Definition: tstring.h:381
static string removeChar(const string &str, const char c)
Removes a given character from a string.
Definition: tstring.h:302
tstring()
Definition: tstring.h:44
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:209
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:95
static string removeFirstCharOf(const string &str, const char c)
Removes the first of a character found in a string.
Definition: tstring.h:319
static unsigned int str2uint(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:51
static string removeLastCharOf(const string &str, const char c)
Removes the last of a character found in a string.
Definition: tstring.h:333
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:115
static string ulong2str(const unsigned long i)
Writes an integer value into a string.
Definition: tstring.h:105
void fatal(const char *str,...)
Definition: output.cc:96
int error(const char *str,...)
Definition: output.cc:77