]>
Commit | Line | Data |
---|---|---|
449ef9d5 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
6fa0bb60 | 2 | * Copyright (C) 2008-2014 Jay Freeman (saurik) |
449ef9d5 JF |
3 | */ |
4 | ||
6d9696a5 | 5 | /* GNU General Public License, Version 3 {{{ */ |
449ef9d5 | 6 | /* |
6d9696a5 JF |
7 | * Cydia is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
449ef9d5 | 11 | * |
6d9696a5 JF |
12 | * Cydia is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
449ef9d5 | 16 | * |
6d9696a5 JF |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with Cydia. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
449ef9d5 JF |
20 | /* }}} */ |
21 | ||
22 | #ifndef Cydia_PerlCompatibleRegEx_HPP | |
23 | #define Cydia_PerlCompatibleRegEx_HPP | |
24 | ||
25 | #include <pcre.h> | |
26 | ||
5c1b89fb | 27 | #include "CyteKit/UCPlatform.h" |
d458596e | 28 | #include "CyteKit/stringWithUTF8Bytes.h" |
449ef9d5 JF |
29 | |
30 | class Pcre { | |
31 | private: | |
32 | pcre *code_; | |
33 | pcre_extra *study_; | |
34 | int capture_; | |
35 | int *matches_; | |
36 | const char *data_; | |
37 | ||
38 | public: | |
39 | Pcre() : | |
40 | code_(NULL), | |
e967efd5 JF |
41 | study_(NULL), |
42 | data_(NULL) | |
449ef9d5 JF |
43 | { |
44 | } | |
45 | ||
e967efd5 | 46 | Pcre(const char *regex, NSString *data = nil) : |
449ef9d5 | 47 | code_(NULL), |
e967efd5 JF |
48 | study_(NULL), |
49 | data_(NULL) | |
449ef9d5 JF |
50 | { |
51 | this->operator =(regex); | |
e967efd5 JF |
52 | |
53 | if (data != nil) | |
54 | this->operator ()(data); | |
449ef9d5 JF |
55 | } |
56 | ||
57 | void operator =(const char *regex) { | |
58 | _assert(code_ == NULL); | |
59 | ||
60 | const char *error; | |
61 | int offset; | |
62 | code_ = pcre_compile(regex, 0, &error, &offset, NULL); | |
63 | ||
64 | if (code_ == NULL) { | |
65 | fprintf(stderr, "%d:%s\n", offset, error); | |
66 | _assert(false); | |
67 | } | |
68 | ||
69 | pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_); | |
e85bcceb | 70 | _assert(capture_ >= 0); |
449ef9d5 JF |
71 | matches_ = new int[(capture_ + 1) * 3]; |
72 | } | |
73 | ||
74 | ~Pcre() { | |
75 | pcre_free(code_); | |
76 | delete matches_; | |
77 | } | |
78 | ||
79 | NSString *operator [](size_t match) const { | |
80 | return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])]; | |
81 | } | |
82 | ||
83 | _finline bool operator ()(NSString *data) { | |
84 | // XXX: length is for characters, not for bytes | |
85 | return operator ()([data UTF8String], [data length]); | |
86 | } | |
87 | ||
88 | _finline bool operator ()(const char *data) { | |
89 | return operator ()(data, strlen(data)); | |
90 | } | |
91 | ||
92 | bool operator ()(const char *data, size_t size) { | |
e967efd5 JF |
93 | if (pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0) { |
94 | data_ = data; | |
95 | return true; | |
96 | } else { | |
97 | data_ = NULL; | |
98 | return false; | |
99 | } | |
100 | } | |
101 | ||
102 | operator bool() const { | |
103 | return data_ != NULL; | |
449ef9d5 JF |
104 | } |
105 | ||
106 | NSString *operator ->*(NSString *format) const { | |
107 | id values[capture_]; | |
108 | for (int i(0); i != capture_; ++i) | |
109 | values[i] = this->operator [](i + 1); | |
110 | ||
111 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; | |
112 | } | |
113 | }; | |
114 | ||
115 | #endif//Cydia_PerlCompatibleRegEx_HPP |