11 #include "Teuchos_Assert.hpp"
24 if (line.length() > 0) rtn.
append(
before(line, comment));
36 const size_t len = input.
length();
37 for (
size_t p=0; p<len; ++p) {
38 const bool isEnd = p==len-1;
39 if( input[p]==
'\n' || input[p]==
'\0' || input[p]==
'\r' || isEnd )
43 subString( input, begin, p+(isEnd?(input[len-1]==
'\n'?0:1):0) )
52 typedef std::string::size_type size_type;
54 size_type cur_pos, last_pos = 0, length = s.
length();
55 while (last_pos < length + 1) {
56 cur_pos = s.find_first_of(sep, last_pos);
57 if (cur_pos == std::string::npos) {
60 if (cur_pos != last_pos) {
62 std::string(s.data() + last_pos, (size_type)cur_pos - last_pos);
65 last_pos = cur_pos + 1;
79 for (
int i=0; i<lines.
length(); i++)
81 if (lines[i].length() == 0)
continue;
83 if (tokens.
length() == 0)
continue;
95 if (line.length() > 0) line[0] =
'\0';
97 if (is.eof())
return false;
98 if (is.getline(c, 499))
100 line = std::string(c);
109 unsigned int start = 0;
111 while(start < str.length())
115 if (wordStart > start)
122 if (start-stop == 0)
return rtn;
123 std::string sub =
subString(str, start, stop);
133 unsigned int start = 0;
135 while(start < str.length())
139 if (start-stop == 0)
return rtn;
140 std::string sub =
subString(str, start, stop);
153 for (
int i=iStart; i<tokens.
length(); i++)
156 if (i < (tokens.
length()-1)) rtn +=
" ";
173 std::string tmp(big);
178 for (
unsigned int i=1; i<big.length(); i++)
180 if (big[i]==
'(') parenDepth++;
181 if (big[i]==
')') parenDepth--;
184 tmp[localCount]=
'\0';
188 if (big[i]==
',' && parenDepth==0)
190 tmp[localCount]=
'\0';
196 tmp[localCount] = big[i];
207 for (
unsigned int i=0; i<(str.length()-offset); i++)
209 if (str[i+offset]==
' ' || str[i+offset]==
'\t' || str[i+offset]==
'\n')
214 return static_cast<int>(str.length());
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());
237 "mismatched variable tables in varTableSubstitute");
239 std::string line = rawLine;
240 for (
int i=0; i<varNames.
length(); i++)
249 const std::string& varName,
250 const std::string& varValue)
252 std::string line = rawLine;
255 while (
find(line, varName) >= 0)
257 std::string b =
before(line, varName);
258 std::string a =
after(line, varName);
259 line = b + varValue + a;
277 std::runtime_error,
"String::before: arg is null pointer");
279 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str());
280 if (p==0)
return str;
281 int subLen =
static_cast<int>(p-str.c_str());
282 std::string rtn(str.c_str(), subLen);
290 std::runtime_error,
"String::after: arg is null pointer");
293 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str()) ;
295 if (p==0)
return std::string();
297 p+= std::strlen(sub.c_str());
298 return std::string(p);
304 char* p = std::strstr((
char*) str.c_str(), (
char*) sub.c_str());
306 return static_cast<int>(p-str.c_str());
312 for (
unsigned int i=0; i<str.length(); i++)
314 unsigned char c = str[i];
315 if (c >= 33 && c <= 126)
326 std::string rtn = str;
327 for (
unsigned int i=0; i<rtn.length(); i++)
329 unsigned char c = rtn[i];
330 if (c < 33 || c > 126)
332 if (c !=
'\t' && c !=
'\n'&& c !=
'\r' && c !=
'\f' && c !=
' ')
343 const std::string& end, std::string& front,
346 front =
before(str, begin);
347 std::string middle =
before(
after(str, begin), end);
348 back =
after(str, end);
355 return std::string(str.c_str()+begin, end-begin);
359 std::string StrUtils::readFromStream(std::istream& )
362 "StrUtils::readFromStream isn't implemented yet");
379 for (
unsigned int i=0; i<rtn.length(); i++)
381 rtn[i] = toupper(rtn[i]);
389 return std::atof(s.c_str());
395 return std::atoi(s.c_str());
401 ,
const std::string &linePrefix
402 ,
const std::string &lines
407 for(
int i = 0; i < static_cast<int>(linesArray.size()); ++i )
409 os << linePrefix << linesArray[i] <<
"\n";
417 std::string::size_type pos=0;
418 bool spacesLeft =
true;
421 pos = stringToClean.find(
" ");
422 if(pos != string::npos){
423 stringToClean.erase(pos,1);
429 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 Array< std::string > splitString(const std::string_view s, const char sep= ',')
Split an input std::string using a seperator char sep.
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.