From 878e3a0751e795f0d7cab5ab41e84844e7406bd1 Mon Sep 17 00:00:00 2001 From: hmelder Date: Mon, 28 Oct 2024 13:18:39 +0100 Subject: [PATCH] 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 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; }