Index: src/OFConstantString.m ================================================================== --- src/OFConstantString.m +++ src/OFConstantString.m @@ -467,20 +467,10 @@ [self finishInitialization]; return [super containsString: string]; } -- (OFString*)substringFromIndex: (size_t)start - toIndex: (size_t)end -{ - if (initialized != SIZE_MAX) - [self finishInitialization]; - - return [super substringFromIndex: start - toIndex: end]; -} - - (OFString*)substringWithRange: (of_range_t)range { if (initialized != SIZE_MAX) [self finishInitialization]; Index: src/OFHTTPRequest.m ================================================================== --- src/OFHTTPRequest.m +++ src/OFHTTPRequest.m @@ -282,12 +282,12 @@ if (![line hasPrefix: @"HTTP/1.0 "] && ![line hasPrefix: @"HTTP/1.1 "]) @throw [OFInvalidServerReplyException newWithClass: isa]; - status = (int)[[line substringFromIndex: 9 - toIndex: 12] decimalValue]; + status = (int)[[line substringWithRange: + of_range(9, 3)] decimalValue]; serverHeaders = [OFMutableDictionary dictionary]; while ((line = [sock readLine]) != nil) { OFString *key, *value; Index: src/OFMutableString.h ================================================================== --- src/OFMutableString.h +++ src/OFMutableString.h @@ -120,36 +120,14 @@ atIndex: (size_t)index; /** * \brief Deletes 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! - */ -- (void)deleteCharactersFromIndex: (size_t)start - toIndex: (size_t)end; - -/** - * \brief Deletes the characters at the specified range. - * * \param range The range of the characters which should be removed */ - (void)deleteCharactersInRange: (of_range_t)range; -/** - * \brief Replaces 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 replacement The string to the replace the characters with - */ -- (void)replaceCharactersFromIndex: (size_t)start - toIndex: (size_t)end - withString: (OFString*)replacement; - /** * \brief Replaces the characters at the specified range. * * \param range The range of the characters which should be replaced * \param replacement The string to the replace the characters with Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -431,13 +431,15 @@ s->cStringLength = newCStringLength; s->length += string->s->length; } -- (void)deleteCharactersFromIndex: (size_t)start - toIndex: (size_t)end +- (void)deleteCharactersInRange: (of_range_t)range { + size_t start = range.start; + size_t end = range.start + range.length; + if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if (end > s->length) @@ -463,20 +465,15 @@ /* We don't really care, as we only made it smaller */ [e release]; } } -- (void)deleteCharactersInRange: (of_range_t)range -{ - [self deleteCharactersFromIndex: range.start - toIndex: range.start + range.length]; -} - -- (void)replaceCharactersFromIndex: (size_t)start - toIndex: (size_t)end - withString: (OFString*)replacement -{ +- (void)replaceCharactersInRange: (of_range_t)range + withString: (OFString*)replacement +{ + size_t start = range.start; + size_t end = range.start + range.length; size_t newCStringLength, newLength; if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; @@ -506,18 +503,10 @@ s->cStringLength = newCStringLength; s->length = newLength; } -- (void)replaceCharactersInRange: (of_range_t)range - withString: (OFString*)replacement -{ - [self replaceCharactersFromIndex: range.start - toIndex: range.start + range.length - withString: replacement]; -} - - (void)replaceOccurrencesOfString: (OFString*)string withString: (OFString*)replacement { const char *cString = [string cString]; const char *replacementCString = [replacement cString]; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -572,19 +572,10 @@ * \param string The string to search * \return Whether the string contains the specified string */ - (BOOL)containsString: (OFString*)string; -/** - * \param start The index where the substring starts - * \param end The index where the substring ends. - * This points BEHIND the last character! - * \return The substring as a new autoreleased OFString - */ -- (OFString*)substringFromIndex: (size_t)start - toIndex: (size_t)end; - /** * \param range The range of the substring * \return The substring as a new autoreleased OFString */ - (OFString*)substringWithRange: (of_range_t)range; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -1396,16 +1396,14 @@ return YES; return NO; } -- (OFString*)substringFromIndex: (size_t)start - toIndex: (size_t)end +- (OFString*)substringWithRange: (of_range_t)range { - if (start > end) - @throw [OFInvalidArgumentException newWithClass: isa - selector: _cmd]; + size_t start = range.start; + size_t end = range.start + range.length; if (end > s->length) @throw [OFOutOfRangeException newWithClass: isa]; if (s->isUTF8) { @@ -1417,16 +1415,10 @@ return [OFString stringWithCString: s->cString + start length: end - start]; } -- (OFString*)substringWithRange: (of_range_t)range -{ - return [self substringFromIndex: range.start - toIndex: range.start + range.length]; -} - - (OFString*)stringByAppendingString: (OFString*)string { OFMutableString *new; new = [OFMutableString stringWithString: self]; Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -74,12 +74,11 @@ withString: @"\n"]; if (cut > 0) { size_t length = [ret length]; - [ret deleteCharactersFromIndex: length - cut - toIndex: length]; + [ret deleteCharactersInRange: of_range(length - cut, cut)]; } if (unescape) return [ret stringByXMLUnescapingWithDelegate: delegate]; @@ -397,12 +396,11 @@ if (!acceptProlog) return NO; acceptProlog = NO; - pi = [pi substringFromIndex: 3 - toIndex: [pi length]]; + pi = [pi substringWithRange: of_range(3, [pi length] - 3)]; pi = [pi stringByDeletingEnclosingWhitespaces]; cString = [pi cString]; length = [pi cStringLength]; Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -218,29 +218,18 @@ [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"ΓΆΓΆ"] == 1 && [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"ΓΆ"] == 2 && [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"π„ž"] == 0 && [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"x"] == OF_INVALID_INDEX) - TEST(@"-[substringFromIndexToIndex:]", - [[@"π„žΓΆΓΆ" substringFromIndex: 1 - toIndex: 2] isEqual: @"ΓΆ"] && - [[@"π„žΓΆΓΆ" substringFromIndex: 3 - toIndex: 3] isEqual: @""]) - - EXPECT_EXCEPTION(@"Detect out of range in " - @"-[substringFromIndex:toIndex:] #1", OFOutOfRangeException, - [@"π„žΓΆΓΆ" substringFromIndex: 2 - toIndex: 4]) - EXPECT_EXCEPTION(@"Detect out of range in " - @"-[substringFromIndex:toIndex:] #2", OFOutOfRangeException, - [@"π„žΓΆΓΆ" substringFromIndex: 4 - toIndex: 4]) - - EXPECT_EXCEPTION(@"Detect start > end in " - @"-[substringFromIndex:toIndex:]", OFInvalidArgumentException, - [@"π„žΓΆΓΆ" substringFromIndex: 2 - toIndex: 0]) + TEST(@"-[substringWithRange:]", + [[@"π„žΓΆΓΆ" substringWithRange: of_range(1, 1)] isEqual: @"ΓΆ"] && + [[@"π„žΓΆΓΆ" substringWithRange: of_range(3, 0)] isEqual: @""]) + + EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #1", + OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(2, 2)]) + EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #2", + OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(4, 0)]) TEST(@"-[stringByAppendingString:]", [[@"foo" stringByAppendingString: @"bar"] isEqual: @"foobar"]) TEST(@"-[stringByPrependingString:]", @@ -412,67 +401,46 @@ @"#1", OFInvalidEncodingException, [@"foo%bar" stringByURLDecoding]) EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " @"#2", OFInvalidEncodingException, [@"foo%FFbar" stringByURLDecoding]) - TEST(@"-[deleteCharactersFromIndex:toIndex:]", - (s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆΓΆbÀ€"]) && - R([s[0] deleteCharactersFromIndex: 1 - toIndex: 4]) && - [s[0] isEqual: @"π„žbÀ€"] && - R([s[0] deleteCharactersFromIndex: 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 " - @"-[deleteCharactersFromIndex:toIndex:] #1", OFOutOfRangeException, - { - s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆ"]; - [s[0] deleteCharactersFromIndex: 2 - toIndex: 4]; + TEST(@"-[deleteCharactersInRange:]", + (s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆΓΆbÀ€"]) && + R([s[0] deleteCharactersInRange: of_range(1, 3)]) && + [s[0] isEqual: @"π„žbÀ€"] && + R([s[0] deleteCharactersInRange: of_range(0, 4)]) && + [s[0] isEqual: @""]) + + TEST(@"-[replaceCharactersInRange:withString:]", + (s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆΓΆbÀ€"]) && + R([s[0] replaceCharactersInRange: of_range(1, 3) + withString: @"Àâü"]) && + [s[0] isEqual: @"π„žΓ€ΓΆΓΌbÀ€"] && + R([s[0] replaceCharactersInRange: of_range(0, 7) + withString: @""]) && + [s[0] isEqual: @""]) + + EXPECT_EXCEPTION(@"Detect OoR in -[deleteCharactersInRange:] #1", + OFOutOfRangeException, + { + s[0] = [OFMutableString stringWithString: @"π„žΓΆΓΆ"]; + [s[0] deleteCharactersInRange: of_range(2, 2)]; }) - EXPECT_EXCEPTION(@"Detect OoR in " - @"-[deleteCharactersFromIndex:toIndex:] #2", OFOutOfRangeException, - [s[0] deleteCharactersFromIndex: 4 - toIndex: 4]) - - EXPECT_EXCEPTION(@"Detect s > e in " - @"-[deleteCharactersFromIndex:toIndex:]", - OFInvalidArgumentException, - [s[0] deleteCharactersFromIndex: 2 - toIndex: 0]) - - EXPECT_EXCEPTION(@"OoR " - @"-[replaceCharactersFromIndex:toIndex:withString:] #1", - OFOutOfRangeException, [s[0] replaceCharactersFromIndex: 2 - toIndex: 4 - withString: @""]) - - EXPECT_EXCEPTION(@"OoR " - @"-[replaceCharactersFromIndex:toIndex:withString:] #2", + EXPECT_EXCEPTION(@"Detect OoR in -[deleteCharactersInRange:] #2", + OFOutOfRangeException, + [s[0] deleteCharactersInRange: of_range(4, 0)]) + + EXPECT_EXCEPTION(@"Detect OoR in -[replaceCharactersInRange:withString:] #1", + OFOutOfRangeException, + [s[0] replaceCharactersInRange: of_range(2, 2) + withString: @""]) + + EXPECT_EXCEPTION(@"Detect OoR in -[replaceCharactersInRange:withString:] #2", OFOutOfRangeException, - [s[0] replaceCharactersFromIndex: 4 - toIndex: 4 - withString: @""]) - - EXPECT_EXCEPTION(@"s>e in " - @"-[replaceCharactersFromIndex:toIndex:withString:]", - OFInvalidArgumentException, [s[0] replaceCharactersFromIndex: 2 - toIndex: 0 - withString: @""]) + [s[0] replaceCharactersInRange: of_range(4, 0) + withString: @""]) TEST(@"-[replaceOccurrencesOfString:withString:]", (s[0] = [OFMutableString stringWithString: @"asd fo asd fofo asd"]) && R([s[0] replaceOccurrencesOfString: @"fo"