word count: state enum is removed
This commit is contained in:
parent
fb546d1f29
commit
6f5bbd1460
1 changed files with 16 additions and 38 deletions
|
|
@ -12,58 +12,36 @@ 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) {
|
if (is_alpha_or_digit) {
|
||||||
|
|
||||||
case OutsideWord:
|
if (begin > end)
|
||||||
|
begin = i;
|
||||||
|
|
||||||
if (is_alpha_or_digit) {
|
end = next(i);
|
||||||
|
}
|
||||||
|
|
||||||
state = InsideWord;
|
if (!is_word_char || next(i) == str.end()) {
|
||||||
begin = i;
|
|
||||||
end = next(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
if (begin < end) {
|
||||||
|
|
||||||
case InsideWord:
|
string word;
|
||||||
|
word.reserve(distance(begin, end));
|
||||||
|
transform(begin, end, back_inserter(word),
|
||||||
|
[=](char c){return tolower(c, loc);});
|
||||||
|
|
||||||
if (is_alpha_or_digit) {
|
result[word]++;
|
||||||
|
|
||||||
end = next(i);
|
begin = str.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_word_char || next(i) == str.end()) {
|
|
||||||
|
|
||||||
state = OutsideWord;
|
|
||||||
|
|
||||||
if (begin != end) {
|
|
||||||
|
|
||||||
string word;
|
|
||||||
word.reserve(distance(begin, end));
|
|
||||||
transform(begin, end, back_inserter(word),
|
|
||||||
[=](char c){return tolower(c, loc);});
|
|
||||||
|
|
||||||
result[word]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue