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, NSString *data = nil) :
65 this->operator =(regex);
68 this->operator ()(data);
71 void operator =(const char *regex) {
72 _assert(regex_ == NULL);
74 regex_ = _rgxcall(uregex_openC, regex, 0, &error);
75 capture_ = _rgxcall(uregex_groupCount, regex_);
82 NSString *operator [](size_t match) const {
86 size = _rgxcall(uregex_group, regex_, match, data, size);
87 return [[[NSString alloc] initWithBytes:data length:(size * sizeof(UChar)) encoding:NSUTF16LittleEndianStringEncoding] autorelease];
90 _finline bool operator ()(NSString *string) {
91 return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]), [string length]);
94 _finline bool operator ()(const char *data) {
95 return operator ()([NSString stringWithUTF8String:data]);
98 bool operator ()(const UChar *data, size_t size) {
100 _rgxcallv(uregex_setText, regex_, data, size);
102 if (_rgxcall(uregex_matches, regex_, -1)) {
106 size_ = _not(size_t);
111 bool operator ()(const char *data, size_t size) {
112 return operator ()([[[NSString alloc] initWithBytes:data length:size encoding:NSUTF8StringEncoding] autorelease]);
115 operator bool() const {
116 return size_ != _not(size_t);
119 NSString *operator ->*(NSString *format) const {
121 for (int i(0); i != capture_; ++i)
122 values[i] = this->operator [](i + 1);
123 return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
127 #endif//Cydia_RegEx_HPP