]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/SecNSAdditions.m
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / utilities / SecNSAdditions.m
1 //
2 // SecNSAdditions.m
3 // utilities
4 //
5
6
7 #import <utilities/SecNSAdditions.h>
8 #include <utilities/SecCFWrappers.h>
9
10 // MARK: NSArray
11
12 @implementation NSArray (compactDescription)
13 - (NSMutableString*) concatenateWithSeparator: (NSString*) separator {
14 NSMutableString* result = [NSMutableString string];
15 NSString* currentSeparator = @"";
16
17 for(NSString* string in self) {
18 [result appendString:currentSeparator];
19 [result appendString:[string description]];
20 currentSeparator = separator;
21 }
22
23 return result;
24 }
25 @end
26
27 // MARK: NSDictionary
28
29 @implementation NSDictionary (SOSDictionaryFormat)
30 - (NSString*) compactDescription
31 {
32 NSMutableArray *elements = [NSMutableArray array];
33 [self enumerateKeysAndObjectsUsingBlock: ^(NSString *key, id obj, BOOL *stop) {
34 [elements addObject: [key stringByAppendingString: @":"]];
35 if ([obj isKindOfClass:[NSArray class]]) {
36 [elements addObject: [(NSArray *)obj componentsJoinedByString: @" "]];
37 } else {
38 [elements addObject: [NSString stringWithFormat:@"%@", obj]];
39 }
40 }];
41 return [elements componentsJoinedByString: @" "];
42 }
43 @end
44
45
46 @implementation NSMutableDictionary (FindAndRemove)
47 -(NSObject*)extractObjectForKey:(NSString*)key
48 {
49 NSObject* result = [self objectForKey:key];
50 [self removeObjectForKey: key];
51 return result;
52 }
53 @end
54
55
56
57 // MARK: NSSet
58
59 @implementation NSSet (Emptiness)
60 - (bool) isEmpty
61 {
62 return [self count] == 0;
63 }
64 @end
65
66 @implementation NSSet (HasElements)
67 - (bool) containsElementsNotIn: (NSSet*) other
68 {
69 __block bool hasElements = false;
70 [self enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
71 if (![other containsObject:obj]) {
72 hasElements = true;
73 *stop = true;
74 }
75 }];
76 return hasElements;
77 }
78
79 @end
80
81 @implementation NSSet (compactDescription)
82 - (NSString*) shortDescription {
83 return [NSString stringWithFormat: @"{[%@]}", [[[self allObjects] sortedArrayUsingSelector:@selector(compare:)] concatenateWithSeparator: @", "]];
84 }
85 @end
86
87 @implementation NSSet (Stringizing)
88 - (NSString*) sortedElementsJoinedByString: (NSString*) separator {
89 return [self sortedElementsTruncated: 0 JoinedByString: separator];
90 }
91
92 - (NSString*) sortedElementsTruncated: (NSUInteger) length JoinedByString: (NSString*) separator
93 {
94 NSMutableArray* strings = [NSMutableArray array];
95
96 [self enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
97 NSString *stringToInsert = nil;
98 if ([obj isKindOfClass: [NSString class]]) {
99 stringToInsert = obj;
100 } else {
101 stringToInsert = [obj description];
102 }
103
104 if (length > 0 && length < stringToInsert.length) {
105 stringToInsert = [stringToInsert substringToIndex:length];
106 }
107
108 [strings insertObject:stringToInsert atIndex:0];
109 }];
110
111 [strings sortUsingSelector: @selector(compare:)];
112
113 return [strings componentsJoinedByString:separator];
114 }
115 @end
116
117 @implementation NSMutableData (filledAndClipped)
118 + (instancetype) dataWithSpace: (NSUInteger) initialSize DEREncode: (uint8_t*(^)(size_t size, uint8_t *buffer)) initialization {
119 NSMutableData* result = [NSMutableData dataWithLength: initialSize];
120
121 uint8_t* beginning = result.mutableBytes;
122 uint8_t* encode_begin = initialization(initialSize, beginning);
123
124 if (beginning <= encode_begin && encode_begin <= (encode_begin + initialSize)) {
125 [result replaceBytesInRange:NSMakeRange(0, encode_begin - beginning) withBytes:NULL length:0];
126 } else {
127 result = nil;
128 }
129
130 return result;
131 }
132 @end