Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -52,10 +52,21 @@ * \param objs A C array of objects, terminated with nil * \return A new autoreleased OFArray */ + arrayWithCArray: (OFObject**)objs; +/** + * Creates a new OFArray with the objects from the specified C array of the + * specified length. + * + * \param objs A C array of objects + * \param len The length of the C array + * \return A new autoreleased OFArray + */ ++ arrayWithCArray: (OFObject**)objs + length: (size_t)length; + /** * Initializes an OFArray with the specified object. * * \param obj An object * \return An initialized OFArray @@ -86,10 +97,21 @@ * \param objs A C array of objects, terminated with nil * \return An initialized OFArray */ - initWithCArray: (OFObject**)objs; +/** + * Initializes an OFArray with the objects from the specified C array of the + * specified length. + * + * \param objs A C array of objects + * \param len The length of the C array + * \return An initialized OFArray + */ +- initWithCArray: (OFObject**)objs + length: (size_t)len; + /** * \return The number of objects in the array */ - (size_t)count; Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -45,10 +45,17 @@ + arrayWithCArray: (OFObject**)objs { return [[[self alloc] initWithCArray: objs] autorelease]; } + ++ arrayWithCArray: (OFObject**)objs + length: (size_t)len +{ + return [[[self alloc] initWithCArray: objs + length: len] autorelease]; +} - init { self = [super init]; @@ -125,10 +132,30 @@ @try { for (obj = objs; *obj != nil; obj++) { [array addItem: obj]; [*obj retain]; + } + } @catch (OFException *e) { + [self dealloc]; + @throw e; + } + + return self; +} + +- initWithCArray: (OFObject**)objs + length: (size_t)len +{ + self = [self init]; + + @try { + size_t i; + + for (i = 0; i < len; i++) { + [array addItem: objs + i]; + [objs[i] retain]; } } @catch (OFException *e) { [self dealloc]; @throw e; } Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -28,11 +28,11 @@ void array_tests() { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFArray *a[2]; + OFArray *a[3]; OFMutableArray *m[2]; OFEnumerator *enumerator; id obj; BOOL ok; size_t i; @@ -41,10 +41,15 @@ TEST(@"+[arrayWithObjects:]", (a[0] = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil])) TEST(@"+[arrayWithCArray:]", (a[1] = [OFArray arrayWithCArray: c_ary])) + + TEST(@"+[arrayWithCArray:length:]", + (a[2] = [OFArray arrayWithCArray: c_ary + length: 3]) && + [a[2] isEqual: a[1]]) TEST(@"-[addObject:]", [m[0] addObject: c_ary[0]] && [m[0] addObject: c_ary[2]]) TEST(@"-[addObject:atIndex:]", [m[0] addObject: c_ary[1]