Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -560,10 +560,18 @@ * \param string The string to append * \return A new autoreleased OFString with the specified string appended */ - (OFString*)stringByAppendingString: (OFString*)string; +/** + * Creates a new string by prepending another string. + * + * \param string The string to prepend + * \return A new autoreleased OFString with the specified string prepended + */ +- (OFString*)stringByPrependingString: (OFString*)string; + /** * \return The string in uppercase */ - (OFString*)uppercaseString; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -1404,10 +1404,26 @@ OFMutableString *new; new = [OFMutableString stringWithString: self]; [new appendString: string_]; + /* + * Class swizzle the string to be immutable. We declared the return type + * to be OFString*, so it can't be modified anyway. But not swizzling it + * would create a real copy each time -[copy] is called. + */ + new->isa = [OFString class]; + return new; +} + +- (OFString*)stringByPrependingString: (OFString*)string_ +{ + OFMutableString *new; + + new = [OFMutableString stringWithString: string_]; + [new appendString: self]; + /* * Class swizzle the string to be immutable. We declared the return type * to be OFString*, so it can't be modified anyway. But not swizzling it * would create a real copy each time -[copy] is called. */ Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -241,10 +241,13 @@ toIndex: 0]) TEST(@"-[stringByAppendingString:]", [[@"foo" stringByAppendingString: @"bar"] isEqual: @"foobar"]) + TEST(@"-[stringByPrependingString:]", + [[@"foo" stringByPrependingString: @"bar"] isEqual: @"barfoo"]) + TEST(@"-[hasPrefix:]", [@"foobar" hasPrefix: @"foo"] && ![@"foobar" hasPrefix: @"foobar0"]) TEST(@"-[hasSuffix:]", [@"foobar" hasSuffix: @"bar"] && ![@"foobar" hasSuffix: @"foobar0"])