-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplater.m
48 lines (45 loc) · 1.74 KB
/
Templater.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#import "Templater.h"
#import <Foundation/NSException.h>
@implementation NSDictionary (Templater)
- (NSString*) applyToTemplate: (NSString*) templateString {
NSMutableString* res = [NSMutableString stringWithCapacity:10];
@autoreleasepool {
Boolean escape = false;
NSMutableString* replacementToken = [NSMutableString stringWithCapacity:10];//String:@""];
NSUInteger length = [templateString length];
NSUInteger i;
for ( i=0; i<length; i++) {
const unichar currentChar = [templateString characterAtIndex:i];
if (escape) {
if (currentChar == [@"»" characterAtIndex:0]) {
NSString* toInsert = [self objectForKey:replacementToken];
if (!toInsert) {
NSException* e = [NSException
exceptionWithName:@"FileNotFoundException"
reason: [NSString stringWithFormat:@"parameter '%@' not found", replacementToken]
userInfo:nil];
NSLog(@"throwing exception");
@throw e;
} else {
[res appendString:toInsert];
}
escape = false;
} else {
[replacementToken appendString:[NSString stringWithCharacters:¤tChar length:1]];
}
} else {
if (currentChar == [@"«" characterAtIndex:0]) {
escape = true;
replacementToken = [NSMutableString stringWithCapacity:10];//alloc]init]autorelease];
} else {
[res appendString:[NSString stringWithCharacters:¤tChar length:1]];
}
}
}
if (escape) {
[[NSException exceptionWithName:@"TemplaterException" reason:@"wrong escape state" userInfo:nil]raise];
}
return res;
}
}
@end