Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/ElemHideBase.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Now passing all filter matching tests (without filter options) Created Jan. 18, 2016, 6:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "ElemHideBase.h"
2 #include "ElemHideFilter.h"
3 #include "ElemHideException.h"
4
5 namespace
6 {
7 class Scanner
8 {
9 private:
10 const std::u16string& str;
11 size_t pos;
12 size_t end;
13 public:
14 Scanner(const std::u16string& str) : str(str), pos(0), end(str.length()) {}
15
16 bool done()
17 {
18 return pos >= end;
19 }
20
21 size_t position()
22 {
23 return pos - 1;
24 }
25
26 char16_t next()
27 {
28 return done() ? u'\0' : str[pos++];
29 }
30 };
31 }
32
33 ElemHideBase::ElemHideBase(const std::u16string& text, size_t domainsEnd,
34 size_t selectorStart)
35 : ActiveFilter(text)
36 {
37 }
38
39 Filter::Type ElemHideBase::Parse(const std::u16string& text, size_t* domainsEnd,
40 size_t* selectorStart)
41 {
42 Scanner scanner(text);
43
44 // Domains part
45 loop:
46 while (!scanner.done())
47 {
48 char16_t next = scanner.next();
49 if (next == u'#')
50 {
51 *domainsEnd = scanner.position();
52 break;
53 }
54
55 switch (next)
56 {
57 case u'/':
58 case u'*':
59 case u'|':
60 case u'@':
61 case u'"':
62 case u'!':
63 return Type::UNKNOWN;
64 }
65 }
66
67 bool exception = false;
68 char16_t next = scanner.next();
69 if (next == u'@')
70 {
71 exception = true;
72 next = scanner.next();
73 }
74
75 if (next != u'#')
76 return Type::UNKNOWN;
77
78 // Selector part
79
80 // Selector shouldn't be empty
81 if (scanner.done())
82 return Type::UNKNOWN;
83
84 *selectorStart = scanner.position() + 1;
85 while (!scanner.done())
86 {
87 switch (scanner.next())
88 {
89 case u'{':
90 case u'}':
91 return Type::UNKNOWN;
92 }
93 }
94
95 return exception ? Type::ELEMHIDEEXCEPTION : Type::ELEMHIDE;
96 }
97
98 ElemHideBase* ElemHideBase::Create(const std::u16string& text)
99 {
100 size_t domainsEnd;
101 size_t selectorStart;
102 Type type = Parse(text, &domainsEnd, &selectorStart);
103 if (type == Type::UNKNOWN)
104 return nullptr;
105 else if (type == Type::ELEMHIDEEXCEPTION)
106 return new ElemHideException(text, domainsEnd, selectorStart);
107 else
108 return new ElemHideFilter(text, domainsEnd, selectorStart);
109 }
OLDNEW
« no previous file with comments | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld