-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSString+Utilities.mm
590 lines (476 loc) · 15.2 KB
/
NSString+Utilities.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
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
587
588
589
590
//
// author: fabrice truillot de chambrier
//
// © 2009-2015, men in silicium sàrl
//
#import "OS/NSString+Utilities.h"
#import "OS/ARC.h"
#import "OS/NSData+Utilities.h"
#import <CommonCrypto/CommonCrypto.h>
#import <Security/Security.h>
#import <Security/SecEncodeTransform.h>
namespace
{
NSArray* rfc1123DateFormatters = nil;
NSArray* jsonDateFormatters = nil;
}
@implementation NSString (Utilities)
+ (instancetype) newWithInt: (int) value
{
return [NSString stringWithInt: value];
}
+ (instancetype) newWithUnsignedInt: (unsigned int) value
{
return [NSString stringWithUnsignedInt: value];
}
+ (instancetype) newWithBCDInt: (unsigned long long) value
{
return [NSString stringWithBCDInt: value];
}
+ (instancetype) newHexadecimalWithNumber: (NSNumber*) number
{
return [NSString stringHexadecimalWithNumber: number];
}
+ (instancetype) newHexadecimalWithData: (NSData*) data
{
return [NSString stringHexadecimalWithData: data];
}
+ (instancetype) newBase64WithData: (NSData*) data
{
return [NSString stringBase64WithData: data];
}
+ (instancetype) newWithFormat: (NSString*) format, ...
{
va_list arguments;
va_start( arguments, format );
NSString* string = [NSString stringWithFormat: format arguments: arguments];
va_end( arguments );
return string;
}
+ (instancetype) newWithFormat: (NSString*) format arguments: (va_list) arguments
{
return [NSString stringWithFormat: format arguments: arguments];
}
+ (instancetype) stringWithInt: (int) value
{
return [NSString stringWithString: @(value).stringValue];
}
+ (instancetype) stringWithUnsignedInt: (unsigned int) value
{
return [NSString stringWithString: @(value).stringValue];
}
+ (instancetype) stringWithBCDInt: (unsigned long long) value
{
NSMutableString* string = nil;
for ( int32_t index = sizeof (value) - 1 ; index >= 0 ; --index )
{
UInt8 byte = (UInt8) ((value >> (index * 8)) & 0xff);
if ( (string == nil) and (byte == 0) ) continue;
UInt8 bcdHigh = (byte >> 4) & 0x0f;
UInt8 bcdLow = byte & 0x0f;
if ( (string == nil) and (bcdHigh != 0) ) string = [NSMutableString new];
if ( string != nil )
{
switch ( bcdHigh )
{
case 0:
[string appendString: @"0"];
break;
case 1:
[string appendString: @"1"];
break;
case 2:
[string appendString: @"2"];
break;
case 3:
[string appendString: @"3"];
break;
case 4:
[string appendString: @"4"];
break;
case 5:
[string appendString: @"5"];
break;
case 6:
[string appendString: @"6"];
break;
case 7:
[string appendString: @"7"];
break;
case 8:
[string appendString: @"8"];
break;
case 9:
[string appendString: @"9"];
break;
}
}
if ( (string == nil) and (bcdLow != 0) ) string = [NSMutableString new];
if ( string != nil )
{
switch ( bcdLow )
{
case 0:
[string appendString: @"0"];
break;
case 1:
[string appendString: @"1"];
break;
case 2:
[string appendString: @"2"];
break;
case 3:
[string appendString: @"3"];
break;
case 4:
[string appendString: @"4"];
break;
case 5:
[string appendString: @"5"];
break;
case 6:
[string appendString: @"6"];
break;
case 7:
[string appendString: @"7"];
break;
case 8:
[string appendString: @"8"];
break;
case 9:
[string appendString: @"9"];
break;
}
}
} // for
if ( string != nil )
{
return [NSString stringWithString: string];
}
else
{
return @"0";
}
}
+ (instancetype) stringHexadecimalWithNumber: (NSNumber*) number
{
unsigned long long value = number.unsignedLongLongValue;
NSData* data = [NSData newWithBytes: &value length: sizeof( value )];
return [self stringHexadecimalWithData: data];
}
+ (instancetype) stringHexadecimalWithData: (NSData*) data
{
NSMutableString* string = [NSMutableString stringWithCapacity: data.length * 2];
for ( NSUInteger index = 0 ; index < data.length ; ++index )
{
uint8_t byte = 0;
[data getBytes: &byte range: NSMakeRange( index, 1 )];
[string appendFormat: @"%hhx", byte];
}
return [self stringWithString: string];
}
+ (instancetype) stringBase64WithData: (NSData*) data
{
CFErrorRef error = nullptr;
SecTransformRef encoder = SecEncodeTransformCreate( kSecBase64Encoding, &error );
if ( error ) { CFShow( error ); exit( -1 ); }
SecTransformSetAttribute( encoder, kSecTransformInputAttributeName, (__bridge CFDataRef) data, &error );
NSData* encodedData = (__arc_claim NSData*) SecTransformExecute( encoder, &error );
NSString* encodedString = [[NSString alloc] initWithData: encodedData encoding: NSUTF8StringEncoding];
CFReleaseSafe( encoder );
encoder = nullptr;
return encodedString;
}
+ (instancetype) stringWithFormat: (NSString*) format arguments: (va_list) arguments
{
return [[NSString alloc] initWithFormat: format arguments: arguments];
}
- (void) setupRFC1123DateFormatters
{
if ( rfc1123DateFormatters == nil )
{
NSMutableArray* dateFormatters = [NSMutableArray new];
NSDateFormatter* dateFormatter = nil;
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSTimeZone* timeZone = [[NSTimeZone alloc] initWithName: @"UTC"];
// @"EEE',' dd MMM yyyy HH':'mm':'ss z"
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss z";
[dateFormatters addObject: dateFormatter];
// @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z"
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z";
[dateFormatters addObject: dateFormatter];
// @"EEE MMM d HH':'mm':'ss yyyy"
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"EEE MMM d HH':'mm':'ss yyyy";
[dateFormatters addObject: dateFormatter];
rfc1123DateFormatters = [dateFormatters copy];
}
}
- (void) setupJSONDateFormatters
{
if ( jsonDateFormatters == nil )
{
NSMutableArray* dateFormatters = [NSMutableArray new];
NSDateFormatter* dateFormatter = nil;
//NSLocale* localeUS = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"];
NSTimeZone* timeZone = [[NSTimeZone alloc] initWithName: @"UTC"];
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
[dateFormatters addObject: dateFormatter];
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'";
[dateFormatters addObject: dateFormatter];
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
[dateFormatters addObject: dateFormatter];
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
[dateFormatters addObject: dateFormatter];
dateFormatter = [NSDateFormatter new];
dateFormatter.locale = locale;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
[dateFormatters addObject: dateFormatter];
jsonDateFormatters = [dateFormatters copy];
}
}
- (BOOL) isMutable
{
return [self isMutableString];
}
- (BOOL) isMutableString
{
return [self respondsToSelector: @selector(appendString:)];
}
- (NSUInteger) unsignedIntegerValue
{
return [NSDecimalNumber decimalNumberWithString: self].unsignedIntegerValue;
}
- (uint64_t) unsignedLongLongValue
{
return [NSDecimalNumber decimalNumberWithString: self].unsignedLongLongValue;
}
- (NSString*) stringValue
{
return self;
}
- (NSNumber*) numberJSON
{
NSNumberFormatter* formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"];
return [formatter numberFromString: self];
}
- (NSDate*) dateRFC1123
{
if ( self.length == 0 ) return nil;
if ( rfc1123DateFormatters == nil ) [self setupRFC1123DateFormatters];
NSDate* date = nil;
for ( NSDateFormatter* dateFormatter in jsonDateFormatters )
{
date = [dateFormatter dateFromString: self];
if ( date != nil ) break;
}
return date;
}
- (NSDate*) dateJSON
{
if ( self.length == 0 ) return nil;
NSDate* date = nil;
// NSScanner @"yyyy-mm-ddThh:mm:ss.ssssssZ"
CFGregorianDate gregorianDate = {};
NSScanner* scanner = [[NSScanner alloc] initWithString: self];
scanner.locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"];
BOOL ok = YES;
int scannedInt = 0;
double scannedDouble = 0;
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.year = (SInt32) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @"-" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.month = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @"-" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.day = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @"T" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.hour = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @":" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.minute = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @":" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanDouble: &scannedDouble];
if ( ok ) gregorianDate.second = scannedDouble;
if ( ok ) ok = ok and [scanner scanString: @"Z" intoString: nullptr];
if ( ok )
{
NSTimeZone* timeZone = [[NSTimeZone alloc] initWithName: @"UTC"];
CFAbsoluteTime absoluteTime = CFGregorianDateGetAbsoluteTime( gregorianDate, (__bridge CFTimeZoneRef) timeZone );
date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: absoluteTime];
}
if ( date != nil ) return date;
// NSDateFormatter
if ( jsonDateFormatters == nil ) [self setupJSONDateFormatters];
for ( NSDateFormatter* dateFormatter in jsonDateFormatters )
{
date = [dateFormatter dateFromString: self];
if ( date != nil ) break;
}
return date;
}
- (NSDate*) dateJSON2
{
if ( self.length == 0 ) return nil;
NSDate* date = nil;
// NSScanner @"yyyy-mm-dd hh:mm:ss.ssssss"
CFGregorianDate gregorianDate = {};
NSScanner* scanner = [[NSScanner alloc] initWithString: self];
scanner.locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"];
BOOL ok = YES;
int scannedInt = 0;
double scannedDouble = 0;
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.year = (SInt32) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @"-" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.month = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @"-" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.day = (SInt8) scannedInt;
//if ( ok ) ok = ok and [scanner scanString: @" " intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.hour = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @":" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanInt: &scannedInt];
if ( ok ) gregorianDate.minute = (SInt8) scannedInt;
if ( ok ) ok = ok and [scanner scanString: @":" intoString: nullptr];
if ( ok ) ok = ok and [scanner scanDouble: &scannedDouble];
if ( ok ) gregorianDate.second = scannedDouble;
if ( ok )
{
NSTimeZone* timeZone = [[NSTimeZone alloc] initWithName: @"UTC"];
CFAbsoluteTime absoluteTime = CFGregorianDateGetAbsoluteTime( gregorianDate, (__bridge CFTimeZoneRef) timeZone );
date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: absoluteTime];
}
return date;
}
- (void) locationISO6709
{
// Documentation: http://en.wikipedia.org/wiki/ISO_6709
__unused NSString* coordinate = @"+44.1617+004.6209+057.000/";
}
- (BOOL) containsString: (NSString*) string
{
return [self rangeOfString: string].length != 0;
}
- (BOOL) containsStringCaseInsensitive: (NSString*) string
{
return [self rangeOfString: string options: NSCaseInsensitiveSearch].length != 0;
}
- (BOOL) isEqualToStringCaseInsensitive: (NSString*) string
{
return [self caseInsensitiveCompare: string] == NSOrderedSame;
}
- (BOOL) hasPrefixCaseInsensitive: (NSString*) prefix
{
if ( [self length] > [prefix length] ) {
return [[self substringToIndex: [prefix length]] caseInsensitiveCompare: prefix] == NSOrderedSame;
}
if ( [self length] == [prefix length] ) {
return [self caseInsensitiveCompare: prefix] == NSOrderedSame;
}
return false;
}
- (NSString*) substringFromIndex: (NSUInteger) from included: (BOOL) included
{
if ( included ) return [self substringFromIndex: from];
else return [self substringFromIndex: from + 1];
}
- (NSString*) substringToString: (NSString*) string
{
NSRange range = [self rangeOfString: string];
NSUInteger index = range.location;
return [self substringToIndex: index];
}
- (NSString*) substringFromString: (NSString*) string
{
NSRange range = [self rangeOfString: string];
NSUInteger index = range.location;
return [self substringFromIndex: index];
}
- (NSString*) substringFromString: (NSString*) string included: (BOOL) included
{
NSRange range = [self rangeOfString: string];
if ( included ) return [self substringFromIndex: range.location];
else return [self substringFromIndex: range.location + range.length];
}
- (NSData*) dataUsingUTF8StringEncoding
{
return [self dataUsingEncoding: NSUTF8StringEncoding];
}
- (NSData*) dataDigestMD5
{
return [self _dataDigestWithType: kSecDigestMD5 length: 128];
}
- (NSString*) digestMD5
{
return [NSString stringHexadecimalWithData: [self dataDigestMD5]];
}
- (NSData*) dataDigestSHA2
{
return [self _dataDigestWithType: kSecDigestSHA2 length: 256];
}
- (NSString*) digestSHA2
{
return [NSString stringHexadecimalWithData: [self dataDigestSHA2]];
}
- (NSData*) dataDigestHMACSHA2
{
return [self _dataDigestWithType: /* kSecDigestHMACSHA2 is broken */ CFSTR( "HMAC-SHA2 Digest Family" ) length: 256];
}
- (NSData*) _dataDigestWithType: (CFTypeRef) digestType length: (CFIndex) length
{
NSData* data = [self dataUsingEncoding: NSUTF8StringEncoding];
if ( data == nil ) return nil;
CFErrorRef error = nullptr;
SecTransformRef encoder = SecDigestTransformCreate( digestType, length, &error );
if ( encoder == nullptr ) return nil;
if ( error != nullptr )
{
CFReleaseSafe( encoder );
encoder = nullptr;
return nil;
}
SecTransformSetAttribute( encoder, kSecTransformInputAttributeName, (__bridge CFDataRef) data, &error );
if ( error != nullptr )
{
CFReleaseSafe( encoder );
encoder = nullptr;
return nil;
}
NSData* encodedData = (__arc_claim NSData*) SecTransformExecute( encoder, &error );
CFReleaseSafe( encoder );
encoder = nullptr;
if ( (encodedData == nil) or (error != nullptr) ) {
return nil;
}
return encodedData;
}
@end