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: Fixed some bugs and exposed/tested more properties Created Jan. 15, 2016, 3: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
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 // API
58 extern "C"
59 {
60 Filter* EMSCRIPTEN_KEEPALIVE Filter_FromText(char16_t* textPtr, int textLen)
Felix Dahlke 2016/01/15 17:00:36 If this stays (see my comments on the C wrappers a
61 {
62 std::u16string text(textPtr, textLen);
63 auto it = knownFilters.find(text);
64 if (it != knownFilters.end())
65 return it->second;
66
67 Filter* filter = CommentFilter::Create(text);
68 if (!filter)
69 filter = ElemHideBase::Create(text);
70 if (!filter)
71 filter = RegExpFilter::Create(text);
72 return knownFilters[text] = filter;
73 }
74
75 char16_t* EMSCRIPTEN_KEEPALIVE Filter_Normalize(char16_t* textPtr, size_t text Len, size_t* resultLen)
76 {
77 std::u16string text(textPtr, textLen);
78
79 // Remove special characters like line breaks
80 for (size_t i = 0, l = text.length(); i < l; ++i)
81 {
82 if (text[i] < u' ')
83 {
84 text.erase(i, 1);
85 --i;
86 --l;
87 }
88 }
89
90 trim_spaces(text);
91
92 {
93 size_t domainsEnd;
94 size_t selectorStart;
95 Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart) ;
96 if (type != Filter::Type::UNKNOWN)
97 {
98 std::u16string domains = text.substr(0, domainsEnd);
99 std::u16string selector = text.substr(selectorStart);
100 remove_spaces(domains);
101 trim_spaces(selector);
102 return stringToBuffer(domains + (type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##") + selector, resultLen);
103 }
104 }
105
106 if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN)
107 remove_spaces(text);
108 return stringToBuffer(text, resultLen);
109 }
110
111 char16_t* EMSCRIPTEN_KEEPALIVE Filter_Serialize(Filter* filter, size_t* result Len)
112 {
113 return stringToBuffer(filter->Serialize(), resultLen);
114 }
115
116 EXPOSE_READONLY_STRPROPERTY(Filter, text)
117 EXPOSE_READONLY_PROPERTY(Filter, Filter::Type, type)
118 }
OLDNEW
« compiled/ElemHideBase.cpp ('K') | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld