2006-12-14 21:16:00 +01:00
|
|
|
/* NSArray+Utilities.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Inverse groupe conseil
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#import "NSArray+Utilities.h"
|
|
|
|
|
|
|
|
@implementation NSArray (SOGoArrayUtilities)
|
|
|
|
|
|
|
|
- (NSArray *) stringsWithFormat: (NSString *) format
|
|
|
|
{
|
|
|
|
NSMutableArray *formattedStrings;
|
|
|
|
NSEnumerator *objects;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
|
|
|
|
objects = [self objectEnumerator];
|
|
|
|
currentObject = [objects nextObject];
|
|
|
|
while (currentObject)
|
|
|
|
{
|
|
|
|
[formattedStrings
|
|
|
|
addObject: [NSString stringWithFormat: format, currentObject]];
|
|
|
|
currentObject = [objects nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedStrings;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2007-05-25 19:30:24 +02:00
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
id currentElement;
|
|
|
|
NSMutableArray *jsonElements;
|
|
|
|
NSEnumerator *elements;
|
|
|
|
NSString *representation;
|
|
|
|
|
|
|
|
jsonElements = [NSMutableArray new];
|
|
|
|
|
|
|
|
elements = [self objectEnumerator];
|
|
|
|
currentElement = [elements nextObject];
|
|
|
|
while (currentElement)
|
|
|
|
{
|
|
|
|
[jsonElements addObject: [currentElement jsonRepresentation]];
|
|
|
|
currentElement = [elements nextObject];
|
|
|
|
}
|
|
|
|
representation = [NSString stringWithFormat: @"[%@]",
|
|
|
|
[jsonElements componentsJoinedByString: @", "]];
|
|
|
|
[jsonElements release];
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
- (void) addObjectUniquely: (id) object
|
|
|
|
{
|
|
|
|
if (![self containsObject: object])
|
|
|
|
[self addObject: object];
|
|
|
|
}
|
|
|
|
|
2007-05-30 22:03:19 +02:00
|
|
|
- (void) addRange: (NSRange) newRange
|
|
|
|
{
|
|
|
|
[self addObject: NSStringFromRange (newRange)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasRangeIntersection: (NSRange) testRange
|
|
|
|
{
|
|
|
|
NSEnumerator *ranges;
|
|
|
|
NSString *currentRangeString;
|
|
|
|
NSRange currentRange;
|
|
|
|
BOOL response;
|
|
|
|
|
|
|
|
response = NO;
|
|
|
|
|
|
|
|
ranges = [self objectEnumerator];
|
|
|
|
currentRangeString = [ranges nextObject];
|
|
|
|
while (!response && currentRangeString)
|
|
|
|
{
|
|
|
|
currentRange = NSRangeFromString (currentRangeString);
|
|
|
|
if (NSLocationInRange (testRange.location, currentRange)
|
|
|
|
|| NSLocationInRange (NSMaxRange (testRange), currentRange))
|
|
|
|
response = YES;
|
|
|
|
else
|
|
|
|
currentRangeString = [ranges nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-05-19 02:41:01 +02:00
|
|
|
@end
|
|
|
|
|