ObjFW  Check-in [e5fe02de61]

Overview
Comment:Add -[OFMutableString replaceOccurrencesOfString:withString:inRange:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e5fe02de61cbf89b3cb32405ec36bd65ffd2f486640a179c8795116825fd18cb
User & Date: js on 2012-01-31 18:59:15
Other Links: manifest | tags
Context
2012-02-01
13:42
Clean up the properties mess. check-in: c1b35e1495 user: js tags: trunk
2012-01-31
18:59
Add -[OFMutableString replaceOccurrencesOfString:withString:inRange:]. check-in: e5fe02de61 user: js tags: trunk
15:16
Remove useless checks. check-in: f68b3b300d user: js tags: trunk
Changes

Modified src/OFMutableString.h from [17025313df] to [33da247c32].

151
152
153
154
155
156
157












158
159
160
161
162
163
164
 *
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 */
- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement;













/**
 * \brief Deletes all whitespaces at the beginning of the string.
 */
- (void)deleteLeadingWhitespaces;

/**
 * \brief Deletes all whitespaces at the end of the string.







>
>
>
>
>
>
>
>
>
>
>
>







151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 *
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 */
- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement;

/**
 * \brief Replaces all occurrences of a string in the specified range with
 *	  another string.
 *
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 * \param range The range in which the string should be replaced
 */
- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   inRange: (of_range_t)range;

/**
 * \brief Deletes all whitespaces at the beginning of the string.
 */
- (void)deleteLeadingWhitespaces;

/**
 * \brief Deletes all whitespaces at the end of the string.

Modified src/OFMutableString.m from [9b4155a1d7] to [9d0d0e1c5d].

23
24
25
26
27
28
29

30
31
32
33
34
35
36
#import "OFString.h"
#import "OFMutableString_UTF8.h"
#import "OFAutoreleasePool.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"


#import "macros.h"

#import "of_asprintf.h"
#import "unicode.h"

static struct {







>







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import "OFString.h"
#import "OFMutableString_UTF8.h"
#import "OFAutoreleasePool.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

#import "of_asprintf.h"
#import "unicode.h"

static struct {
408
409
410
411
412
413
414









415
416
417
418
419
420
421
422
423



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
	[self deleteCharactersInRange: range];
	[self insertString: replacement
		   atIndex: range.start];
}

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









{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init], *pool2;
	const of_unichar_t *unicodeString;
	const of_unichar_t *searchString = [string unicodeString];
	size_t length = [self length];
	size_t searchLength = [string length];
	size_t replacementLength = [replacement length];
	size_t i;




	if (searchLength > length) {
		[pool release];
		return;
	}

	pool2 = [[OFAutoreleasePool alloc] init];
	unicodeString = [self unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t)))
			continue;

		[self replaceCharactersInRange: of_range(i, searchLength)
				    withString: replacement];

		length -= searchLength;
		length += replacementLength;

		i += replacementLength - 1;

		[pool2 releaseObjects];

		unicodeString = [self unicodeString];
	}







>
>
>
>
>
>
>
>
>




<




>
>
>
|







|







|
|







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	[self deleteCharactersInRange: range];
	[self insertString: replacement
		   atIndex: range.start];
}

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
{
	[self replaceOccurrencesOfString: string
			      withString: replacement
				 inRange: of_range(0, [self length])];
}

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   inRange: (of_range_t)range
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init], *pool2;
	const of_unichar_t *unicodeString;
	const of_unichar_t *searchString = [string unicodeString];

	size_t searchLength = [string length];
	size_t replacementLength = [replacement length];
	size_t i;

	if (range.start + range.length > [self length])
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if (searchLength > range.length) {
		[pool release];
		return;
	}

	pool2 = [[OFAutoreleasePool alloc] init];
	unicodeString = [self unicodeString];

	for (i = range.start; i <= range.length - searchLength; i++) {
		if (memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t)))
			continue;

		[self replaceCharactersInRange: of_range(i, searchLength)
				    withString: replacement];

		range.length -= searchLength;
		range.length += replacementLength;

		i += replacementLength - 1;

		[pool2 releaseObjects];

		unicodeString = [self unicodeString];
	}

Modified src/OFMutableString_UTF8.m from [8b0f05ccee] to [6763734ad5].

535
536
537
538
539
540
541

542
543
544
545
546
547
548
549


550








551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585

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

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

{
	const char *UTF8String = [string UTF8String];
	const char *replacementUTF8String = [replacement UTF8String];
	size_t UTF8StringLength = [string UTF8StringLength];
	size_t replacementUTF8StringLength = [replacement UTF8StringLength];
	size_t i, last, newCStringLength, newLength;
	char *newCString;



	if (UTF8StringLength > s->cStringLength)








		return;

	newCString = NULL;
	newCStringLength = 0;
	newLength = s->length;

	for (i = 0, last = 0; i <= s->cStringLength - UTF8StringLength; i++) {
		if (memcmp(s->cString + i, UTF8String, UTF8StringLength))
			continue;

		@try {
			newCString = [self
			    resizeMemory: newCString
				  toSize: newCStringLength + i - last +
					  replacementUTF8StringLength + 1];
		} @catch (id e) {
			[self freeMemory: newCString];
			@throw e;
		}
		memcpy(newCString + newCStringLength, s->cString + last,
		    i - last);
		memcpy(newCString + newCStringLength + i - last,
		    replacementUTF8String, replacementUTF8StringLength);

		newCStringLength += i - last + replacementUTF8StringLength;
		newLength = newLength - [string length] + [replacement length];

		i += UTF8StringLength - 1;
		last = i + 1;
	}

	@try {
		newCString = [self
		    resizeMemory: newCString
			  toSize: newCStringLength +







>

|
|
|
|



>
>
|
>
>
>
>
>
>
>
>






|
|






|







|

|


|







535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

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

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   inRange: (of_range_t)range
{
	const char *searchString = [string UTF8String];
	const char *replacementString = [replacement UTF8String];
	size_t searchLength = [string UTF8StringLength];
	size_t replacementLength = [replacement UTF8StringLength];
	size_t i, last, newCStringLength, newLength;
	char *newCString;

	if (s->UTF8) {
		range.start = of_string_index_to_position(s->cString,
		    range.start, s->cStringLength);
		range.length = of_string_index_to_position(s->cString,
		    range.start + range.length, s->cStringLength) - range.start;
	}

	if (range.start + range.length > [self UTF8StringLength])
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if ([string UTF8StringLength] > range.length)
		return;

	newCString = NULL;
	newCStringLength = 0;
	newLength = s->length;

	for (i = range.start, last = 0; i <= range.length - searchLength; i++) {
		if (memcmp(s->cString + i, searchString, searchLength))
			continue;

		@try {
			newCString = [self
			    resizeMemory: newCString
				  toSize: newCStringLength + i - last +
					  replacementLength + 1];
		} @catch (id e) {
			[self freeMemory: newCString];
			@throw e;
		}
		memcpy(newCString + newCStringLength, s->cString + last,
		    i - last);
		memcpy(newCString + newCStringLength + i - last,
		    replacementString, replacementLength);

		newCStringLength += i - last + replacementLength;
		newLength = newLength - [string length] + [replacement length];

		i += searchLength - 1;
		last = i + 1;
	}

	@try {
		newCString = [self
		    resizeMemory: newCString
			  toSize: newCStringLength +

Modified src/OFString.h from [9670456400] to [14c6115375].

647
648
649
650
651
652
653













654
655
656
657
658
659
660
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 * \return A new string with the occurrences of the specified string replaced
 */
- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement;














/**
 * \brief Returns the string in uppercase.
 *
 * \return The string in uppercase
 */
- (OFString*)uppercaseString;








>
>
>
>
>
>
>
>
>
>
>
>
>







647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 * \return A new string with the occurrences of the specified string replaced
 */
- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement;

/**
 * \brief Creates a new string by replacing the occurrences of the specified
 *	  string in the specified range with the specified replacement.
 *
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 * \param range The range in which to replace the string
 * \return A new string with the occurrences of the specified string replaced
 */
- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement
					  inRange: (of_range_t)range;

/**
 * \brief Returns the string in uppercase.
 *
 * \return The string in uppercase
 */
- (OFString*)uppercaseString;

Modified src/OFString.m from [3219cfa2ad] to [793d434ba8].

1389
1390
1391
1392
1393
1394
1395















1396
1397
1398
1399
1400
1401
1402
- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new replaceOccurrencesOfString: string
			     withString: replacement];
















	[new makeImmutable];

	return new;
}

- (OFString*)uppercaseString







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new replaceOccurrencesOfString: string
			     withString: replacement];

	[new makeImmutable];

	return new;
}

- (OFString*)stringByReplacingOccurrencesOfString: (OFString*)string
				       withString: (OFString*)replacement
					  inRange: (of_range_t)range
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new replaceOccurrencesOfString: string
			     withString: replacement
				inRange: range];

	[new makeImmutable];

	return new;
}

- (OFString*)uppercaseString

Modified tests/OFStringTests.m from [99e29fe6af] to [3cd3c8ef1d].

475
476
477
478
479
480
481








482
483
484
485
486
487
488
	    R([s[0] replaceOccurrencesOfString: @"fo"
				    withString: @"foo"]) &&
	    [s[0] isEqual: @"asd foo asd foofoo asd"] &&
	    (s[0] = [OFMutableString stringWithString: @"XX"]) &&
	    R([s[0] replaceOccurrencesOfString: @"X"
				    withString: @"XX"]) &&
	    [s[0] isEqual: @"XXXX"])









	TEST(@"-[deleteLeadingWhitespaces]",
	    (s[0] = [OFMutableString stringWithString: whitespace[0]]) &&
	    R([s[0] deleteLeadingWhitespaces]) &&
	    [s[0] isEqual: @"asd  \t \t\t\r\n"] &&
	    (s[0] = [OFMutableString stringWithString: whitespace[1]]) &&
	    R([s[0] deleteLeadingWhitespaces]) && [s[0] isEqual: @""])







>
>
>
>
>
>
>
>







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
	    R([s[0] replaceOccurrencesOfString: @"fo"
				    withString: @"foo"]) &&
	    [s[0] isEqual: @"asd foo asd foofoo asd"] &&
	    (s[0] = [OFMutableString stringWithString: @"XX"]) &&
	    R([s[0] replaceOccurrencesOfString: @"X"
				    withString: @"XX"]) &&
	    [s[0] isEqual: @"XXXX"])

	TEST(@"-[replaceOccurrencesOfString:withString:inRange:]",
	    (s[0] = [OFMutableString stringWithString:
	    @"foofoobarfoobarfoo"]) &&
	    R([s[0] replaceOccurrencesOfString: @"oo"
				    withString: @"óò"
				       inRange: of_range(2, 15)]) &&
	    [s[0] isEqual: @"foofóòbarfóòbarfoo"])

	TEST(@"-[deleteLeadingWhitespaces]",
	    (s[0] = [OFMutableString stringWithString: whitespace[0]]) &&
	    R([s[0] deleteLeadingWhitespaces]) &&
	    [s[0] isEqual: @"asd  \t \t\t\r\n"] &&
	    (s[0] = [OFMutableString stringWithString: whitespace[1]]) &&
	    R([s[0] deleteLeadingWhitespaces]) && [s[0] isEqual: @""])