51 lines
973 B
C++
51 lines
973 B
C++
#include "word_count.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
namespace word_count {
|
|
|
|
using namespace std;
|
|
|
|
|
|
Words words(const string &str, const locale &loc)
|
|
{
|
|
Words result;
|
|
|
|
string::const_iterator begin, end, i;
|
|
i = end = str.begin();
|
|
begin = str.end();
|
|
|
|
for (; i != str.end(); ++i) {
|
|
|
|
bool is_alpha_or_digit = isalpha(*i, loc) || isdigit(*i, loc);
|
|
bool is_word_char = is_alpha_or_digit || *i == '\'';
|
|
|
|
if (is_alpha_or_digit) {
|
|
|
|
if (begin > end)
|
|
begin = i;
|
|
|
|
end = next(i);
|
|
}
|
|
|
|
if (!is_word_char || next(i) == str.end()) {
|
|
|
|
if (begin < end) {
|
|
|
|
string word;
|
|
word.reserve(distance(begin, end));
|
|
transform(begin, end, back_inserter(word),
|
|
[=](char c){return tolower(c, loc);});
|
|
|
|
result[word]++;
|
|
|
|
begin = str.end();
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|