Fixed some bugs

This commit is contained in:
achmizs 2021-12-04 18:44:04 -05:00
parent e4e7f5a71a
commit 9eed63136d

View File

@ -347,14 +347,14 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
if (maxSplits == 0)
return @[ self ];
static NSRegularExpression *regexp;
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regexp = [@"(?:^|\\S|$)+" regularExpression];
regex = [@"^\\S*|(?<=\\s)$|\\S+" regularExpression];
});
NSMutableArray <NSString *> *components = [NSMutableArray array];
[regexp enumerateMatchesInString:self
[regex enumerateMatchesInString:self
options:(NSMatchingOptions) 0
range:self.fullRange
usingBlock:^(NSTextCheckingResult * _Nullable result,
@ -533,7 +533,7 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
-(NSRegularExpression *) regularExpressionWithOptions:(NSRegularExpressionOptions)options {
NSError *error;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:self
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:self
options:options
error:&error];
if (error) {
@ -544,7 +544,7 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
return nil;
}
return regexp;
return regex;
}
/**********************************************/
@ -552,48 +552,45 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
**********************************************/
-(NSArray <NSString *> *) matchesForRegex:(NSRegularExpression *)regex {
NSMutableArray <NSString *> *matches = [NSMutableArray arrayWithCapacity:regex.numberOfCaptureGroups];
[regex enumerateMatchesInString:self
options:(NSMatchingOptions) 0
range:self.fullRange
usingBlock:^(NSTextCheckingResult * _Nullable result,
NSMatchingFlags flags,
BOOL *stop) {
[NSIndexSet from:0
for:result.numberOfRanges
do:^(NSUInteger idx) {
NSString *resultString = ([result rangeAtIndex:idx].location == NSNotFound
? @""
: [self substringWithRange:[result rangeAtIndex:idx]]);
[matches addObject:resultString];
}];
return [self matchesForRegex:regex
all:NO];
*stop = YES;
}];
return matches;
}
-(NSArray <NSArray <NSString *> *> *) allMatchesForRegex:(NSRegularExpression *)regex {
NSMutableArray <NSMutableArray <NSString *> *> *matches = [NSMutableArray arrayWithCapacity:regex.numberOfCaptureGroups];
[NSIndexSet from:0
for:regex.numberOfCaptureGroups
do:^(NSUInteger idx) {
[matches addObject:[NSMutableArray array]];
}];
return [self matchesForRegex:regex
all:YES];
}
/* Helper method (private).
*/
-(NSArray *) matchesForRegex:(NSRegularExpression *)regex
all:(BOOL)all {
NSMutableArray *matches = [NSMutableArray array];
[regex enumerateMatchesInString:self
options:(NSMatchingOptions) 0
range:self.fullRange
usingBlock:^(NSTextCheckingResult * _Nullable result,
NSMatchingFlags flags,
BOOL *stop) {
if (all)
[matches addObject:[NSMutableArray array]];
[NSIndexSet from:0
for:result.numberOfRanges
do:^(NSUInteger idx) {
NSString *resultString = ([result rangeAtIndex:idx].location == NSNotFound
? @""
: [self substringWithRange:[result rangeAtIndex:idx]]);
[matches[idx] addObject:resultString];
if (all) {
[((NSMutableArray *) matches.lastObject) addObject:resultString];
} else {
[matches addObject:resultString];
}
}];
if (!all)
*stop = YES;
}];
return matches;
}
@ -636,14 +633,14 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
withTemplate:(NSString *)template
regularExpressionOptions:(NSRegularExpressionOptions)regexpOptions
matchingOptions:(NSMatchingOptions)matchingOptions {
NSRegularExpression *regexp = [pattern regularExpressionWithOptions:regexpOptions];
NSTextCheckingResult *match = [regexp firstMatchInString:self
NSRegularExpression *regex = [pattern regularExpressionWithOptions:regexpOptions];
NSTextCheckingResult *match = [regex firstMatchInString:self
options:matchingOptions
range:self.fullRange];
if ( match
&& match.range.location != NSNotFound) {
return [self stringByReplacingCharactersInRange:match.range
withString:[regexp replacementStringForResult:match
withString:[regex replacementStringForResult:match
inString:self
offset:0
template:template]];
@ -813,13 +810,13 @@ static BOOL _SA_NSStringExtensions_RaiseRegularExpressionCreateException = YES;
withTemplate:(NSString *)template
regularExpressionOptions:(NSRegularExpressionOptions)regexpOptions
matchingOptions:(NSMatchingOptions)matchingOptions {
NSRegularExpression *regexp = [pattern regularExpressionWithOptions:regexpOptions];
NSTextCheckingResult *match = [regexp firstMatchInString:self
NSRegularExpression *regex = [pattern regularExpressionWithOptions:regexpOptions];
NSTextCheckingResult *match = [regex firstMatchInString:self
options:matchingOptions
range:self.fullRange];
if ( match
&& match.range.location != NSNotFound) {
NSString *replacementString = [regexp replacementStringForResult:match
NSString *replacementString = [regex replacementStringForResult:match
inString:self
offset:0
template:template];