50 static unsigned int str2uint (
const string& str)
52 istringstream IN(str);
63 istringstream IN(str);
74 istringstream IN(str);
85 istringstream IN(str);
127 static vector<string>
split(
const string& str,
const char delim,
bool splitOnce =
false)
129 vector<string> tokens;
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);
140 tokens.push_back( removeEnclosingChar(out,
' ',
' ',
true) );
155 const string& encloser =
"([{\"")
157 if(str.find_first_of(encloser) == string::npos)
return split(str, delim);
159 string::size_type open, delim_pos, next_index = 0;
160 string pair, block, tail(str);
161 vector<string> tokens;
163 while (tail.find(delim) != string::npos) {
165 delim_pos = tail.find_first_of(delim);
168 open = tail.find_first_of(encloser);
171 while (open < delim_pos) {
173 block = getBlock(tail.substr(open, string::npos), tail.at(open));
175 next_index = open + block.size();
177 pair += tail.substr(0, next_index);
179 tail = tail.substr(next_index);
181 delim_pos = tail.find_first_of(delim);
183 open = tail.find_first_of(encloser);
186 pair += tail.substr(0, delim_pos);
188 pair = removeEnclosingChar(pair,
' ',
' ',
true);
190 tokens.push_back(pair);
193 if(delim_pos != string::npos) tail = tail.substr(delim_pos + 1);
197 if(tail.size() != 0) {
198 if(removeEnclosingChar(tail,
' ',
' ',
true).size() != 0)
199 tokens.push_back( removeEnclosingChar(tail,
' ',
' ',
true) );
212 static string getBlock(
const string& str,
const char start_c,
const char end_c =
'\0',
213 bool includeEnclosing =
true)
219 istringstream IN( removeEnclosingChar(str,
' ',
' ',
true) );
238 error(
"tstring::getBlock: unknown start of block \'%c\'\n", c);
245 while (IN.get(c) && IN.good() && !IN.eof() ) {
253 }
else if(c == start_c) {
255 out += __get_block(IN, start_c, end);
262 if(!closed)
fatal(
"missing closing character '%c' in %s.\n", end, str.c_str());
270 static string __get_block(istringstream& IN,
const char start_c,
const char end_c)
278 while (IN.get(c) && IN.good() && !IN.eof() ) {
286 }
else if(c == start_c) {
288 out += __get_block(IN, start_c, end_c);
295 if(!closed)
fatal(
"missing closing character '%c' in %s.\n", end_c, IN.str().c_str());
309 for (
unsigned int i = 0; i < s.size(); i++) {
311 s.erase(s.begin() + i);
327 if( (pos = out.find_first_of(c)) != string::npos )
328 out.erase(out.begin() + pos);
341 if( (pos = out.find_last_of(c)) != string::npos )
342 out.erase(out.begin() + pos);
355 static string removeEnclosingChar (
const string& str,
const char o,
const char c,
bool allowMissing =
false)
360 first = s.find_first_of(o, 0);
363 error(
"tstring::removeEnclosingChar:: string \"%s\" not starting with \"%c\"\n.", s.c_str(), o);
369 last = s.find_last_of(c);
370 if(last != s.length()-1 || last == string::npos) {
372 error(
"tstring::removeEnclosingChar:: string \"%s\" not ending with \"%c\"\n.", s.c_str(), c);
387 while(isspace(out[0]))
388 out.erase(out.begin());
393 static string replaceChar (
const string& str,
const char target,
const char replacement)
396 size_t pos = out.find_first_of(target, 0);
398 while(pos != string::npos) {
399 out[pos] = replacement;
400 pos = out.find_first_of(target, pos);
413 while(i < str.size()) {
415 if(str[i] !=
'.' && str[i] !=
'e' && str[i] !=
'-' && str[i] !=
'+')
425 static bool isNA(
const string& str)
427 if(
"NA" == str)
return true;
429 if(
"na" == str)
return true;
431 if(
"NaN" == str)
return true;
433 if(
"NAN" == str)
return true;
435 if(
"nan" == str)
return true;
A global class to handle string conversions and operations.
Definition: tstring.h:40
static double str2dble(const string &str)
Converts a string into a double.
Definition: tstring.h:83
static string removeEnclosingChar(const string &str, const char o, const char c, bool allowMissing=false)
Removes characters enclosing a string.
Definition: tstring.h:355
static int str2int(const string &str)
Converts a string into an integer.
Definition: tstring.h:72
static unsigned long str2ulong(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:61
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:154
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 removeLeadingWhiteSpace(const string &str)
Definition: tstring.h:383
static bool isNA(const string &str)
Check whether the string is NA | NaN.
Definition: tstring.h:425
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
static bool isanumber(const string &str)
Check whether the string is a number.
Definition: tstring.h:409
static string removeChar(const string &str, const char c)
Removes a given character from a string.
Definition: tstring.h:305
tstring()
Definition: tstring.h:43
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
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:94
static string removeFirstCharOf(const string &str, const char c)
Removes the first of a character found in a string.
Definition: tstring.h:322
static unsigned int str2uint(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:50
static string removeLastCharOf(const string &str, const char c)
Removes the last of a character found in a string.
Definition: tstring.h:336
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:114
static string ulong2str(const unsigned long i)
Writes an integer value into a string.
Definition: tstring.h:104
static string replaceChar(const string &str, const char target, const char replacement)
Definition: tstring.h:393
void fatal(const char *str,...)
Definition: output.cc:99
int error(const char *str,...)
Definition: output.cc:78