update trimString(), add tokenize()
This commit is contained in:
parent
cf4e09814f
commit
874be25658
36
WFObj.cc
36
WFObj.cc
@ -20,14 +20,12 @@ using namespace std;
|
||||
|
||||
/****** static functions ******/
|
||||
|
||||
static string trimString(string s)
|
||||
static string trimString(const string in)
|
||||
{
|
||||
size_t lastpos = s.find_last_not_of(WHITESPACE);
|
||||
if (lastpos == string::npos)
|
||||
size_t firstpos = in.find_first_not_of(WHITESPACE);
|
||||
if (firstpos == string::npos)
|
||||
return "";
|
||||
s.erase(lastpos + 1);
|
||||
s.erase(0, s.find_first_not_of(WHITESPACE));
|
||||
return s;
|
||||
return string(in, firstpos, in.find_last_not_of(WHITESPACE) - firstpos + 1);
|
||||
}
|
||||
|
||||
static string stripFirstToken(string & input)
|
||||
@ -41,6 +39,20 @@ static string stripFirstToken(string & input)
|
||||
return token;
|
||||
}
|
||||
|
||||
vector<string> tokenize(const string & input)
|
||||
{
|
||||
vector<string> tokens;
|
||||
string in = input;
|
||||
for (;;)
|
||||
{
|
||||
string token = stripFirstToken(in);
|
||||
if (token == "")
|
||||
break;
|
||||
tokens.push_back(token);
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
static vector<string> splitString(const string & str, char delim)
|
||||
{
|
||||
vector<string> ret;
|
||||
@ -134,8 +146,7 @@ bool WFObj::load(const WFObj::Buffer &buff)
|
||||
size_t idx = 0;
|
||||
while (idx < buff.length)
|
||||
{
|
||||
string line = getLine(buff, idx, &idx);
|
||||
string input = trimString(line);
|
||||
string input = trimString(getLine(buff, idx, &idx));
|
||||
int sz = input.size();
|
||||
if (sz == 0 || input[0] == '#')
|
||||
continue;
|
||||
@ -182,14 +193,7 @@ string WFObj::getLine(const Buffer & buff, size_t idx, size_t *update_idx)
|
||||
void WFObj::processInputLine(const std::string & input)
|
||||
{
|
||||
string line = input;
|
||||
vector<string> tokens;
|
||||
for (;;)
|
||||
{
|
||||
string token = stripFirstToken(line);
|
||||
if (token == "")
|
||||
break;
|
||||
tokens.push_back(token);
|
||||
}
|
||||
vector<string> tokens = tokenize(line);
|
||||
|
||||
if (tokens.size() == 0)
|
||||
return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user