OLD | NEW |
(Empty) | |
| 1 #include <emscripten.h> |
| 2 |
| 3 #include "RegExpFilter.h" |
| 4 #include "WhiteListFilter.h" |
| 5 #include "InvalidFilter.h" |
| 6 |
| 7 RegExpFilter::RegExpFilter(const std::u16string& text, |
| 8 const std::u16string& pattern, const std::u16string& options) |
| 9 : ActiveFilter(text), regexpId(0) |
| 10 { |
| 11 size_t len = pattern.length(); |
| 12 if (len >= 2 && pattern[0] == u'/' && pattern[len - 1] == u'/') |
| 13 { |
| 14 std::u16string param = pattern.substr(1, len - 2); |
| 15 regexpId = EM_ASM_INT(return regexps.create($0, $1), ¶m, false); |
| 16 |
| 17 std::u16string* error = reinterpret_cast<std::u16string*>(EM_ASM_INT(return
regexps.getError($0), regexpId)); |
| 18 if (error) |
| 19 { |
| 20 EM_ASM_ARGS(regexps.delete($0), regexpId); |
| 21 throw std::u16string(*error); |
| 22 } |
| 23 } |
| 24 else |
| 25 regexpSource = pattern; |
| 26 } |
| 27 |
| 28 RegExpFilter::~RegExpFilter() |
| 29 { |
| 30 if (regexpId) |
| 31 EM_ASM_ARGS(regexps.delete($0), regexpId); |
| 32 } |
| 33 |
| 34 Filter* RegExpFilter::Create(const std::u16string& text) |
| 35 { |
| 36 bool blocking = true; |
| 37 size_t patternStart = 0; |
| 38 if (!text.compare(0, 2, u"@@")) |
| 39 { |
| 40 blocking = false; |
| 41 patternStart = 2; |
| 42 } |
| 43 |
| 44 size_t patternEnd = text.find(u'$', patternStart); |
| 45 size_t patternLength = (patternEnd != std::u16string::npos ? |
| 46 patternEnd - patternStart : patternEnd); |
| 47 std::u16string pattern(text.substr(patternStart, patternLength)); |
| 48 std::u16string options(patternEnd != std::u16string::npos ? |
| 49 text.substr(patternEnd) : u""); |
| 50 |
| 51 try |
| 52 { |
| 53 if (blocking) |
| 54 return new RegExpFilter(text, pattern, options); |
| 55 else |
| 56 return new WhiteListFilter(text, pattern, options); |
| 57 } |
| 58 catch (const std::u16string& reason) |
| 59 { |
| 60 return new InvalidFilter(text, reason); |
| 61 } |
| 62 } |
| 63 |
| 64 Filter::Type RegExpFilter::GetType() const |
| 65 { |
| 66 return Type::BLOCKING; |
| 67 } |
| 68 |
| 69 bool RegExpFilter::Matches(const std::u16string& location) |
| 70 { |
| 71 return EM_ASM_INT(return regexps.test($0, $1), regexpId, &location); |
| 72 } |
OLD | NEW |