This repository was archived by the owner on Dec 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.cpp
More file actions
64 lines (52 loc) · 1.83 KB
/
rule.cpp
File metadata and controls
64 lines (52 loc) · 1.83 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
#include "rule.h"
#include <cctype>
static bool isWordCharacter(int ch) {
return std::isalnum(ch) || ch == '_';
}
static bool isLineBreakCharacter(int ch) {
return ch == '\r' || ch == '\n';
}
static bool isWordBoundary(const std::string &text, size_t pos) {
bool left = pos != 0 && isWordCharacter(text[pos - 1]);
bool right = pos != text.length() && isWordCharacter(text[pos]);
return left != right;
}
int Rule::match(const std::string &text, int pos) const {
if (type == EPSILON)
return 0;
if (type == ANCHOR)
return anchorMatches(text, pos) ? 0 : -1;
return pos != (int)text.length() && matches(text[pos]) ? 1 : -1;
}
bool Rule::matches(unsigned char ch) const {
if (type == NORMAL)
return ch == by.front();
if (type == RANGE)
return ch >= by.front() && ch <= to.front();
if (type == SPECIAL) {
switch (by.front()) {
case '*': return true;
case '.': return !isLineBreakCharacter(ch);
case 'd': return std::isdigit(ch);
case 's': return std::isspace(ch);
case 'w': return isWordCharacter(ch);
case 'D': return !std::isdigit(ch);
case 'S': return !std::isspace(ch);
case 'W': return !isWordCharacter(ch);
}
}
return false;
}
bool Rule::anchorMatches(const std::string &text, int pos) const {
if (type == ANCHOR) {
switch (by.front()) {
case '^': return pos == 0;
case 'A': return pos == 0 || isLineBreakCharacter(text[pos - 1]);
case '$': return pos == (int)text.length();
case 'Z': return pos == (int)text.length() || isLineBreakCharacter(text[pos]);
case 'b': return isWordBoundary(text, pos);
case 'B': return !isWordBoundary(text, pos);
}
}
return false;
}