Index: src/OFDictionary.h ================================================================== --- src/OFDictionary.h +++ src/OFDictionary.h @@ -255,10 +255,17 @@ * * @return An array of all objects */ - (OFArray OF_GENERIC(ObjectType) *)allObjects; +/*! + * @brief Creates a string by URL-encoding the contents of the dictionary. + * + * @return A URL-encoded string with the contents of the dictionary + */ +- (OFString *)stringByURLEncoding; + /*! * @brief Returns an OFEnumerator to enumerate through the dictionary's keys. * * @return An OFEnumerator to enumerate through the dictionary's keys */ Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -560,10 +560,38 @@ objc_autoreleasePoolPop(pool2); } [ret replaceOccurrencesOfString: @"\n" withString: @"\n\t"]; [ret appendString: @";\n}"]; + + [ret makeImmutable]; + + objc_autoreleasePoolPop(pool); + + return ret; +} + +- (OFString *)stringByURLEncoding +{ + OFMutableString *ret = [OFMutableString string]; + void *pool = objc_autoreleasePoolPush(); + OFEnumerator *keyEnumerator = [self keyEnumerator]; + OFEnumerator *objectEnumerator = [self objectEnumerator]; + bool first = true; + id key, object; + + while ((key = [keyEnumerator nextObject]) != nil && + (object = [objectEnumerator nextObject]) != nil) { + if OF_UNLIKELY (first) + first = false; + else + [ret appendString: @"&"]; + + [ret appendString: [[key description] stringByURLEncoding]]; + [ret appendString: @"="]; + [ret appendString: [[object description] stringByURLEncoding]]; + } [ret makeImmutable]; objc_autoreleasePoolPop(pool); Index: tests/OFDictionaryTests.m ================================================================== --- tests/OFDictionaryTests.m +++ tests/OFDictionaryTests.m @@ -259,10 +259,15 @@ TEST(@"Detection of mutation during Fast Enumeration", ok) [mutDict removeObjectForKey: @""]; + TEST(@"-[stringByURLEncoding]", + [[[OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", + @"q&x", @"q=x", nil] + stringByURLEncoding] isEqual: @"q%26x=q%3Dx&foo=bar"]) + #ifdef OF_HAVE_BLOCKS { __block size_t i = 0; __block bool ok = true;