Skip to content

Instantly share code, notes, and snippets.

@kharmabum
Created March 9, 2014 22:37
Show Gist options
  • Save kharmabum/9456008 to your computer and use it in GitHub Desktop.
Save kharmabum/9456008 to your computer and use it in GitHub Desktop.
String objects are not normalized unless you perform that step manually. This means that comparing strings that contain combining character sequences can potentially lead to wrong results. Both isEqual: and isEqualToString: compare strings byte for byte. If you want precomposed and decomposed variants to match, you must normalize the strings first.
// http://www.objc.io/issue-9/unicode.html
NSString *s = @"\u00E9"; // é
NSString *t = @"e\u0301"; // e + ´
BOOL isEqual = [s isEqualToString:t];
NSLog(@"%@ is %@ to %@", s, isEqual ? @"equal" : @"not equal", t);
// => é is not equal to é
// Normalizing to form C
NSString *sNorm = [s precomposedStringWithCanonicalMapping];
NSString *tNorm = [t precomposedStringWithCanonicalMapping];
BOOL isEqualNorm = [sNorm isEqualToString:tNorm];
NSLog(@"%@ is %@ to %@", sNorm, isEqualNorm ? @"equal" : @"not equal", tNorm);
// => é is equal to é
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment