Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSString: Fix -commonPrefixWithString:options: behaviour #455

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2024-10-28 Hugo Melder <[email protected]>
* 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 <[email protected]>

* Source/NSFileManager.m: Create an NSError object when we fail to
Expand Down
5 changes: 5 additions & 0 deletions Source/NSString.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Tests/base/NSString/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
39 changes: 39 additions & 0 deletions Tests/base/NSString/common_prefix.m
Original file line number Diff line number Diff line change
@@ -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];
}

Loading