Index: src/OFMutableString.h ================================================================== --- src/OFMutableString.h +++ src/OFMutableString.h @@ -103,10 +103,19 @@ /** * Lower the OFString. */ - (void)lower; +/** + * Inserts a string at the specified index. + * + * \param str The string to insert + * \param idx The index + */ +- (void)insertString: (OFString*)str + atIndex: (size_t)idx; + /** * Removes the characters at the specified range. * * \param start The index where the deletion should be started * \param end The index until which the characters should be deleted. Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -351,10 +351,32 @@ - (void)lower { [self _applyTable: of_unicode_lower_table withSize: OF_UNICODE_LOWER_TABLE_SIZE]; } + +- (void)insertString: (OFString*)str + atIndex: (size_t)idx +{ + size_t nlen; + + if (isUTF8) + idx = of_string_index_to_position(string, idx, length); + + if (idx > length) + @throw [OFOutOfRangeException newWithClass: isa]; + + nlen = length + [str cStringLength]; + string = [self resizeMemory: string + toSize: nlen + 1]; + + memmove(string + idx + [str cStringLength], string + idx, length - idx); + memcpy(string + idx, [str cString], [str cStringLength]); + string[nlen] = '\0'; + + length = nlen; +} - (void)removeCharactersFromIndex: (size_t)start toIndex: (size_t)end { if (isUTF8) { Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -273,10 +273,16 @@ [[@"foo\"ba'_~$" stringByURLEncoding] isEqual: @"foo%22ba%27_~%24"]) TEST(@"-[stringByURLDecoding]", [[@"foo%20bar%22+%24" stringByURLDecoding] isEqual: @"foo bar\" $"]) + TEST(@"-[insertString:atIndex:]", + (s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆΓΆbÀ€"]) && + R([s[0] insertString: @"Àâü" + atIndex: 3]) && + [s[0] isEqual: @"π„žΓΆΓΆΓ€ΓΆΓΌΓΆbÀ€"]) + EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " @"#1", OFInvalidEncodingException, [@"foo%bar" stringByURLDecoding]) EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " @"#2", OFInvalidEncodingException, [@"foo%FFbar" stringByURLDecoding])