ObjFW  Check-in [4d892e0db1]

Overview
Comment:Fix -[replaceCharactersInRange:withString:].

The resizing is now done before the memmove() if the new string is
bigger and after the memmove() if the new string is shorter. The added
comment explains why this is necessary.

This also adds a test for -[replaceCharactersInRange:withString:] that
makes the string bigger and another one that makes it smaller again.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4d892e0db130107b9a3dd7b85f5ee9d5cb38588297a4e9a3495c14c85842ec5b
User & Date: js on 2013-07-04 20:49:52
Other Links: manifest | tags
Context
2013-07-05
16:54
Map WSA error codes to standard error codes. check-in: e790f0e1f0 user: js tags: trunk
2013-07-04
20:49
Fix -[replaceCharactersInRange:withString:]. check-in: 4d892e0db1 user: js tags: trunk
14:04
OFBlock: Initialize spinlocks in +[load]. check-in: 8fbc6b4e63 user: js tags: trunk
Changes

Modified src/OFMutableString_UTF8.m from [7fb8de4d28] to [9492a9e3d8].

627
628
629
630
631
632
633










634
635
636
637
638
639
640
641
642








643
644
645
646
647
648
649
		end = of_string_utf8_get_position(_s->cString, end,
		    _s->cStringLength);
	}

	newCStringLength = _s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];
	_s->hashed = false;










	_s->cString = [self resizeMemory: _s->cString
				    size: newCStringLength + 1];

	memmove(_s->cString + start + [replacement UTF8StringLength],
	    _s->cString + end, _s->cStringLength - end);
	memcpy(_s->cString + start, [replacement UTF8String],
	    [replacement UTF8StringLength]);
	_s->cString[newCStringLength] = '\0';









	_s->cStringLength = newCStringLength;
	_s->length = newLength;
}

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   options: (int)options







>
>
>
>
>
>
>
>
>
>
|
|







>
>
>
>
>
>
>
>







627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
		end = of_string_utf8_get_position(_s->cString, end,
		    _s->cStringLength);
	}

	newCStringLength = _s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];
	_s->hashed = false;

	/*
	 * If the new string is bigger, we need to resize it first so we can
	 * memmove() the rest of the string to the end.
	 *
	 * We must not resize the string if the new string is smaller, because
	 * then we can't memove() the rest of the string forward as the rest is
	 * lost due to the resize!
	 */
	if (newCStringLength > _s->cStringLength)
		_s->cString = [self resizeMemory: _s->cString
					    size: newCStringLength + 1];

	memmove(_s->cString + start + [replacement UTF8StringLength],
	    _s->cString + end, _s->cStringLength - end);
	memcpy(_s->cString + start, [replacement UTF8String],
	    [replacement UTF8StringLength]);
	_s->cString[newCStringLength] = '\0';

	/*
	 * If the new string is smaller, we can safely resize it now as we're
	 * done with memmove().
	 */
	if (newCStringLength < _s->cStringLength)
		_s->cString = [self resizeMemory: _s->cString
					    size: newCStringLength + 1];

	_s->cStringLength = newCStringLength;
	_s->length = newLength;
}

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   options: (int)options

Modified tests/OFStringTests.m from [a5554c7700] to [fbc369ef09].

477
478
479
480
481
482
483
484



485
486
487
488
489
490
491
	    [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,







|
>
>
>







477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
	    [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(4, 2)
				  withString: @"b"]) &&
	    [s[0] isEqual: @"π„žΓ€ΓΆΓΌbÀ€"] &&
	    R([s[0] replaceCharactersInRange: of_range(0, 7)
				  withString: @""]) &&
	    [s[0] isEqual: @""])

	EXPECT_EXCEPTION(@"Detect OoR in -[deleteCharactersInRange:] #1",
	    OFOutOfRangeException,