From cf4c985e4646c0fc2ee3802fcdc89e5f2e810cc8 Mon Sep 17 00:00:00 2001 From: Hugo Melder Date: Mon, 28 Oct 2024 06:42:41 -0700 Subject: [PATCH] NSString: Fix -commonPrefixWithString:options: behaviour (#455) * Update changelog * NSString: fix -commonPrefixWithString:options: behaviour * NSString: More test cases --- ChangeLog | 6 +++++ Source/NSString.m | 5 ++++ Tests/base/NSString/basic.m | 3 +++ Tests/base/NSString/common_prefix.m | 39 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 Tests/base/NSString/common_prefix.m diff --git a/ChangeLog b/ChangeLog index 4be99f770f..2e58f28858 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-10-28 Hugo Melder + * Source/NSString.m: + -commonPrefixWithString:options: returns nil when string supplied as + first argument is nil. On macOS, the empty string is returned instead. + Align implementation with macOS. + 2024-10-13 Richard Frith-Macdonald * Source/NSFileManager.m: Create an NSError object when we fail to diff --git a/Source/NSString.m b/Source/NSString.m index b0c85b4af9..8c7a296932 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -3227,6 +3227,11 @@ - (NSUInteger) hash - (NSString*) commonPrefixWithString: (NSString*)aString options: (NSUInteger)mask { + // Return empty string to match behaviour on macOS + if (nil == aString) + { + return @""; + } if (mask & NSLiteralSearch) { int prefix_len = 0; diff --git a/Tests/base/NSString/basic.m b/Tests/base/NSString/basic.m index d94b539c0b..2b87f05d2f 100644 --- a/Tests/base/NSString/basic.m +++ b/Tests/base/NSString/basic.m @@ -144,6 +144,9 @@ int main() PASS([@"" isEqual: nil] == NO, "an empty string is not null"); PASS([@"" isEqualToString: nil] == NO, "an empty string is not null"); + s = [@"test" commonPrefixWithString: nil options: 0]; + PASS_EQUAL(s, @"", "Common prefix of some string with nil is empty string"); + [arp release]; arp = nil; return 0; } diff --git a/Tests/base/NSString/common_prefix.m b/Tests/base/NSString/common_prefix.m new file mode 100644 index 0000000000..447dc45074 --- /dev/null +++ b/Tests/base/NSString/common_prefix.m @@ -0,0 +1,39 @@ +#import +#import +#import "Testing.h" + +int main() +{ + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSString *result; + + result = [@"abc" commonPrefixWithString:nil options:0]; + PASS_EQUAL(result, @"", "common prefix of some string with nil is empty string"); + + result = [@"abc" commonPrefixWithString:@"abc" options:0]; + PASS_EQUAL(result, @"abc", "common prefix of identical strings is the entire string"); + + result = [@"abc" commonPrefixWithString:@"abx" options:0]; + PASS_EQUAL(result, @"ab", "common prefix of 'abc' and 'abx' is 'ab'"); + + result = [@"abc" commonPrefixWithString:@"def" options:0]; + PASS_EQUAL(result, @"", "common prefix of completely different strings is empty"); + + result = [@"abc" commonPrefixWithString:@"" options:0]; + PASS_EQUAL(result, @"", "common prefix with an empty string is empty"); + + result = [@"abc" commonPrefixWithString:@"a" options:0]; + PASS_EQUAL(result, @"a", "common prefix of 'abc' and 'a' is 'a'"); + + result = [@"abc" commonPrefixWithString:@"aöç" options:0]; + PASS_EQUAL(result, @"a", "common prefix of 'abc' and 'aöç' is 'a'"); + + result = [@"" commonPrefixWithString:@"abc" options:0]; + PASS_EQUAL(result, @"", "common prefix with an empty base string is empty"); + + result = [@"abc" commonPrefixWithString:@"abcx" options:0]; + PASS_EQUAL(result, @"abc", "common prefix of 'abc' and 'abcx' is 'abc'"); + + [arp drain]; +} +