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