Fixed some bugs

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

View File

@ -347,29 +347,29 @@ 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,
NSMatchingFlags flags, NSMatchingFlags flags,
BOOL * _Nonnull stop) { BOOL * _Nonnull stop) {
if ( dropEmptyString if ( dropEmptyString
&& result.range.length == 0) { && result.range.length == 0) {
// Nothing. // Nothing.
} else if (components.count < maxSplits) { } else if (components.count < maxSplits) {
[components addObject:[self substringWithRange:result.range]]; [components addObject:[self substringWithRange:result.range]];
} else { } else {
[components addObject:[self substringWithRange:[self rangeToEndFrom:result.range]]]; [components addObject:[self substringWithRange:[self rangeToEndFrom:result.range]]];
*stop = YES; *stop = YES;
} }
}]; }];
return components; return components;
} }
@ -533,9 +533,9 @@ 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) {
if (NSString.SA_NSStringExtensions_RaiseRegularExpressionCreateException == YES) if (NSString.SA_NSStringExtensions_RaiseRegularExpressionCreateException == YES)
[NSException raise:@"SA_NSStringExtensions_RegularExpressionCreateException" [NSException raise:@"SA_NSStringExtensions_RegularExpressionCreateException"
@ -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,17 +633,17 @@ 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]];
} else { } else {
return self; return self;
} }
@ -813,16 +810,16 @@ 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];
[self replaceCharactersInRange:match.range [self replaceCharactersInRange:match.range
withString:replacementString]; withString:replacementString];
} }