Skip to content

Instantly share code, notes, and snippets.

@timothyekl
Created November 27, 2013 17:13
Show Gist options
  • Save timothyekl/7679409 to your computer and use it in GitHub Desktop.
Save timothyekl/7679409 to your computer and use it in GitHub Desktop.
#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