ObjFW  Diff

Differences From Artifact [47a649e523]:

To Artifact [eb8762e376]:


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
}

size_t
of_unicode_string_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

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

	return (uintptr_t)string_ - (uintptr_t)string;
}

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







|


|







246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
}

size_t
of_unicode_string_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

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

	return (size_t)(string_ - string);
}

@implementation OFString
+ string
{
	return [[[self alloc] init] autorelease];
}
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
596
597
598
599
600
601
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
- initWithUnicodeString: (of_unichar_t*)string_
		 length: (size_t)length_
{
	self = [super init];

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

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

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

		length = length_;
		string = [self allocMemoryWithSize: length + 1];

		while (*string_ != '\0') {
			size_t characterLen;

			if (swap)
				characterLen = of_string_unicode_to_utf8(
				    of_bswap32(*string_), buffer);
			else
				characterLen = of_string_unicode_to_utf8(
				    *string_, buffer);

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

				length++;
				string = [self resizeMemory: string
						     toSize: length + 1];

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

				break;
			case 3:
				isUTF8 = YES;

				length += 2;
				string = [self resizeMemory: string
						     toSize: length + 1];

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

				break;
			case 4:
				isUTF8 = YES;

				length += 3;
				string = [self resizeMemory: string
						     toSize: length + 1];

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

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


			string_++;
		}







		string[i] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







|














|

<
<
|
<
|
|
<
<
|



|



<

<
<

|
|




<

<
<

|
|




<

<
<

|
|






|
>
|
|
>
>
>
>
>
>
|
<







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
596
597
598
599
600

601


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
- initWithUnicodeString: (of_unichar_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_ == 0xFFFE0000) {
			swap = YES;
			string_++;
			length_--;
		}

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