Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour from CFStringCompareWithOptions

I'm trying to find out exactly how I can match the first n characters of a string with another. Here's some code I've got at the moment:

CFStringRef myStringRef = CFSTR("hello");
CFStringRef otherStringRef = CFSTR("helloworld");

CFIndex cmpChars = CFStringGetLength(myStringRef);

CFComparisonResult res = CFStringCompareWithOptions(myStringRef, otherStringRef, CFRangeMake(0, cmpChars), kCFCompareCaseInsensitive);

printf("Res: %i\n", (int)res);

I get the value of res as -1, meaning 'less than' according to the documentation. Surely though, since I specified the range to match it should only take that range into account?

like image 862
benwad Avatar asked Aug 07 '26 10:08

benwad


1 Answers

The documentation could be clearer, but the range is applied only to the first string, not the second. Here's a link to an old mailing list message. What you can't tell from the archive, but I know from my personal archive, is that the guy who said so was an Apple engineer.

To do what you want, you may need to use CFStringCreateWithSubstring too.

like image 200
JWWalker Avatar answered Aug 09 '26 22:08

JWWalker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!