ObjFW  Check-in [cacfcf1b9d]

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 | 1.1
Files: files | file ages | folders
SHA3-256: cacfcf1b9d70ebd6788ea775af1554585e7e59137c88463da0c4ee6c2d0d0598
User & Date: js on 2024-08-24 23:43:39
Other Links: branch diff | manifest | tags
Context
2024-08-25
21:29
OFINIFile: Fix parsing `=` within `"` check-in: a7ef90581c user: js tags: 1.1
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:20
OFString: Fix converting from some encodings check-in: 6e79f005c4 user: js tags: 1.1
Changes

Modified src/OFMutableUTF8String.m from [0d2c04f6f8] to [dda54dd86e].

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







+





+





-
+
-










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








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










462
463
464
465
466
467
468
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
471


472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488







+
-
+








+
+
+
-
+



-
-
-
+
+
+
-









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







	} @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;

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







+
+
















+
+
+
-
-
+
+














-
-
-
+
+
+
-

















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








- (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
{
631
632
633
634
635
636
637
638
639










640
641
642
643
644
645
646
663
664
665
666
667
668
669


670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686







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







	_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.h from [f1efc16deb] to [a9510f1ddf].

42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
42
43
44
45
46
47
48

49
50
51
52
53
54
55
56







-
+







	struct OFUTF8StringIvars _storage;
}
@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 [2832b02ca4] to [3e5753e6f2].

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







-
-
-
-
-
-
-





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







{
	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;
}