Skip to content

Instantly share code, notes, and snippets.

@exalted
Created December 10, 2010 00:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exalted/735593 to your computer and use it in GitHub Desktop.
Save exalted/735593 to your computer and use it in GitHub Desktop.
String capitalization for NSString while keeping roman numerals all capitalized.
//
// 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
//
// 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:&regexValidError] == 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