Index: src/OFArray_adjacent.m ================================================================== --- src/OFArray_adjacent.m +++ src/OFArray_adjacent.m @@ -102,12 +102,12 @@ @try { for (i = 0; i < count; i++) [objects[i] retain]; - [array addNItems: count - fromCArray: objects]; + [array addItemsFromCArray: objects + count: count]; } @catch (id e) { for (i = 0; i < count; i++) [objects[i] release]; /* Prevent double-release of objects */ @@ -130,12 +130,12 @@ size_t i; for (i = 0; i < count; i++) [objects[i] retain]; - [array addNItems: count - fromCArray: objects]; + [array addItemsFromCArray: objects + count: count]; } @catch (id e) { size_t i; for (i = 0; i < count; i++) [objects[i] release]; Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -186,55 +186,46 @@ * \brief Adds an item to the OFDataArray at the specified index. * * \param item A pointer to an arbitrary item * \param index The index where the item should be added */ -- (void)addItem: (const void*)item - atIndex: (size_t)index; +- (void)insertItem: (const void*)item + atIndex: (size_t)index; /** * \brief Adds items from a C array to the OFDataArray. * - * \param nItems The number of items to add + * \param count The number of items to add * \param cArray A C array containing the items to add */ -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray; +- (void)addItemsFromCArray: (const void*)cArray + count: (size_t)count; /** * \brief Adds items from a C array to the OFDataArray at the specified index. * - * \param nItems The number of items to add * \param cArray A C array containing the items to add * \param index The index where the items should be added + * \param count The number of items to add */ -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray - atIndex: (size_t)index; +- (void)insertItemsFromCArray: (const void*)cArray + atIndex: (size_t)index + count: (size_t)count; /** * \brief Removes the item at the specified index. * * \param index The index of the item to remove */ - (void)removeItemAtIndex: (size_t)index; -/** - * \brief Removes the specified amount of items from the end of the OFDataArray. - * - * \param nItems The number of items to remove - */ -- (void)removeNItems: (size_t)nItems; - /** * \brief Removes the specified amount of items at the specified index. * - * \param nItems The number of items to remove - * \param index The index at which the items are removed + * \param range The range of items to remove */ -- (void)removeNItems: (size_t)nItems - atIndex: (size_t)index; +- (void)removeItemsInRange: (of_range_t)range; /** * \brief Removes the last item. */ - (void)removeLastItem; Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -111,12 +111,12 @@ while (![file isAtEndOfStream]) { size_t length; length = [file readNBytes: of_pagesize intoBuffer: buffer]; - [self addNItems: length - fromCArray: buffer]; + [self addItemsFromCArray: buffer + count: length]; } [self freeMemory: buffer]; } @finally { [file release]; @@ -263,20 +263,20 @@ memcpy(data + count * itemSize, item, itemSize); count++; } -- (void)addItem: (const void*)item - atIndex: (size_t)index +- (void)insertItem: (const void*)item + atIndex: (size_t)index { - [self addNItems: 1 - fromCArray: item - atIndex: index]; + [self insertItemsFromCArray: item + atIndex: index + count: 1]; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray +- (void)addItemsFromCArray: (const void*)cArray + count: (size_t)nItems { if (nItems > SIZE_MAX - count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data @@ -285,13 +285,13 @@ memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray - atIndex: (size_t)index +- (void)insertItemsFromCArray: (const void*)cArray + atIndex: (size_t)index + count: (size_t)nItems { if (nItems > SIZE_MAX - count || index > count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data @@ -305,40 +305,23 @@ count += nItems; } - (void)removeItemAtIndex: (size_t)index { - [self removeNItems: 1 - atIndex: index]; -} - -- (void)removeNItems: (size_t)nItems -{ - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - - count -= nItems; - @try { - data = [self resizeMemory: data - toNItems: count - ofSize: itemSize]; - } @catch (OFOutOfMemoryException *e) { - /* We don't really care, as we only made it smaller */ - } -} - -- (void)removeNItems: (size_t)nItems - atIndex: (size_t)index -{ - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - memmove(data + index * itemSize, data + (index + nItems) * itemSize, - (count - index - nItems) * itemSize); - - count -= nItems; + [self removeItemsInRange: of_range(index, 1)]; +} + +- (void)removeItemsInRange: (of_range_t)range +{ + if (range.start + range.length > count) + @throw [OFOutOfRangeException exceptionWithClass: isa]; + + memmove(data + range.start * itemSize, + data + (range.start + range.length) * itemSize, + (count - range.start - range.length) * itemSize); + + count -= range.length; @try { data = [self resizeMemory: data toNItems: count ofSize: itemSize]; } @catch (OFOutOfMemoryException *e) { @@ -371,12 +354,12 @@ - copy { OFDataArray *copy = [[isa alloc] initWithItemSize: itemSize]; - [copy addNItems: count - fromCArray: data]; + [copy addItemsFromCArray: data + count: count]; return copy; } - (BOOL)isEqual: (id)object @@ -503,12 +486,12 @@ count++; size = newSize; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray +- (void)addItemsFromCArray: (const void*)cArray + count: (size_t)nItems { size_t newSize, lastPageByte; if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: isa]; @@ -524,17 +507,18 @@ count += nItems; size = newSize; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray - atIndex: (size_t)index +- (void)insertItemsFromCArray: (const void*)cArray + atIndex: (size_t)index + count: (size_t)nItems { size_t newSize, lastPageByte; - if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) + if (nItems > SIZE_MAX - count || index > count || + count + nItems > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; @@ -548,39 +532,22 @@ count += nItems; size = newSize; } -- (void)removeNItems: (size_t)nItems -{ - size_t newSize, lastPageByte; - - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - count -= nItems; - lastPageByte = of_pagesize - 1; - newSize = (count * itemSize + lastPageByte) & ~lastPageByte; - - if (size != newSize) - data = [self resizeMemory: data - toSize: newSize]; - size = newSize; -} - -- (void)removeNItems: (size_t)nItems - atIndex: (size_t)index -{ - size_t newSize, lastPageByte; - - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - memmove(data + index * itemSize, data + (index + nItems) * itemSize, - (count - index - nItems) * itemSize); - - count -= nItems; +- (void)removeItemsInRange: (of_range_t)range +{ + size_t newSize, lastPageByte; + + if (range.start + range.length > count) + @throw [OFOutOfRangeException exceptionWithClass: isa]; + + memmove(data + range.start * itemSize, + data + (range.start + range.length) * itemSize, + (count - range.start - range.length) * itemSize); + + count -= range.length; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data Index: src/OFHTTPRequest.m ================================================================== --- src/OFHTTPRequest.m +++ src/OFHTTPRequest.m @@ -429,12 +429,12 @@ didReceiveData: buffer withLength: length]; [pool2 releaseObjects]; bytesReceived += length; - [data addNItems: length - fromCArray: buffer]; + [data addItemsFromCArray: buffer + count: length]; toRead -= length; } @try { @@ -459,12 +459,12 @@ didReceiveData: buffer withLength: length]; [pool2 releaseObjects]; bytesReceived += length; - [data addNItems: length - fromCArray: buffer]; + [data addItemsFromCArray: buffer + count: length]; if (contentLengthHeader != nil && bytesReceived >= contentLength) break; } Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -24,21 +24,21 @@ * \brief An abstract class for storing, adding and removing objects in anr * array. */ @interface OFMutableArray: OFArray /** - * \brief Adds an object to the OFArray. + * \brief Adds an object to the end of the array. * * \param object An object to add */ - (void)addObject: (id)object; /** * \brief Inserts an object to the OFArray at the specified index. * * \param object An object to add - * \param index The index where the object should be added + * \param index The index where the object should be inserted */ - (void)insertObject: (id)object atIndex: (size_t)index; /** Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -15,10 +15,12 @@ */ #include "config.h" #include + +#include #import "OFMutableArray.h" #import "OFMutableArray_adjacent.h" #import "OFAutoreleasePool.h" Index: src/OFMutableArray_adjacent.m ================================================================== --- src/OFMutableArray_adjacent.m +++ src/OFMutableArray_adjacent.m @@ -24,10 +24,12 @@ #import "OFAutoreleasePool.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" #import "OFOutOfRangeException.h" + +#import "macros.h" @implementation OFMutableArray_adjacent + (void)initialize { if (self == [OFMutableArray_adjacent class]) @@ -43,12 +45,12 @@ } - (void)insertObject: (id)object atIndex: (size_t)index { - [array addItem: &object - atIndex: index]; + [array insertItem: &object + atIndex: index]; [object retain]; mutations++; } @@ -156,11 +158,12 @@ copy = [self allocMemoryForNItems: nObjects ofSize: sizeof(id)]; memcpy(copy, objects + (count - nObjects), nObjects * sizeof(id)); @try { - [array removeNItems: nObjects]; + [array removeItemsInRange: + of_range(count - nObjects, nObjects)]; mutations++; for (i = 0; i < nObjects; i++) [copy[i] release]; } @finally { @@ -190,12 +193,11 @@ copy = [self allocMemoryForNItems: range.length ofSize: sizeof(id)]; memcpy(copy, objects + range.start, range.length * sizeof(id)); @try { - [array removeNItems: range.length - atIndex: range.start]; + [array removeItemsInRange: range]; mutations++; for (i = 0; i < range.length; i++) [copy[i] release]; } @finally { Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -440,28 +440,28 @@ } - (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize andNItems: (size_t)nItems { - OFDataArray *da; + OFDataArray *dataArray; char *tmp; - da = [OFDataArray dataArrayWithItemSize: itemSize]; + dataArray = [OFDataArray dataArrayWithItemSize: itemSize]; tmp = [self allocMemoryForNItems: nItems ofSize: itemSize]; @try { [self readExactlyNBytes: nItems * itemSize intoBuffer: tmp]; - [da addNItems: nItems - fromCArray: tmp]; + [dataArray addItemsFromCArray: tmp + count: nItems]; } @finally { [self freeMemory: tmp]; } - return da; + return dataArray; } - (OFDataArray*)readDataArrayTillEndOfStream { OFDataArray *dataArray; @@ -474,12 +474,12 @@ while (![self isAtEndOfStream]) { size_t length; length = [self readNBytes: of_pagesize intoBuffer: buffer]; - [dataArray addNItems: length - fromCArray: buffer]; + [dataArray addItemsFromCArray: buffer + count: length]; } } @finally { [self freeMemory: buffer]; } Index: src/OFStreamObserver.m ================================================================== --- src/OFStreamObserver.m +++ src/OFStreamObserver.m @@ -352,13 +352,13 @@ default: assert(0); } } - [queue removeNObjects: count]; - [queueInfo removeNItems: count]; - [queueFDs removeNItems: count]; + [queue removeAllObjects]; + [queueInfo removeAllItems]; + [queueFDs removeAllItems]; } @finally { [mutex unlock]; } } Index: src/OFStreamObserver_kqueue.m ================================================================== --- src/OFStreamObserver_kqueue.m +++ src/OFStreamObserver_kqueue.m @@ -125,11 +125,11 @@ default: assert(0); } } - [changeList removeNItems: [changeList count]]; + [changeList removeAllItems]; if (events == 0) { [pool release]; return NO; } Index: src/OFXMLElement.m ================================================================== --- src/OFXMLElement.m +++ src/OFXMLElement.m @@ -638,12 +638,12 @@ else child = [childrenObjects[j] XMLStringWithIndentation: ind level: level + 1]; - [tmp addNItems: [child UTF8StringLength] - fromCArray: [child UTF8String]]; + [tmp addItemsFromCArray: [child UTF8String] + count: [child UTF8StringLength]]; } if (indent) [tmp addItem: "\n"]; Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -43,19 +43,19 @@ static OF_INLINE void cache_append(OFDataArray *cache, const char *string, of_string_encoding_t encoding, size_t length) { if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8)) - [cache addNItems: length - fromCArray: string]; + [cache addItemsFromCArray: string + count: length]; else { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFString *tmp = [OFString stringWithCString: string encoding: encoding length: length]; - [cache addNItems: [tmp UTF8StringLength] - fromCArray: [tmp UTF8String]]; + [cache addItemsFromCArray: [tmp UTF8String] + count: [tmp UTF8StringLength]]; [pool release]; } } static OFString* Index: src/base64.m ================================================================== --- src/base64.m +++ src/base64.m @@ -148,11 +148,11 @@ db[0] = (sb & 0xFF0000) >> 16; db[1] = (sb & 0x00FF00) >> 8; db[2] = sb & 0x0000FF; - [data addNItems: count - fromCArray: db]; + [data addItemsFromCArray: db + count: count]; } return YES; } Index: tests/OFDataArrayTests.m ================================================================== --- tests/OFDataArrayTests.m +++ tests/OFDataArrayTests.m @@ -57,14 +57,14 @@ other = (class == [OFDataArray class] ? [OFBigDataArray class] : [OFDataArray class]); TEST(@"-[isEqual:]", (array[1] = [other dataArrayWithItemSize: 4096]) && - R([array[1] addNItems: [array[0] count] - fromCArray: [array[0] cArray]]) && + R([array[1] addItemsFromCArray: [array[0] cArray] + count: [array[0] count]]) && [array[1] isEqual: array[0]] && - R([array[1] removeNItems: 1]) && ![array[0] isEqual: array[1]]) + R([array[1] removeLastItem]) && ![array[0] isEqual: array[1]]) TEST(@"-[copy]", (array[1] = [[array[0] copy] autorelease]) && [array[0] isEqual: array[1]]) array[2] = [OFDataArray dataArray]; @@ -71,34 +71,33 @@ array[3] = [OFDataArray dataArray]; [array[2] addItem: "a"]; [array[2] addItem: "a"]; [array[3] addItem: "z"]; TEST(@"-[compare]", [array[0] compare: array[1]] == 0 && - R([array[1] removeNItems: 1]) && + R([array[1] removeLastItem]) && [array[0] compare: array[1]] == OF_ORDERED_DESCENDING && [array[1] compare: array[0]] == OF_ORDERED_ASCENDING && [array[2] compare: array[3]] == OF_ORDERED_ASCENDING) TEST(@"-[hash]", [array[0] hash] == 0x634A529F) array[0] = [class dataArray]; - [array[0] addNItems: 6 - fromCArray: "abcdef"]; + [array[0] addItemsFromCArray: "abcdef" + count: 6]; - TEST(@"-[removeNItems:]", R([array[0] removeNItems: 1]) && + TEST(@"-[removeLastItem]", R([array[0] removeLastItem]) && [array[0] count] == 5 && !memcmp([array[0] cArray], "abcde", 5)) - TEST(@"-[removeNItems:atIndex:]", - R([array[0] removeNItems: 2 - atIndex: 1]) && [array[0] count] == 3 && - !memcmp([array[0] cArray], "ade", 3)) - - TEST(@"-[addNItems:atIndex:]", - R([array[0] addNItems: 2 - fromCArray: "bc" - atIndex: 1]) && [array[0] count] == 5 && + TEST(@"-[removeItemsInRange:]", + R([array[0] removeItemsInRange: of_range(1, 2)]) && + [array[0] count] == 3 && !memcmp([array[0] cArray], "ade", 3)) + + TEST(@"-[insertItemsFromCArray:atIndex:count:]", + R([array[0] insertItemsFromCArray: "bc" + atIndex: 1 + count: 2]) && [array[0] count] == 5 && !memcmp([array[0] cArray], "abcde", 5)) TEST(@"-[MD5Hash]", [[array[0] MD5Hash] isEqual: [@"abcde" MD5Hash]]) TEST(@"-[SHA1Hash]", [[array[0] SHA1Hash] isEqual: [@"abcde" SHA1Hash]]) @@ -110,24 +109,25 @@ !memcmp([[class dataArrayWithBase64EncodedString: @"YWJjZGU="] cArray], "abcde", 5)) TEST(@"Building strings", (array[0] = [class dataArray]) && - R([array[0] addNItems: 6 - fromCArray: (void*)str]) && R([array[0] addItem: ""]) && + R([array[0] addItemsFromCArray: (void*)str + count: 6]) && R([array[0] addItem: ""]) && !strcmp([array[0] cArray], str)) EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]", OFOutOfRangeException, [array[0] itemAtIndex: [array[0] count]]) - EXPECT_EXCEPTION(@"Detect out of range in -[addNItems:fromCArray:]", - OFOutOfRangeException, [array[0] addNItems: SIZE_MAX - fromCArray: NULL]) + EXPECT_EXCEPTION(@"Detect out of range in " + @"-[addItemsFromCArray:count:]", + OFOutOfRangeException, [array[0] addItemsFromCArray: NULL + count: SIZE_MAX]) - EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", + EXPECT_EXCEPTION(@"Detect out of range in -[removeItemsInRange:]", OFOutOfRangeException, - [array[0] removeNItems: [array[0] count] + 1]) + [array[0] removeItemsInRange: of_range([array[0] count], 1)]) } - (void)dataArrayTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; Index: tests/OFSerializationTests.m ================================================================== --- tests/OFSerializationTests.m +++ tests/OFSerializationTests.m @@ -65,12 +65,12 @@ [OFCountedSet setWithObjects: @"foo", @"foo", @"bar", nil]]; [d setObject: @"list" forKey: l]; - [da addNItems: 39 - fromCArray: "0123456789:;