43 #include "Teuchos_Assert.hpp"
56 if (line.length() > 0) rtn.
append(
before(line, comment));
68 const size_t len = input.
length();
69 for (
size_t p=0; p<len; ++p) {
70 const bool isEnd = p==len-1;
71 if( input[p]==
'\n' || input[p]==
'\0' || input[p]==
'\r' || isEnd )
75 subString( input, begin, p+(isEnd?(input[len-1]==
'\n'?0:1):0) )
92 for (
int i=0; i<lines.
length(); i++)
94 if (lines[i].length() == 0)
continue;
96 if (tokens.
length() == 0)
continue;
108 if (line.length() > 0) line[0] =
'\0';
110 if (is.eof())
return false;
111 if (is.getline(c, 499))
113 line = std::string(c);
122 unsigned int start = 0;
124 while(start < str.length())
128 if (wordStart > start)
135 if (start-stop == 0)
return rtn;
136 std::string sub =
subString(str, start, stop);
146 unsigned int start = 0;
148 while(start < str.length())
152 if (start-stop == 0)
return rtn;
153 std::string sub =
subString(str, start, stop);
166 for (
int i=iStart; i<tokens.
length(); i++)
169 if (i < (tokens.
length()-1)) rtn +=
" ";
186 std::string tmp(big);
191 for (
unsigned int i=1; i<big.length(); i++)
193 if (big[i]==
'(') parenDepth++;
194 if (big[i]==
')') parenDepth--;
197 tmp[localCount]=
'\0';
201 if (big[i]==
',' && parenDepth==0)
203 tmp[localCount]=
'\0';
209 tmp[localCount] = big[i];
220 for (
unsigned int i=0; i<(str.length()-offset); i++)
222 if (str[i+offset]==
' ' || str[i+offset]==
'\t' || str[i+offset]==
'\n')
227 return static_cast<int>(str.length());
233 for (
unsigned int i=0; i<(str.length()-offset); i++)
235 if (!(str[i+offset]==
' ' || str[i+offset]==
'\t' || str[i+offset]==
'\n'))
240 return static_cast<int>(str.length());
250 "mismatched variable tables in varTableSubstitute");
252 std::string line = rawLine;
253 for (
int i=0; i<varNames.
length(); i++)
262 const std::string& varName,
263 const std::string& varValue)
265 std::string line = rawLine;
268 while (
find(line, varName) >= 0)
270 std::string b =
before(line, varName);
271 std::string a =
after(line, varName);
272 line = b + varValue + a;
290 std::runtime_error,
"String::before: arg is null pointer");
292 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str());
293 if (p==0)
return str;
294 int subLen =
static_cast<int>(p-str.c_str());
295 std::string rtn(str.c_str(), subLen);
303 std::runtime_error,
"String::after: arg is null pointer");
306 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str()) ;
308 if (p==0)
return std::string();
310 p+= std::strlen(sub.c_str());
311 return std::string(p);
317 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str());
319 return static_cast<int>(p-str.c_str());
325 for (
unsigned int i=0; i<str.length(); i++)
327 unsigned char c = str[i];
328 if (c >= 33 && c <= 126)
339 std::string rtn = str;
340 for (
unsigned int i=0; i<rtn.length(); i++)
342 unsigned char c = rtn[i];
343 if (c < 33 || c > 126)
345 if (c !=
'\t' && c !=
'\n'&& c !=
'\r' && c !=
'\f' && c !=
' ')
356 const std::string& end, std::string& front,
359 front =
before(str, begin);
360 std::string middle =
before(
after(str, begin), end);
361 back =
after(str, end);
368 return std::string(str.c_str()+begin, end-begin);
372 std::string StrUtils::readFromStream(std::istream& )
375 "StrUtils::readFromStream isn't implemented yet");
392 for (
unsigned int i=0; i<rtn.length(); i++)
394 rtn[i] = toupper(rtn[i]);
402 return std::atof(s.c_str());
408 return std::atoi(s.c_str());
414 ,
const std::string &linePrefix
415 ,
const std::string &lines
420 for(
int i = 0; i < static_cast<int>(linesArray.size()); ++i )
422 os << linePrefix << linesArray[i] <<
"\n";
430 std::string::size_type pos=0;
431 bool spacesLeft =
true;
434 pos = stringToClean.find(
" ");
435 if(pos != string::npos){
436 stringToClean.erase(pos,1);
442 return stringToClean;
void reserve(size_type n)
static std::string removeAllSpaces(std::string stringToClean)
Removes all the spaces in a string.
Array< T > & append(const T &x)
Add a new entry at the end of the array.
static int findNextNonWhitespace(const std::string &str, int offset)
static std::string allCaps(const std::string &str)
Converts a std::string to all upper case.
static Array< Array< std::string > > tokenizeFile(std::istream &is, char comment)
Tokenize a file into whitespace-delimited tokens.
static std::string after(const std::string &str, const std::string &sub)
Find the substring after a specified substring. For example, before("abcdefghij", "gh") returns "ij"...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static Array< std::string > splitIntoLines(const std::string &input)
Split an input std::string that contains newlines into an array of strings, one for each line...
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
static bool readLine(std::istream &is, std::string &line)
Read a single line into a std::string.
static double atof(const std::string &str)
Returns the double value of a std::string.
A std::string utilities class for Teuchos.
static std::string between(const std::string &str, const std::string &begin, const std::string &end, std::string &front, std::string &back)
Returns the std::string between two delimiting strings, and returns by reference the strings before a...
static std::string varSubstitute(const std::string &rawLine, const std::string &varName, const std::string &varValue)
static std::string subString(const std::string &str, int begin, int end)
Returns the substring between two positions.
static std::string reassembleFromTokens(const Array< std::string > &tokens, int iStart=0)
static std::string before(const std::string &str, const std::string &sub)
Find the substring before a specified substring. For example, before("abcdefghij", "gh") returns "abcdef".
static std::string varTableSubstitute(const std::string &rawLine, const Array< std::string > &varNames, const Array< std::string > &varValues)
void resize(size_type new_size, const value_type &x=value_type())
static bool isWhite(const std::string &str)
Returns true if a std::string consists entirely of whitespace.
static std::string fixUnprintableCharacters(const std::string &str)
Convert unprintable non-null characters to whitespace.
int length() const
Return number of elements in the array.
static Array< std::string > stringTokenizer(const std::string &str)
static Array< std::string > readFile(std::istream &is, char comment)
Read a file, putting each line into a std::string.
static int atoi(const std::string &str)
Returns the int value of a std::string.
static Array< std::string > getTokensPlusWhitespace(const std::string &str)
static void splitList(const std::string &bigstring, Array< std::string > &elements)
static int findNextWhitespace(const std::string &str, int offset)
static int find(const std::string &str, const std::string &sub)
Find the position at which a substring first occurs. For example, find("abcdefghij", "gh") returns 6.