1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #ifndef Cydia_RegEx_HPP
23 #define Cydia_RegEx_HPP
25 #include <unicode/uregex.h>
27 #include "CyteKit/UCPlatform.h"
28 #include "CyteKit/stringWithUTF8Bytes.h"
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)); \
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)); \
50 URegularExpression *regex_;
61 RegEx(const char *regex) :
65 this->operator =(regex);
68 template <typename Type_>
69 RegEx(const char *regex, const Type_ &data) :
72 this->operator ()(data);
75 void operator =(const char *regex) {
76 _assert(regex_ == NULL);
78 regex_ = _rgxcall(uregex_openC, regex, 0, &error);
79 capture_ = _rgxcall(uregex_groupCount, regex_);
86 NSString *operator [](size_t match) const {
90 size = _rgxcall(uregex_group, regex_, match, data, size);
91 return [[[NSString alloc] initWithBytes:data length:(size * sizeof(UChar)) encoding:NSUTF16LittleEndianStringEncoding] autorelease];
94 _finline bool operator ()(NSString *string) {
95 return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]), [string length]);
98 _finline bool operator ()(const char *data) {
99 return operator ()([NSString stringWithUTF8String:data]);
102 bool operator ()(const UChar *data, size_t size) {
104 _rgxcallv(uregex_setText, regex_, data, size);
106 if (_rgxcall(uregex_matches, regex_, -1)) {
110 size_ = _not(size_t);
115 bool operator ()(const char *data, size_t size) {
116 return operator ()([[[NSString alloc] initWithBytes:data length:size encoding:NSUTF8StringEncoding] autorelease]);
119 operator bool() const {
120 return size_ != _not(size_t);
123 NSString *operator ->*(NSString *format) const {
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];
131 #endif//Cydia_RegEx_HPP