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

Side by Side Diff: compiled/Filter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Back to manual approach for API Created Jan. 18, 2016, 12:41 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/Filter.h ('k') | compiled/InvalidFilter.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 <unordered_map>
2
3 #include "Filter.h"
4 #include "CommentFilter.h"
5 #include "RegExpFilter.h"
6 #include "ElemHideFilter.h"
7 #include "ElemHideException.h"
8
9 namespace
10 {
11 std::unordered_map<std::u16string,FilterPtr> knownFilters;
12
13 void trim_spaces(std::u16string& str)
14 {
15 size_t pos;
16
17 // Remove leading whitespace
18 pos = str.find_first_not_of(u' ');
19 if (pos > 0)
20 str.erase(0, pos);
21
22 // Remove trailing whitespace
23 pos = str.find_last_not_of(u' ');
24 if (pos < str.length() - 1)
25 str.erase(pos + 1);
26 }
27
28 void remove_spaces(std::u16string& str)
29 {
30 for (size_t i = 0, l = str.length(); i < l; ++i)
31 {
32 if (str[i] == u' ')
33 {
34 str.erase(i, 1);
35 --i;
36 --l;
37 }
38 }
39 }
40 }
41
42 Filter::Filter(const std::u16string& text)
43 {
44 this->text = text;
45 }
46
47 const std::u16string Filter::Serialize()
48 {
49 return (
50 u"[Filter]\n"
51 u"text=" + text + u"\n"
52 );
53 }
54
55 FilterPtr Filter::FromText(const std::u16string& text)
56 {
57 auto it = knownFilters.find(text);
58 if (it != knownFilters.end())
59 return it->second;
60
61 FilterPtr filter = FilterPtr(CommentFilter::Create(text));
62 if (!filter)
63 filter.reset(ElemHideBase::Create(text));
64 if (!filter)
65 filter.reset(RegExpFilter::Create(text));
66 return knownFilters[text] = filter;
67 }
68
69 const std::u16string Filter::Normalize(const std::u16string& text)
70 {
71 std::u16string result(text);
72
73 // Remove special characters like line breaks
74 for (size_t i = 0, l = result.length(); i < l; ++i)
75 {
76 if (result[i] < u' ')
77 {
78 result.erase(i, 1);
79 --i;
80 --l;
81 }
82 }
83
84 trim_spaces(result);
85
86 {
87 size_t domainsEnd;
88 size_t selectorStart;
89 Filter::Type type = ElemHideBase::Parse(result, &domainsEnd, &selectorStart) ;
90 if (type != Filter::Type::UNKNOWN)
91 {
92 std::u16string domains = result.substr(0, domainsEnd);
93 std::u16string selector = result.substr(selectorStart);
94 remove_spaces(domains);
95 trim_spaces(selector);
96 return domains + (
97 type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##"
98 ) + selector;
99 }
100 }
101
102 if (CommentFilter::Parse(result) == Filter::Type::UNKNOWN)
103 remove_spaces(result);
104 return result;
105 }
OLDNEW
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld