| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <cwctype> | 18 #include <cwctype> |
| 19 #include <mutex> | |
| 19 #include <regex> | 20 #include <regex> |
| 20 | 21 |
| 21 #include "String.h" | 22 #include "String.h" |
| 23 #include "Utils.h" | |
| 24 #include "debug.h" | |
| 22 #include "library.h" | 25 #include "library.h" |
| 23 | 26 |
| 24 | 27 |
| 25 char16_t CharToLower(char16_t charCode) | 28 char16_t CharToLower(char16_t charCode) |
| 26 { | 29 { |
| 27 return std::towlower(charCode); | 30 return std::towlower(charCode); |
| 28 } | 31 } |
| 29 | 32 |
| 30 | 33 |
| 31 void JSNotifyFilterChange(FilterNotifier::Topic topic, Filter& filter, | 34 void JSNotifyFilterChange(FilterNotifier::Topic topic, Filter& filter, |
| 32 Subscription* subscription, unsigned int position) | 35 Subscription* subscription, unsigned int position) |
| 33 { | 36 { |
|
sergei
2018/01/11 08:54:07
stubs are OK for present.
| |
| 34 } | 37 } |
| 35 | 38 |
| 36 void JSNotifySubscriptionChange(FilterNotifier::Topic topic, | 39 void JSNotifySubscriptionChange(FilterNotifier::Topic topic, |
| 37 Subscription& subscription) | 40 Subscription& subscription) |
| 38 { | 41 { |
| 39 } | 42 } |
| 40 | 43 |
| 41 namespace { | 44 namespace { |
| 42 std::vector<std::unique_ptr<std::wregex>> mRegexPool; | 45 std::vector<std::unique_ptr<std::wregex>> regexPool; |
|
sergei
2018/01/11 08:54:06
BTW, this code is not thread safe, we should be ve
hub
2018/01/11 20:41:27
Done.
|
sergei
2018/01/12 09:58:12
I missed it first time, but what is the whole poin
hub
2018/01/12 14:33:53
This is because of the original code and bindings
|
| 43 | 46 std::mutex regexPoolMutex; |
| 44 std::wstring from_string(const String& str) | |
|
sergei
2018/01/11 08:54:07
What about putting this method into some utils.h (
hub
2018/01/11 20:41:27
Done.
| |
| 45 { | |
| 46 std::wstring s; | |
| 47 for (String::size_type idx = 0; idx < str.length(); idx++) | |
| 48 s.push_back(str[idx]); | |
|
sergei
2018/01/11 08:54:07
std::string has the constructor accepting a pointe
hub
2018/01/11 20:41:27
std::wstring::value_type is a wchar_t which is 32-
sergei
2018/01/12 09:58:12
Acknowledged.
| |
| 49 | |
| 50 return s; | |
| 51 } | |
| 52 } | 47 } |
| 53 | 48 |
| 54 int GenerateRegExp(const String& regexp, bool matchCase) | 49 int GenerateRegExp(const String& regexp, bool matchCase) |
| 55 { | 50 { |
| 56 auto index = mRegexPool.size(); | 51 std::lock_guard<std::mutex> guard(regexPoolMutex); |
| 52 auto index = regexPool.size(); | |
| 57 auto flags = std::regex_constants::ECMAScript; | 53 auto flags = std::regex_constants::ECMAScript; |
| 58 if (!matchCase) | 54 if (!matchCase) |
| 59 flags |= std::regex_constants::icase; | 55 flags |= std::regex_constants::icase; |
| 60 mRegexPool.emplace_back(new std::wregex(from_string(regexp), flags)); | 56 regexPool.emplace_back(new std::wregex(StdWStringFromString(regexp), flags)); |
| 61 return index; | 57 return index; |
| 62 } | 58 } |
| 63 | 59 |
| 64 void DeleteRegExp(int id) | 60 void DeleteRegExp(int id) |
| 65 { | 61 { |
| 66 if (id < mRegexPool.size()) | 62 std::lock_guard<std::mutex> guard(regexPoolMutex); |
| 67 mRegexPool[id].reset(); | 63 if (id < regexPool.size()) |
|
sergei
2018/01/11 08:54:07
What about using of std::list and its iterator as
hub
2018/01/11 20:41:27
How do you know if the id is valid? erase() invali
sergei
2018/01/12 09:58:12
IMO there is no necessity in it, as soon as the re
| |
| 64 regexPool[id].reset(); | |
| 68 } | 65 } |
| 69 | 66 |
| 70 bool TestRegExp(int id, const String& str) | 67 bool TestRegExp(int id, const String& str) |
| 71 { | 68 { |
| 72 if ((id < mRegexPool.size()) && mRegexPool[id]) | 69 std::lock_guard<std::mutex> guard(regexPoolMutex); |
| 73 return std::regex_match(from_string(str), *mRegexPool[id]); | 70 if ((id < regexPool.size()) && regexPool[id]) |
| 71 return std::regex_match(StdWStringFromString(str), *regexPool[id]); | |
| 74 | 72 |
| 73 assert2(id < regexPool.size() && regexPool[id], "Invalid RegExp index."); | |
| 75 return false; | 74 return false; |
|
sergei
2018/01/11 08:54:07
Could please add an assert here because it should
hub
2018/01/11 20:41:27
Done.
| |
| 76 } | 75 } |
| 77 | 76 |
| LEFT | RIGHT |