Replace some code in StringReader::getTokens() with std::string::find_first_of(). Since this makes found_pos unsigned, a few comparisons involving found_pos must be adjusted (unsigned integers are always >= 0).

This commit is contained in:
Jason Orendorff
2013-10-25 02:26:31 -07:00
parent a18b4f41a7
commit 64a405cab1
2 changed files with 9 additions and 33 deletions

View File

@@ -14,7 +14,7 @@ class StringReader
void setString(std::string inputString); void setString(std::string inputString);
std::string word(bool truncateEnd = true); std::string word(bool truncateEnd = true);
std::string line(bool truncateEnd = true); std::string line(bool truncateEnd = true);
std::string getTokens(std::vector<std::string> get_chars, bool truncateEnd = true); std::string getTokens(const char *get_chars, bool truncateEnd = true);
std::string truncateEnd(std::string to_truncate); std::string truncateEnd(std::string to_truncate);
static void test(); static void test();

View File

@@ -25,47 +25,22 @@ void StringReader::setString(std::string inputString)
std::string StringReader::word(bool truncateEnd) std::string StringReader::word(bool truncateEnd)
{ {
std::vector<std::string> stop_chars; std::string result = getTokens(" \n\t", truncateEnd);
stop_chars.push_back(" ");
stop_chars.push_back("\n");
stop_chars.push_back("\t");
std::string result = getTokens(stop_chars, truncateEnd);
while (result == " " || result == "\n" || result == "\t") while (result == " " || result == "\n" || result == "\t")
{ {
result = getTokens(stop_chars, truncateEnd); result = getTokens(" \n\t", truncateEnd);
} }
return(result); return(result);
} }
std::string StringReader::line(bool truncateEnd) std::string StringReader::line(bool truncateEnd)
{ {
std::vector<std::string> stop_chars; return getTokens("\n", truncateEnd);
stop_chars.push_back("\n");
return getTokens(stop_chars, truncateEnd);
} }
std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool truncateEnd) std::string StringReader::getTokens(const char *stop_chars, bool truncateEnd)
{ {
int found_pos, new_found_pos; size_t found_pos = rd_string.find_first_of(stop_chars, str_pos);
std::string stop_char;
found_pos = rd_string.find(stop_chars[0], str_pos);
stop_char = stop_chars[0];
for (unsigned int i = 1; i < stop_chars.size(); i++)
{
new_found_pos = rd_string.find(stop_chars[i], str_pos);
//Ok, if the position we found is closer than what we have and is not the end of file, OR the position we are at is the end of file
//assign the new found position to the currrent found position
if ( ((new_found_pos <= found_pos) && (new_found_pos != std::string::npos)) || found_pos == std::string::npos )
{
found_pos = new_found_pos;
stop_char = stop_chars[i];
}
}
if (rd_string[str_pos] == '\"') { if (rd_string[str_pos] == '\"') {
//See if we have an even or odd number of backslashes (that is, this quote is not or is escaped) //See if we have an even or odd number of backslashes (that is, this quote is not or is escaped)
@@ -82,7 +57,7 @@ std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool tr
//Check to see if the quote is escaped //Check to see if the quote is escaped
numBackslashes = 0; numBackslashes = 0;
countBack = 1; countBack = 1;
while (found_pos-countBack >= 0 && rd_string[found_pos-countBack] == '\\') { while (found_pos >= countBack && rd_string[found_pos-countBack] == '\\') {
numBackslashes++; numBackslashes++;
countBack++; countBack++;
} }
@@ -93,7 +68,7 @@ std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool tr
//Check to see if it's escaped //Check to see if it's escaped
numBackslashes = 0; numBackslashes = 0;
countBack = 1; countBack = 1;
while (found_pos-countBack >= 0 && rd_string[found_pos-countBack] == '\\') { while (found_pos >= countBack && rd_string[found_pos-countBack] == '\\') {
numBackslashes++; numBackslashes++;
countBack++; countBack++;
} }
@@ -103,6 +78,7 @@ std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool tr
if (found_pos == str_pos) //We are at the endline if (found_pos == str_pos) //We are at the endline
{ {
std::string stop_char(1, rd_string[str_pos]);
str_pos++; str_pos++;
return stop_char; return stop_char;
} else if (found_pos == std::string::npos) //We are at the end of the file } else if (found_pos == std::string::npos) //We are at the end of the file