]> git.saurik.com Git - cydia.git/blame - CyteKit/RegEx.hpp
Use libicucore's uregex to avoid Depends: libpcre.
[cydia.git] / CyteKit / RegEx.hpp
CommitLineData
b4fd1197
JF
1/* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2014 Jay Freeman (saurik)
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
48class 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
61 RegEx(const char *regex, NSString *data = nil) :
62 regex_(NULL),
63 size_(_not(size_t))
64 {
65 this->operator =(regex);
66
67 if (data != nil)
68 this->operator ()(data);
69 }
70
71 void operator =(const char *regex) {
72 _assert(regex_ == NULL);
73 UParseError error;
74 regex_ = _rgxcall(uregex_openC, regex, 0, &error);
75 capture_ = _rgxcall(uregex_groupCount, regex_);
76 }
77
78 ~RegEx() {
79 uregex_close(regex_);
80 }
81
82 NSString *operator [](size_t match) const {
83 UParseError error;
84 size_t size(size_);
85 UChar data[size];
86 size = _rgxcall(uregex_group, regex_, match, data, size);
87 return [[[NSString alloc] initWithBytes:data length:(size * sizeof(UChar)) encoding:NSUTF16LittleEndianStringEncoding] autorelease];
88 }
89
90 _finline bool operator ()(NSString *string) {
91 return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]), [string length]);
92 }
93
94 _finline bool operator ()(const char *data) {
95 return operator ()([NSString stringWithUTF8String:data]);
96 }
97
98 bool operator ()(const UChar *data, size_t size) {
99 UParseError error;
100 _rgxcallv(uregex_setText, regex_, data, size);
101
102 if (_rgxcall(uregex_matches, regex_, -1)) {
103 size_ = size;
104 return true;
105 } else {
106 size_ = _not(size_t);
107 return false;
108 }
109 }
110
111 bool operator ()(const char *data, size_t size) {
112 return operator ()([[[NSString alloc] initWithBytes:data length:size encoding:NSUTF8StringEncoding] autorelease]);
113 }
114
115 operator bool() const {
116 return size_ != _not(size_t);
117 }
118
119 NSString *operator ->*(NSString *format) const {
120 id values[capture_];
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];
124 }
125};
126
127#endif//Cydia_RegEx_HPP