ObjFW  Diff

Differences From Artifact [c99ccfd8e4]:

To Artifact [3c842a2c2f]:


251
252
253
254
255
256
257











258
259
260
261
262
263
264
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}












@implementation OFString
+ string
{
	return [[[self alloc] init] autorelease];
}








>
>
>
>
>
>
>
>
>
>
>







251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

size_t
of_utf16_string_length(const uint16_t *string)
{
	const uint16_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

@implementation OFString
+ string
{
	return [[[self alloc] init] autorelease];
}

302
303
304
305
306
307
308












309
310
311
312
313
314
315

+ stringWithUnicodeString: (of_unichar_t*)string
		   length: (size_t)length
{
	return [[[self alloc] initWithUnicodeString: string
					     length: length] autorelease];
}













+ stringWithFormat: (OFString*)format, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, format);







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







313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

+ stringWithUnicodeString: (of_unichar_t*)string
		   length: (size_t)length
{
	return [[[self alloc] initWithUnicodeString: string
					     length: length] autorelease];
}

+ stringWithUTF16String: (uint16_t*)string
{
	return [[[self alloc] initWithUTF16String: string] autorelease];
}

+ stringWithUTF16String: (uint16_t*)string
		 length: (size_t)length
{
	return [[[self alloc] initWithUTF16String: string
					   length: length] autorelease];
}

+ stringWithFormat: (OFString*)format, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, format);
579
580
581
582
583
584
585














































































































586
587
588
589
590
591
592
		length = length_;
		string = [self allocMemoryWithSize: (length * 4) + 1];

		for (i = 0; i < length_; i++) {
			size_t characterLen = of_string_unicode_to_utf8(
			    (swap ? of_bswap32(string_[i]) : string_[i]),
			    buffer);















































































































			switch (characterLen) {
			case 1:
				string[j++] = buffer[0];
				break;
			case 2:
				isUTF8 = YES;







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







602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
		length = length_;
		string = [self allocMemoryWithSize: (length * 4) + 1];

		for (i = 0; i < length_; i++) {
			size_t characterLen = of_string_unicode_to_utf8(
			    (swap ? of_bswap32(string_[i]) : string_[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				string[j++] = buffer[0];
				break;
			case 2:
				isUTF8 = YES;
				length++;

				memcpy(string + j, buffer, 2);
				j += 2;

				break;
			case 3:
				isUTF8 = YES;
				length += 2;

				memcpy(string + j, buffer, 3);
				j += 3;

				break;
			case 4:
				isUTF8 = YES;
				length += 3;

				memcpy(string + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		string[j] = '\0';

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

	return self;
}

- initWithUTF16String: (uint16_t*)string_
{
	return [self initWithUTF16String: string_
				  length: of_utf16_string_length(string_)];
}

- initWithUTF16String: (uint16_t*)string_
	       length: (size_t)length_
{
	self = [super init];

	@try {
		char buffer[4];
		size_t i, j = 0;
		BOOL swap = NO;

		if (*string_ == 0xFEFF) {
			string_++;
			length_--;
		}

		if (*string_ == 0xFFFE) {
			swap = YES;
			string_++;
			length_--;
		}

		length = length_;
		string = [self allocMemoryWithSize: (length * 4) + 1];

		for (i = 0; i < length_; i++) {
			of_unichar_t character =
			    (swap ? of_bswap16(string_[i]) : string_[i]);
			size_t characterLen;

			/* Missed the high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			if ((character & 0xFC00) == 0xD800) {
				uint16_t nextCharacter;

				if (length <= i + 1)
					@throw [OFInvalidEncodingException
						newWithClass: isa];

				nextCharacter = (swap
				    ? of_bswap16(string_[i + 1])
				    : string_[i + 1]);
				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
			}

			characterLen = of_string_unicode_to_utf8(
			    character, buffer);

			switch (characterLen) {
			case 1:
				string[j++] = buffer[0];
				break;
			case 2:
				isUTF8 = YES;