OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 22 matching lines...) Expand all Loading... |
33 { | 33 { |
34 StringMap<Filter*> knownFilters(8192); | 34 StringMap<Filter*> knownFilters(8192); |
35 | 35 |
36 void NormalizeWhitespace(DependentString& text) | 36 void NormalizeWhitespace(DependentString& text) |
37 { | 37 { |
38 String::size_type start = 0; | 38 String::size_type start = 0; |
39 String::size_type end = text.length(); | 39 String::size_type end = text.length(); |
40 | 40 |
41 // Remove leading spaces and special characters like line breaks | 41 // Remove leading spaces and special characters like line breaks |
42 for (; start < end; start++) | 42 for (; start < end; start++) |
43 if (text[start] > ' ') | 43 if (text[start] > ABP_TEXT(' ')) |
44 break; | 44 break; |
45 | 45 |
46 // Now look for invalid characters inside the string | 46 // Now look for invalid characters inside the string |
47 String::size_type pos; | 47 String::size_type pos; |
48 for (pos = start; pos < end; pos++) | 48 for (pos = start; pos < end; pos++) |
49 if (text[pos] < ' ') | 49 if (text[pos] < ABP_TEXT(' ')) |
50 break; | 50 break; |
51 | 51 |
52 if (pos < end) | 52 if (pos < end) |
53 { | 53 { |
54 // Found invalid characters, copy all the valid characters while skipping | 54 // Found invalid characters, copy all the valid characters while skipping |
55 // the invalid ones. | 55 // the invalid ones. |
56 String::size_type delta = 1; | 56 String::size_type delta = 1; |
57 for (pos = pos + 1; pos < end; pos++) | 57 for (pos = pos + 1; pos < end; pos++) |
58 { | 58 { |
59 if (text[pos] < ' ') | 59 if (text[pos] < ABP_TEXT(' ')) |
60 delta++; | 60 delta++; |
61 else | 61 else |
62 text[pos - delta] = text[pos]; | 62 text[pos - delta] = text[pos]; |
63 } | 63 } |
64 end -= delta; | 64 end -= delta; |
65 } | 65 } |
66 | 66 |
67 // Remove trailing spaces | 67 // Remove trailing spaces |
68 for (; end > 0; end--) | 68 for (; end > 0; end--) |
69 if (text[end - 1] != ' ') | 69 if (text[end - 1] != ABP_TEXT(' ')) |
70 break; | 70 break; |
71 | 71 |
72 // Set new string boundaries | 72 // Set new string boundaries |
73 text.reset(text, start, end - start); | 73 text.reset(text, start, end - start); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 Filter::Filter(Type type, const String& text) | 77 Filter::Filter(Type type, const String& text) |
78 : mText(text), mType(type) | 78 : mText(text), mType(type) |
79 { | 79 { |
80 annotate_address(this, "Filter"); | 80 annotate_address(this, "Filter"); |
81 } | 81 } |
82 | 82 |
83 Filter::~Filter() | 83 Filter::~Filter() |
84 { | 84 { |
85 knownFilters.erase(mText); | 85 knownFilters.erase(mText); |
86 } | 86 } |
87 | 87 |
88 OwnedString Filter::Serialize() const | 88 OwnedString Filter::Serialize() const |
89 { | 89 { |
90 OwnedString result(u"[Filter]\ntext="_str); | 90 OwnedString result(ABP_TEXT("[Filter]\ntext="_str)); |
91 result.append(mText); | 91 result.append(mText); |
92 result.append(u'\n'); | 92 result.append(ABP_TEXT('\n')); |
93 return result; | 93 return result; |
94 } | 94 } |
95 | 95 |
96 Filter* Filter::FromText(DependentString& text) | 96 Filter* Filter::FromText(DependentString& text) |
97 { | 97 { |
98 NormalizeWhitespace(text); | 98 NormalizeWhitespace(text); |
99 if (text.empty()) | 99 if (text.empty()) |
100 return nullptr; | 100 return nullptr; |
101 | 101 |
102 // Parsing also normalizes the filter text, so it has to be done before the | 102 // Parsing also normalizes the filter text, so it has to be done before the |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 break; | 146 break; |
147 case ElemHideFilter::classType: | 147 case ElemHideFilter::classType: |
148 filter = FilterPtr(new ElemHideFilter(text, data.elemhide), false); | 148 filter = FilterPtr(new ElemHideFilter(text, data.elemhide), false); |
149 break; | 149 break; |
150 case ElemHideException::classType: | 150 case ElemHideException::classType: |
151 filter = FilterPtr(new ElemHideException(text, data.elemhide), false); | 151 filter = FilterPtr(new ElemHideException(text, data.elemhide), false); |
152 break; | 152 break; |
153 case ElemHideEmulationFilter::classType: | 153 case ElemHideEmulationFilter::classType: |
154 filter = FilterPtr(new ElemHideEmulationFilter(text, data.elemhide), false
); | 154 filter = FilterPtr(new ElemHideEmulationFilter(text, data.elemhide), false
); |
155 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric()) | 155 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric()) |
156 filter = FilterPtr(new InvalidFilter(text, u"filter_elemhideemulation_no
domain"_str), false); | 156 filter = FilterPtr(new InvalidFilter(text, ABP_TEXT("filter_elemhideemul
ation_nodomain"_str)), false); |
157 break; | 157 break; |
158 default: | 158 default: |
159 // This should never happen but just in case | 159 // This should never happen but just in case |
160 return nullptr; | 160 return nullptr; |
161 } | 161 } |
162 | 162 |
163 enter_context("Adding to known filters"); | 163 enter_context("Adding to known filters"); |
164 if (text != filter->mText) | 164 if (text != filter->mText) |
165 knownFilters[filter->mText] = filter.get(); | 165 knownFilters[filter->mText] = filter.get(); |
166 else | 166 else |
167 // This is a hack: we looked up the entry using text but create it using | 167 // This is a hack: we looked up the entry using text but create it using |
168 // filter->mText. This works because both are equal at this point. However, | 168 // filter->mText. This works because both are equal at this point. However, |
169 // text refers to a temporary buffer which will go away. | 169 // text refers to a temporary buffer which will go away. |
170 knownFilter.assign(filter->mText, filter.get()); | 170 knownFilter.assign(filter->mText, filter.get()); |
171 exit_context(); | 171 exit_context(); |
172 | 172 |
173 return filter.release(); | 173 return filter.release(); |
174 } | 174 } |
OLD | NEW |