Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -12,10 +12,12 @@ #include #import "OFObject.h" #import "OFDataArray.h" +@class OFString; + /** * The OFArray class provides a class for storing objects in an array. */ @interface OFArray: OFObject { @@ -124,10 +126,18 @@ /** * \return The last object of the OFArray or nil */ - (id)lastObject; +/** + * Creates a string by joining all objects of the array. + * + * \param separator The string with which the objects should be joined + * \return A string containing all objects joined by the separator + */ +- (OFString*)componentsJoinedByString: (OFString*)separator; + - addObject: (OFObject*)obj; - addObject: (OFObject*)obj atIndex: (size_t)index; - removeObject: (OFObject*)obj; - removeObjectIdenticalTo: (OFObject*)obj; @@ -136,5 +146,6 @@ - removeNObjects: (size_t)nobjects atIndex: (size_t)index; @end #import "OFMutableArray.h" +#import "OFString.h" Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -213,10 +213,37 @@ { id *last = [array lastItem]; return (last != NULL ? *last : nil); } + +- (OFString*)componentsJoinedByString: (OFString*)separator +{ + OFString *str; + OFString **objs = [array cArray]; + size_t i, count = [array count]; + IMP append; + + if (count == 0) + return [OFString string]; + + str = [[OFMutableString alloc] init]; + @try { + append = [str methodForSelector: @selector(appendString:)]; + + for (i = 0; i < count - 1; i++) { + append(str, @selector(appendString:), objs[i]); + append(str, @selector(appendString:), separator); + } + append(str, @selector(appendString:), objs[i]); + } @catch (OFException *e) { + [str release]; + @throw e; + } + + return [str autorelease]; +} - (BOOL)isEqual: (OFObject*)obj { OFObject **objs, **objs2; size_t i, count, count2; Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -90,7 +90,11 @@ OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]]) EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", OFOutOfRangeException, [a[0] removeNObjects: [a[0] count] + 1]) + a[0] = [OFArray arrayWithObjects: @"foo", @"bar", @"baz", nil]; + TEST(@"-[componentsJoinedByString:]", + [[a[0] componentsJoinedByString: @" "] isEqual: @"foo bar baz"]) + [pool drain]; }