-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSString+ETCategories.m
executable file
·586 lines (518 loc) · 18.2 KB
/
NSString+ETCategories.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// NSString+ETCategories.m
//
// Author: ab
//
// CVS Info: $Id: NSString+ETCategories.m,v 1.11 2000/10/30 17:55:52 cmp Exp $
//
// Copyright (c) 1996-1999 Annard Brouwer. All rights reserved.
// Read the "License" file included in the distribution to find out what you can do with this code.
//#import "ETFoundationMessages.h"
//#import "ETFoundationExceptions.h"
//#import "ETDefaults.h"
//#import "NSCharacterSet+ETCategories.h"
#import "NSData+ETCategories.h"
#import "NSString+ETCategories.h"
#define CF_CLEAN_STR_MAP_KEY @"cleansedStringMapping"
#define CF_URL_STR_MAP_KEY @"urlStringMapping"
#define KBYTES_BASE (unsigned long long)1024
#define MBYTES_BASE (unsigned long long)(1000 * KBYTES_BASE)
#define GBYTES_BASE (unsigned long long)(1000 * MBYTES_BASE)
#define TBYTES_BASE (unsigned long long)(1000 * GBYTES_BASE)
#define PBYTES_BASE (unsigned long long)(1000 * TBYTES_BASE)
// Boundaries of Unicode character ranges not to escape for URIs
#define ASCII_NUMERIC_START 0x0030
#define ASCII_NUMERIC_END 0x0039
#define ASCII_UPPERCASE_START 0x0041
#define ASCII_UPPERCASE_END 0x005a
#define ASCII_LOWERCASE_START 0x0061
#define ASCII_LOWERCASE_END 0x007a
@interface NSString (ETStringEncodingPrivate)
static NSCharacterSet *_escapedCharacterSet;
static NSCharacterSet *_quotedCharacterSet;
+ (NSCharacterSet *)escapedCharacterSet;
+ (NSCharacterSet *)quotedCharacterSet;
@end
@implementation NSString (ETCategories)
//
// These categories enhance the NSString class cluster which extra functionality.
//
// Defines key/values:
// * "cleansedStringMapping" (dictionary): key is a string which will be replaced
// by its value
// * "urlStringMapping" (dictionary): key is a character string which will be
// replaced by an encoded sequence for url GET method key/value encoding
//
+ (NSString *)stringWithData:(NSData *)someData
encoding:(NSStringEncoding)anEncoding
{
return [[[self alloc] initWithData: someData
encoding: anEncoding] autorelease];
}
+ (NSString *)emptyString
// Returns the empty string.
{
return @"";
}
+ (id)readPropertyListFromFile:(NSString *)aPath
mustBeOfType:(Class)targetClass
logErrors:(BOOL)showErrors
// Invokes 'readPropertyListFromFile:encoding:mustBeOfType:logErrors:'
// with the NSISOLatin1StringEncoding.
{
return [self readPropertyListFromFile: aPath
encoding: NSISOLatin1StringEncoding
mustBeOfType: targetClass
logErrors: showErrors];
}
+ (id)readPropertyListFromFile:(NSString *)aPath
encoding:(NSStringEncoding)anEncoding
mustBeOfType:(Class)targetClass
logErrors:(BOOL)showErrors
// Reads a property file from aPath with the given encoding and returns the
// property list decoding of it. If showErrors equals YES the errors will
// be logged using NSLog(), if it equals NO it will raise.
// ETFilePplParseErrorException is raised if an error occurs while parsing
// the file or when the returned class is not equal to targetClass.
{
NSString *string;
id plist;
plist = nil;
string = [[NSString allocWithZone: NULL]
initWithData: [NSData dataWithContentsOfFile: aPath]
encoding: anEncoding];
if (showErrors && nil == string)
{
NSLog(@"%@: string could not be read from '%@'",
NSStringFromSelector (_cmd), aPath);
}
NS_DURING
plist = [string propertyList];
[string release];
NS_HANDLER
[string release];
plist = nil;
if (showErrors)
NSLog(@"%@: exception was raised while parsing: %@",
NSStringFromSelector (_cmd), localException);
else
[NSException raise: ETFilePplParseErrorException
format: ET_PPL_FILE_PARSE_ERROR, aPath, [localException reason]];
NS_ENDHANDLER
if (Nil != targetClass && ![plist isKindOfClass: targetClass])
{
if (showErrors)
NSLog(@"%@: property list is not of desired type '%@'. It's an '%@'.",
NSStringFromSelector (_cmd), NSStringFromClass (targetClass),
NSStringFromClass ([plist class]));
else
[NSException raise: ETFilePplParseErrorException
format: ET_PPL_FILE_PARSE_ERROR, aPath,
[NSString stringWithFormat: @"Wrong class: %@",
NSStringFromClass ([plist class])]];
plist = nil;
}
return plist;
}
+ (NSString *)formattedStringForNumberOfBytes:(NSNumber *)bytesNumber;
// Convenience for WebScript to pretty print a number of bytes.
//
// See also: -formattedStringForBytes:
{
return [self formattedStringForBytes: (unsigned long long)[bytesNumber intValue]];
}
+ (NSString *)formattedStringForBytes:(unsigned long long)bytes
// Takes a number of bytes and returns a nice, user-friendly string describing
// how many bytes there are. For example: passing in 1100000 bytes returns "1.07 MB".
{
if (bytes > 0)
{
// Return as bytes (under 1K)
if (bytes < KBYTES_BASE)
{
// under 1 K, display as some number of bytes
return [NSString stringWithFormat: ET_STRING_BYTES_FORMAT_STRING, bytes];
}
else if (bytes < MBYTES_BASE)
{
return [NSString stringWithFormat: ET_STRING_KBYTES_FORMAT_STRING, (float)bytes / KBYTES_BASE];
}
else if (bytes < GBYTES_BASE)
{
// Return as MegaBytes
return [NSString stringWithFormat: ET_STRING_MBYTES_FORMAT_STRING, (float)bytes / MBYTES_BASE];
}
else if (bytes < TBYTES_BASE)
{
// Return as GigaBytes
return [NSString stringWithFormat: ET_STRING_GBYTES_FORMAT_STRING, (float)bytes / GBYTES_BASE];
}
else if (bytes < PBYTES_BASE)
{
// Return as TeraBytes
return [NSString stringWithFormat: ET_STRING_TBYTES_FORMAT_STRING, (float)bytes / TBYTES_BASE];
}
else if (bytes >= PBYTES_BASE)
{
// Return as PetaBytes
return [NSString stringWithFormat: ET_STRING_PBYTES_FORMAT_STRING, (float)bytes / PBYTES_BASE];
}
}
else
{
// Return 0 Bytes.
return [NSString stringWithFormat: ET_STRING_BYTES_FORMAT_STRING, 0];
}
return self;
}
+ (NSString *)formattedStringForNumberOfSeconds:(NSNumber *)secondsNumber;
// By taking in an NSNumber consisting of seconds, this method will
// use this information to return a user-friendly string displaying
// time as hours, minutes and seconds.
{
int seconds;
NSString *formattedString;
seconds = [secondsNumber intValue];
formattedString = [NSString emptyString];
// Return empty string if supplied interval is nil
if (!seconds)
return formattedString;
if (seconds < 60)
{
// under 1 minute: display as xx seconds
formattedString = [NSString stringWithFormat: ET_STRING_SEC_FORMAT_STRING, seconds];
}
else
{
int minutesInHour;
int secondsInMinute;
if (seconds > 60 && seconds < 3600)
{
// less than 1 hour: display as xx minutes xx seconds
minutesInHour = seconds / 60;
secondsInMinute = seconds - (minutesInHour * 60);
formattedString = [NSString stringWithFormat: ET_STRING_MIN_SEC_FORMAT_STRING,
minutesInHour, secondsInMinute];
}
else if (seconds > 3600)
{
int hours;
// more than 1 hour: display as xx hours xx minutes xx seconds
hours = seconds / 3600;
minutesInHour = (seconds - (hours * 3600)) / 60;
secondsInMinute = (seconds - (hours * 3600) - (minutesInHour * 60));
formattedString = [NSString stringWithFormat: ET_STRING_HR_MIN_SEC_FORMAT_STRING,
hours, minutesInHour, secondsInMinute];
}
}
return formattedString;
}
- (NSString *)stringWithCharactersFromSet:(NSCharacterSet *)aSet
// Find the substring of the receiver composed of members of the given
// aSet.
{
unsigned int len;
NSScanner *scanner;
NSMutableString *newStr;
len = [self length];
scanner = [NSScanner localizedScannerWithString: self];
newStr = [NSMutableString stringWithCapacity: len];
while ([scanner scanLocation] < len)
{
NSString *str;
if ([scanner scanCharactersFromSet: aSet intoString: &str])
[newStr appendString: str];
else
break;
}
return newStr;
}
- (NSString *)stringWithStrippedWhiteSpace
// Cover method for `stringWithCharactersFromSet:' which removes the
// members of [NSCharacterSet
// nonWhitespaceOrCarriageReturnOrNewlineCharacterSet].
{
return [self stringWithCharactersFromSet:
[NSCharacterSet nonWhitespaceOrCarriageReturnOrNewlineCharacterSet]];
}
- (NSString *)stringByTruncatingWhiteSpace
// Returns an autoreleased string with contents equal to that of the receiver
// minus any trailing whitespace.
{
int charPtr;
charPtr = [self length];
while (--charPtr >= 0)
{
if (![[NSCharacterSet whitespaceAndNewlineCharacterSet]
characterIsMember: [self characterAtIndex: charPtr]])
return [self substringToIndex: charPtr + 1];
}
return [NSString emptyString];
}
- (NSString *)stringWithTrimmedWhitespace
// Removes whitespace from the beginning and the end of the string.
{
NSString *truncStr;
int charPtr;
truncStr = [self stringByTruncatingWhiteSpace];
if ([truncStr length] > 0)
{
charPtr = 0;
while ([[NSCharacterSet whitespaceAndNewlineCharacterSet]
characterIsMember: [truncStr characterAtIndex: charPtr]])
{
charPtr++;
}
if (charPtr > 0)
return [truncStr substringFromIndex: charPtr];
}
return truncStr;
}
- (BOOL)containsOnlyDigits
//Checks to see if all of the character in the given string are digits.
{
int i;
for (i = [self length]-1; i>=0; i--)
{
char currentChar = [self characterAtIndex:i];
if (isdigit(currentChar))
{
continue;
} else {
return NO;
}
}
return YES;
}
- (BOOL)stringConsistsOfWhitespace
// Returns YES if string consists of whitespace only, otherwise returns NO.
{
NSCharacterSet *whiteSpaceSet;
unsigned len;
unsigned index;
whiteSpaceSet = [NSCharacterSet whitespaceAndCarriageReturnAndNewlineCharacterSet];
len = [self length];
for (index=0; index < len; index++)
{
if (![whiteSpaceSet characterIsMember: [self characterAtIndex: index]])
return NO;
}
return YES;
}
- (NSData *)base64DecodedStringData
{
return [NSData dataWithBase64String: self];
}
- (NSString *)uriEncodedString
// Convert the receiver into an http-safe string for passing key/value pairs
// when using the GET method to transfer information using an URI.
{
const char *utf8DataPtr;
NSMutableData *tempData;
NSMutableString *escString;
NSString *retStr;
utf8DataPtr = [self UTF8String];
tempData = [[NSMutableData alloc] initWithCapacity: [self length]];
escString = [[NSMutableString alloc] initWithCapacity: 4];
while (*utf8DataPtr)
{
unsigned char testChar;
testChar = *utf8DataPtr++;
if ((testChar >= ASCII_NUMERIC_START && testChar <= ASCII_NUMERIC_END)
|| (testChar >= ASCII_UPPERCASE_START && testChar <= ASCII_UPPERCASE_END)
|| (testChar >= ASCII_LOWERCASE_START && testChar <= ASCII_LOWERCASE_END))
{
char c;
c = (char)testChar; // This is safe now
[tempData appendBytes: &c length: sizeof (char)];
}
else
{
// Escape it using the value in (uppercased) hex with a `%' in front of it.
[escString appendFormat: @"%%%02X", testChar];
[tempData appendData: [escString dataUsingEncoding: NSASCIIStringEncoding]];
[escString setString: @""];
}
}
retStr = [NSString stringWithData: tempData encoding: NSASCIIStringEncoding];
[tempData release];
[escString release];
return retStr;
}
- (NSString *)cleansedString
// This method converts strings with "illegal" characters in strings to
// more "usable" strings. The mapping is done according to the dictionary
// which can be found in the defaults under the `cleansedStringMapping'
// key.
{
NSDictionary *mappingDictionary;
mappingDictionary = [[ETDefaults standardDefaults]
dictionaryForKey: CF_CLEAN_STR_MAP_KEY];
if (!mappingDictionary || ![mappingDictionary count])
[NSException raise: ETDefaultValueNotFoundException
format: ET_DEFAULT_MISSING_KEY_VALUE, CF_CLEAN_STR_MAP_KEY, @"<none>"];
// Make this string "windows safe"
#if defined(WIN32)
return [[self encodedStringUsingDictionary: mappingDictionary] winSafePathComponent];
#endif
return [self encodedStringUsingDictionary: mappingDictionary];
}
- (NSString *)encodedStringUsingDictionary:(NSDictionary *)encodingDictionary
// General method for converting a string using encodingDictionary. Each
// key in encodingDictionary found in the receiver will be replaced by
// the corresponding value. Returns the newly encoded string.
{
NSEnumerator *encodingEnumerator;
NSString *mapKey;
NSString *tmpString;
tmpString = [NSString stringWithString: self];
encodingEnumerator = [encodingDictionary keyEnumerator];
while ((mapKey = [encodingEnumerator nextObject]))
{
tmpString = [[tmpString componentsSeparatedByString: mapKey]
componentsJoinedByString: [encodingDictionary objectForKey: mapKey]];
}
return tmpString;
}
/* NOTE: It is a bad idea to have an "isEmpty" method because of the tendancy for
a developer to write code such as "if ([aString isEmpty]) then", because
if aString is nil, this will evaluate to NO (FALSE), which is not what
any sane person would want to occur. The opposite "isNotEmpty" method
is OK because it will behave logically if aString is nil.
- (BOOL)isEmpty
// is the string an empty string (eg, length 0)
{
if ( [self length] > 0 )
return NO;
return YES;
}
*/
- (BOOL)isNotEmpty
// does the string contain at least 1 character?
{
if ([self length] > 0) {
return YES;
} else {
return NO;
}
}
- (BOOL)containsNonWhitespaceCharacter
// does the string contain at least 1 non-whitespace character?
{
if ([self length] > 0) {
return (! [self stringConsistsOfWhitespace]);
} else {
return NO;
}
}
- (BOOL)containsString: (NSString *)aString
// does this string contain the aString argument?
{
if (([self length] == 0) || ([aString length] == 0)) {
return NO;
} else {
NSRange range = [self rangeOfString:aString];
return (range.length > 0) ? YES : NO;
}
}
- (SEL)selectorValue
{
return NSSelectorFromString(self);
}
- (NSString *)asAccessor
// automagically generate accessor name
// assumes accessor name is lower case for first character
{
if ( [self isNotEmpty] )
{
return [NSString stringWithFormat:@"%@%@",
[[self substringToIndex:1] lowercaseString], [self substringFromIndex:1]];
}
return self;
}
- (NSString *)asSetter
// autmagically generate setter name
{
if ([self isNotEmpty]) {
return [NSString stringWithFormat:@"set%@%@:", [[self substringToIndex:1] uppercaseString], [self substringFromIndex:1]];
}
return self;
}
// ***** ETStringEncodingProtocol implementation *****
- (NSString *)stringEncoding
// Returns a description of this string in ppl list format. The string
// will be quoted if it contains characters other than letters. If the
// characters '"' and '\' are present, they will be escaped with a
// backslash character.
{
NSMutableString *escapedString;
NSRange escapedCharRange, quoteStringRange;
if ([self length] == 0)
return @"\"\"";
escapedString = nil;
// If the string contains '"' or '\' we have to escape those with a \.
escapedCharRange = [self rangeOfCharacterFromSet: [NSString escapedCharacterSet]];
while (NSNotFound != escapedCharRange.location)
{
NSString *replacementString;
NSRange newSearchRange;
if (!escapedString)
escapedString = [NSMutableString stringWithString: self];
replacementString = [NSString stringWithFormat: @"\\%@",
[escapedString substringWithRange: escapedCharRange]];
[escapedString replaceCharactersInRange: escapedCharRange
withString: replacementString];
// Set the new search range.
newSearchRange.location = escapedCharRange.location + 2;
newSearchRange.length = [escapedString length] - newSearchRange.location;
// Test if the end of the string has been reached.
if (newSearchRange.length <= 0)
break;
// Find new character to escape...
escapedCharRange = [escapedString rangeOfCharacterFromSet: [NSString escapedCharacterSet]
options: NSLiteralSearch
range: newSearchRange];
}
if (!escapedString)
escapedString = (NSMutableString *)self;
quoteStringRange = [escapedString rangeOfCharacterFromSet: [NSString quotedCharacterSet]];
if (NSNotFound == quoteStringRange.location)
return escapedString;
else
return [NSString stringWithFormat: @"\"%@\"", escapedString];
}
- (BOOL)caseInsensitiveIsEqual:(NSString *)otherString
// Return YES if the two strings are equal, ignoring case.
// Really just a cover method for caseInsensitiveCompare:,
// but returns a BOOL, so you can use it any place you might
// otherwise use isEqual:
{
return ([self caseInsensitiveCompare:otherString] == NSOrderedSame);
}
@end
@implementation NSString (ETStringEncodingPrivate)
+ (NSCharacterSet *)escapedCharacterSet
// Returns a set of characters to escape for ETStringEncodingProtocol,
// these are: '"' and '\'.
{
if (!_escapedCharacterSet)
{
_escapedCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:
@"\"\\"] retain];
}
return _escapedCharacterSet;
}
+ (NSCharacterSet *)quotedCharacterSet
// Returns a set of characters which are not alphanumeric or `_'.
{
if (!_quotedCharacterSet)
{
NSMutableCharacterSet *tempSet;
tempSet = [[NSCharacterSet ASCIIAlphanumericCharacterSet] mutableCopy];
[tempSet addCharactersInString: @"_"];
_quotedCharacterSet = [tempSet invertedSet];
[tempSet release];
[_quotedCharacterSet retain];
}
return _quotedCharacterSet;
}
@end