1 /* NSArray+Utilities.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSString.h>
25 #import "NSArray+Utilities.h"
27 @implementation NSArray (SOGoArrayUtilities)
29 - (NSArray *) stringsWithFormat: (NSString *) format
31 NSMutableArray *formattedStrings;
32 NSEnumerator *objects;
35 formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
37 objects = [self objectEnumerator];
38 currentObject = [objects nextObject];
42 addObject: [NSString stringWithFormat: format, currentObject]];
43 currentObject = [objects nextObject];
46 return formattedStrings;
49 - (NSArray *) keysWithFormat: (NSString *) format
51 NSMutableArray *formattedStrings;
52 NSEnumerator *objects;
55 formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
57 objects = [self objectEnumerator];
58 currentObject = [objects nextObject];
61 [formattedStrings addObject: [currentObject keysWithFormat: format]];
62 currentObject = [objects nextObject];
65 return formattedStrings;
68 - (NSArray *) objectsForKey: (NSString *) key
70 NSMutableArray *objectsForKey;
71 unsigned int count, max;
75 objectsForKey = [NSMutableArray arrayWithCapacity: max];
77 for (count = 0; count < max; count++)
79 value = [[self objectAtIndex: count] objectForKey: key];
80 [objectsForKey addObject: value];
86 - (NSArray *) flattenedArray
88 NSMutableArray *flattenedArray;
89 NSEnumerator *objects;
92 flattenedArray = [NSMutableArray array];
93 objects = [self objectEnumerator];
94 currentObject = [objects nextObject];
97 [flattenedArray addObjectsFromArray: currentObject];
98 currentObject = [objects nextObject];
101 return flattenedArray;
104 - (void) makeObjectsPerform: (SEL) selector
105 withObject: (id) object1
106 withObject: (id) object2
111 for (count = 0; count < max; count++)
112 [[self objectAtIndex: count] performSelector: selector
114 withObject: object2];
117 - (NSString *) jsonRepresentation
120 NSMutableArray *jsonElements;
121 NSEnumerator *elements;
122 NSString *representation;
124 jsonElements = [NSMutableArray new];
126 elements = [self objectEnumerator];
127 currentElement = [elements nextObject];
128 while (currentElement)
130 [jsonElements addObject: [currentElement jsonRepresentation]];
131 currentElement = [elements nextObject];
133 representation = [NSString stringWithFormat: @"[%@]",
134 [jsonElements componentsJoinedByString: @", "]];
135 [jsonElements release];
137 return representation;
140 - (BOOL) containsCaseInsensitiveString: (NSString *) match
143 NSString *currentString, *cmpObject;
144 NSEnumerator *objects;
148 cmpObject = [match lowercaseString];
149 objects = [self objectEnumerator];
150 currentString = [objects nextObject];
151 while (currentString && !response)
152 if ([[currentString lowercaseString] isEqualToString: cmpObject])
155 currentString = [objects nextObject];
162 @implementation NSMutableArray (SOGoArrayUtilities)
164 - (void) addObjectUniquely: (id) object
166 if (![self containsObject: object])
167 [self addObject: object];
170 - (void) addRange: (NSRange) newRange
172 [self addObject: NSStringFromRange (newRange)];
175 - (BOOL) hasRangeIntersection: (NSRange) testRange
177 NSEnumerator *ranges;
178 NSString *currentRangeString;
179 NSRange currentRange;
184 ranges = [self objectEnumerator];
185 currentRangeString = [ranges nextObject];
186 while (!response && currentRangeString)
188 currentRange = NSRangeFromString (currentRangeString);
189 if (NSLocationInRange (testRange.location, currentRange)
190 || NSLocationInRange (NSMaxRange (testRange), currentRange))
193 currentRangeString = [ranges nextObject];