From 6f5bbd146010697c76c41f9696dcafc43eff5827 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Mon, 22 Feb 2016 00:33:18 +0300 Subject: [PATCH] word count: state enum is removed --- cpp/word-count/word_count.cpp | 54 +++++++++++------------------------ 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/cpp/word-count/word_count.cpp b/cpp/word-count/word_count.cpp index 47ca382..db3c1a9 100644 --- a/cpp/word-count/word_count.cpp +++ b/cpp/word-count/word_count.cpp @@ -12,58 +12,36 @@ Words words(const string &str, const locale &loc) { Words result; - enum { - - InsideWord, - OutsideWord - - } state(OutsideWord); - - string::const_iterator begin, end, i; - i = begin = end = str.begin(); + 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 == '\''; - switch (state) { + if (is_alpha_or_digit) { - case OutsideWord: + if (begin > end) + begin = i; - if (is_alpha_or_digit) { + end = next(i); + } - state = InsideWord; - begin = i; - end = next(i); - } + if (!is_word_char || next(i) == str.end()) { - 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); - } - - 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; + begin = str.end(); + } } }