-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.cpp
More file actions
100 lines (82 loc) · 2.37 KB
/
Token.cpp
File metadata and controls
100 lines (82 loc) · 2.37 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* This file is part of MemphisNow.
*
* MemphisNow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MemphisNow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MemphisNow. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Token.hpp"
#include <memory>
#include <boost/tokenizer.hpp>
typedef boost::char_separator<wchar_t> BoostSeparator;
typedef boost::tokenizer<BoostSeparator, std::wstring::const_iterator, std::wstring> BoostTokenizer;
namespace Memphis
{
Token::Token(Token* parent,
const std::wstring& text,
const std::wstring& separators,
bool discard) :
mParent(parent),
mText(text),
mSeparators(separators),
mDiscard(discard)
{
}
Token::~Token()
{
}
void Token::Split()
{
if (mSeparators.empty())
return;
// setup boost tokenizer
BoostSeparator separ(mSeparators.c_str());
BoostTokenizer tokenizer(mText, separ);
// cleanup previous token
mSubTokens.clear();
// does it have only one token?
BoostTokenizer::iterator iter = tokenizer.begin();
for (; iter != tokenizer.end(); ) {
bool equals = (mText == std::wstring(*iter));
++iter;
if (iter == tokenizer.end() && equals) {
return; // no subtokens, bail out
}
else
break; // has more than one token, add them
}
// create sub-tokens
iter = tokenizer.begin();
int count = 0;
for (; iter != tokenizer.end(); ++iter) {
SharedPtrToken subtoken (new Token(this, *iter, L"", mDiscard));
mSubTokens.push_back (subtoken);
++count;
}
}
bool Token::IsSubtoken(const Token* token) const
{
SharedTokensContainer::const_iterator iter = mSubTokens.cbegin();
for (; iter != mSubTokens.end(); ++iter)
{
if (iter->get() == token)
{
return true;
}
if ((*iter)->CountSubtokens() > 0)
{
// TBD: iterate subtokens
}
}
return false;
}
}