Re: Problem with rangeOfString and Umlauts
NSRange is 0-based, so the 16th character is index 15. Also you might want to use -localizedStandardRangeOfString: instead... Keary Suska Esoteritech, Inc. "Demystifying technology for your home or business” > On Mar 11, 2022, at 9:10 AM, Jack Brindle via Cocoa-dev > wrote: > > I just placed your code into a short tool in Xcode. What I get is interesting: > > prefix_lenunsigned int15 > > Which his as you say. But: > > space_in_filename NSRange location=15, length=1 > > Which is not the same as you say. This is in Xcode 13.2.1. Is something else > going on? > > For the record, the body of main contains: > > @autoreleasepool { > > NSString *file_basename = @"Morgendämmerung (1)"; > NSString *info_item = @"Morgendämmerung"; > > NSString * prefix = [info_item commonPrefixWithString: > file_basename options: NSCaseInsensitiveSearch ]; > unsigned int prefix_len = (unsigned int) [prefix length]; > > NSRange space_in_filename = [file_basename rangeOfString: @" " > options: > NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch|NSWidthInsensitiveSearch]; > > NSLog(@"Break heere"); > } > > So what is different? > > Jack > > >> On Mar 11, 2022, at 11:17 AM, Gabriel Zachmann via Cocoa-dev >> 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/jackbrindle%40me.com >> >> This email sent to jackbrin...@me.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/cocoa-dev%40esoteritech.com > > This email sent to cocoa-...@esoteritech.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
Re: Problem with rangeOfString and Umlauts
> On Mar 11, 2022, at 12:17 PM, Gabriel Zachmann via Cocoa-dev > 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? It’s hard to tell from the above snippet, but I suspect your strings are different in normalization. Specifically, I suspect that file_basename uses two Unicode codepoints for the ä, and info_item uses only one. As an experiment, try running both strings through -precomposedStringWithCanonicalMapping before doing your comparisons. This method returns a new precomposed string—it doesn’t alter the receiver. Matt ___ 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
Problem with rangeOfString and Umlauts
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 smime.p7s Description: S/MIME cryptographic signature ___ 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