From f133f1d9678afea79003629fb78b77395dcfa71a Mon Sep 17 00:00:00 2001 From: hmelder Date: Mon, 28 Oct 2024 13:16:01 +0100 Subject: [PATCH 1/3] Update changelog --- ChangeLog | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ChangeLog b/ChangeLog index 4be99f770..2e58f2885 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 From 878e3a0751e795f0d7cab5ab41e84844e7406bd1 Mon Sep 17 00:00:00 2001 From: hmelder Date: Mon, 28 Oct 2024 13:18:39 +0100 Subject: [PATCH 2/3] NSString: fix -commonPrefixWithString:options: behaviour --- Source/NSString.m | 5 +++++ Tests/base/NSString/basic.m | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Source/NSString.m b/Source/NSString.m index b0c85b4af..8c7a29693 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 d94b539c0..2b87f05d2 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; } From 70518556fb4f8fb828d766c4b521be7baad2b6e8 Mon Sep 17 00:00:00 2001 From: hmelder Date: Mon, 28 Oct 2024 13:37:31 +0100 Subject: [PATCH 3/3] NSString: More test cases --- Tests/base/NSString/common_prefix.m | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Tests/base/NSString/common_prefix.m diff --git a/Tests/base/NSString/common_prefix.m b/Tests/base/NSString/common_prefix.m new file mode 100644 index 000000000..447dc4507 --- /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]; +} +