1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2014 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_PerlCompatibleRegEx_HPP
23 #define Cydia_PerlCompatibleRegEx_HPP
27 #include "CyteKit/UCPlatform.h"
28 #include "CyteKit/stringWithUTF8Bytes.h"
46 Pcre(const char *regex, NSString *data = nil) :
51 this->operator =(regex);
54 this->operator ()(data);
57 void operator =(const char *regex) {
58 _assert(code_ == NULL);
62 code_ = pcre_compile(regex, 0, &error, &offset, NULL);
65 fprintf(stderr, "%d:%s\n", offset, error);
69 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
70 _assert(capture_ >= 0);
71 matches_ = new int[(capture_ + 1) * 3];
79 NSString *operator [](size_t match) const {
80 return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])];
83 _finline bool operator ()(NSString *data) {
84 // XXX: length is for characters, not for bytes
85 return operator ()([data UTF8String], [data length]);
88 _finline bool operator ()(const char *data) {
89 return operator ()(data, strlen(data));
92 bool operator ()(const char *data, size_t size) {
93 if (pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0) {
102 operator bool() const {
103 return data_ != NULL;
106 NSString *operator ->*(NSString *format) const {
108 for (int i(0); i != capture_; ++i)
109 values[i] = this->operator [](i + 1);
111 return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
115 #endif//Cydia_PerlCompatibleRegEx_HPP