NSString: Fix -commonPrefixWithString:options: behaviour (#455)

* Update changelog

* NSString: fix -commonPrefixWithString:options: behaviour

* NSString: More test cases
This commit is contained in:
Hugo Melder 2024-10-28 06:42:41 -07:00 committed by GitHub
parent 8b65241b03
commit cf4c985e46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 53 additions and 0 deletions

View file

@ -1,3 +1,9 @@
2024-10-28 Hugo Melder <hugo@algoriddim.com>
* 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 <rfm@gnu.org>
* Source/NSFileManager.m: Create an NSError object when we fail to

View file

@ -3227,6 +3227,11 @@ register_printf_atsign ()
- (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;

View file

@ -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;
}

View file

@ -0,0 +1,39 @@
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#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];
}