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
101
102
103
104
105
106
107
108
109
110
111
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "specialtokenregistry.h"
namespace {
class QueryVisitor : public IQueryExprVisitor {
private:
juniper::SpecialTokenRegistry & _registry;
public:
QueryVisitor(juniper::SpecialTokenRegistry & registry) : _registry(registry) {}
void VisitQueryNode(QueryNode *) override { }
void RevisitQueryNode(QueryNode *) override { }
void VisitQueryTerm(QueryTerm * t) override {
if (t->isSpecialToken()) {
_registry.addSpecialToken(t);
}
}
};
}
namespace juniper {
SpecialTokenRegistry::CharStream::CharStream(const char * srcBuf, const char * srcEnd,
ucs4_t * dstBuf, ucs4_t * dstEnd) :
_srcBuf(srcBuf),
_srcItr(srcBuf),
_srcEnd(srcEnd),
_nextStart(srcBuf),
_dstBuf(dstBuf),
_dstItr(dstBuf),
_dstEnd(dstEnd),
_isStartWordChar(false)
{
if (srcBuf < srcEnd) {
ucs4_t ch = getNextChar();
_nextStart = _srcItr;
_isStartWordChar = Fast_UnicodeUtil::IsWordChar(ch);
reset();
}
}
bool
SpecialTokenRegistry::CharStream::resetAndInc()
{
_srcItr = _nextStart;
if (hasMoreChars()) {
ucs4_t ch = getNextChar();
_isStartWordChar = Fast_UnicodeUtil::IsWordChar(ch);
_srcBuf = _nextStart; // move start to next character
_nextStart = _srcItr; // move next start to the next next character
reset();
return true;
} else {
return false;
}
}
bool
SpecialTokenRegistry::match(const ucs4_t * qsrc, const ucs4_t * qend, CharStream & stream) const
{
for (; (qsrc < qend) && stream.hasMoreChars(); ++qsrc) {
ucs4_t ch = stream.getNextChar();
if (ch != *qsrc) {
return false;
}
}
return (qsrc == qend);
}
SpecialTokenRegistry::SpecialTokenRegistry(QueryExpr * query) :
_specialTokens()
{
QueryVisitor qv(*this);
query->Accept(qv); // find the special tokens
}
const char *
SpecialTokenRegistry::tokenize(const char * buf, const char * bufend,
ucs4_t * dstbuf, ucs4_t * dstbufend,
const char * & origstart, size_t & tokenlen) const
{
CharStream stream(buf, bufend, dstbuf, dstbufend);
bool foundWordChar = false;
while(!foundWordChar && stream.hasMoreChars() && stream.hasMoreSpace()) {
for (size_t i = 0; i < _specialTokens.size(); ++i) {
const ucs4_t * qsrc = _specialTokens[i]->ucs4_term();
const ucs4_t * qend = qsrc + _specialTokens[i]->ucs4_len;
// try to match the given special token with the input stream
if (match(qsrc, qend, stream)) {
origstart = stream.getSrcStart();
tokenlen = stream.getNumChars();
return stream.getSrcItr();
}
stream.reset();
}
foundWordChar = stream.isStartWordChar();
stream.resetAndInc();
}
return NULL;
}
} // namespace juniper
|