Implement +[NSRegularExpression escapedPatternForString:]

This commit is contained in:
ethanc8 2023-01-03 20:33:47 -06:00 committed by ethanc8
parent 567e957769
commit 7862434d85
2 changed files with 17 additions and 0 deletions

View file

@ -149,6 +149,9 @@ GS_EXPORT_CLASS
inString: (NSString*)string
offset: (NSInteger)offset
template: (NSString*)templat;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
+ (NSString *)escapedPatternForString:(NSString *)string;
#endif
#if GS_HAS_DECLARED_PROPERTIES
@property (readonly) NSRegularExpressionOptions options;
@property (readonly) NSUInteger numberOfCaptureGroups;

View file

@ -1064,6 +1064,20 @@ prepareResult(NSRegularExpression *regex,
}
#endif
+ (NSString *)escapedPatternForString:(NSString *)string {
// https://unicode-org.github.io/icu/userguide/strings/regexp.html
// Need to escape * ? + [ ( ) { } ^ $ | \ .
return [[NSRegularExpression
regularExpressionWithPattern: @"([*?+\\[(){}^$|\\\\.])"
options: 0
error: NULL]
stringByReplacingMatchesInString: string
options: 0
range: NSMakeRange(0, [string length])
withTemplate: @"\\\\$1"
];
}
- (NSRegularExpressionOptions) options
{
return options;