Created
November 27, 2013 17:13
-
-
Save timothyekl/7679409 to your computer and use it in GitHub Desktop.
Test for http://mortoray.com/2013/11/27/the-string-type-is-broken/ in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (Reversing) | |
- (NSString *)reverseString; | |
@end | |
@implementation NSString (Reversing) | |
- (NSString *)reverseString; | |
{ | |
NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]]; | |
[self enumerateSubstringsInRange:NSMakeRange(0, [self length]) | |
options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences) | |
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { | |
[reversedString appendString:substring]; | |
}]; | |
return reversedString; | |
} | |
@end | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSString *str = nil; | |
str = @"noël"; // decomposed | |
NSLog(@"=== %@ ===", str); | |
NSLog(@"%@", [str reverseString]); // expect lëon | |
NSLog(@"%@", [str substringToIndex:3]); // expect noë | |
NSLog(@"%tu", [str length]); // expect 4 | |
str = @"😸😾"; // emoji | |
NSLog(@"=== %@ ===", str); | |
NSLog(@"%@", [str reverseString]); // expect 😾😸 | |
NSLog(@"%@", [str substringFromIndex:1]); // expect 😾 | |
NSLog(@"%tu", [str length]); // expect 2 | |
str = @"baffle"; // with ligature | |
NSLog(@"=== %@ ===", str); | |
NSLog(@"%@", [str uppercaseString]); // expect BAFFLE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment