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: Created Jan. 14, 2016, 4:40 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 <emscripten.h>
2 #include <unordered_map>
3
4 #include "Filter.h"
5 #include "CommentFilter.h"
6 #include "RegExpFilter.h"
7 #include "ElemHideFilter.h"
8 #include "ElemHideException.h"
9 #include "tools.h"
10
11 Filter::Filter(const std::u16string& text)
12 {
13 this->text = text;
14 }
15
16 const std::u16string Filter::Serialize()
17 {
18 return (
19 u"[Filter]\n"
20 u"text=" + text + u"\n"
21 );
22 }
23
24 namespace
25 {
26 std::unordered_map<std::u16string,Filter*> knownFilters;
27
28 void trim_spaces(std::u16string& str)
29 {
30 size_t pos;
31
32 // Remove leading whitespace
33 pos = str.find_first_not_of(u' ');
34 if (pos > 0)
35 str.erase(0, pos);
36
37 // Remove trailing whitespace
38 pos = str.find_last_not_of(u' ');
39 if (pos < str.length() - 1)
40 str.erase(pos + 1);
41 }
42
43 void remove_spaces(std::u16string& str)
44 {
45 for (size_t i = 0, l = str.length(); i < l; ++i)
46 {
47 if (str[i] == u' ')
48 {
49 str.erase(i, 1);
50 --i;
51 --l;
52 }
53 }
54 }
55 }
56
57 extern "C"
58 {
59 Filter* EMSCRIPTEN_KEEPALIVE Filter_FromText(char16_t* textPtr, int textLen)
60 {
61 std::u16string text(textPtr, textLen);
62 auto it = knownFilters.find(text);
63 if (it != knownFilters.end())
64 return it->second;
65
66 Filter* filter = CommentFilter::Create(text);
67 if (!filter)
68 filter = ElemHideBase::Create(text);
69 if (!filter)
70 filter = RegExpFilter::Create(text);
71 return knownFilters[text] = filter;
72 }
73 char16_t* EMSCRIPTEN_KEEPALIVE Filter_Normalize(char16_t* textPtr, size_t text Len, size_t* resultLen)
74 {
75 std::u16string text(textPtr, textLen);
76
77 // Remove special characters like line breaks
78 for (size_t i = 0, l = text.length(); i < l; ++i)
79 {
80 if (text[i] < u' ')
81 {
82 text.erase(i, 1);
83 --i;
84 --l;
85 }
86 }
87
88 trim_spaces(text);
89
90 {
91 size_t domainsEnd;
92 size_t selectorStart;
93 Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart) ;
94 if (type != Filter::Type::UNKNOWN)
95 {
96 std::u16string domains = text.substr(0, domainsEnd);
97 std::u16string selector = text.substr(selectorStart);
98 remove_spaces(domains);
99 trim_spaces(selector);
100 return stringToBuffer(domains + (type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##") + selector, resultLen);
101 }
102 }
103
104 if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN)
105 remove_spaces(text);
106 return stringToBuffer(text, resultLen);
107 }
108
109 Filter::Type EMSCRIPTEN_KEEPALIVE Filter_get_type(Filter* filter)
110 {
111 return filter->GetType();
112 }
113
114 char16_t* EMSCRIPTEN_KEEPALIVE Filter_Serialize(Filter* filter, size_t* result Len)
115 {
116 return stringToBuffer(filter->Serialize(), resultLen);
117 }
118 }
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