Index: src/OFMutableString.h ================================================================== --- src/OFMutableString.h +++ src/OFMutableString.h @@ -103,10 +103,20 @@ /** * Lower the OFString. */ - lower; +/** + * 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. + * This points BEHIND the last character! + */ +- removeCharactersFromIndex: (size_t)start + toIndex: (size_t)end; + /** * 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 @@ -286,10 +286,35 @@ if (is_utf8) @throw [OFInvalidEncodingException newWithClass: isa]; while (--p >= string) *p = tolower((int)*p); + + return self; +} + +- removeCharactersFromIndex: (size_t)start + toIndex: (size_t)end +{ + if (start > end) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + if (end > length) + @throw [OFOutOfRangeException newWithClass: isa]; + + memmove(string + start, string + end, length - end); + length -= end - start; + string[length] = 0; + + @try { + string = [self resizeMemory: string + toSize: length + 1]; + } @catch (OFOutOfMemoryException *e) { + /* We don't really care, as we only made it smaller */ + [e dealloc]; + } return self; } - replaceOccurrencesOfString: (OFString*)str Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -270,10 +270,12 @@ - appendWithFormat: (OFString*)fmt arguments: (va_list)args; - reverse; - upper; - lower; +- removeCharactersFromIndex: (size_t)start + toIndex: (size_t)end; - replaceOccurrencesOfString: (OFString*)str withString: (OFString*)repl; - removeLeadingWhitespaces; - removeTrailingWhitespaces; - removeLeadingAndTrailingWhitespaces; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -684,10 +684,17 @@ @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - lower +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- removeCharactersFromIndex: (size_t)start + toIndex: (size_t)end { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -150,11 +150,10 @@ if ([cache length] > 0) { OFString *str; pool = [[OFAutoreleasePool alloc] init]; str = transform_string(cache, self); - str = [[str copy] autorelease]; [delegate xmlParser: self foundString: str]; [pool release]; } @@ -453,22 +452,22 @@ if (buf[i] == '-') state = OF_XMLPARSER_IN_COMMENT_4; break; case OF_XMLPARSER_IN_COMMENT_4: if (buf[i] == '-') { - OFString *str; + size_t cache_len; [cache appendCString: buf + last withLength: i - last]; + cache_len = [cache length]; pool = [[OFAutoreleasePool alloc] init]; - str = [OFMutableString - stringWithCString: [cache cString] - length: [cache length] - 1]; - [str removeLeadingAndTrailingWhitespaces]; + [cache removeCharactersFromIndex: cache_len - 1 + toIndex: cache_len]; + [cache removeLeadingAndTrailingWhitespaces]; [delegate xmlParser: self - foundComment: str]; + foundComment: cache]; [pool release]; [cache setToCString: ""]; last = i + 1; Index: tests/OFString/OFString.m ================================================================== --- tests/OFString/OFString.m +++ tests/OFString/OFString.m @@ -22,11 +22,11 @@ #define ZD "%zd" #else #define ZD "%u" #endif -#define NUM_TESTS 71 +#define NUM_TESTS 73 #define SUCCESS \ printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m", \ (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS); \ fflush(stdout); #define FAIL \ @@ -189,10 +189,19 @@ CHECK_EXCEPT([@"foo%bar" stringByURLDecoding], OFInvalidEncodingException) CHECK_EXCEPT([@"foo%FFbar" stringByURLDecoding], OFInvalidEncodingException) + /* Remove tests */ + s1 = [@"fooobar" mutableCopy]; + [s1 removeCharactersFromIndex: 1 + toIndex: 4]; + CHECK([s1 isEqual: @"fbar"]) + [s1 removeCharactersFromIndex: 0 + toIndex: 4]; + CHECK([s1 isEqual: @""]) + /* Replace tests */ s1 = [@"asd fo asd fofo asd" mutableCopy]; [s1 replaceOccurrencesOfString: @"fo" withString: @"foo"]; CHECK([s1 isEqual: @"asd foo asd foofoo asd"])