-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestWordAccumulator.cpp
63 lines (54 loc) · 1.41 KB
/
LongestWordAccumulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "LongestWordAccumulator.h"
#include <cctype>
namespace longest_word {
namespace {
bool IsWordCharacter(char character)
{
if (character == '-') {
return true;
}
if (character == '\'') {
return true;
}
if (isalpha(character) != 0) {
return true;
}
return false;
}
}
LongestWordAccumulator::LongestWordAccumulator()
: mIndex(0U)
, mCurrentWordStart(0U)
, mCurrentWordSize(0U)
, mLongestWordStart(0U)
, mLongestWordSize(0U)
{
}
LongestWordAccumulator operator+(LongestWordAccumulator accumulator, char character)
{
if (IsWordCharacter(character)) {
const bool foundNewWord = accumulator.mCurrentWordSize == 0;
if (foundNewWord) {
accumulator.mCurrentWordStart = accumulator.mIndex;
}
++accumulator.mCurrentWordSize;
} else {
accumulator.mCurrentWordSize = 0;
}
const bool foundNewLongestWord = accumulator.mCurrentWordSize > accumulator.mLongestWordSize;
if (foundNewLongestWord) {
accumulator.mLongestWordSize = accumulator.mCurrentWordSize;
accumulator.mLongestWordStart = accumulator.mCurrentWordStart;
}
++accumulator.mIndex;
return accumulator;
}
size_t LongestWordAccumulator::Start() const
{
return mLongestWordStart;
}
size_t LongestWordAccumulator::Size() const
{
return mLongestWordSize;
}
}