ObjFW  Diff

Differences From Artifact [503640087e]:

To Artifact [3ce37a1895]:


184
185
186
187
188
189
190
191

192
193
194
195
196
197
198
184
185
186
187
188
189
190

191
192
193
194
195
196
197
198







-
+








		_s->cString = storage;
		_s->cStringLength = UTF8StringLength;

		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &_s->length)) {
		case 1:
			_s->isUTF8 = YES;
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}

		memcpy(_s->cString, UTF8String, UTF8StringLength);
231
232
233
234
235
236
237
238

239
240
241
242
243
244
245
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245







-
+







			switch (of_string_utf8_check(cString, cStringLength,
			    &_s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				_s->isUTF8 = YES;
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}

			memcpy(_s->cString, cString, cStringLength);
257
258
259
260
261
262
263
264

265
266
267
268
269
270
271
257
258
259
260
261
262
263

264
265
266
267
268
269
270
271







-
+







				size_t bytes;

				if (!(cString[i] & 0x80)) {
					_s->cString[j++] = cString[i];
					continue;
				}

				_s->isUTF8 = YES;
				_s->isUTF8 = true;
				bytes = of_string_utf8_encode(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

307
308
309
310
311
312
313
314

315
316
317
318
319
320
321
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321







-
+








			character = table[(uint8_t)cString[i]];

			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			_s->isUTF8 = YES;
			_s->isUTF8 = true;
			characterBytes = of_string_utf8_encode(character,
			    buffer);

			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

334
335
336
337
338
339
340
341

342
343
344
345
346
347
348
334
335
336
337
338
339
340

341
342
343
344
345
346
347
348







-
+







		@throw e;
	}

	return self;
}

- initWithUTF8StringNoCopy: (char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone
	      freeWhenDone: (bool)freeWhenDone
{
	self = [super init];

	@try {
		size_t UTF8StringLength = strlen(UTF8String);

		if (UTF8StringLength >= 3 &&
358
359
360
361
362
363
364
365

366
367
368
369
370
371
372
358
359
360
361
362
363
364

365
366
367
368
369
370
371
372







-
+








		if (freeWhenDone)
			_s->freeWhenDone = UTF8String;

		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &_s->length)) {
		case 1:
			_s->isUTF8 = YES;
			_s->isUTF8 = true;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
	} @catch (id e) {
		[self release];
385
386
387
388
389
390
391
392

393
394
395
396
397
398
399
385
386
387
388
389
390
391

392
393
394
395
396
397
398
399







-
+








		_s->cStringLength = [string UTF8StringLength];

		if ([string isKindOfClass: [OFString_UTF8 class]] ||
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			_s->isUTF8 = ((OFString_UTF8*)string)->_s->isUTF8;
		else
			_s->isUTF8 = YES;
			_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];
424
425
426
427
428
429
430
431

432
433
434
435
436
437
438
424
425
426
427
428
429
430

431
432
433
434
435
436
437
438







-
+







			switch (len) {
			case 1:
				_s->cString[j++] = buffer[0];
				break;
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = YES;
				_s->isUTF8 = true;

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

				break;
			default:
				@throw [OFInvalidEncodingException
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
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







-
+





-
+



-
+







	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder
{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;
		bool swap = false;

		if (length > 0 && *string == 0xFEFF) {
			string++;
			length--;
		} else if (length > 0 && *string == 0xFFFE) {
			swap = YES;
			swap = true;
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;
			swap = true;

		_s = &_storage;

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

		for (i = 0; i < length; i++) {
520
521
522
523
524
525
526
527

528
529
530
531
532
533
534
520
521
522
523
524
525
526

527
528
529
530
531
532
533
534







-
+







			switch (len) {
			case 1:
				_s->cString[j++] = buffer[0];
				break;
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = YES;
				_s->isUTF8 = true;

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

				break;
			default:
				@throw [OFInvalidEncodingException
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
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







-
+





-
+



-
+



















-
+







	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder
{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;
		bool swap = false;

		if (length > 0 && *characters == 0xFEFF) {
			characters++;
			length--;
		} else if (length > 0 && *characters == 0xFFFE0000) {
			swap = YES;
			swap = true;
			characters++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;
			swap = true;

		_s = &_storage;

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

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

			switch (len) {
			case 1:
				_s->cString[j++] = buffer[0];
				break;
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = YES;
				_s->isUTF8 = true;

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

				break;
			default:
				@throw [OFInvalidEncodingException
643
644
645
646
647
648
649
650

651
652
653
654
655
656
657
643
644
645
646
647
648
649

650
651
652
653
654
655
656
657







-
+








		_s->cStringLength = cStringLength;

		@try {
			switch (of_string_utf8_check(tmp, cStringLength,
			    &_s->length)) {
			case 1:
				_s->isUTF8 = YES;
				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}

			_s->cString = [self
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
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







-
+















-
+








		if ([firstComponent isKindOfClass: [OFString_UTF8 class]] ||
		    [firstComponent isKindOfClass:
		    [OFMutableString_UTF8 class]])
			_s->isUTF8 =
			    ((OFString_UTF8*)firstComponent)->_s->isUTF8;
		else
			_s->isUTF8 = YES;
			_s->isUTF8 = true;

		_s->length = [firstComponent length];

		/* Calculate length and see if we need UTF-8 */
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			_s->cStringLength += 1 + [component UTF8StringLength];
			_s->length += 1 + [component length];

			if ([component isKindOfClass: [OFString_UTF8 class]] ||
			    [component isKindOfClass:
			    [OFMutableString_UTF8 class]])
				_s->isUTF8 =
				    ((OFString_UTF8*)component)->_s->isUTF8;
			else
				_s->isUTF8 = YES;
				_s->isUTF8 = true;
		}

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

		cStringLength = [firstComponent UTF8StringLength];
		memcpy(_s->cString, [firstComponent UTF8String], cStringLength);
		i = cStringLength;
802
803
804
805
806
807
808
809

810
811
812
813
814

815
816
817

818
819
820
821
822
823

824
825
826
827
828
829

830
831
832

833
834

835
836
837
838
839
840
841
802
803
804
805
806
807
808

809
810
811
812
813

814
815
816

817
818
819
820
821
822

823
824
825
826
827
828

829
830
831

832
833

834
835
836
837
838
839
840
841







-
+




-
+


-
+





-
+





-
+


-
+

-
+







}

- (size_t)UTF8StringLength
{
	return _s->cStringLength;
}

- (BOOL)isEqual: (id)object
- (bool)isEqual: (id)object
{
	OFString_UTF8 *otherString;

	if (object == self)
		return YES;
		return true;

	if (![object isKindOfClass: [OFString class]])
		return NO;
		return false;

	otherString = object;

	if ([otherString UTF8StringLength] != _s->cStringLength ||
	    [otherString length] != _s->length)
		return NO;
		return false;

	if (([otherString isKindOfClass: [OFString_UTF8 class]] ||
	    [otherString isKindOfClass: [OFMutableString_UTF8 class]]) &&
	    _s->hashed && otherString->_s->hashed &&
	    _s->hash != otherString->_s->hash)
		return NO;
		return false;

	if (strcmp(_s->cString, [otherString UTF8String]))
		return NO;
		return false;

	return YES;
	return true;
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
{
	OFString *otherString;
	size_t otherCStringLength, minimumCStringLength;
	int compare;
977
978
979
980
981
982
983
984

985
986
987
988
989
990
991
977
978
979
980
981
982
983

984
985
986
987
988
989
990
991







-
+








		i += length - 1;
	}

	OF_HASH_FINALIZE(hash);

	_s->hash = hash;
	_s->hashed = YES;
	_s->hashed = true;

	return hash;
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t character;
1080
1081
1082
1083
1084
1085
1086
1087

1088
1089
1090
1091
1092
1093

1094
1095
1096

1097
1098
1099
1100

1101
1102

1103
1104
1105
1106
1107
1108
1109
1080
1081
1082
1083
1084
1085
1086

1087
1088
1089
1090
1091
1092

1093
1094
1095

1096
1097
1098
1099

1100
1101

1102
1103
1104
1105
1106
1107
1108
1109







-
+





-
+


-
+



-
+

-
+







			}
		}
	}

	return of_range(OF_NOT_FOUND, 0);
}

- (BOOL)containsString: (OFString*)string
- (bool)containsString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

	if (cStringLength == 0)
		return YES;
		return true;

	if (cStringLength > _s->cStringLength)
		return NO;
		return false;

	for (i = 0; i <= _s->cStringLength - cStringLength; i++)
		if (!memcmp(_s->cString + i, cString, cStringLength))
			return YES;
			return true;

	return NO;
	return false;
}

- (OFString*)substringWithRange: (of_range_t)range
{
	size_t start = range.location;
	size_t end = range.location + range.length;

1117
1118
1119
1120
1121
1122
1123
1124

1125
1126
1127
1128
1129

1130
1131
1132
1133
1134

1135
1136
1137
1138
1139

1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152

1153
1154
1155
1156
1157
1158
1159
1117
1118
1119
1120
1121
1122
1123

1124
1125
1126
1127
1128

1129
1130
1131
1132
1133

1134
1135
1136
1137
1138

1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

1152
1153
1154
1155
1156
1157
1158
1159







-
+




-
+




-
+




-
+












-
+







		    _s->cStringLength);
	}

	return [OFString stringWithUTF8String: _s->cString + start
				       length: end - start];
}

- (BOOL)hasPrefix: (OFString*)prefix
- (bool)hasPrefix: (OFString*)prefix
{
	size_t cStringLength = [prefix UTF8StringLength];

	if (cStringLength > _s->cStringLength)
		return NO;
		return false;

	return !memcmp(_s->cString, [prefix UTF8String], cStringLength);
}

- (BOOL)hasSuffix: (OFString*)suffix
- (bool)hasSuffix: (OFString*)suffix
{
	size_t cStringLength = [suffix UTF8StringLength];

	if (cStringLength > _s->cStringLength)
		return NO;
		return false;

	return !memcmp(_s->cString + (_s->cStringLength - cStringLength),
	    [suffix UTF8String], cStringLength);
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
				options: (int)options
{
	void *pool;
	OFMutableArray *array;
	const char *cString = [delimiter UTF8String];
	size_t cStringLength = [delimiter UTF8StringLength];
	BOOL skipEmpty = (options & OF_STRING_SKIP_EMPTY);
	bool skipEmpty = (options & OF_STRING_SKIP_EMPTY);
	size_t i, last;
	OFString *component;

	array = [OFMutableArray array];
	pool = objc_autoreleasePoolPush();

	if (cStringLength > _s->cStringLength) {
1372
1373
1374
1375
1376
1377
1378
1379

1380
1381
1382
1383

1384
1385
1386
1387
1388
1389
1390
1372
1373
1374
1375
1376
1377
1378

1379
1380
1381
1382

1383
1384
1385
1386
1387
1388
1389
1390







-
+



-
+








#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool;
	const char *cString = _s->cString;
	const char *last = cString;
	BOOL stop = NO, lastCarriageReturn = NO;
	bool stop = false, lastCarriageReturn = false;

	while (!stop && *cString != 0) {
		if (lastCarriageReturn && *cString == '\n') {
			lastCarriageReturn = NO;
			lastCarriageReturn = false;

			cString++;
			last++;

			continue;
		}