Index: src/OFMutableString.h ================================================================== --- src/OFMutableString.h +++ src/OFMutableString.h @@ -120,10 +120,31 @@ * * \param range The range of the characters which should be removed */ - (void)removeCharactersInRange: (of_range_t)range; +/** + * Removes the characters at the specified range. + * + * \param start The index where the replacement should be started + * \param end The index until which the characters should be replaced. + * This points BEHIND the last character! + * \param repl The string to the replace the characters with + */ +- (void)replaceCharactersFromIndex: (size_t)start + toIndex: (size_t)end + withString: (OFString*)repl; + +/** + * Removes the characters at the specified range. + * + * \param range The range of the characters which should be replaced + * \param repl The string to the replace the characters with + */ +- (void)replaceCharactersInRange: (of_range_t)range + withString: (OFString*)repl; + /** * Replaces all occurrences of a string with another string. * * \param str The string to replace * \param repl The string with which it should be replaced Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -385,10 +385,48 @@ - (void)removeCharactersInRange: (of_range_t)range { [self removeCharactersFromIndex: range.start toIndex: range.start + range.length]; } + +- (void)replaceCharactersFromIndex: (size_t)start + toIndex: (size_t)end + withString: (OFString*)repl +{ + size_t nlen; + + if (isUTF8) { + start = of_string_index_to_position(string, start, length); + end = of_string_index_to_position(string, end, length); + } + + if (start > end) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + if (end > length) + @throw [OFOutOfRangeException newWithClass: isa]; + + nlen = length - (end - start) + [repl cStringLength]; + string = [self resizeMemory: string + toSize: nlen + 1]; + + memmove(string + end, string + start + [repl cStringLength], + length - end); + memcpy(string + start, [repl cString], [repl cStringLength]); + string[nlen] = '\0'; + + length = nlen; +} + +- (void)replaceCharactersInRange: (of_range_t)range + withString: (OFString*)repl +{ + [self replaceCharactersFromIndex: range.start + toIndex: range.start + range.length + withString: repl]; +} - (void)replaceOccurrencesOfString: (OFString*)str withString: (OFString*)repl { const char *str_c = [str cString]; Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -287,10 +287,21 @@ toIndex: 4]) && [s[0] isEqual: @"π„žbÀ€"] && R([s[0] removeCharactersFromIndex: 0 toIndex: 4]) && [s[0] isEqual: @""]) + + TEST(@"-[replaceCharactersFromIndex:toIndex:withString:]", + (s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆΓΆbÀ€"]) && + R([s[0] replaceCharactersFromIndex: 1 + toIndex: 4 + withString: @"Àâü"]) && + [s[0] isEqual: @"π„žΓ€ΓΆΓΌbÀ€"] && + R([s[0] replaceCharactersFromIndex: 0 + toIndex: 7 + withString: @""]) && + [s[0] isEqual: @""]) EXPECT_EXCEPTION(@"Detect OoR in " @"-[removeCharactersFromIndex:toIndex:] #1", OFOutOfRangeException, { s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆ"];