ObjFW  Diff

Differences From Artifact [e280f8208a]:

To Artifact [b307aaa0bd]:


239
240
241
242
243
244
245











246
247
248
249
250
251
252
	for (i = 0; i <= index; i++)
		if (OF_UNLIKELY((string[i] & 0xC0) == 0x80))
			if (++index > length)
				return OF_INVALID_INDEX;

	return index;
}












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








>
>
>
>
>
>
>
>
>
>
>







239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	for (i = 0; i <= index; i++)
		if (OF_UNLIKELY((string[i] & 0xC0) == 0x80))
			if (++index > length)
				return OF_INVALID_INDEX;

	return index;
}

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

278
279
280
281
282
283
284





285
286
287
288
289
290
291
				       length: length] autorelease];
}

+ stringWithString: (OFString*)string
{
	return [[[self alloc] initWithString: string] autorelease];
}






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

	va_start(arguments, format);







>
>
>
>
>







289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
				       length: length] autorelease];
}

+ stringWithString: (OFString*)string
{
	return [[[self alloc] initWithString: string] autorelease];
}

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

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

	va_start(arguments, format);
517
518
519
520
521
522
523














































































524
525
526
527
528
529
530
			free(string);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}















































































	return self;
}

- initWithFormat: (OFString*)format, ...
{
	id ret;







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







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
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
			free(string);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUnicodeString: (of_unichar_t*)string_
{
	self = [super init];

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

		if (*string_ == 0xFEFF)
			string_++;

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

		length = of_unicode_string_length(string_);
		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:
				length++;
				string = [self resizeMemory: string
						     toSize: length + 1];

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

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

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

				break;
			case 4:
				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;
}

- initWithFormat: (OFString*)format, ...
{
	id ret;