Index: src/OFURL.h ================================================================== --- src/OFURL.h +++ src/OFURL.h @@ -33,19 +33,19 @@ OFString *query; OFString *fragment; } #ifdef OF_HAVE_PROPERTIES -@property (readonly, copy) OFString *scheme; -@property (readonly, copy) OFString *host; -@property (readonly, assign) uint16_t port; -@property (readonly, copy) OFString *user; -@property (readonly, copy) OFString *password; -@property (readonly, copy) OFString *path; -@property (readonly, copy) OFString *parameters; -@property (readonly, copy) OFString *query; -@property (readonly, copy) OFString *fragment; +@property (copy) OFString *scheme; +@property (copy) OFString *host; +@property (assign) uint16_t port; +@property (copy) OFString *user; +@property (copy) OFString *password; +@property (copy) OFString *path; +@property (copy) OFString *parameters; +@property (copy) OFString *query; +@property (copy) OFString *fragment; #endif /** * \param str A string describing an URL * \return A new, autoreleased OFURL @@ -63,45 +63,108 @@ /** * \return The scheme part of the URL */ - (OFString*)scheme; +/** + * Set the scheme part of the URL. + * + * \param scheme The scheme part of the URL to set + */ +- (void)setScheme: (OFString*)scheme; + /** * \return The host part of the URL */ - (OFString*)host; +/** + * Set the host part of the URL. + * + * \param host The host part of the URL to set + */ +- (void)setHost: (OFString*)host; + /** * \return The port part of the URL */ - (uint16_t)port; +/** + * Set the port part of the URL. + * + * \param port The port part of the URL to set + */ +- (void)setPort: (uint16_t)port; + /** * \return The user part of the URL */ - (OFString*)user; +/** + * Set the user part of the URL. + * + * \param user The user part of the URL to set + */ +- (void)setUser: (OFString*)user; + /** * \return The password part of the URL */ - (OFString*)password; +/** + * Set the password part of the URL. + * + * \param password The password part of the URL to set + */ +- (void)setPassword: (OFString*)password; + /** * \return The path part of the URL */ - (OFString*)path; +/** + * Set the path part of the URL. + * + * \param path The path part of the URL to set + */ +- (void)setPath: (OFString*)path; + /** * \return The parameters part of the URL */ - (OFString*)parameters; +/** + * Set the parameters part of the URL. + * + * \param parameters The parameters part of the URL to set + */ +- (void)setParameters: (OFString*)parameters; + /** * \return The query part of the URL */ - (OFString*)query; +/** + * Set the query part of the URL. + * + * \param query The query part of the URL to set + */ +- (void)setQuery: (OFString*)query; + /** * \return The fragment part of the URL */ - (OFString*)fragment; + +/** + * Set the fragment part of the URL. + * + * \param fragment The fragment part of the URL to set + */ +- (void)setFragment: (OFString*)fragment; @end Index: src/OFURL.m ================================================================== --- src/OFURL.m +++ src/OFURL.m @@ -22,10 +22,18 @@ #import "OFURL.h" #import "OFString.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" +#import "macros.h" + +#define ADD_STR_HASH(str) \ + h = [str hash]; \ + OF_HASH_ADD(hash, h >> 24); \ + OF_HASH_ADD(hash, (h >> 16) & 0xFF); \ + OF_HASH_ADD(hash, (h >> 8) & 0xFF); \ + OF_HASH_ADD(hash, h & 0xFF); @implementation OFURL + URLWithString: (OFString*)str { return [[[self alloc] initWithString: str] autorelease]; @@ -189,10 +197,34 @@ if (![url->fragment isEqual: fragment]) return NO; return YES; } + +- (uint32_t)hash +{ + uint32_t hash, h; + + OF_HASH_INIT(hash); + + ADD_STR_HASH(scheme); + ADD_STR_HASH(host); + + OF_HASH_ADD(hash, (port >> 8) & 0xFF); + OF_HASH_ADD(hash, port & 0xFF); + + ADD_STR_HASH(user); + ADD_STR_HASH(password); + ADD_STR_HASH(path); + ADD_STR_HASH(parameters); + ADD_STR_HASH(query); + ADD_STR_HASH(fragment); + + OF_HASH_FINALIZE(hash); + + return hash; +} - copy { OFURL *new = [[OFURL alloc] init]; @@ -211,50 +243,115 @@ - (OFString*)scheme { return [[scheme copy] autorelease]; } + +- (void)setScheme: (OFString*)scheme_ +{ + if (![scheme_ isEqual: @"http"] && ![scheme_ isEqual: @"https"]) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + OFString *old = scheme; + scheme = [scheme_ copy]; + [old release]; +} - (OFString*)host { return [[host copy] autorelease]; } + +- (void)setHost: (OFString*)host_ +{ + OFString *old = host; + host = [host_ copy]; + [old release]; +} - (uint16_t)port { return port; } + +- (void)setPort: (uint16_t)port_ +{ + port = port_; +} - (OFString*)user { return [[user copy] autorelease]; } + +- (void)setUser: (OFString*)user_ +{ + OFString *old = user; + user = [user_ copy]; + [old release]; +} - (OFString*)password { return [[password copy] autorelease]; } + +- (void)setPassword: (OFString*)password_ +{ + OFString *old = password; + password = [password_ copy]; + [old release]; +} - (OFString*)path { return [[path copy] autorelease]; } + +- (void)setPath: (OFString*)path_ +{ + OFString *old = path; + path = [path_ copy]; + [old release]; +} - (OFString*)parameters { return [[parameters copy] autorelease]; } + +- (void)setParameters: (OFString*)parameters_ +{ + OFString *old = parameters; + parameters = [parameters_ copy]; + [old release]; +} - (OFString*)query { return [[query copy] autorelease]; } + +- (void)setQuery: (OFString*)query_ +{ + OFString *old = query; + query = [query_ copy]; + [old release]; +} - (OFString*)fragment { return [[fragment copy] autorelease]; } + +- (void)setFragment: (OFString*)fragment_ +{ + OFString *old = fragment; + fragment = [fragment_ copy]; + [old release]; +} - (OFString*)description { OFMutableString *desc = [OFMutableString stringWithFormat: @"%s://", [scheme cString]]; Index: tests/OFURLTests.m ================================================================== --- tests/OFURLTests.m +++ tests/OFURLTests.m @@ -53,10 +53,11 @@ TEST(@"-[fragment]", [[u1 fragment] isEqual: @"f"]) TEST(@"-[copy]", R(u4 = [[u1 copy] autorelease])) TEST(@"-[isEqual:]", [u1 isEqual: u4] && ![u2 isEqual: u3]) + TEST(@"-[hash:]", [u1 hash] == [u4 hash] && [u2 hash] != [u3 hash]) EXPECT_EXCEPTION(@"Detection of invalid format", OFInvalidFormatException, [OFURL URLWithString: @"http"]) EXPECT_EXCEPTION(@"Detection of invalid scheme",