Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
NSString Categories for localization, formatting, regex
//
// NSString+FMAdditions.h
// Use it. maybe keep a link to the author or sth.
//
#import <Foundation/Foundation.h>
// StringWithFormat Shorthand
#define f(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]
// Localization Tools
#define l(key) [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
#define lf(key, ...) [NSString stringWithFormat:l(key), ##__VA_ARGS__]
@interface NSString (FMStringArrayArgs)
+ (instancetype)stringWithFormat:(NSString *)format args:(NSArray*)args;
@end
@interface NSString (FMStringRegex)
- (NSString*)regexMatchWithPattern:(NSString*)pattern;
- (NSArray*)multipleRegexMatchWithPattern:(NSString*)pattern;
- (NSString*)replaceRegex:(NSString*)pattern intoTemplate:(NSString*)replacementTemplate;
@end
@interface NSString (FMSimpleLocalization)
- (NSString*)localizedStringWithDefaultPattern;
- (NSString*)localizedStringWithPattern:(NSString*)pattern argumentDelimiter:(NSString*)delimiter;
@end
//
// NSString+FMAdditions.m
// Use it. maybe keep a link to the author or sth.
//
#import "NSString+FMAdditions.h"
@implementation NSString (FMStringArrayArgs)
+ (instancetype)stringWithFormat:(NSString *)format args:(NSArray*)args {
if (args.count) {
// http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
char* argsList = (char*) malloc(sizeof(NSString*) * args.count);
[args getObjects:(id*) argsList];
NSString* message = [[NSString alloc] initWithFormat:format arguments:argsList];
free(argsList);
return [message autorelease];
}
return format;
}
@end
@implementation NSString (FMSimpleLocalization)
- (NSString *)localizedStringWithDefaultPattern {
return [self localizedStringWithPattern:@"@lo\\{([^\\{\\(\\)\\}]+)(?:\\(([^\\)]+)\\))?\\}"
argumentDelimiter:@","];
}
- (NSString *)localizedStringWithPattern:(NSString *)pattern argumentDelimiter:(NSString *)delimiter {
NSArray* localizationMatches = nil;
NSString* resultText = [self.copy autorelease];
while ((localizationMatches = [resultText multipleRegexMatchWithPattern:pattern])) {
if (localizationMatches.count < 2) break;
NSString* localizationKey = localizationMatches[1];
NSArray* localizationArgs = localizationMatches.count > 2 ? [localizationMatches[2] componentsSeparatedByString:delimiter] : nil;
NSString* replacement = [NSString stringWithFormat:l(localizationKey) args:localizationArgs];
resultText = [resultText stringByReplacingOccurrencesOfString:localizationMatches[0] withString:replacement];
}
return resultText;
}
@end
@implementation NSString (FMStringRegex)
- (NSString*)regexMatchWithPattern:(NSString*)pattern {
NSArray* matches = [self multipleRegexMatchWithPattern:pattern skipFirst:YES];
if (matches.count==0) return nil;
return matches.lastObject;
}
- (NSArray*)multipleRegexMatchWithPattern:(NSString*)pattern {
return [self multipleRegexMatchWithPattern:pattern skipFirst:NO];
}
- (NSArray*)multipleRegexMatchWithPattern:(NSString*)pattern skipFirst:(BOOL)skipFirst {
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionDotMatchesLineSeparators
error:&error];
if (error) {
NSLog(@"ERROR IN REGEX : %@ | %@", [error localizedFailureReason], [error localizedDescription]);
return nil;
}
NSArray* results = [regex matchesInString:self options:0 range:NSMakeRange(0, [self length])];
NSMutableArray* strResults = [NSMutableArray array];
for (NSTextCheckingResult* r in results) {
// NSLog(@"Result with %d ranges", r.numberOfRanges);
for (int i=0; i<r.numberOfRanges; i++) {
if (r.numberOfRanges>1 && i == 0 && skipFirst) continue;
if ([r rangeAtIndex:i].length <= 0) continue;
// NSLog(@"\t - %@", [self substringWithRange:[r rangeAtIndex:i]]);
[strResults addObject:[self substringWithRange:[r rangeAtIndex:i]]];
}
}
if (strResults.count==0) return nil;
return strResults;
}
- (NSString*)replaceRegex:(NSString*)pattern intoTemplate:(NSString*)replacementTemplate {
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error)
return nil;
return [regex stringByReplacingMatchesInString:self
options:0
range:NSMakeRange(0, [self length])
withTemplate:replacementTemplate];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment