2006-12-14 21:16:00 +01:00
|
|
|
/* NSArray+Utilities.m - this file is part of SOGo
|
|
|
|
*
|
2015-08-26 15:31:08 +02:00
|
|
|
* Copyright (C) 2006-2015 Inverse inc.
|
2006-12-14 21:16:00 +01:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2007-10-23 23:59:46 +02:00
|
|
|
#import <Foundation/NSNull.h>
|
2009-06-10 22:54:02 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2006-12-14 21:16:00 +01:00
|
|
|
|
|
|
|
#import "NSArray+Utilities.h"
|
|
|
|
|
|
|
|
@implementation NSArray (SOGoArrayUtilities)
|
|
|
|
|
2010-01-15 00:19:19 +01:00
|
|
|
+ (id) arrayWithObject: (id) member
|
|
|
|
repeatCount: (int) repeatCount
|
|
|
|
{
|
|
|
|
NSMutableArray *newArray;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
newArray = [NSMutableArray arrayWithCapacity: repeatCount];
|
|
|
|
for (count = 0; count < repeatCount; count++)
|
|
|
|
[newArray addObject: member];
|
|
|
|
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
2008-06-13 21:55:59 +02:00
|
|
|
- (id *) asPointersOfObjects
|
|
|
|
{
|
|
|
|
id *pointers;
|
|
|
|
unsigned int max;
|
|
|
|
|
|
|
|
max = [self count];
|
2010-01-05 23:34:35 +01:00
|
|
|
pointers = NSZoneMalloc (NULL, sizeof (id) * (max + 1));
|
2008-06-13 21:55:59 +02:00
|
|
|
[self getObjects: pointers];
|
|
|
|
*(pointers + max) = nil;
|
|
|
|
|
|
|
|
return pointers;
|
|
|
|
}
|
|
|
|
|
2006-12-14 21:16:00 +01:00
|
|
|
- (NSArray *) stringsWithFormat: (NSString *) format
|
|
|
|
{
|
|
|
|
NSMutableArray *formattedStrings;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
|
2007-08-15 21:50:20 +02:00
|
|
|
|
2006-12-14 21:16:00 +01:00
|
|
|
objects = [self objectEnumerator];
|
|
|
|
currentObject = [objects nextObject];
|
|
|
|
while (currentObject)
|
|
|
|
{
|
2007-10-23 23:59:46 +02:00
|
|
|
if ([currentObject isKindOfClass: [NSNull class]])
|
|
|
|
[formattedStrings addObject: @""];
|
|
|
|
else
|
|
|
|
[formattedStrings
|
|
|
|
addObject: [NSString stringWithFormat: format, currentObject]];
|
2006-12-14 21:16:00 +01:00
|
|
|
currentObject = [objects nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedStrings;
|
|
|
|
}
|
|
|
|
|
2007-08-15 21:50:20 +02:00
|
|
|
- (NSArray *) keysWithFormat: (NSString *) format
|
|
|
|
{
|
|
|
|
NSMutableArray *formattedStrings;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
|
|
|
|
|
|
|
|
objects = [self objectEnumerator];
|
|
|
|
currentObject = [objects nextObject];
|
|
|
|
while (currentObject)
|
|
|
|
{
|
|
|
|
[formattedStrings addObject: [currentObject keysWithFormat: format]];
|
|
|
|
currentObject = [objects nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) objectsForKey: (NSString *) key
|
2008-10-23 17:35:20 +02:00
|
|
|
notFoundMarker: (id) marker
|
2007-08-15 21:50:20 +02:00
|
|
|
{
|
|
|
|
NSMutableArray *objectsForKey;
|
|
|
|
unsigned int count, max;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
objectsForKey = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
value = [[self objectAtIndex: count] objectForKey: key];
|
2008-10-23 17:35:20 +02:00
|
|
|
if (value)
|
|
|
|
[objectsForKey addObject: value];
|
|
|
|
else if (marker)
|
|
|
|
[objectsForKey addObject: marker];
|
2007-08-15 21:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return objectsForKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) flattenedArray
|
|
|
|
{
|
|
|
|
NSMutableArray *flattenedArray;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
flattenedArray = [NSMutableArray array];
|
|
|
|
objects = [self objectEnumerator];
|
2009-07-23 19:32:09 +02:00
|
|
|
while ((currentObject = [objects nextObject]))
|
2011-06-01 23:10:25 +02:00
|
|
|
if ([currentObject isKindOfClass: [NSArray class]])
|
|
|
|
[flattenedArray addObjectsFromArray: [(NSArray *)currentObject flattenedArray]];
|
|
|
|
else
|
|
|
|
[flattenedArray addObject: currentObject];
|
2007-08-15 21:50:20 +02:00
|
|
|
|
|
|
|
return flattenedArray;
|
|
|
|
}
|
|
|
|
|
2009-09-22 23:11:08 +02:00
|
|
|
- (NSDictionary *) flattenedDictionaries
|
|
|
|
{
|
|
|
|
NSMutableDictionary *newDict;
|
|
|
|
NSDictionary *currentDictionary;
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
newDict = [NSMutableDictionary dictionary];
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
currentDictionary = [self objectAtIndex: count];
|
|
|
|
[newDict addEntriesFromDictionary: currentDictionary];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDict;
|
|
|
|
}
|
|
|
|
|
2008-02-22 22:21:24 +01:00
|
|
|
- (NSArray *) uniqueObjects
|
|
|
|
{
|
|
|
|
NSMutableArray *newArray;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
newArray = [NSMutableArray array];
|
|
|
|
|
|
|
|
objects = [self objectEnumerator];
|
|
|
|
while ((currentObject = [objects nextObject]))
|
|
|
|
[newArray addObjectUniquely: currentObject];
|
|
|
|
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
- (NSArray *) trimmedComponents
|
|
|
|
{
|
|
|
|
return [self resultsOfSelector: @selector (stringByTrimmingSpaces)];
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GNUSTEP_BASE_LIBRARY
|
2007-02-08 17:30:07 +01:00
|
|
|
- (void) makeObjectsPerform: (SEL) selector
|
|
|
|
withObject: (id) object1
|
|
|
|
withObject: (id) object2
|
|
|
|
{
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[[self objectAtIndex: count] performSelector: selector
|
|
|
|
withObject: object1
|
|
|
|
withObject: object2];
|
|
|
|
}
|
2010-01-05 23:34:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
- (NSArray *) resultsOfSelector: (SEL) operation
|
|
|
|
{
|
|
|
|
NSMutableArray *results;
|
|
|
|
int count, max;
|
|
|
|
id result;
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
results = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
result = [[self objectAtIndex: count] performSelector: operation];
|
|
|
|
[results addObject: result];
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2007-02-08 17:30:07 +01:00
|
|
|
|
2010-10-26 20:28:22 +02:00
|
|
|
- (NSArray *) mergedArrayWithArray: (NSArray *) otherArray
|
|
|
|
{
|
|
|
|
NSMutableArray *mergedArray;
|
|
|
|
NSUInteger count, max;
|
|
|
|
id object;
|
|
|
|
|
|
|
|
max = [otherArray count];
|
|
|
|
mergedArray = [NSMutableArray arrayWithCapacity: (max + [self count])];
|
|
|
|
[mergedArray setArray: self];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
object = [otherArray objectAtIndex: count];
|
|
|
|
if (![mergedArray containsObject: object])
|
|
|
|
[mergedArray addObject: object];
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergedArray;
|
|
|
|
}
|
|
|
|
|
2007-05-25 19:30:24 +02:00
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
id currentElement;
|
|
|
|
NSMutableArray *jsonElements;
|
|
|
|
NSEnumerator *elements;
|
|
|
|
NSString *representation;
|
|
|
|
|
2009-11-04 17:26:22 +01:00
|
|
|
jsonElements = [NSMutableArray array];
|
2007-05-25 19:30:24 +02:00
|
|
|
elements = [self objectEnumerator];
|
2009-11-04 17:26:22 +01:00
|
|
|
while ((currentElement = [elements nextObject]))
|
|
|
|
[jsonElements addObject: [currentElement jsonRepresentation]];
|
2007-05-25 19:30:24 +02:00
|
|
|
representation = [NSString stringWithFormat: @"[%@]",
|
|
|
|
[jsonElements componentsJoinedByString: @", "]];
|
|
|
|
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2007-06-01 06:13:22 +02:00
|
|
|
- (BOOL) containsCaseInsensitiveString: (NSString *) match
|
|
|
|
{
|
|
|
|
BOOL response;
|
|
|
|
NSString *currentString, *cmpObject;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
|
|
|
|
response = NO;
|
|
|
|
|
|
|
|
cmpObject = [match lowercaseString];
|
|
|
|
objects = [self objectEnumerator];
|
|
|
|
currentString = [objects nextObject];
|
|
|
|
while (currentString && !response)
|
|
|
|
if ([[currentString lowercaseString] isEqualToString: cmpObject])
|
|
|
|
response = YES;
|
|
|
|
else
|
|
|
|
currentString = [objects nextObject];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2006-12-14 21:16:00 +01:00
|
|
|
@end
|
2007-05-19 02:41:01 +02:00
|
|
|
|
|
|
|
@implementation NSMutableArray (SOGoArrayUtilities)
|
|
|
|
|
2009-06-10 22:54:02 +02:00
|
|
|
- (void) addNonNSObject: (void *) objectPtr
|
|
|
|
withSize: (size_t) objectSize
|
|
|
|
copy: (BOOL) doCopy
|
2007-05-19 02:41:01 +02:00
|
|
|
{
|
2009-06-10 22:54:02 +02:00
|
|
|
void *newObjectPtr;
|
|
|
|
|
|
|
|
if (doCopy)
|
|
|
|
{
|
|
|
|
newObjectPtr = NSZoneMalloc (NULL, objectSize);
|
|
|
|
memcpy (newObjectPtr, objectPtr, objectSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newObjectPtr = objectPtr;
|
|
|
|
|
|
|
|
[self addObject: [NSValue valueWithPointer: newObjectPtr]];
|
2007-05-19 02:41:01 +02:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:54:02 +02:00
|
|
|
- (void) freeNonNSObjects
|
2007-05-30 22:03:19 +02:00
|
|
|
{
|
2009-06-10 22:54:02 +02:00
|
|
|
unsigned int count, max;
|
|
|
|
void *objectPtr;
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
objectPtr = [[self objectAtIndex: count] pointerValue];
|
|
|
|
NSZoneFree (NULL, objectPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addObjectUniquely: (id) object
|
|
|
|
{
|
|
|
|
if (![self containsObject: object])
|
|
|
|
[self addObject: object];
|
2007-05-30 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2008-08-22 01:35:34 +02:00
|
|
|
- (BOOL) hasRangeIntersection: (NSRange) testRange
|
2007-05-30 22:03:19 +02:00
|
|
|
{
|
|
|
|
NSEnumerator *ranges;
|
2009-06-16 03:36:29 +02:00
|
|
|
NSValue *currentRangePtr;
|
|
|
|
NSRange *currentRange;
|
2007-05-30 22:03:19 +02:00
|
|
|
BOOL response;
|
|
|
|
|
|
|
|
response = NO;
|
|
|
|
|
|
|
|
ranges = [self objectEnumerator];
|
2009-06-16 03:36:29 +02:00
|
|
|
while (!response && (currentRangePtr = [ranges nextObject]))
|
2007-05-30 22:03:19 +02:00
|
|
|
{
|
2009-06-16 03:36:29 +02:00
|
|
|
currentRange = [currentRangePtr pointerValue];
|
|
|
|
if (NSLocationInRange (testRange.location, *currentRange)
|
2009-06-30 21:09:57 +02:00
|
|
|
|| NSLocationInRange (currentRange->location, testRange))
|
2007-05-30 22:03:19 +02:00
|
|
|
response = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2012-03-01 22:55:41 +01:00
|
|
|
- (void) removeDoubles
|
|
|
|
{
|
|
|
|
NSMutableDictionary *valueDicts;
|
|
|
|
NSNull *dummy;
|
|
|
|
NSUInteger count, max;
|
|
|
|
|
|
|
|
dummy = [NSNull null];
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
valueDicts = [NSMutableDictionary dictionaryWithCapacity: max];
|
|
|
|
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[valueDicts setObject: dummy forKey: [self objectAtIndex: count]];
|
|
|
|
[self setArray: [valueDicts allKeys]];
|
|
|
|
}
|
|
|
|
|
2007-05-19 02:41:01 +02:00
|
|
|
@end
|