ObjFW  Diff

Differences From Artifact [3976c4d518]:

To Artifact [521dc8393f]:


344
345
346
347
348
349
350

351
352
353
354
355

356
357
358
359
360
361

362
363
364
365
366
367
368
369
370
371
372
373
374










375
376
377
378
379
380
381
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

363

364
365
366
367
368
369
370
371
372
373


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390







+





+





-
+
-










-
-
+
+
+
+
+
+
+
+
+
+








		objc_autoreleasePoolPop(pool);
	}
}

- (void)appendString: (OFString *)string
{
	const char *UTF8String;
	size_t UTF8StringLength;

	if (string == nil)
		@throw [OFInvalidArgumentException exception];

	UTF8String = string.UTF8String;
	UTF8StringLength = string.UTF8StringLength;

	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, string.UTF8String,
	memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength);
	    UTF8StringLength);

	_s->cStringLength += UTF8StringLength;
	_s->length += string.length;

	_s->cString[_s->cStringLength] = 0;

	if ([string isKindOfClass: [OFUTF8String class]] ||
	    [string isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)string)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
	} else {
		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    NULL)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
}

- (void)appendCharacters: (const OFUnichar *)characters length: (size_t)length
{
	char *tmp = OFAllocMemory((length * 4) + 1, 1);

	@try {
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
461
462
463










464
465
466
467
468
469
470
438
439
440
441
442
443
444
445

446
447
448
449
450
451
452
453
454
455
456
457

458
459
460
461



462
463
464

465
466
467
468
469
470
471
472
473


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490







+
-
+








+
+
+
-
+



-
-
-
+
+
+
-









-
-
+
+
+
+
+
+
+
+
+
+







	} @finally {
		free(UTF8String);
	}
}

- (void)insertString: (OFString *)string atIndex: (size_t)idx
{
	const char *UTF8String;
	size_t newCStringLength;
	size_t UTF8StringLength, newCStringLength;

	if (idx > _s->length)
		@throw [OFOutOfRangeException exception];

	if (_s->isUTF8)
		idx = _OFUTF8StringIndexToPosition(_s->cString, idx,
		    _s->cStringLength);

	UTF8String = string.UTF8String;
	UTF8StringLength = string.UTF8StringLength;

	newCStringLength = _s->cStringLength + string.UTF8StringLength;
	newCStringLength = _s->cStringLength + UTF8StringLength;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, 1);

	memmove(_s->cString + idx + string.UTF8StringLength,
	    _s->cString + idx, _s->cStringLength - idx);
	memcpy(_s->cString + idx, string.UTF8String,
	memmove(_s->cString + idx + UTF8StringLength, _s->cString + idx,
	    _s->cStringLength - idx);
	memcpy(_s->cString + idx, UTF8String, UTF8StringLength);
	    string.UTF8StringLength);
	_s->cString[newCStringLength] = '\0';

	_s->cStringLength = newCStringLength;
	_s->length += string.length;

	if ([string isKindOfClass: [OFUTF8String class]] ||
	    [string isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)string)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
	} else {
		switch (_OFUTF8StringCheck(UTF8String, UTF8StringLength,
		    NULL)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
}

- (void)deleteCharactersInRange: (OFRange)range
{
	size_t start = range.location;
	size_t end = range.location + range.length;

495
496
497
498
499
500
501


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517



518
519


520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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







+
+
















+
+
+
-
-
+
+














-
-
-
+
+
+
-

















-
-
+
+
+
+
+
+
+
+
+
+








- (void)replaceCharactersInRange: (OFRange)range
		      withString: (OFString *)replacement
{
	size_t start = range.location;
	size_t end = range.location + range.length;
	size_t newCStringLength, newLength;
	const char *replacementString;
	size_t replacementLength;

	if (replacement == nil)
		@throw [OFInvalidArgumentException exception];

	if (range.length > SIZE_MAX - range.location || end > _s->length)
		@throw [OFOutOfRangeException exception];

	newLength = _s->length - range.length + replacement.length;

	if (_s->isUTF8) {
		start = _OFUTF8StringIndexToPosition(_s->cString, start,
		    _s->cStringLength);
		end = _OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	replacementString = replacement.UTF8String;
	replacementLength = replacement.UTF8StringLength;

	newCStringLength = _s->cStringLength - (end - start) +
	    replacement.UTF8StringLength;
	newCStringLength =
	    _s->cStringLength - (end - start) + replacementLength;
	_s->hasHash = 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 memmove() the rest of the string forward as the rest is
	 * lost due to the resize!
	 */
	if (newCStringLength > _s->cStringLength)
		_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1,
		    1);

	memmove(_s->cString + start + replacement.UTF8StringLength,
	    _s->cString + end, _s->cStringLength - end);
	memcpy(_s->cString + start, replacement.UTF8String,
	memmove(_s->cString + start + replacementLength, _s->cString + end,
	    _s->cStringLength - end);
	memcpy(_s->cString + start, replacementString, replacementLength);
	    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 = OFResizeMemory(_s->cString, newCStringLength + 1,
		    1);

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

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
	} else {
		switch (_OFUTF8StringCheck(replacementString, replacementLength,
		    NULL)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
}

- (void)replaceOccurrencesOfString: (OFString *)string
			withString: (OFString *)replacement
			   options: (int)options
			     range: (OFRange)range
{
633
634
635
636
637
638
639
640
641










642
643
644
645
646
647
648
665
666
667
668
669
670
671


672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688







-
-
+
+
+
+
+
+
+
+
+
+







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

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
	} else {
		switch (_OFUTF8StringCheck(replacementString, replacementLength,
		    NULL)) {
		case 1:
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException exception];
		}
	}
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < _s->cStringLength; i++)