mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
Added NSRegularExpressionSearch (10.7) support to NSString.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33600 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
5c802d5d57
commit
d9fda65728
4 changed files with 65 additions and 1 deletions
|
@ -110,6 +110,17 @@ enum
|
|||
NSWidthInsensitiveSearch = 256,
|
||||
NSForcedOrderingSearch = 512
|
||||
#endif
|
||||
#if OS_API_VERSION(100700,GS_API_LATEST)
|
||||
,
|
||||
/**
|
||||
* Treats the search string as a regular expression. This option may be
|
||||
* combined with NSCaseInsensitiveSearch and NSAnchoredSearch, but no other
|
||||
* search options.
|
||||
*
|
||||
* This option may only be used with the -rangeOfString: family of methods.
|
||||
*/
|
||||
NSRegularExpressionSearch = 1024
|
||||
#endif
|
||||
};
|
||||
typedef NSUInteger NSStringCompareOptions;
|
||||
|
||||
|
|
|
@ -3239,6 +3239,10 @@ agree, create a new GSCInlineString otherwise.
|
|||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"[%@ -%@] not a string argument",
|
||||
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
||||
if ((mask & NSRegularExpressionSearch) == NSRegularExpressionSearch)
|
||||
{
|
||||
return [super rangeOfString: aString options: mask range: aRange];
|
||||
}
|
||||
return rangeOfString_c((GSStr)self, aString, mask, aRange);
|
||||
}
|
||||
|
||||
|
@ -3573,6 +3577,10 @@ agree, create a new GSCInlineString otherwise.
|
|||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"[%@ -%@] not a string argument",
|
||||
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
||||
if ((mask & NSRegularExpressionSearch) == NSRegularExpressionSearch)
|
||||
{
|
||||
return [super rangeOfString: aString options: mask range: aRange];
|
||||
}
|
||||
return rangeOfString_u((GSStr)self, aString, mask, aRange);
|
||||
}
|
||||
|
||||
|
@ -4425,6 +4433,10 @@ NSAssert(_flags.owned == 1 && _zone != 0, NSInternalInconsistencyException);
|
|||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"[%@ -%@] not a string argument",
|
||||
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
||||
if ((mask & NSRegularExpressionSearch) == NSRegularExpressionSearch)
|
||||
{
|
||||
return [super rangeOfString: aString options: mask range: aRange];
|
||||
}
|
||||
if (_flags.wide == 1)
|
||||
return rangeOfString_u((GSStr)self, aString, mask, aRange);
|
||||
else
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
#import "Foundation/NSPortCoder.h"
|
||||
#import "Foundation/NSPathUtilities.h"
|
||||
#import "Foundation/NSRange.h"
|
||||
#import "Foundation/NSRegularExpression.h"
|
||||
#import "Foundation/NSException.h"
|
||||
#import "Foundation/NSData.h"
|
||||
#import "Foundation/NSURL.h"
|
||||
|
@ -2037,6 +2038,28 @@ handle_printf_atsign (FILE *stream,
|
|||
GS_RANGE_CHECK(aRange, [self length]);
|
||||
if (aString == nil)
|
||||
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
||||
if ((mask & NSRegularExpressionSearch) == NSRegularExpressionSearch)
|
||||
{
|
||||
NSRange r = {NSNotFound, 0};
|
||||
NSError *e = nil;
|
||||
NSUInteger options = 0;
|
||||
NSRegularExpression *regex = [NSRegularExpression alloc];
|
||||
if ((mask & NSCaseInsensitiveSearch) == NSCaseInsensitiveSearch)
|
||||
{
|
||||
options |= NSRegularExpressionCaseInsensitive;
|
||||
}
|
||||
regex = [regex initWithPattern: aString options: options error: &e];
|
||||
if (nil == e)
|
||||
{
|
||||
options = ((mask & NSAnchoredSearch) == NSAnchoredSearch) ?
|
||||
NSMatchingAnchored : 0;
|
||||
r = [regex rangeOfFirstMatchInString: self
|
||||
options: options
|
||||
range: aRange];
|
||||
}
|
||||
[regex release];
|
||||
return r;
|
||||
}
|
||||
return strRangeNsNs(self, aString, mask, aRange);
|
||||
}
|
||||
|
||||
|
@ -2045,7 +2068,11 @@ handle_printf_atsign (FILE *stream,
|
|||
range: (NSRange)searchRange
|
||||
locale: (NSLocale *)locale
|
||||
{
|
||||
return NSMakeRange(0, 0); // FIXME
|
||||
// FIXME: Doing a locale-insensitive search is better than returning {0, 0},
|
||||
// but it's still wrong.
|
||||
return [self rangeOfString: aString
|
||||
options: mask
|
||||
range: searchRange];
|
||||
}
|
||||
|
||||
- (NSUInteger) indexOfString: (NSString *)substring
|
||||
|
|
14
Tests/base/NSString/regex.m
Normal file
14
Tests/base/NSString/regex.m
Normal file
|
@ -0,0 +1,14 @@
|
|||
#import <Foundation/NSString.h>
|
||||
#import "ObjectTesting.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
NSString *regex = @"abcd*";
|
||||
NSString *source = @"abcdddddd e f g";
|
||||
NSRange r = [source rangeOfString: regex options: NSRegularExpressionSearch];
|
||||
PASS(r.length == 9, "Correct length for regex, expected 9 got %d", (int)r.length);
|
||||
regex = @"aBcD*";
|
||||
r = [source rangeOfString: regex options: (NSRegularExpressionSearch | NSCaseInsensitiveSearch)];
|
||||
PASS(r.length == 9, "Correct length for regex, expected 9 got %d", (int)r.length);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue