Created
April 9, 2011 07:50
-
-
Save abailly/911223 to your computer and use it in GitHub Desktop.
trim and tokenize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
vector<string> tokenize(string const &line, char separator) { | |
vector<string> tokens; | |
istringstream elements(line); | |
while(!elements.eof()) { | |
string token; | |
getline(elements, token, separator); | |
tokens.push_back(token); | |
} | |
return tokens; | |
} | |
namespace { | |
void skipTrailingWS( string::const_iterator const & leading, string::const_iterator& trailing, string const &aString ) | |
{ | |
while(trailing != leading && *--trailing == ' ') ; | |
if(trailing != aString.end() && trailing != aString.begin()) ++trailing; | |
} | |
void skipLeadingWS( string::const_iterator& leading, string::const_iterator const & trailing ) | |
{ | |
while(leading != trailing && *leading == ' ') ++leading; | |
} | |
} | |
string trim(string const &aString) { | |
string::const_iterator trailing = aString.end(); | |
string::const_iterator leading = aString.begin(); | |
skipTrailingWS(leading, trailing, aString); | |
skipLeadingWS(leading, trailing); | |
ostringstream out; | |
while(leading != trailing) out << *leading++; | |
return out.str(); | |
} | |
// Gtest tests | |
TEST(UtilTest, TrimmingAStringWithoutLeadingOrTrailingWhitespaceIsNoop) | |
{ | |
std::string nothingToTrim = "no leading or trailing whitespace"; | |
ASSERT_EQ(nothingToTrim, trim(nothingToTrim)); | |
} | |
TEST(UtilTest, TrimmingAStringRemovesLeadingWhitespace) | |
{ | |
std::string leadingWhitespace = " leading whitespace"; | |
std::string trimmed = "leading whitespace"; | |
ASSERT_EQ(trimmed, trim(leadingWhitespace)); | |
} | |
TEST(UtilTest, TrimmingAStringRemovesTrailingWhitespace) | |
{ | |
std::string trailingWhitespace = "trailing whitespace "; | |
std::string trimmed = "trailing whitespace"; | |
ASSERT_EQ(trimmed, trim(trailingWhitespace)); | |
} | |
TEST(UtilTest, TrimmingAStringWithOnlySpaceYieldsEmptyString) | |
{ | |
std::string onlyWhitespace = " "; | |
std::string trimmed = ""; | |
ASSERT_EQ(trimmed, trim(onlyWhitespace)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment