Skip to content

Commit 6ea48fb

Browse files
committed
Cleanup
- Renamed parameter to defaultTableName - Renamed method to stringRepresentationWithEncoding - Fixed some indents and braces
1 parent a6ae102 commit 6ea48fb

File tree

7 files changed

+140
-133
lines changed

7 files changed

+140
-133
lines changed

Core/Source/DTLocalizableStringAggregator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@property (nonatomic, assign) NSStringEncoding inputEncoding;
1616
@property (nonatomic, retain) NSSet *tablesToSkip;
1717
@property (nonatomic, retain) NSString *customMacroPrefix;
18-
@property (nonatomic, retain) NSString *customTableName;
18+
@property (nonatomic, retain) NSString *defaultTableName;
1919

2020
- (void)beginProcessingFile:(NSURL *)fileURL;
2121

Core/Source/DTLocalizableStringAggregator.m

Lines changed: 127 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ - (void)addEntryToTables:(DTLocalizableStringEntry *)entry;
2121

2222
@implementation DTLocalizableStringAggregator
2323
{
24-
NSDictionary *_validMacros;
25-
NSMutableDictionary *_stringTables;
26-
27-
NSOperationQueue *_processingQueue;
28-
dispatch_queue_t _tableQueue;
29-
dispatch_group_t _tableGroup;
24+
NSDictionary *_validMacros;
25+
NSMutableDictionary *_stringTables;
26+
27+
NSOperationQueue *_processingQueue;
28+
dispatch_queue_t _tableQueue;
29+
dispatch_group_t _tableGroup;
3030

3131
DTLocalizableStringEntryWriteCallback _entryWriteCallback;
3232
}
@@ -37,38 +37,38 @@ @implementation DTLocalizableStringAggregator
3737
@synthesize inputEncoding = _inputEncoding;
3838
@synthesize tablesToSkip = _tablesToSkip;
3939
@synthesize customMacroPrefix = _customMacroPrefix;
40-
@synthesize customTableName = _customTableName;
40+
@synthesize defaultTableName = _defaultTableName;
4141

4242
- (id)init
4343
{
44-
self = [super init];
45-
if (self)
46-
{
47-
_tableQueue = dispatch_queue_create("DTLocalizableStringAggregator", 0);
48-
_tableGroup = dispatch_group_create();
49-
50-
_processingQueue = [[NSOperationQueue alloc] init];
51-
[_processingQueue setMaxConcurrentOperationCount:10];
52-
53-
_wantsPositionalParameters = YES; // default
54-
_inputEncoding = NSUTF8StringEncoding; // default
55-
}
56-
return self;
44+
self = [super init];
45+
if (self)
46+
{
47+
_tableQueue = dispatch_queue_create("DTLocalizableStringAggregator", 0);
48+
_tableGroup = dispatch_group_create();
49+
50+
_processingQueue = [[NSOperationQueue alloc] init];
51+
[_processingQueue setMaxConcurrentOperationCount:10];
52+
53+
_wantsPositionalParameters = YES; // default
54+
_inputEncoding = NSUTF8StringEncoding; // default
55+
}
56+
return self;
5757
}
5858

59-
- (void)dealloc
59+
- (void)dealloc
6060
{
6161
dispatch_release(_tableQueue);
62-
dispatch_release(_tableGroup);
62+
dispatch_release(_tableGroup);
6363
}
6464

65-
- (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
65+
- (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
6666
{
67-
if (customMacroPrefix != _customMacroPrefix)
68-
{
69-
_customMacroPrefix = customMacroPrefix;
70-
_validMacros = nil;
71-
}
67+
if (customMacroPrefix != _customMacroPrefix)
68+
{
69+
_customMacroPrefix = customMacroPrefix;
70+
_validMacros = nil;
71+
}
7272
}
7373

7474
#define KEY @"rawKey"
@@ -77,144 +77,147 @@ - (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
7777
#define BUNDLE @"bundle"
7878
#define TABLE @"tableName"
7979

80-
- (NSDictionary *)validMacros
80+
- (NSDictionary *)validMacros
8181
{
82-
if (!_validMacros)
83-
{
84-
// we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
85-
// there's no need to parse this stuff when we know what format things must be
86-
NSArray *prefixes = [NSArray arrayWithObjects:@"NSLocalizedString", @"CFCopyLocalizedString", _customMacroPrefix, nil];
87-
NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
88-
[NSArray arrayWithObjects:KEY, COMMENT, nil], @"",
89-
[NSArray arrayWithObjects:KEY, TABLE, COMMENT, nil], @"FromTable",
90-
[NSArray arrayWithObjects:KEY, TABLE, BUNDLE, COMMENT, nil], @"FromTableInBundle",
91-
[NSArray arrayWithObjects:KEY, TABLE, BUNDLE, VALUE, COMMENT, nil], @"WithDefaultValue",
92-
nil];
93-
94-
NSMutableDictionary *validMacros = [NSMutableDictionary dictionary];
95-
for (NSString *prefix in prefixes)
96-
{
97-
for (NSString *suffix in suffixes)
98-
{
99-
NSString *macroName = [prefix stringByAppendingString:suffix];
100-
NSArray *parameters = [suffixes objectForKey:suffix];
101-
102-
[validMacros setObject:parameters forKey:macroName];
103-
}
104-
}
105-
106-
_validMacros = validMacros;
107-
}
108-
109-
return _validMacros;
82+
if (!_validMacros)
83+
{
84+
// we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
85+
// there's no need to parse this stuff when we know what format things must be
86+
NSArray *prefixes = [NSArray arrayWithObjects:@"NSLocalizedString", @"CFCopyLocalizedString", _customMacroPrefix, nil];
87+
NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
88+
[NSArray arrayWithObjects:KEY, COMMENT, nil], @"",
89+
[NSArray arrayWithObjects:KEY, TABLE, COMMENT, nil], @"FromTable",
90+
[NSArray arrayWithObjects:KEY, TABLE, BUNDLE, COMMENT, nil], @"FromTableInBundle",
91+
[NSArray arrayWithObjects:KEY, TABLE, BUNDLE, VALUE, COMMENT, nil], @"WithDefaultValue",
92+
nil];
93+
94+
NSMutableDictionary *validMacros = [NSMutableDictionary dictionary];
95+
for (NSString *prefix in prefixes)
96+
{
97+
for (NSString *suffix in suffixes)
98+
{
99+
NSString *macroName = [prefix stringByAppendingString:suffix];
100+
NSArray *parameters = [suffixes objectForKey:suffix];
101+
102+
[validMacros setObject:parameters forKey:macroName];
103+
}
104+
}
105+
106+
_validMacros = validMacros;
107+
}
108+
109+
return _validMacros;
110110
}
111111

112112
#define QUOTE @"\""
113113

114114
- (void)beginProcessingFile:(NSURL *)fileURL
115115
{
116-
NSDictionary *validMacros = [self validMacros];
117-
118-
DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc] initWithContentsOfURL:fileURL encoding:_inputEncoding validMacros:validMacros];
119-
120-
[scanner setEntryFoundCallback:^(DTLocalizableStringEntry *entry)
116+
NSDictionary *validMacros = [self validMacros];
117+
118+
DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc] initWithContentsOfURL:fileURL encoding:_inputEncoding validMacros:validMacros];
119+
120+
[scanner setEntryFoundCallback:^(DTLocalizableStringEntry *entry)
121121
{
122-
NSString *key = [entry rawKey];
123-
NSString *value = [entry rawValue];
124-
BOOL shouldBeAdded = ([key hasPrefix:QUOTE] && [key hasSuffix:QUOTE]);
125-
126-
if (value)
127-
{
128-
shouldBeAdded &= ([value hasPrefix:QUOTE] && [value hasSuffix:QUOTE]);
129-
}
130-
131-
if (shouldBeAdded)
132-
{
133-
dispatch_group_async(_tableGroup, _tableQueue, ^{
134-
[self addEntryToTables:entry];
135-
});
136-
}
137-
else
138-
{
139-
NSLog(@"skipping: %@", entry);
140-
}
122+
NSString *key = [entry rawKey];
123+
NSString *value = [entry rawValue];
124+
BOOL shouldBeAdded = ([key hasPrefix:QUOTE] && [key hasSuffix:QUOTE]);
125+
126+
if (value)
127+
{
128+
shouldBeAdded &= ([value hasPrefix:QUOTE] && [value hasSuffix:QUOTE]);
129+
}
130+
131+
if (shouldBeAdded)
132+
{
133+
dispatch_group_async(_tableGroup, _tableQueue, ^{
134+
[self addEntryToTables:entry];
135+
});
136+
}
137+
else
138+
{
139+
NSLog(@"skipping: %@", entry);
140+
}
141141
}];
142-
143-
[_processingQueue addOperation:scanner];
142+
143+
[_processingQueue addOperation:scanner];
144144
}
145145

146146
- (void)addEntryToTables:(DTLocalizableStringEntry *)entry
147147
{
148-
NSAssert(dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
149-
if (!_stringTables)
150-
{
151-
_stringTables = [NSMutableDictionary dictionary];
152-
}
153-
154-
if ([entry.tableName length] == 0)
155-
entry.tableName = _customTableName ?: @"Localizable";
156-
157-
NSString *tableName = [entry tableName];
148+
NSAssert(dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
149+
if (!_stringTables)
150+
{
151+
_stringTables = [NSMutableDictionary dictionary];
152+
}
158153

159-
BOOL shouldSkip = [_tablesToSkip containsObject:tableName];
160-
161-
if (!shouldSkip)
162-
{
163-
// find the string table for this token, or create it
164-
DTLocalizableStringTable *table = [_stringTables objectForKey:tableName];
165-
if (!table)
166-
{
167-
// need to create it
154+
// use default table name is no name set
155+
if (![entry.tableName length])
156+
{
157+
entry.tableName = _defaultTableName ? _defaultTableName : @"Localizable";
158+
}
159+
160+
NSString *tableName = [entry tableName];
161+
162+
BOOL shouldSkip = [_tablesToSkip containsObject:tableName];
163+
164+
if (!shouldSkip)
165+
{
166+
// find the string table for this token, or create it
167+
DTLocalizableStringTable *table = [_stringTables objectForKey:tableName];
168+
if (!table)
169+
{
170+
// need to create it
168171
table = [[DTLocalizableStringTable alloc] initWithName:tableName];
169-
[_stringTables setObject:table forKey:tableName];
170-
}
171-
172-
if (entry.rawValue)
173-
{
172+
[_stringTables setObject:table forKey:tableName];
173+
}
174+
175+
if (entry.rawValue)
176+
{
174177
// ...WithDefaultValue
175-
if (_wantsPositionalParameters)
176-
{
178+
if (_wantsPositionalParameters)
179+
{
177180
entry.rawValue = [entry.rawValue stringByNumberingFormatPlaceholders];
178181
}
179182

180183
[table addEntry:entry];
181184
}
182-
else
183-
{
185+
else
186+
{
184187
// all other options use the key and variations thereof
185188

186189
// support for predicate token splitting
187190
NSArray *keyVariants = [entry.rawKey variantsFromPredicateVariations];
188191

189192
// add all variants
190-
for (NSString *oneVariant in keyVariants)
191-
{
193+
for (NSString *oneVariant in keyVariants)
194+
{
192195
DTLocalizableStringEntry *splitEntry = [entry copy];
193196

194197
NSString *value = oneVariant;
195-
if (_wantsPositionalParameters)
196-
{
198+
if (_wantsPositionalParameters)
199+
{
197200
value = [oneVariant stringByNumberingFormatPlaceholders];
198201
}
199-
202+
200203
// adjust key and value of the new entry
201204
splitEntry.rawKey = oneVariant;
202205
splitEntry.rawValue = value;
203-
206+
204207
// add token to this table
205208
[table addEntry:splitEntry];
206209
}
207210
}
208-
}
211+
}
209212
}
210213

211-
- (NSArray *)aggregatedStringTables
214+
- (NSArray *)aggregatedStringTables
212215
{
213-
// wait for both of these things to finish
214-
[_processingQueue waitUntilAllOperationsAreFinished];
215-
dispatch_group_wait(_tableGroup, DISPATCH_TIME_FOREVER);
216-
217-
return [_stringTables allValues];
216+
// wait for both of these things to finish
217+
[_processingQueue waitUntilAllOperationsAreFinished];
218+
dispatch_group_wait(_tableGroup, DISPATCH_TIME_FOREVER);
219+
220+
return [_stringTables allValues];
218221
}
219222

220223
@end

Core/Source/DTLocalizableStringTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef void(^DTLocalizableStringEntryWriteCallback)(DTLocalizableStringEntry *)
2121

2222
- (void)addEntry:(DTLocalizableStringEntry *)entry;
2323

24-
- (NSString*)writeAsStringEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
24+
- (NSString *)stringRepresentationWithEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
2525
- (BOOL)writeToFolderAtURL:(NSURL *)url encoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
2626

2727
@end

Core/Source/DTLocalizableStringTable.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ - (void)addEntry:(DTLocalizableStringEntry *)entry
7575
[_entryIndexByKey setObject:entry forKey:entry.rawKey];
7676
}
7777

78-
- (NSString*)writeAsStringEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback
78+
- (NSString*)stringRepresentationWithEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback
7979
{
8080
NSArray *sortedEntries = [_entries sortedArrayUsingSelector:@selector(compare:)];
8181

@@ -143,7 +143,7 @@ - (BOOL)writeToFolderAtURL:(NSURL *)url encoding:(NSStringEncoding)encoding erro
143143
return NO;
144144
}
145145

146-
NSString *tmpString = [self writeAsStringEncoding:encoding error:error entryWriteCallback:entryWriteCallback];
146+
NSString *tmpString = [self stringRepresentationWithEncoding:encoding error:error entryWriteCallback:entryWriteCallback];
147147

148148
return [tmpString writeToURL:tableURL
149149
atomically:YES

Core/Source/NSString+DTLocalizableStringScanner.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ - (NSString *)stringByNumberingFormatPlaceholders
3232
[tmpString appendString:@"1$"];
3333
}
3434
[tmpString appendString:[self substringWithRange:NSMakeRange(lastLocation + 1, currentLocation - lastLocation)]];
35-
[tmpString appendFormat:@"%d$", placeholderCount];
35+
[tmpString appendFormat:@"%ld$", placeholderCount];
3636
}
3737
lastLocation = currentLocation;
3838
}];

Demo/Source/main.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main (int argc, const char *argv[])
2929
BOOL wantsDecodedUnicodeSequences = NO;
3030
NSMutableSet *tablesToSkip = [NSMutableSet set];
3131
NSString *customMacroPrefix = nil;
32-
NSString *customTableName = nil;
32+
NSString *defaultTableName = nil;
3333

3434
// analyze options
3535
BOOL optionsInvalid = NO;
@@ -150,7 +150,7 @@ int main (int argc, const char *argv[])
150150
break;
151151
}
152152

153-
customTableName = [NSString stringWithUTF8String:argv[i]];
153+
defaultTableName = [NSString stringWithUTF8String:argv[i]];
154154
}
155155

156156
i++;
@@ -171,7 +171,7 @@ int main (int argc, const char *argv[])
171171
aggregator.inputEncoding = inputStringEncoding;
172172
aggregator.customMacroPrefix = customMacroPrefix;
173173
aggregator.tablesToSkip = tablesToSkip;
174-
aggregator.customTableName = customTableName;
174+
aggregator.defaultTableName = defaultTableName;
175175

176176
// go, go, go!
177177
for (NSURL *file in files) {

0 commit comments

Comments
 (0)