7 #import <utilities/SecNSAdditions.h>
8 #include <utilities/SecCFWrappers.h>
12 @implementation NSArray (compactDescription)
13 - (NSMutableString*) concatenateWithSeparator: (NSString*) separator {
14 NSMutableString* result = [NSMutableString string];
15 NSString* currentSeparator = @"";
17 for(NSString* string in self) {
18 [result appendString:currentSeparator];
19 [result appendString:[string description]];
20 currentSeparator = separator;
29 @implementation NSDictionary (SOSDictionaryFormat)
30 - (NSString*) compactDescription
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: @" "]];
38 [elements addObject: [NSString stringWithFormat:@"%@", obj]];
41 return [elements componentsJoinedByString: @" "];
46 @implementation NSMutableDictionary (FindAndRemove)
47 -(NSObject*)extractObjectForKey:(NSString*)key
49 NSObject* result = [self objectForKey:key];
50 [self removeObjectForKey: key];
59 @implementation NSSet (Emptiness)
62 return [self count] == 0;
66 @implementation NSSet (HasElements)
67 - (bool) containsElementsNotIn: (NSSet*) other
69 __block bool hasElements = false;
70 [self enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
71 if (![other containsObject:obj]) {
81 @implementation NSSet (compactDescription)
82 - (NSString*) shortDescription {
83 return [NSString stringWithFormat: @"{[%@]}", [[[self allObjects] sortedArrayUsingSelector:@selector(compare:)] concatenateWithSeparator: @", "]];
87 @implementation NSSet (Stringizing)
88 - (NSString*) sortedElementsJoinedByString: (NSString*) separator {
89 return [self sortedElementsTruncated: 0 JoinedByString: separator];
92 - (NSString*) sortedElementsTruncated: (NSUInteger) length JoinedByString: (NSString*) separator
94 NSMutableArray* strings = [NSMutableArray array];
96 [self enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
97 NSString *stringToInsert = nil;
98 if ([obj isKindOfClass: [NSString class]]) {
101 stringToInsert = [obj description];
104 if (length > 0 && length < stringToInsert.length) {
105 stringToInsert = [stringToInsert substringToIndex:length];
108 [strings insertObject:stringToInsert atIndex:0];
111 [strings sortUsingSelector: @selector(compare:)];
113 return [strings componentsJoinedByString:separator];
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];
121 uint8_t* beginning = result.mutableBytes;
122 uint8_t* encode_begin = initialization(initialSize, beginning);
124 if (beginning <= encode_begin && encode_begin <= (encode_begin + initialSize)) {
125 [result replaceBytesInRange:NSMakeRange(0, encode_begin - beginning) withBytes:NULL length:0];