mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
Implement -isEqual: and -hash on NSRegularExpression, so that copies are
actually equal git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@39871 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
491474cbf3
commit
ac2d08d2a1
4 changed files with 115 additions and 1 deletions
|
@ -1,3 +1,8 @@
|
|||
2016-06-17 Niels Grewe <niels.grewe@halbordnung.de>
|
||||
|
||||
* Source/NSRegularExpression.m: Implement -isEqual: and -hash
|
||||
* Tests/base/NSRegularExpression/basic.m: Test NSCopying
|
||||
|
||||
2016-06-14 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/GSSocketStream.m: Fix for failure of connect() call.
|
||||
|
|
|
@ -133,6 +133,42 @@ NSRegularExpressionOptionsToURegexpFlags(NSRegularExpressionOptions opts)
|
|||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) isEqual: (id)obj
|
||||
{
|
||||
if ([obj isKindOfClass: [NSRegularExpression class]])
|
||||
{
|
||||
if (self == obj)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
else if (options != ((NSRegularExpression*)obj)->options)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
UErrorCode myErr = 0;
|
||||
UErrorCode theirErr = 0;
|
||||
const UText *myText = uregex_patternUText(regex, &myErr);
|
||||
const UText *theirText =
|
||||
uregex_patternUText(((NSRegularExpression*)obj)->regex, &theirErr);
|
||||
if (U_FAILURE(myErr) != U_FAILURE(theirErr))
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
else if (U_FAILURE(myErr) && U_FAILURE(theirErr))
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
return utext_equals(myText, theirText);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return [super isEqual: obj];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString*) pattern
|
||||
{
|
||||
UErrorCode s = 0;
|
||||
|
@ -178,12 +214,58 @@ NSRegularExpressionOptionsToURegexpFlags(NSRegularExpressionOptions opts)
|
|||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) isEqual: (id)obj
|
||||
{
|
||||
if ([obj isKindOfClass: [NSRegularExpression class]])
|
||||
{
|
||||
if (self == obj)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
else if (options != ((NSRegularExpression*)obj)->options)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
UErrorCode myErr = 0;
|
||||
UErrorCode theirErr = 0;
|
||||
int32_t myLen = 0;
|
||||
int32_t theirLen = 0;
|
||||
const UChar *myText = uregex_pattern(regex, &myLen, &myErr);
|
||||
const UChar *theirText = uregex_pattern(
|
||||
((NSRegularExpression*)obj)->regex,
|
||||
&theirLen, &theirErr);
|
||||
if (U_FAILURE(myErr) != U_FAILURE(theirErr))
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
else if (U_FAILURE(myErr) && U_FAILURE(theirErr))
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
if (myLen != theirLen)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
return
|
||||
(0 == memcmp((const void*)myText, (const void*)theirText, myLen));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return [super isEqual: obj];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
- (NSString*) pattern
|
||||
{
|
||||
UErrorCode s = 0;
|
||||
int32_t length;
|
||||
const unichar *pattern = uregex_pattern(regex, &length, &s);
|
||||
|
||||
|
||||
if (U_FAILURE(s))
|
||||
{
|
||||
return nil;
|
||||
|
@ -192,6 +274,11 @@ NSRegularExpressionOptionsToURegexpFlags(NSRegularExpressionOptions opts)
|
|||
}
|
||||
#endif
|
||||
|
||||
- (NSUInteger) hash
|
||||
{
|
||||
return [[self pattern] hash] ^ options;
|
||||
}
|
||||
|
||||
static UBool
|
||||
callback(const void *context, int32_t steps)
|
||||
{
|
||||
|
|
0
Tests/base/NSRegularExpression/TestInfo
Normal file
0
Tests/base/NSRegularExpression/TestInfo
Normal file
22
Tests/base/NSRegularExpression/basic.m
Normal file
22
Tests/base/NSRegularExpression/basic.m
Normal file
|
@ -0,0 +1,22 @@
|
|||
#import "ObjectTesting.h"
|
||||
#import <Foundation/NSAutoreleasePool.h>
|
||||
#import <Foundation/NSRegularExpression.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
||||
id testObj = [[NSRegularExpression alloc] initWithPattern: @"^a"
|
||||
options: 0
|
||||
error: NULL];
|
||||
|
||||
test_NSObject(@"NSRegularExpression",
|
||||
[NSArray arrayWithObject:
|
||||
[[NSRegularExpression alloc] initWithPattern: @"^a"
|
||||
options: 0
|
||||
error: NULL]]);
|
||||
test_NSCopying(@"NSRegularExpression",@"NSRegularExpression",
|
||||
[NSArray arrayWithObject:testObj],NO,NO);
|
||||
|
||||
[arp release]; arp = nil;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue