word count: state enum is removed

This commit is contained in:
Dmitry Kokorin 2016-02-22 00:33:18 +03:00
parent fb546d1f29
commit 6f5bbd1460

View file

@ -12,47 +12,26 @@ Words words(const string &str, const locale &loc)
{ {
Words result; Words result;
enum {
InsideWord,
OutsideWord
} state(OutsideWord);
string::const_iterator begin, end, i; string::const_iterator begin, end, i;
i = begin = end = str.begin(); i = end = str.begin();
begin = str.end();
for (; i != str.end(); ++i) { for (; i != str.end(); ++i) {
bool is_alpha_or_digit = isalpha(*i, loc) || isdigit(*i, loc); bool is_alpha_or_digit = isalpha(*i, loc) || isdigit(*i, loc);
bool is_word_char = is_alpha_or_digit || *i == '\''; bool is_word_char = is_alpha_or_digit || *i == '\'';
switch (state) {
case OutsideWord:
if (is_alpha_or_digit) { if (is_alpha_or_digit) {
state = InsideWord; if (begin > end)
begin = i; begin = i;
end = next(i);
}
break;
case InsideWord:
if (is_alpha_or_digit) {
end = next(i); end = next(i);
} }
if (!is_word_char || next(i) == str.end()) { if (!is_word_char || next(i) == str.end()) {
state = OutsideWord; if (begin < end) {
if (begin != end) {
string word; string word;
word.reserve(distance(begin, end)); word.reserve(distance(begin, end));
@ -60,10 +39,9 @@ Words words(const string &str, const locale &loc)
[=](char c){return tolower(c, loc);}); [=](char c){return tolower(c, loc);});
result[word]++; result[word]++;
}
}
break; begin = str.end();
}
} }
} }