ObjFW  Diff

Differences From Artifact [62af83b584]:

To Artifact [c4bb148231]:


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
- init
{
	self = [super init];

	@try {
		_s = &_storage;

		_s->cString = [self allocMemoryWithSize: 1];
		_s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;







|







175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
- init
{
	self = [super init];

	@try {
		_s = &_storage;

		_s->cString = (void *)[self allocMemoryWithSize: 1];
		_s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
241
242
243
244
245
246
247

248
249
250
251
252
253
254
255
		    memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
			cString += 3;
			cStringLength -= 3;
		}

		_s = &_storage;


		_s->cString = [self allocMemoryWithSize: cStringLength + 1];
		_s->cStringLength = cStringLength;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_utf8_check(cString, cStringLength,
			    &_s->length)) {
			case 1:







>
|







241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
		    memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
			cString += 3;
			cStringLength -= 3;
		}

		_s = &_storage;

		_s->cString = (void *)[self
		    allocMemoryWithSize: cStringLength + 1];
		_s->cStringLength = cStringLength;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_utf8_check(cString, cStringLength,
			    &_s->length)) {
			case 1:
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exception];

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

				memcpy(_s->cString + j, buffer, bytes);
				j += bytes;
			}








|







289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exception];

				_s->cStringLength += bytes - 1;
				_s->cString = (void *)[self
				    resizeMemory: _s->cString
					    size: _s->cStringLength + 1];

				memcpy(_s->cString + j, buffer, bytes);
				j += bytes;
			}

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
			_s->isUTF8 = true;
			byteLength = of_string_utf8_encode(unichar, buffer);

			if (byteLength == 0)
				@throw [OFInvalidEncodingException exception];

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

			memcpy(_s->cString + j, buffer, byteLength);
			j += byteLength;
		}








|







370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
			_s->isUTF8 = true;
			byteLength = of_string_utf8_encode(unichar, buffer);

			if (byteLength == 0)
				@throw [OFInvalidEncodingException exception];

			_s->cStringLength += byteLength - 1;
			_s->cString = (void *)[self
			    resizeMemory: _s->cString
				    size: _s->cStringLength + 1];

			memcpy(_s->cString + j, buffer, byteLength);
			j += byteLength;
		}

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
491
492
493
494
495
496
497
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			_s->isUTF8 = ((OFString_UTF8 *)string)->_s->isUTF8;
		else
			_s->isUTF8 = true;

		_s->length = [string length];


		_s->cString = [self allocMemoryWithSize: _s->cStringLength + 1];
		memcpy(_s->cString, [string UTF8String], _s->cStringLength + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCharacters: (const of_unichar_t *)characters
	      length: (size_t)length
{
	self = [super init];

	@try {
		size_t j;

		_s = &_storage;


		_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			size_t len = of_string_utf8_encode(characters[i],
			    _s->cString + j);

			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
				_s->isUTF8 = true;

			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}







>
|



















>
|




















|
|







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
491
492
493
494
495
496
497
498
499
500
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			_s->isUTF8 = ((OFString_UTF8 *)string)->_s->isUTF8;
		else
			_s->isUTF8 = true;

		_s->length = [string length];

		_s->cString = (void *)[self
		    allocMemoryWithSize: _s->cStringLength + 1];
		memcpy(_s->cString, [string UTF8String], _s->cStringLength + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCharacters: (const of_unichar_t *)characters
	      length: (size_t)length
{
	self = [super init];

	@try {
		size_t j;

		_s = &_storage;

		_s->cString = (void *)[self
		    allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			size_t len = of_string_utf8_encode(characters[i],
			    _s->cString + j);

			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
				_s->isUTF8 = true;

			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = (void *)[self resizeMemory: _s->cString
							    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
517
518
519
520
521
522
523

524
525
526
527
528
529
530
531
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = true;

		_s = &_storage;


		_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			of_unichar_t character =
			    (swap ? OF_BSWAP16(string[i]) : string[i]);
			size_t len;







>
|







520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = true;

		_s = &_storage;

		_s->cString = (void *)[self
		    allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			of_unichar_t character =
			    (swap ? OF_BSWAP16(string[i]) : string[i]);
			size_t len;
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}







|
|







571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
			j += len;
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = (void *)[self resizeMemory: _s->cString
							    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
602
603
604
605
606
607
608

609
610
611
612
613
614
615
616
			characters++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = true;

		_s = &_storage;


		_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(characters[i]) : characters[i]),







>
|







606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
			characters++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = true;

		_s = &_storage;

		_s->cString = (void *)[self
		    allocMemoryWithSize: (length * 4) + 1];
		_s->length = length;

		j = 0;
		for (size_t i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(characters[i]) : characters[i]),
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
			}
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}







|
|







639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
			}
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = (void *)[self resizeMemory: _s->cString
							    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
			case 1:
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}

			_s->cString = [self
			    allocMemoryWithSize: cStringLength + 1];
			memcpy(_s->cString, tmp, cStringLength + 1);
		} @finally {
			free(tmp);
		}
	} @catch (id e) {
		[self release];







|







682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
			case 1:
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}

			_s->cString = (void *)[self
			    allocMemoryWithSize: cStringLength + 1];
			memcpy(_s->cString, tmp, cStringLength + 1);
		} @finally {
			free(tmp);
		}
	} @catch (id e) {
		[self release];