ObjFW  Check-in [82c5846734]

Overview
Comment:Don't assume all custom string classes use Unicode

Initially, isUTF8 was set to true for all custom string classes because
having isUTF8 set to false was merely an optimization. However,
-[OFUTF8String cStringWithEncoding: OFStringEncodingASCII] throws an
exception when isUTF8 is true.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 82c584673415ba9d2a14c9f2664fe1d079b8e3ff09973d597725705e57fd61c6
User & Date: js on 2024-08-24 23:41:16
Other Links: manifest | tags
Context
2024-08-25
00:47
Add support for tagged pointer strings check-in: 8ca952eaa4 user: js tags: trunk
2024-08-24
23:43
Don't assume all custom string classes use Unicode check-in: cacfcf1b9d user: js tags: 1.1
23:41
Don't assume all custom string classes use Unicode check-in: 82c5846734 user: js tags: trunk
22:19
OFString: Fix converting from some encodings check-in: d6a8d57148 user: js tags: trunk
Changes

Modified src/OFMutableUTF8String.m from [3976c4d518] to [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++)

Modified src/OFUTF8String+Private.h from [ec0d6fe6c1] to [ccfb13bd6d].

27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
27
28
29
30
31
32
33

34
35
36
37
38
39
40
41







-
+







			       length: (size_t)UTF8StringLength
			      storage: (char *)storage OF_METHOD_FAMILY(init);
@end

#ifdef __cplusplus
extern "C" {
#endif
extern int _OFUTF8StringCheck(const char *, size_t, size_t *)
extern int _OFUTF8StringCheck(const char *, size_t, size_t *_Nullable)
    OF_VISIBILITY_HIDDEN;
extern size_t _OFUTF8StringIndexToPosition(const char *, size_t, size_t)
    OF_VISIBILITY_HIDDEN;
#ifdef __cplusplus
}
#endif

Modified src/OFUTF8String.m from [69877c8b33] to [eb22322b98].

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
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







-
-
-
-
-
-
-





+
+
+
+
+
+
+
+
+
+
+
+
+
+







{
	self = [super init];

	@try {
		_s = &_storage;

		_s->cStringLength = string.UTF8StringLength;

		if ([string isKindOfClass: [OFUTF8String class]] ||
		    [string isKindOfClass: [OFMutableUTF8String class]])
			_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
		else
			_s->isUTF8 = true;

		_s->length = string.length;

		_s->cString = OFAllocMemory(_s->cStringLength + 1, 1);
		memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1);
		_s->freeWhenDone = true;

		if ([string isKindOfClass: [OFUTF8String class]] ||
		    [string isKindOfClass: [OFMutableUTF8String class]])
			_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
		else {
			switch (_OFUTF8StringCheck(_s->cString,
			    _s->cStringLength, NULL)) {
			case 1:
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}