This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
K3AStringFormatter.mm
69 lines (58 loc) · 1.69 KB
/
K3AStringFormatter.mm
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by K3A on 5/20/12.
// Copyright (c) 2012 K3A. All rights reserved.
//
#import "K3AStringFormatter.h"
@implementation K3AStringFormatter
@synthesize format;
+(K3AStringFormatter*)formatterWithFormat:(NSString*)f
{
return [[K3AStringFormatter alloc] initWithFormatString:f];
}
-(K3AStringFormatter*)initWithFormatString:(NSString*)f
{
if ( (self = [super init]) )
{
_args = [NSMutableArray new];
self.format = f;
}
return self;
}
-(void)dealloc
{
[_args release];
[format release];
[super dealloc];
}
-(K3AStringFormatter*)addString:(NSString*)arg
{
[_args addObject:arg];
return self;
}
-(K3AStringFormatter*)addFloat:(float)arg
{
[_args addObject:[NSString stringWithFormat:@"%.2f", arg]];
return self;
}
-(K3AStringFormatter*)addInt:(int)arg
{
[_args addObject:[NSString stringWithFormat:@"%d", arg]];
return self;
}
-(NSString*)generateString
{
unsigned numArgs = [_args count];
NSMutableString* outStr = [NSMutableString stringWithString:format];
for (unsigned i=0; i<numArgs; i++)
{
NSString* str = [NSString stringWithFormat:@"%%%d", i+1];
[outStr replaceOccurrencesOfString:str withString:[_args objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [outStr length])];
}
//replance pauses
[outStr replaceOccurrencesOfString:@"/100/" withString:@"\x1b\\pause=100\\" options:NSLiteralSearch range:NSMakeRange(0, [outStr length])];
[outStr replaceOccurrencesOfString:@"/500/" withString:@"\x1b\\pause=500\\" options:NSLiteralSearch range:NSMakeRange(0, [outStr length])];
return outStr;
}
@end