@@ -25,11 +25,28 @@ int _OFString_PathAdditions_reference; @implementation OFString (PathAdditions) + (OFString *)pathWithComponents: (OFArray *)components { - return [components componentsJoinedByString: @"/"]; + void *pool = objc_autoreleasePoolPush(); + OFString *ret; + + if ([[components firstObject] isEqual: @"/"]) { + OFMutableArray OF_GENERIC(OFString *) *mutableComponents = + [[components mutableCopy] autorelease]; + + [mutableComponents replaceObjectAtIndex: 0 + withObject: @""]; + + components = mutableComponents; + } + + ret = [components componentsJoinedByString: @"/"]; + + [ret retain]; + objc_autoreleasePoolPop(pool); + return [ret autorelease]; } - (bool)isAbsolutePath { return [self hasPrefix: @"/"]; @@ -58,10 +75,14 @@ last = i + 1; } } [ret addObject: [OFString stringWithUTF8String: cString + last length: i - last]]; + + if ([[ret firstObject] isEqual: @""]) + [ret replaceObjectAtIndex: 0 + withObject: @"/"]; [ret makeImmutable]; objc_autoreleasePoolPop(pool); @@ -210,20 +231,33 @@ } - (OFString *)stringByStandardizingPath { void *pool = objc_autoreleasePoolPush(); - OFArray OF_GENERIC(OFString *) *components = [self pathComponents]; + OFArray OF_GENERIC(OFString *) *components; OFMutableArray OF_GENERIC(OFString *) *array; - OFString *ret; - bool done = false, startsWithEmpty, endsWithEmpty; + OFString *firstComponent, *ret; + bool done = false, startsWithSlash, endsWithEmpty; + + if ([self length] == 0) + return @""; + + components = [self pathComponents]; + + if ([components count] == 1) { + objc_autoreleasePoolPop(pool); + return self; + } array = [[components mutableCopy] autorelease]; + firstComponent = [array firstObject]; + startsWithSlash = + ([firstComponent isEqual: @"/"] || [firstComponent length] == 0); + endsWithEmpty = ([[array lastObject] length] == 0); - if ((startsWithEmpty = [[array firstObject] isEqual: @""])) + if (startsWithSlash) [array removeObjectAtIndex: 0]; - endsWithEmpty = [[array lastObject] isEqual: @""]; while (!done) { size_t length = [array count]; done = true; @@ -250,13 +284,14 @@ break; } } } - if (startsWithEmpty) + if (startsWithSlash) [array insertObject: @"" atIndex: 0]; + if (endsWithEmpty) [array addObject: @""]; ret = [[array componentsJoinedByString: @"/"] retain];