Created
December 10, 2010 00:50
-
-
Save exalted/735593 to your computer and use it in GitHub Desktop.
String capitalization for NSString while keeping roman numerals all capitalized.
This file contains hidden or 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
// | |
// NSString+Punctuation.h | |
// by Ali Servet Donmez <asd NOSPAM pittle.org>, 2010 | |
// http://www.pittle.org/ | |
// | |
/* | |
* Requirements | |
* ------------ | |
* - "RegexKitLite" (http://regexkit.sourceforge.net/#RegexKitLite) | |
* | |
* Shameless documentation | |
* ----------------------- | |
* This will add string capitalization for NSString while keeping roman numerals all capitalized. | |
* For instance: | |
* [@"WORLD WAR III" capitalizedString]; // "World War Iii" (ugly...) | |
* [@"WORLD WAR III" capitalizedStringWithRomanNumerals]; // "World War III" (sweet!) | |
* | |
*/ | |
#import <Foundation/Foundation.h> | |
@interface NSString (Punctuation) | |
- (id)capitalizedStringWithRomanNumerals; | |
@end |
This file contains hidden or 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
// | |
// NSString+Punctuation.m | |
// by Ali Servet Donmez <asd NOSPAM pittle.org>, 2010 | |
// http://www.pittle.org/ | |
// | |
#import "NSString+Punctuation.h" | |
#import "RegexKitLite.h" | |
@implementation NSString (Punctuation) | |
- (id)capitalizedStringWithRomanNumerals | |
{ | |
self = [self capitalizedString]; | |
NSString *regEx = @"(?<=(?:^|\\s{1,2}|[(\\[]))(X{0,3}(?:IX|IV|V?I{0,3}))(?=(?:[)\\]]|\\s{1,2}|$))"; | |
NSError *regexValidError = NULL; | |
if([regEx isRegexValidWithOptions:RKLNoOptions error:®exValidError] == NO) { | |
DebugLog(@"The regular expression is invalid. Error: %@", regexValidError); | |
} | |
NSError *replaceError = NULL; | |
self = [self stringByReplacingOccurrencesOfRegex:regEx | |
options:RKLCaseless | |
inRange:NSMakeRange(0UL, [self length]) | |
error:&replaceError | |
enumerationOptions:RKLRegexEnumerationNoOptions | |
usingBlock: | |
^(NSInteger captureCount, | |
NSString * const capturedStrings[captureCount], | |
const NSRange capturedRanges[captureCount], | |
volatile BOOL * const stop) { | |
return (NSString *)([NSString stringWithFormat:@"%@", | |
[capturedStrings[1] uppercaseString]]); | |
}]; | |
if (replaceError != NULL) { | |
DebugLog(@"Error in replace: %@", replaceError); | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment