Perhaps worth noting that the docs for commonPrefixWithString:options: cover 
cases like this:

> Discussion
> The returned string is based on the characters of the receiver. For example, 
> if the receiver is “Ma¨dchen” and aString is “Mädchenschule”, the string 
> returned is “Ma¨dchen”, not “Mädchen”.


Depending on your needs, you could phrase your search for the common prefix the 
other way round and get the desired result.

If that’s not possible, my advice is that having found the common prefix, use 
an anchored search to find the range of it within the other string:

NSRange prefixRange = [file_basename rangeOfString:prefix 
options:NSAnchoredSearch | NSCaseInsensitiveSearch];
assert(prefixRange.location != NSNotFound);

NSRange spaceSearchRange = NSMakeRange(NSMaxRange(prefixRange), 
file_basename.length - NSMaxRange(prefixRange));
NSRange space_in_filename = [file_basename rangeOfString:@“ “ options:… 
range:spaceSearchRange];

Mike.

> On 11 Mar 2022, at 17:17, Gabriel Zachmann via Cocoa-dev 
> <cocoa-dev@lists.apple.com> wrote:
> 
> 
> Well, SSIA.
> In more detail, I've got two strings:
> 
> file_basename = @"Morgendämmerung (1)"
> info_item = @"Morgendämmerung"
> 
> This code
> 
>    NSString * prefix = [ info_item commonPrefixWithString: file_basename 
> options: NSCaseInsensitiveSearch ];
>    unsigned int prefix_len = (unsigned int) [prefix length];
> 
> yields prefix_len = 15
> as it should.
> 
> The problem arises with this line of code:
> 
>    NSRange space_in_filename = [file_basename rangeOfString: @" " options: 
> NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch|NSWidthInsensitiveSearch];
> 
> which yields space_in_filename.location = 16 !
> 
> This thwarts the rest of the code, which, in this case, would expect 
> space_in_filename.location = 15.
> 
> Needless to say that with strings that do not contain Umlauts, the call of 
> rangeOfString:options: works as expected, i.e., in the above example 
> space_in_filename.location is as expected (i.e., first character has 
> location=0).
> 
> I have also tried localizedStandardRangeOfString, with the same effect.
> 
> Any ideas what I can do?
> 
> 
> Best regards, Gabriel
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/mabdullah%40karelia.com
> 
> This email sent to mabdul...@karelia.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to