Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #include "ElemHideBase.h" | |
2 #include "ElemHideFilter.h" | |
3 #include "ElemHideException.h" | |
4 #include "tools.h" | |
5 | |
6 class Scanner | |
Felix Dahlke
2016/01/15 17:00:36
Please wrap global definitions in an unnamed names
Wladimir Palant
2016/01/15 20:36:15
Done.
| |
7 { | |
8 private: | |
9 const std::u16string& str; | |
10 size_t pos; | |
11 size_t end; | |
12 public: | |
13 Scanner(const std::u16string& str) : str(str), pos(0), end(str.length()) {} | |
14 | |
15 bool done() | |
16 { | |
17 return pos >= end; | |
18 } | |
19 | |
20 size_t position() | |
21 { | |
22 return pos - 1; | |
23 } | |
24 | |
25 char16_t next() | |
26 { | |
27 return done() ? u'\0' : str[pos++]; | |
28 } | |
29 }; | |
30 | |
31 ElemHideBase::ElemHideBase(const std::u16string& text, size_t domainsEnd, size_t selectorStart) | |
32 : ActiveFilter(text) | |
33 { | |
34 } | |
35 | |
36 Filter::Type ElemHideBase::Parse(const std::u16string& text, size_t* domainsEnd, size_t* selectorStart) | |
37 { | |
38 Scanner scanner(text); | |
39 | |
40 // Domains part | |
41 loop: | |
42 while (!scanner.done()) | |
43 { | |
44 char16_t next = scanner.next(); | |
45 if (next == u'#') | |
46 { | |
47 *domainsEnd = scanner.position(); | |
48 break; | |
49 } | |
50 | |
51 switch (next) | |
52 { | |
53 case u'/': | |
54 case u'*': | |
55 case u'|': | |
56 case u'@': | |
57 case u'"': | |
58 case u'!': | |
59 return Type::UNKNOWN; | |
60 } | |
61 } | |
62 | |
63 bool exception = false; | |
64 char16_t next = scanner.next(); | |
65 if (next == u'@') | |
66 { | |
67 exception = true; | |
68 next = scanner.next(); | |
69 } | |
70 | |
71 if (next != u'#') | |
72 return Type::UNKNOWN; | |
73 | |
74 // Selector part | |
75 | |
76 // Selector shouldn't be empty | |
77 if (scanner.done()) | |
78 return Type::UNKNOWN; | |
79 | |
80 *selectorStart = scanner.position() + 1; | |
81 while (!scanner.done()) | |
82 { | |
83 switch (scanner.next()) | |
84 { | |
85 case u'{': | |
86 case u'}': | |
87 return Type::UNKNOWN; | |
88 } | |
89 } | |
90 | |
91 return exception ? Type::ELEMHIDEEXCEPTION : Type::ELEMHIDE; | |
92 } | |
93 | |
94 ElemHideBase* ElemHideBase::Create(const std::u16string& text) | |
95 { | |
96 size_t domainsEnd; | |
97 size_t selectorStart; | |
98 Type type = Parse(text, &domainsEnd, &selectorStart); | |
99 if (type == Type::UNKNOWN) | |
100 return nullptr; | |
101 else if (type == Type::ELEMHIDEEXCEPTION) | |
102 return new ElemHideException(text, domainsEnd, selectorStart); | |
103 else | |
104 return new ElemHideFilter(text, domainsEnd, selectorStart); | |
105 } | |
OLD | NEW |