Index: src/OFMutableURL.h ================================================================== --- src/OFMutableURL.h +++ src/OFMutableURL.h @@ -82,12 +82,22 @@ * * @return An initialized OFMutableURL */ - (instancetype)init; +/*! + * @brief Sets the URL's path from the specified path components. + * + * The first component must always be empty to designate the root. + * + * @param components The path components to set the URL's path from + */ +- (void)setPathComponents: + (nullable OFArray OF_GENERIC(OFString *) *)components; + /*! * @brief Converts the mutable URL to an immutable URL. */ - (void)makeImmutable; @end OF_ASSUME_NONNULL_END Index: src/OFMutableURL.m ================================================================== --- src/OFMutableURL.m +++ src/OFMutableURL.m @@ -15,13 +15,16 @@ */ #include "config.h" #import "OFMutableURL.h" -#import "OFURL+Private.h" +#import "OFArray.h" #import "OFNumber.h" #import "OFString.h" +#import "OFURL+Private.h" + +#import "OFInvalidFormatException.h" @implementation OFMutableURL @dynamic scheme, host, port, user, password, path, parameters, query, fragment; + (instancetype)URL @@ -94,10 +97,30 @@ { OFString *old = _fragment; _fragment = [fragment copy]; [old release]; } + +- (void)setPathComponents: (OFArray *)components +{ + void *pool = objc_autoreleasePoolPush(); + + if (components == nil) { + [self setPath: nil]; + return; + } + + if ([components count] == 0) + @throw [OFInvalidFormatException exception]; + + if ([[components firstObject] length] != 0) + @throw [OFInvalidFormatException exception]; + + [self setPath: [components componentsJoinedByString: @"/"]]; + + objc_autoreleasePoolPop(pool); +} - (id)copy { OFMutableURL *copy = [self mutableCopy]; Index: src/OFURL.h ================================================================== --- src/OFURL.h +++ src/OFURL.h @@ -17,10 +17,11 @@ #import "OFObject.h" #import "OFSerialization.h" OF_ASSUME_NONNULL_BEGIN +@class OFArray OF_GENERIC(ObjectType); @class OFNumber; @class OFString; /*! * @class OFURL OFURL.h ObjFW/OFURL.h @@ -176,10 +177,19 @@ * * @return The URL as a string */ - (OFString *)string; +/*! + * @brief Returns the path of the URL split into components. + * + * The first component is always empty to designate the root. + * + * @return The path of the URL split into components + */ +- (nullable OFArray OF_GENERIC(OFString *) *)pathComponents; + /*! * @brief Returns the local file system representation for a file URL. * * This only exists for URLs with the file scheme and throws an exception * otherwise. Index: src/OFURL.m ================================================================== --- src/OFURL.m +++ src/OFURL.m @@ -520,10 +520,15 @@ [ret makeImmutable]; return ret; } + +- (OFArray *)pathComponents +{ + return [_path componentsSeparatedByString: @"/"]; +} - (OFString *)fileSystemRepresentation { void *pool = objc_autoreleasePoolPush(); OFString *path;