]>
Commit | Line | Data |
---|---|---|
b4fd1197 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
4c66fad9 | 2 | * Copyright (C) 2008-2015 Jay Freeman (saurik) |
b4fd1197 JF |
3 | */ |
4 | ||
5 | /* GNU General Public License, Version 3 {{{ */ | |
6 | /* | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | **/ | |
20 | /* }}} */ | |
21 | ||
22 | #ifndef Cydia_RegEx_HPP | |
23 | #define Cydia_RegEx_HPP | |
24 | ||
25 | #include <unicode/uregex.h> | |
26 | ||
27 | #include "CyteKit/UCPlatform.h" | |
28 | #include "CyteKit/stringWithUTF8Bytes.h" | |
29 | ||
30 | #define _rgxcall(code, args...) ({ \ | |
31 | UErrorCode status(U_ZERO_ERROR); \ | |
32 | auto _value(code(args, &status)); \ | |
33 | if (U_FAILURE(status)) { \ | |
34 | fprintf(stderr, "%d:%s\n", error.offset, u_errorName(status)); \ | |
35 | _assert(false); \ | |
36 | } \ | |
37 | _value; }) | |
38 | ||
39 | #define _rgxcallv(code, args...) ({ \ | |
40 | UErrorCode status(U_ZERO_ERROR); \ | |
41 | code(args, &status); \ | |
42 | if (U_FAILURE(status)) { \ | |
43 | fprintf(stderr, "%d:%s\n", error.offset, u_errorName(status)); \ | |
44 | _assert(false); \ | |
45 | } \ | |
46 | }) | |
47 | ||
48 | class RegEx { | |
49 | private: | |
50 | URegularExpression *regex_; | |
51 | int capture_; | |
52 | size_t size_; | |
53 | ||
54 | public: | |
55 | RegEx() : | |
56 | regex_(NULL), | |
57 | size_(_not(size_t)) | |
58 | { | |
59 | } | |
60 | ||
f4f6714a | 61 | RegEx(const char *regex) : |
b4fd1197 JF |
62 | regex_(NULL), |
63 | size_(_not(size_t)) | |
64 | { | |
65 | this->operator =(regex); | |
f4f6714a | 66 | } |
b4fd1197 | 67 | |
f4f6714a JF |
68 | template <typename Type_> |
69 | RegEx(const char *regex, const Type_ &data) : | |
70 | RegEx(regex) | |
71 | { | |
72 | this->operator ()(data); | |
b4fd1197 JF |
73 | } |
74 | ||
75 | void operator =(const char *regex) { | |
76 | _assert(regex_ == NULL); | |
77 | UParseError error; | |
78 | regex_ = _rgxcall(uregex_openC, regex, 0, &error); | |
79 | capture_ = _rgxcall(uregex_groupCount, regex_); | |
80 | } | |
81 | ||
82 | ~RegEx() { | |
83 | uregex_close(regex_); | |
84 | } | |
85 | ||
86 | NSString *operator [](size_t match) const { | |
87 | UParseError error; | |
88 | size_t size(size_); | |
89 | UChar data[size]; | |
90 | size = _rgxcall(uregex_group, regex_, match, data, size); | |
91 | return [[[NSString alloc] initWithBytes:data length:(size * sizeof(UChar)) encoding:NSUTF16LittleEndianStringEncoding] autorelease]; | |
92 | } | |
93 | ||
94 | _finline bool operator ()(NSString *string) { | |
95 | return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]), [string length]); | |
96 | } | |
97 | ||
98 | _finline bool operator ()(const char *data) { | |
99 | return operator ()([NSString stringWithUTF8String:data]); | |
100 | } | |
101 | ||
102 | bool operator ()(const UChar *data, size_t size) { | |
103 | UParseError error; | |
104 | _rgxcallv(uregex_setText, regex_, data, size); | |
105 | ||
106 | if (_rgxcall(uregex_matches, regex_, -1)) { | |
107 | size_ = size; | |
108 | return true; | |
109 | } else { | |
110 | size_ = _not(size_t); | |
111 | return false; | |
112 | } | |
113 | } | |
114 | ||
115 | bool operator ()(const char *data, size_t size) { | |
116 | return operator ()([[[NSString alloc] initWithBytes:data length:size encoding:NSUTF8StringEncoding] autorelease]); | |
117 | } | |
118 | ||
119 | operator bool() const { | |
120 | return size_ != _not(size_t); | |
121 | } | |
122 | ||
123 | NSString *operator ->*(NSString *format) const { | |
124 | id values[capture_]; | |
125 | for (int i(0); i != capture_; ++i) | |
126 | values[i] = this->operator [](i + 1); | |
127 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; | |
128 | } | |
129 | }; | |
130 | ||
131 | #endif//Cydia_RegEx_HPP |