1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
Description: Port to PCRE2.
Bug-Debian: https://bugs.debian.org/1000075
Author: Yavor Doganov <yavor@gnu.org>
Forwarded: no
Last-Update: 2021-12-27
---
--- gtamsanalyzer.app.orig/Source/AGRegex.h
+++ gtamsanalyzer.app/Source/AGRegex.h
@@ -41,7 +41,7 @@
@interface AGRegexMatch : NSObject {
AGRegex *regex;
NSString *string;
- int *matchv;
+ size_t *matchv;
int count;
}
@@ -149,8 +149,9 @@
*/
@interface AGRegex : NSObject {
void *regex;
- void *extra;
- int groupCount;
+ uint32_t groupCount;
+@private
+ void *matchv;
}
/*!
--- gtamsanalyzer.app.orig/Source/AGRegex.m
+++ gtamsanalyzer.app/Source/AGRegex.m
@@ -10,7 +10,8 @@
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#import "pcre.h"
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.h>
#import "AGRegex.h"
#import <Foundation/Foundation.h>
@@ -53,11 +54,11 @@
#endif
@interface AGRegex (Private)
-- (const pcre *)pcre;
+- (const pcre2_code *)pcre;
@end
@interface AGRegexMatch (Private)
-- (id)initWithRegex:(AGRegex *)re string:(NSString *)str vector:(int *)mv count:(int)c;
+- (id)initWithRegex:(AGRegex *)re string:(NSString *)str vector:(size_t *)mv count:(uint32_t)c;
@end
@interface AGRegexMatchEnumerator : NSEnumerator {
@@ -96,15 +97,16 @@
- (id)initWithPattern:(NSString *)pat options:(int)opts {
if ((self = [super init])) {
- const char *emsg;
- int eloc, copts = 0;
- if (opts & AGRegexCaseInsensitive) copts |= PCRE_CASELESS;
- if (opts & AGRegexDotAll) copts |= PCRE_DOTALL;
- if (opts & AGRegexExtended) copts |= PCRE_EXTENDED;
- if (opts & AGRegexLazy) copts |= PCRE_UNGREEDY;
- if (opts & AGRegexMultiline) copts |= PCRE_MULTILINE;
+ int emsg;
+ PCRE2_SIZE eloc;
+ uint32_t copts = 0;
+ if (opts & AGRegexCaseInsensitive) copts |= PCRE2_CASELESS;
+ if (opts & AGRegexDotAll) copts |= PCRE2_DOTALL;
+ if (opts & AGRegexExtended) copts |= PCRE2_EXTENDED;
+ if (opts & AGRegexLazy) copts |= PCRE2_UNGREEDY;
+ if (opts & AGRegexMultiline) copts |= PCRE2_MULTILINE;
#ifdef SUPPORT_UTF8
- copts |= PCRE_UTF8;
+ copts |= PCRE2_UTF;
#else
// check for valid ASCII string
if (![pat canBeConvertedToEncoding:NSASCIIStringEncoding]) {
@@ -112,11 +114,11 @@
return nil;
}
#endif
- if (!(regex = pcre_compile([pat UTF8String], copts, &emsg, &eloc, NULL))) {
+ if (!(regex = pcre2_compile((PCRE2_SPTR)[pat UTF8String], PCRE2_ZERO_TERMINATED, copts, &emsg, &eloc, NULL))) {
[self release];
return nil;
}
- if (pcre_fullinfo(regex, NULL, PCRE_INFO_CAPTURECOUNT, &groupCount)) {
+ if (pcre2_pattern_info(regex, PCRE2_INFO_CAPTURECOUNT, &groupCount)) {
[self release];
return nil;
}
@@ -126,8 +128,8 @@
}
- (void)dealloc {
- pcre_free(regex);
- pcre_free(extra);
+ pcre2_code_free(regex);
+ pcre2_match_data_free(matchv);
[super dealloc];
}
@@ -136,7 +138,7 @@
}
- (AGRegexMatch *)findInString:(NSString *)str range:(NSRange)range {
- int error, length, options, *matchv;
+ int error, length, options;
length = [str length];
options = 0;
#ifndef SUPPORT_UTF8
@@ -149,21 +151,21 @@
[NSException raise:NSRangeException format:@"range %@ out of bounds", NSStringFromRange(range)];
// don't match $ anchor if range is before end of string
if (range.location + range.length < length)
- options |= PCRE_NOTEOL;
+ options |= PCRE2_NOTEOL;
// allocate match vector
- NSAssert1(matchv = malloc(sizeof(int) * groupCount * 3), @"couldn't allocate match vector for %d items", groupCount * 3);
+ NSAssert1(matchv = pcre2_match_data_create(groupCount * 3, NULL), @"couldn't allocate match vector for %d items", groupCount * 3);
// convert character range to byte range
range.length = strlen([[str substringWithRange:range] UTF8String]);
range.location = strlen([[str substringToIndex:range.location] UTF8String]);
// try match
- if ((error = pcre_exec(regex, extra, [str UTF8String], range.location + range.length, range.location, options, matchv, groupCount * 3)) == PCRE_ERROR_NOMATCH) {
- free(matchv);
+ if ((error = pcre2_match(regex, (PCRE2_SPTR)[str UTF8String], range.location + range.length, range.location, options, matchv, NULL)) == PCRE2_ERROR_NOMATCH) {
+ pcre2_match_data_free(matchv);
return nil;
}
// should not get any error besides PCRE_ERROR_NOMATCH
- NSAssert1(error > 0, @"unexpected error pcre_exec(): %d", error);
+ NSAssert1(error > 0, @"unexpected error pcre2_match(): %d", error);
// return the match, match object takes ownership of matchv
- return [[[AGRegexMatch alloc] initWithRegex:self string:str vector:matchv count:groupCount] autorelease];
+ return [[[AGRegexMatch alloc] initWithRegex:self string:str vector:pcre2_get_ovector_pointer(matchv) count:groupCount] autorelease];
}
- (NSArray *)findAllInString:(NSString *)str {
@@ -226,7 +228,7 @@
int idx;
if (IS_NAMED_BACKREF(backref)) {
NSString *backrefName = BACKREF_NAME(backref);
- while ((idx = pcre_get_stringnumber(regex, [backrefName UTF8String])) == PCRE_ERROR_NOSUBSTRING && !BACKREF_IS_PARENTHESIZED(backref)) {
+ while ((idx = pcre2_substring_number_from_name(regex, (PCRE2_SPTR)[backrefName UTF8String])) == PCRE2_ERROR_NOSUBSTRING && !BACKREF_IS_PARENTHESIZED(backref)) {
if (backrefMatchRange.length < 3) // need at least one letter
[NSException raise:NSInvalidArgumentException format:@"no backreference named %@ in pattern", backrefName];
backrefName = [backrefName substringToIndex:[backrefName length] - 1];
@@ -344,14 +346,13 @@
return result;
}
-- (const pcre *)pcre { return regex; }
+- (const pcre2_code *)pcre { return regex; }
@end
@implementation AGRegexMatch
-// takes ownership of the passed match vector, free on dealloc
-- (id)initWithRegex:(AGRegex *)re string:(NSString *)str vector:(int *)mv count:(int)c {
+- (id)initWithRegex:(AGRegex *)re string:(NSString *)str vector:(size_t *)mv count:(uint32_t)c {
if ((self = [super init])) {
regex = [re retain];
string = [str copy]; // really only copies if the string is mutable, immutable strings are just retained
@@ -362,7 +363,6 @@
}
- (void)dealloc {
- free(matchv);
[regex release];
[string release];
[super dealloc];
@@ -382,8 +382,8 @@
}
- (NSString *)groupNamed:(NSString *)name {
- int idx = pcre_get_stringnumber([regex pcre], [name UTF8String]);
- if (idx == PCRE_ERROR_NOSUBSTRING)
+ int idx = pcre2_substring_number_from_name([regex pcre], (PCRE2_SPTR)[name UTF8String]);
+ if (idx == PCRE2_ERROR_NOSUBSTRING)
[NSException raise:NSInvalidArgumentException format:@"no group named %@", name];
return [self groupAtIndex:idx];
}
@@ -405,8 +405,8 @@
}
- (NSRange)rangeNamed:(NSString *)name {
- int idx = pcre_get_stringnumber([regex pcre], [name UTF8String]);
- if (idx == PCRE_ERROR_NOSUBSTRING)
+ int idx = pcre2_substring_number_from_name([regex pcre], (PCRE2_SPTR)[name UTF8String]);
+ if (idx == PCRE2_ERROR_NOSUBSTRING)
[NSException raise:NSInvalidArgumentException format:@"no group named %@", name];
return [self rangeAtIndex:idx];
}
@@ -466,4 +466,4 @@
return all;
}
-@end
\ No newline at end of file
+@end
|