44 #ifndef GNSSTK_STRINGUTILS_HPP
45 #define GNSSTK_STRINGUTILS_HPP
61 #pragma comment(lib, "regex.lib")
159 const std::string&
data,
176 const std::string&
data,
177 const std::string& tag,
192 const std::string& aString,
193 std::string::size_type num = std::string::npos);
206 const std::string& aString,
207 std::string::size_type num = std::string::npos)
208 { std::string t(s);
stripLeading(t, aString, num);
return t; }
222 std::string::size_type num = std::string::npos)
237 std::string::size_type num = std::string::npos)
238 { std::string t(s);
stripLeading(t, std::string(pString), num);
return t; }
251 const char aCharacter,
252 std::string::size_type num = std::string::npos)
253 {
return stripLeading(s, std::string(1,aCharacter), num); }
266 const char aCharacter,
267 std::string::size_type num = std::string::npos)
268 { std::string t(s);
stripLeading(t, std::string(1,aCharacter), num);
return t; }
280 std::string::size_type num = std::string::npos)
293 std::string::size_type num = std::string::npos)
294 { std::string t(s);
stripLeading(t,std::string(1,
' '),num);
return t; }
307 const std::string& aString,
308 std::string::size_type num = std::string::npos);
321 const std::string& aString,
322 std::string::size_type num = std::string::npos)
323 { std::string t(s);
stripTrailing(t, aString, num);
return t;}
337 std::string::size_type num = std::string::npos)
352 std::string::size_type num = std::string::npos)
353 { std::string t(s);
stripTrailing(t, std::string(pString), num);
return t; }
366 const char aCharacter,
367 std::string::size_type num = std::string::npos)
381 const char aCharacter,
382 std::string::size_type num = std::string::npos)
383 { std::string t(s);
stripTrailing(t, std::string(1,aCharacter), num);
return t; }
395 std::string::size_type num = std::string::npos)
408 std::string::size_type num = std::string::npos)
409 { std::string t(s);
stripTrailing(t, std::string(1,
' '), num);
return t;}
421 inline std::string&
strip(std::string& s,
422 const std::string& aString,
423 std::string::size_type num = std::string::npos);
436 inline std::string
strip(
const std::string& s,
437 const std::string& aString,
438 std::string::size_type num = std::string::npos)
439 { std::string t(s);
strip(t, aString, num);
return t; }
452 inline std::string&
strip(std::string& s,
454 std::string::size_type num = std::string::npos)
455 {
return strip(s, std::string(pString), num); }
467 inline std::string
strip(
const std::string& s,
469 std::string::size_type num = std::string::npos)
470 { std::string t(s);
strip(t, std::string(pString), num);
return t; }
482 inline std::string&
strip(std::string& s,
483 const char aCharacter,
484 std::string::size_type num = std::string::npos)
485 {
return strip(s, std::string(1,aCharacter), num); }
497 inline std::string
strip(
const std::string& s,
498 const char aCharacter,
499 std::string::size_type num = std::string::npos)
500 { std::string t(s);
strip(t, std::string(1,aCharacter), num);
return t;}
511 inline std::string&
strip(std::string& s,
512 std::string::size_type num = std::string::npos)
513 {
return strip(s, std::string(1,
' '), num); }
524 inline std::string
strip(
const std::string& s,
525 std::string::size_type num = std::string::npos)
526 { std::string t(s);
strip(t, std::string(1,
' '), num);
return t;}
539 inline std::string
translate(
const std::string& aString,
540 const std::string& inputChars,
541 const std::string& outputChars,
542 const char pad =
' ');
563 inline std::string
change(
const std::string& aString,
564 const std::string& inputString,
565 const std::string& outputString,
566 std::string::size_type startPos = 0,
567 unsigned numChanges = (std::numeric_limits<unsigned>().
max()));
588 inline std::string&
change(std::string& aString,
589 const std::string& inputString,
590 const std::string& outputString,
591 std::string::size_type startPos = 0,
606 const std::string::size_type length,
607 const char pad =
' ');
621 const std::string::size_type length,
622 const char pad =
' ')
623 { std::string t(s);
return rightJustify(t, length, pad); }
637 const std::string::size_type length,
638 const char pad =
' ');
652 const std::string::size_type length,
653 const char pad =
' ')
654 { std::string t(s);
return leftJustify(t, length, pad); }
673 inline std::string&
center(std::string& s,
674 const std::string::size_type length,
675 const char pad =
' ');
695 inline std::string
center(
const std::string& s,
696 const std::string::size_type length,
697 const char pad =
' ')
698 { std::string t(s);
return center(t, length, pad); }
706 {
return strtod(s.c_str(), 0); }
713 inline long asInt(
const std::string& s)
714 {
return strtol(s.c_str(), 0, 10); }
722 {
return strtoul(s.c_str(), 0, 10); }
730 inline float asFloat(
const std::string& s);
749 inline X
asData(
const std::string& s);
757 inline std::string
asString(
const long double x,
758 const std::string::size_type precision = 21);
766 inline std::string
asString(
const double x,
767 const std::string::size_type precision = 17);
776 inline std::string
asString(
const X x);
787 inline std::string&
d2x(std::string& s);
799 inline std::string
d2x(
const std::string& s)
800 { std::string t(s);
return d2x(t); }
811 inline std::string&
x2d(std::string& s);
823 inline std::string
x2d(
const std::string& s)
824 { std::string t(s);
return x2d(t); }
834 inline std::string&
c2x(std::string& s);
842 inline std::string
c2x(
const std::string& s)
843 { std::string t(s);
return c2x(t); }
852 inline unsigned int x2uint(
const std::string& s);
860 inline std::string
int2x(
const unsigned int& i);
870 inline std::string&
replaceAll(std::string& s,
871 const std::string& oldString,
872 const std::string& newString );
926 inline std::string
matches(
const std::string& s,
927 const std::string& aPattern,
928 const char zeroOrMore =
'*',
929 const char oneOrMore =
'+',
930 const char anyChar =
'.' );
949 const std::string& aPattern,
950 const char zeroOrMore =
'*',
951 const char oneOrMore =
'+',
952 const char anyChar =
'.' )
953 {
return matches(s, aPattern, zeroOrMore, oneOrMore, anyChar) !=
974 const char* pPattern,
975 const char zeroOrMore =
'*',
976 const char oneOrMore =
'+',
977 const char anyChar =
'.' )
978 {
return matches(s, std::string(pPattern),
979 zeroOrMore, oneOrMore, anyChar) != std::string(); }
1000 const std::string& pat,
1001 const std::string& rep,
1010 inline std::string
subString(
const std::string& s,
1011 const std::string::size_type startPos = 0,
1012 const std::string::size_type length = std::string::npos,
1013 const char pad =
' ' );
1021 inline std::string&
lowerCase(std::string& s);
1030 { std::string t(s);
return lowerCase(t); }
1038 inline std::string&
upperCase(std::string& s);
1047 { std::string t(s);
return upperCase(t); }
1059 const std::string::size_type size);
1069 inline std::string
firstWord(
const std::string& s,
1070 const char delimiter =
' ');
1080 inline int numWords(
const std::string& s,
1081 const char delimiter =
' ');
1096 inline std::string
words(
const std::string& s,
1097 const std::string::size_type
firstWord = 0,
1098 const std::string::size_type
numWords = std::string::npos,
1099 const char delimiter =
' ');
1112 inline std::string
word(
const std::string& s,
1113 const std::string::size_type wordNum = 0,
1114 const char delimiter =
' ')
1115 {
return words(s, wordNum, 1, delimiter); }
1127 const char delimiter =
' ');
1136 inline std::vector<std::string>
split(
const std::string& str,
1137 const char delimiter =
' ');
1156 const std::string::size_type first = 0,
1157 const std::string::size_type wordsToReplace = std::string::npos,
1158 const char delimiter =
' ');
1173 inline std::string
doub2sci(
const double& d,
1174 const std::string::size_type length,
1175 const std::string::size_type expLen,
1176 const bool showSign =
true,
1177 const bool checkSwitch =
true);
1191 const std::string::size_type length,
1192 const std::string::size_type precision,
1193 const std::string::size_type explen,
1194 bool showPlus=
false);
1217 inline std::string&
sci2for(std::string& aStr,
1218 const std::string::size_type startPos = 0,
1219 const std::string::size_type length = std::string::npos,
1220 const std::string::size_type expLen = 3,
1221 const bool checkSwitch =
true);
1240 inline std::string
doub2for(
const double& d,
1241 const std::string::size_type length,
1242 const std::string::size_type expLen,
1243 const bool checkSwitch =
true);
1257 inline double for2doub(
const std::string& aStr,
1258 const std::string::size_type startPos = 0,
1259 const std::string::size_type length = std::string::npos);
1288 unsigned exponent,
unsigned width = 0,
1302 inline std::string
printable(
const std::string& aStr);
1316 inline std::string&
prettyPrint(std::string& aStr,
1317 const std::string& lineDelim =
"\n",
1318 const std::string& indent =
"",
1319 const std::string& firstIndent =
" ",
1320 const std::string::size_type len = 80,
1321 const char wordDelim =
' ');
1336 const std::string& lineDelim =
"\n",
1337 const std::string& indent =
"",
1338 const std::string& firstIndent =
" ",
1339 const std::string::size_type len = 80,
1340 const char wordDelim =
' ')
1342 std::string
temp(aStr);
1358 inline std::string
tabularize(
const std::vector<std::string>& cells,
1359 std::string::size_type width = 80,
1360 bool force =
false);
1368 inline std::vector<std::string>
split(
const std::string& aStr,
1369 const std::string& theDelimiters,
1370 bool trimWhitespace =
false,
1371 bool ignoreEmpty =
true);
1380 inline std::vector<std::string>
splitWithQuotes(
const std::string& aStr,
1381 const char delimiter =
' ',
1382 bool trimWhitespace =
true,
1383 bool ignoreEmpty =
true);
1393 const char delimiter =
' ',
1394 bool trimWhitespace =
true,
1395 bool ignoreEmpty =
true);
1407 namespace StringUtils
1412 std::string tagstr(indent,
' ');
1417 const std::string& tag,
1427 const std::string& aString,
1428 std::string::size_type num)
1436 (s.find(aString,0) == 0) &&
1439 s.erase(0,aString.length());
1444 catch(std::exception &e)
1446 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1454 const std::string& aString,
1455 std::string::size_type num)
1460 if (s.length() < aString.length() || (aString ==
""))
1463 std::string::size_type
pos = s.length() - aString.length();
1466 (s.length() >= aString.length()) &&
1467 (s.rfind(aString,
pos) ==
pos))
1469 s.erase(
pos, std::string::npos);
1471 pos = s.length() - aString.length();
1475 catch(std::exception &e)
1477 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1482 inline std::string&
strip(std::string& s,
1483 const std::string& aString,
1484 std::string::size_type num)
1492 const std::string& inputChars,
1493 const std::string& outputChars,
1496 std::string rv = aString;
1497 std::string::size_type aspos = 0;
1498 std::string::size_type inpos = std::string::npos;
1504 while ((aspos = rv.find_first_of(inputChars, aspos))
1505 != std::string::npos)
1508 inpos = inputChars.find(rv[aspos]);
1509 if (outputChars.length()-1 < inpos)
1512 toc = outputChars[inpos];
1521 inline std::string
change(
const std::string& aString,
1522 const std::string& inputString,
1523 const std::string& outputString,
1524 std::string::size_type startPos,
unsigned numChanges)
1526 std::string rv(aString);
1527 change(rv, inputString, outputString, startPos, numChanges);
1531 inline std::string&
change(std::string& aString,
const std::string& inputString,
1532 const std::string& outputString,
1533 std::string::size_type startPos,
unsigned numChanges)
1535 if(inputString.empty() || aString.empty())
return aString;
1537 std::string::size_type opos = startPos;
1539 while (count < numChanges)
1541 std::string::size_type
pos = aString.find(inputString, opos);
1542 if (
pos != std::string::npos)
1545 aString.replace(
pos, inputString.length(), outputString);
1546 opos =
pos + outputString.length();
1558 const std::string::size_type length,
1563 if(length < s.length())
1565 s = s.substr(s.length()-length, std::string::npos);
1569 s.insert((std::string::size_type)0, length-s.length(), pad);
1573 catch(std::exception &e)
1575 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1583 const std::string::size_type length,
1588 if(length < s.length())
1590 s = s.substr(0, length);
1594 s.append(length-s.length(), pad);
1598 catch(std::exception &e)
1600 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1608 const std::string::size_type length,
1613 if(length < s.length())
1618 std::string::size_type leftOff = s.length() + (length - s.length()) / 2;
1624 catch(StringException &e)
1628 catch(std::exception &e)
1630 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1640 std::istringstream is(s);
1645 catch(std::exception &e)
1647 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1656 std::istringstream is(s);
1661 catch(std::exception &e)
1663 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1673 std::istringstream is(s);
1678 catch(std::exception &e)
1680 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1685 inline std::string
asString(
const long double x,
const std::string::size_type precision)
1687 std::ostringstream ss;
1688 ss << std::fixed << std::setprecision(precision) << x ;
1692 inline std::string
asString(
const double x,
const std::string::size_type precision)
1694 std::ostringstream ss;
1695 ss << std::fixed << std::setprecision(precision) << x;
1702 std::ostringstream ss;
1708 inline std::string&
d2x(std::string& s)
1720 std::ostringstream st;
1721 st << std::hex << l << std::dec;
1728 catch(StringException &e)
1732 catch(std::exception &e)
1734 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1740 inline std::string&
c2x(std::string& s)
1742 const char hexDigits[] =
"0123456789ABCDEF";
1746 const unsigned char *pSource = (
unsigned char *)old.c_str();
1747 unsigned n = old.length();
1751 for (
int i = 0; i < (int)n * 2;)
1753 unsigned char c = *pSource++;
1754 s[i++] = hexDigits[ c / 16 ];
1755 s[i++] = hexDigits[ c % 16 ];
1760 catch(StringException &e)
1764 catch(std::exception &e)
1766 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1773 inline unsigned int x2uint(
const std::string& s)
1779 std::istringstream iss(s);
1781 iss >> std::hex >> ui;
1784 catch(StringException &e)
1788 catch(std::exception &e)
1790 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1797 inline std::string&
x2d(std::string& s)
1810 std::istringstream strstr(s);
1812 strstr >> std::hex >> i;
1816 s.insert(0,asString<int>(i));
1819 catch(StringException &e)
1823 catch(std::exception &e)
1825 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1830 inline std::string
int2x(
const unsigned int& i)
1834 std::ostringstream ss;
1835 ss << std::hex << i;
1838 catch(StringException &e)
1842 catch(std::exception &e)
1844 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1850 const std::string& oldString,
1851 const std::string& newString)
1855 int spot = s.find(oldString, 0);
1856 while (spot != (
int)std::string::npos)
1858 s.replace(spot, oldString.length(), newString);
1859 spot += newString.length();
1860 spot = s.find(oldString, spot);
1864 catch(std::exception &e)
1866 StringException strexc(
"Exception thrown: " + std::string(e.what()));
1876 std::string::size_type index = 0;
1877 if((s[0] ==
'-') || (s[0] ==
'+'))
1879 for( ; index < s.size(); index++)
1880 if (!isdigit(s[index]))
1890 std::string::size_type index = 0;
1891 bool sawdot =
false;
1892 if((s[0] ==
'-') || (s[0] ==
'+'))
1894 for( ; index < s.size(); index++)
1896 if (s[index] ==
'.')
1902 else if (!isdigit(s[index]))
1913 std::string::size_type
pos = s.find_first_of(
"EeDd");
1914 if(
pos == std::string::npos)
1917 std::string mant=s.substr(0,
pos);
1918 std::string exp=s.substr(
pos+1);
1927 std::string::size_type index;
1928 for(index = 0; index < s.size(); index++)
1929 if (!isalpha(s[index]))
1935 const std::string& aPattern,
1936 const char zeroOrMore,
1937 const char oneOrMore,
1940 std::string thisPattern(aPattern);
1941 std::string thisStr(s);
1945 if (zeroOrMore !=
'*')
1948 replaceAll(thisPattern, std::string(1, zeroOrMore),
"*");
1950 if (oneOrMore !=
'+')
1953 replaceAll(thisPattern, std::string(1, oneOrMore),
"+");
1958 replaceAll(thisPattern, std::string(1, anyChar),
".");
1961 #if defined(_WIN32) && _MSC_VER >= 1700
1964 std::regex reg (thisPattern);
1967 if(std::regex_search(thisStr,sm,reg,
1968 std::regex_constants::match_not_bol|
1969 std::regex_constants::match_not_eol))
1975 return std::string();
1978 catch(std::regex_error& e)
1980 Exception E(std::string(
"std::regex_error: ") + e.what() );
1986 const std::string::size_type regErrorBufSize = 512;
1991 char errorMsg[regErrorBufSize];
1992 int rc = regcomp(®Exp, thisPattern.c_str(), REG_EXTENDED);
1996 regerror(rc,
NULL, errorMsg, regErrorBufSize - 1);
1998 StringException strerr(
"Regexp error: " + std::string(errorMsg));
2001 rc = regexec(®Exp, thisStr.c_str(), 1, &
matches,
2002 REG_NOTBOL | REG_NOTEOL);
2003 if ( (rc != 0) && (rc != REG_NOMATCH) )
2005 regerror(rc, ®Exp, errorMsg, regErrorBufSize - 1);
2007 StringException strerr(
"Regexp error: " + std::string(errorMsg));
2012 if (rc == REG_NOMATCH)
2013 return std::string();
2021 const std::string& pat,
2022 const std::string& rep,
2025 #if defined(_WIN32) && _MSC_VER >= 1700
2027 std::string rv(fmt);
2031 std::regex reg(pat);
2034 while (std::regex_search (rv,m,reg))
2036 std::string mac = m.str();
2040 sprintf(buffer, mac.c_str(), to);
2042 rv.replace(m.position(), m.length(), std::string(buffer));
2045 catch(std::regex_error& e)
2047 Exception E(std::string(
"std::regex_error:")+e.what());
2054 const size_t bufferSize = 513;
2055 char buffer[bufferSize];
2056 int rc = regcomp(&re, pat.c_str(), REG_EXTENDED);
2060 regerror(rc,
NULL, buffer, bufferSize - 1);
2062 StringException
se(
"Regexp error: " + std::string(buffer));
2067 std::string rv = fmt;
2069 while ( regexec(&re, rv.c_str(), 1, &r, 0) == 0 )
2071 size_t len = r.rm_eo - r.rm_so;
2072 std::string mac = rv.substr(r.rm_so, len);
2073 mac =
replaceAll(mac, rep.substr(0,1), rep.substr(1));
2074 sprintf(buffer, mac.c_str(), to);
2075 rv.replace(r.rm_so, len, std::string(buffer));
2084 const std::string::size_type startPos,
2085 const std::string::size_type length,
2090 if(startPos >= s.length())
2092 return std::string(length, pad);
2094 std::string
temp = s.substr(startPos, length);
2097 catch(StringException &e)
2101 catch(std::exception &e)
2103 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2110 for(std::string::size_type i = 0; i < s.length(); i++)
2112 s[i] = tolower(s[i]);
2119 for(std::string::size_type i = 0; i < s.length(); i++)
2121 s[i] = toupper(s[i]);
2127 const std::string::size_type size)
2129 unsigned char* q = (
unsigned char*)p;
2130 std::string s(size,
'\0');
2131 for (
int i=0; i<(int)size; i++)
2133 s[i] = (
unsigned char)(*q++);
2139 const char delimiter)
2144 std::string::size_type
pos = s.find_first_not_of(delimiter);
2145 if (
pos == std::string::npos)
2150 std::string::size_type endPos = s.find(delimiter,
pos);
2151 if (endPos == std::string::npos)
2153 return std::string(s,
pos, endPos);
2157 return std::string(s,
pos, endPos -
pos);
2160 catch(StringException &e)
2164 catch(std::exception &e)
2166 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2172 const char delimiter)
2188 catch(StringException &e)
2192 catch(std::exception &e)
2194 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2199 inline std::string
words(
const std::string& s,
2201 const std::string::size_type
numWords,
2202 const char delimiter)
2212 std::string::size_type wordNum = 0;
2213 std::string::size_type
pos = 0, startPos = std::string::npos;
2215 std::string toReturn;
2218 pos = s.find_first_not_of(delimiter,
pos);
2219 while ((
pos != std::string::npos) && (
pos <= s.length()))
2225 pos = s.find(delimiter,
pos);
2230 pos = s.find_first_not_of(delimiter,
pos);
2234 if (startPos == std::string::npos)
2236 if (
pos == std::string::npos)
2237 return ((wordNum >=
firstWord) ? s.substr(startPos) :
"");
2239 return s.substr(startPos,
pos-startPos);
2241 catch(StringException &e)
2245 catch(std::exception &e)
2247 StringException strexc(
"Exception thrown: " +
2248 std::string(e.what()));
2254 const char delimiter)
2259 std::string toReturn =
firstWord(s, delimiter);
2264 catch(StringException &e)
2268 catch(std::exception &e)
2270 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2275 inline std::vector<std::string>
split(
const std::string& str,
2276 const char delimiter)
2279 std::vector<std::string> rvec;
2280 std::string tempStr(str);
2282 while(tempStr.size() > 0)
2286 catch(StringException &e)
2290 catch(std::exception &e)
2292 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2297 inline std::vector<std::string>
split(
const std::string& aStr,
2298 const std::string& theDelimiters,
2299 bool trimWhitespace,
2302 std::vector<std::string> toReturn;
2304 std::string::size_type lastPos = aStr.find_first_not_of(theDelimiters, 0);
2305 std::string::size_type
pos = aStr.find_first_of(theDelimiters, lastPos);
2307 while (std::string::npos !=
pos || std::string::npos != lastPos)
2309 std::string token = aStr.substr(lastPos,
pos - lastPos);
2313 if(!token.empty() || !ignoreEmpty) toReturn.push_back(token);
2315 lastPos = aStr.find_first_not_of(theDelimiters,
pos);
2316 pos = aStr.find_first_of(theDelimiters, lastPos);
2323 const char delimiter,
2324 bool trimWhitespace,
2327 std::vector<std::string> toReturn;
2328 std::string::size_type begPos = 0;
2329 std::string::size_type endPos = 0;
2330 std::string::size_type tokenLength;
2331 char currentDelimiter;
2333 if(delimiter ==
'\'' || delimiter ==
'"')
2334 GNSSTK_THROW(StringException(
"Delimiter must not be quote"));
2336 while(endPos != std::string::npos && endPos <= aStr.length())
2338 currentDelimiter = delimiter;
2341 if(aStr.compare(begPos,1,
"\"") == 0) currentDelimiter =
'"';
2342 if(aStr.compare(begPos,1,
"\'") == 0) currentDelimiter =
'\'';
2345 endPos = aStr.find_first_of(currentDelimiter,
2346 begPos + (currentDelimiter == delimiter ? 0 : 1));
2350 if(currentDelimiter != delimiter && std::string::npos != endPos) endPos++;
2353 tokenLength = endPos - begPos;
2355 std::string token = aStr.substr(begPos, tokenLength);
2358 if(currentDelimiter != delimiter)
2365 if(!token.empty() || !ignoreEmpty) toReturn.push_back(token);
2369 if(begPos != std::string::npos) begPos++;
2377 const char delimiter,
2378 bool trimWhitespace,
2381 std::vector<std::string> toReturn;
2382 std::string::size_type begPos = 0;
2383 std::string::size_type endPos = 0;
2384 std::string::size_type tokenLength;
2385 char currentDelimiter;
2387 if(delimiter ==
'"')
2388 GNSSTK_THROW(StringException(
"Delimiter must not be quote"));
2390 while(endPos != std::string::npos && endPos <= aStr.length())
2392 currentDelimiter = delimiter;
2395 if(aStr.compare(begPos,1,
"\"") == 0) currentDelimiter =
'"';
2398 endPos = aStr.find_first_of(currentDelimiter,
2399 begPos + (currentDelimiter == delimiter ? 0 : 1));
2403 if(currentDelimiter != delimiter && std::string::npos != endPos) endPos++;
2406 tokenLength = endPos - begPos;
2408 std::string token = aStr.substr(begPos, tokenLength);
2411 if(currentDelimiter != delimiter)
2418 if(!token.empty() || !ignoreEmpty) toReturn.push_back(token);
2422 if(begPos != std::string::npos) begPos++;
2430 const std::string::size_type first,
2431 const std::string::size_type wordsToReplace,
2432 const char delimiter)
2436 std::string::size_type rmStart = std::string::npos;
2437 std::string::size_type rmCount = std::string::npos;
2440 std::string::size_type sPos = s.find_first_not_of(delimiter);
2442 for (std::string::size_type thisWord = 0;
2443 sPos != std::string::npos;
2447 if (thisWord == first)
2450 if (wordsToReplace == std::string::npos)
2455 else if ( (wordsToReplace != std::string::npos)
2456 && (thisWord >= first + wordsToReplace))
2458 rmCount = sPos - rmStart;
2462 sPos = s.find(delimiter, sPos);
2463 if (sPos != std::string::npos)
2466 sPos = s.find_first_not_of(delimiter, sPos);
2470 if (rmStart != std::string::npos)
2472 s.erase(rmStart, rmCount);
2473 if (rmCount == std::string::npos)
2480 catch(StringException &e)
2484 catch(std::exception &e)
2486 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2492 const std::string::size_type length,
2493 const std::string::size_type expLen,
2494 const bool showSign,
2495 const bool checkSwitch)
2497 std::string toReturn;
2498 short exponentLength = expLen;
2501 if (exponentLength < 0) exponentLength = 1;
2502 if (exponentLength > 3 && checkSwitch) exponentLength = 3;
2504 std::stringstream c;
2505 c.setf(std::ios::scientific, std::ios::floatfield);
2515 c.precision(length - 3 - exponentLength - 1 - expSize);
2526 const std::string::size_type length,
2527 const std::string::size_type precision,
2528 const std::string::size_type explen,
2532 std::string::size_type elen = (explen > 0 ? (explen < 3 ? explen : 3) : 1);
2533 std::string::size_type prec = (precision > 0 ? precision : 1);
2534 std::string::size_type leng = (length > 0 ? length : 1);
2537 size_t i = (int(leng) - int(elen) - 4);
2539 if(i > 0 && leng < i) leng = std::string::size_type(i);
2542 std::stringstream ss;
2543 ss << std::scientific << std::setprecision(prec);
2544 if(showPlus) ss << std::showpos;
2552 std::string::size_type
pos =
str1.find_first_of(
"EDed");
2567 const std::string::size_type startPos,
2568 const std::string::size_type length,
2569 const std::string::size_type expLen,
2570 const bool checkSwitch)
2574 std::string::size_type idx = aStr.find(
'.', startPos);
2580 bool redoexp=!checkSwitch;
2583 if ((idx == 0) || (idx >= (startPos + length - expLen - 1)))
2585 StringException e(
"sci2for: no decimal point in string");
2598 aStr[idx] = aStr[idx-1];
2601 if (
asDouble(aStr.substr(startPos, length)) != 0.0)
2605 idx = aStr.find(
'e', startPos);
2606 if (idx == std::string::npos)
2608 idx = aStr.find(
'E', startPos);
2609 if (idx == std::string::npos)
2611 StringException e(
"sci2for:no 'e' or 'E' in string");
2624 exp = aStr.substr(idx + 1, std::string::npos);
2628 aStr.erase(idx + 1);
2644 aStr.insert((std::string::size_type)0, 1,
' ');
2650 aStr.insert((std::string::size_type)1, 1,
'0');
2656 catch(StringException &e)
2660 catch(std::exception &e)
2662 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2669 const std::string::size_type length,
2670 const std::string::size_type expLen,
2671 const bool checkSwitch)
2675 short exponentLength = expLen;
2678 if (exponentLength < 0) exponentLength = 1;
2679 if (exponentLength > 3 && checkSwitch) exponentLength = 3;
2681 std::string toReturn =
doub2sci(d, length, exponentLength,
true, checkSwitch);
2682 sci2for(toReturn, 0, length, exponentLength, checkSwitch);
2686 catch(StringException &e)
2690 catch(std::exception &e)
2692 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2699 const std::string::size_type startPos,
2700 const std::string::size_type length)
2702 std::string s(aStr, startPos, length);
2711 std::string::size_type
pos = s.find_first_of(
"EDd");
2712 if (
pos != std::string::npos)
2719 return asDouble(aStr.substr(startPos, length));
2722 std::stringstream st;
2736 size_t len = aStr.length();
2739 for (
int i = 0; i < len; i++)
2742 if (c > 31 && c < 127)
2748 if (c < 0 || c > 127)
2750 rv.append(
"<" +
c2x(aStr.substr(i,1)) +
">");
2754 char ctrl = (int)c ^ 0x40;
2763 catch(StringException &e)
2767 catch(std::exception &e)
2769 StringException strexc(
"Exception thrown: " + std::string(e.what()));
2775 const std::string& lineDelim,
2776 const std::string& indent,
2777 const std::string& firstIndent,
2778 const std::string::size_type len,
2779 const char wordDelim)
2783 std::string newStr(firstIndent);
2785 std::string::size_type wordPos = 0, linePos = 0, curPos = 0,
2786 curLineLen = newStr.length(), minPos = 0, wordLen = 0;
2787 bool wordDelimited =
false;
2788 while (curPos != std::string::npos)
2790 wordPos = aStr.find(wordDelim, curPos);
2791 if (wordPos == curPos)
2798 wordDelimited =
false;
2799 linePos = aStr.find(lineDelim, curPos);
2800 if (linePos == curPos)
2802 curPos += lineDelim.length();
2804 newStr += lineDelim + indent;
2805 curLineLen = indent.length();
2809 minPos =
std::min(wordPos, linePos);
2810 if (minPos != std::string::npos)
2811 wordLen = minPos - curPos;
2813 wordLen = aStr.length() - curPos;
2814 if ((curLineLen + wordLen + 1) > len)
2816 newStr += lineDelim + indent;
2817 curLineLen = indent.length();
2819 newStr += wordDelim;
2820 newStr += aStr.substr(curPos, wordLen);
2821 curLineLen += wordLen + 1;
2824 aStr = newStr + lineDelim;
2827 catch (std::exception &e)
2829 StringException strexc(
"Exception thrown: " +
2830 std::string(e.what()));
2835 inline std::string
tabularize(
const std::vector<std::string>& cells,
2836 std::string::size_type width,
2841 std::string::size_type maxWidth = 0;
2842 for (
unsigned i = 0; i < cells.size(); i++)
2844 maxWidth =
std::max(maxWidth, cells[i].size());
2850 char *colch =
getenv(
"COLUMNS");
2853 std::string colStr(colch);
2854 width =
asInt(colStr);
2858 std::string::size_type numCols = width / maxWidth;
2859 for (
unsigned i = 0; i < cells.size(); i++)
2862 if (((i % numCols) == (numCols-1)) ||
2863 (i == (cells.size() - 1)))
2876 #endif // GNSSTK_STRINGUTILS_HPP