ObjFW  Diff

Differences From Artifact [2057b791a1]:

To Artifact [b9f1bdcce3]:


35
36
37
38
39
40
41
42

43
44

45
46

47
48

49
50

51
52

53
54

55
56

57
58

59
60

61
62

63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78

79
80
81

82
83
84
85
86
87
88
35
36
37
38
39
40
41

42
43

44
45

46
47

48
49

50
51

52
53

54
55

56
57

58
59

60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75

76
77

78
79
80

81
82
83
84
85
86
87
88







-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+













-
+

-
+


-
+







#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "of_asprintf.h"
#import "unicode.h"

extern const of_char16_t of_iso_8859_2_table[];
extern const OFChar16 of_iso_8859_2_table[];
extern const size_t of_iso_8859_2_table_offset;
extern const of_char16_t of_iso_8859_3_table[];
extern const OFChar16 of_iso_8859_3_table[];
extern const size_t of_iso_8859_3_table_offset;
extern const of_char16_t of_iso_8859_15_table[];
extern const OFChar16 of_iso_8859_15_table[];
extern const size_t of_iso_8859_15_table_offset;
extern const of_char16_t of_windows_1251_table[];
extern const OFChar16 of_windows_1251_table[];
extern const size_t of_windows_1251_table_offset;
extern const of_char16_t of_windows_1252_table[];
extern const OFChar16 of_windows_1252_table[];
extern const size_t of_windows_1252_table_offset;
extern const of_char16_t of_codepage_437_table[];
extern const OFChar16 of_codepage_437_table[];
extern const size_t of_codepage_437_table_offset;
extern const of_char16_t of_codepage_850_table[];
extern const OFChar16 of_codepage_850_table[];
extern const size_t of_codepage_850_table_offset;
extern const of_char16_t of_codepage_858_table[];
extern const OFChar16 of_codepage_858_table[];
extern const size_t of_codepage_858_table_offset;
extern const of_char16_t of_mac_roman_table[];
extern const OFChar16 of_mac_roman_table[];
extern const size_t of_mac_roman_table_offset;
extern const of_char16_t of_koi8_r_table[];
extern const OFChar16 of_koi8_r_table[];
extern const size_t of_koi8_r_table_offset;
extern const of_char16_t of_koi8_u_table[];
extern const OFChar16 of_koi8_u_table[];
extern const size_t of_koi8_u_table_offset;

static inline int
memcasecmp(const char *first, const char *second, size_t length)
{
	for (size_t i = 0; i < length; i++) {
		unsigned char f = first[i];
		unsigned char s = second[i];

		f = of_ascii_toupper(f);
		s = of_ascii_toupper(s);

		if (f > s)
			return OF_ORDERED_DESCENDING;
			return OFOrderedDescending;
		if (f < s)
			return OF_ORDERED_ASCENDING;
			return OFOrderedAscending;
	}

	return OF_ORDERED_SAME;
	return OFOrderedSame;
}

int
of_string_utf8_check(const char *UTF8String, size_t UTF8Length, size_t *length)
{
	size_t tmpLength = UTF8Length;
	int isUTF8 = 0;
223
224
225
226
227
228
229
230

231
232
233
234
235
236

237
238
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
264
265
266
267
268
269
270
271
272
273
274
275
276

277
278
279
280
281
282
283
223
224
225
226
227
228
229

230
231
232
233
234
235

236
237
238

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
264
265
266
267
268
269
270
271
272
273
274
275

276
277
278
279
280
281
282
283







-
+





-
+


-
+












-
-
+
+



-
+


















-
+







		@throw e;
	}

	return self;
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (of_string_encoding_t)encoding
		       encoding: (OFStringEncoding)encoding
			 length: (size_t)cStringLength
{
	self = [super init];

	@try {
		const of_char16_t *table;
		const OFChar16 *table;
		size_t tableOffset, j;

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		if (encoding == OFStringEncodingUTF8 &&
		    cStringLength >= 3 &&
		    memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
			cString += 3;
			cStringLength -= 3;
		}

		_s = &_storage;

		_s->cString = of_alloc(cStringLength + 1, 1);
		_s->cStringLength = cStringLength;
		_s->freeWhenDone = true;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
		if (encoding == OFStringEncodingUTF8 ||
		    encoding == OFStringEncodingASCII) {
			switch (of_string_utf8_check(cString, cStringLength,
			    &_s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
				if (encoding == OFStringEncodingASCII)
					@throw [OFInvalidEncodingException
					    exception];

				_s->isUTF8 = true;
				break;
			case -1:
				@throw [OFInvalidEncodingException exception];
			}

			memcpy(_s->cString, cString, cStringLength);
			_s->cString[cStringLength] = 0;

			return self;
		}

		/* All other encodings we support are single byte encodings */
		_s->length = cStringLength;

		if (encoding == OF_STRING_ENCODING_ISO_8859_1) {
		if (encoding == OFStringEncodingISO8859_1) {
			j = 0;
			for (size_t i = 0; i < cStringLength; i++) {
				char buffer[4];
				size_t bytes;

				if (!(cString[i] & 0x80)) {
					_s->cString[j++] = cString[i];
308
309
310
311
312
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
339

340
341
342

343
344
345

346
347
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
308
309
310
311
312
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

339
340
341

342
343
344

345
346
347
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362







-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+









-
+







		switch (encoding) {
#define CASE(encoding, var)				\
		case encoding:				\
			table = var;			\
			tableOffset = var##_offset;	\
			break;
#ifdef HAVE_ISO_8859_2
		CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table)
		CASE(OFStringEncodingISO8859_2, of_iso_8859_2_table)
#endif
#ifdef HAVE_ISO_8859_3
		CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table)
		CASE(OFStringEncodingISO8859_3, of_iso_8859_3_table)
#endif
#ifdef HAVE_ISO_8859_15
		CASE(OF_STRING_ENCODING_ISO_8859_15, of_iso_8859_15_table)
		CASE(OFStringEncodingISO8859_15, of_iso_8859_15_table)
#endif
#ifdef HAVE_WINDOWS_1251
		CASE(OF_STRING_ENCODING_WINDOWS_1251, of_windows_1251_table)
		CASE(OFStringEncodingWindows1251, of_windows_1251_table)
#endif
#ifdef HAVE_WINDOWS_1252
		CASE(OF_STRING_ENCODING_WINDOWS_1252, of_windows_1252_table)
		CASE(OFStringEncodingWindows1252, of_windows_1252_table)
#endif
#ifdef HAVE_CODEPAGE_437
		CASE(OF_STRING_ENCODING_CODEPAGE_437, of_codepage_437_table)
		CASE(OFStringEncodingCodepage437, of_codepage_437_table)
#endif
#ifdef HAVE_CODEPAGE_850
		CASE(OF_STRING_ENCODING_CODEPAGE_850, of_codepage_850_table)
		CASE(OFStringEncodingCodepage850, of_codepage_850_table)
#endif
#ifdef HAVE_CODEPAGE_858
		CASE(OF_STRING_ENCODING_CODEPAGE_858, of_codepage_858_table)
		CASE(OFStringEncodingCodepage858, of_codepage_858_table)
#endif
#ifdef HAVE_MAC_ROMAN
		CASE(OF_STRING_ENCODING_MAC_ROMAN, of_mac_roman_table)
		CASE(OFStringEncodingMacRoman, of_mac_roman_table)
#endif
#ifdef HAVE_KOI8_R
		CASE(OF_STRING_ENCODING_KOI8_R, of_koi8_r_table)
		CASE(OFStringEncodingKOI8R, of_koi8_r_table)
#endif
#ifdef HAVE_KOI8_U
		CASE(OF_STRING_ENCODING_KOI8_U, of_koi8_u_table)
		CASE(OFStringEncodingKOI8U, of_koi8_u_table)
#endif
#undef CASE
		default:
			@throw [OFInvalidEncodingException exception];
		}

		j = 0;
		for (size_t i = 0; i < cStringLength; i++) {
			unsigned char character = (unsigned char)cString[i];
			of_unichar_t unichar;
			OFUnichar unichar;
			char buffer[4];
			size_t byteLength;

			if (character < tableOffset) {
				_s->cString[j++] = cString[i];
				continue;
			}
457
458
459
460
461
462
463
464

465
466
467
468
469
470
471
457
458
459
460
461
462
463

464
465
466
467
468
469
470
471







-
+







		[self release];
		@throw e;
	}

	return self;
}

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

	@try {
		size_t j;

501
502
503
504
505
506
507
508

509
510

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

526
527
528
529
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544
545

546
547
548
549
550
551
552
501
502
503
504
505
506
507

508
509

510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

525
526
527
528
529
530
531
532
533
534
535

536
537
538
539
540
541
542
543
544

545
546
547
548
549
550
551
552







-
+

-
+














-
+










-
+








-
+







		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithUTF16String: (const of_char16_t *)string
- (instancetype)initWithUTF16String: (const OFChar16 *)string
			     length: (size_t)length
			  byteOrder: (of_byte_order_t)byteOrder
			  byteOrder: (OFByteOrder)byteOrder
{
	self = [super init];

	@try {
		size_t j;
		bool swap = false;

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

		_s = &_storage;

		_s->cString = of_alloc((length * 4) + 1, 1);
		_s->length = length;
		_s->freeWhenDone = true;

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

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException exception];

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

				if (length <= i + 1)
					@throw [OFInvalidEncodingException
					    exception];

				nextCharacter = (swap
				    ? OF_BSWAP16(string[i + 1])
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
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







-
+

-
+














-
+







		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithUTF32String: (const of_char32_t *)characters
- (instancetype)initWithUTF32String: (const OFChar32 *)characters
			     length: (size_t)length
			  byteOrder: (of_byte_order_t)byteOrder
			  byteOrder: (OFByteOrder)byteOrder
{
	self = [super init];

	@try {
		size_t j;
		bool swap = false;

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

		_s = &_storage;

		_s->cString = of_alloc((length * 4) + 1, 1);
		_s->length = length;
		_s->freeWhenDone = true;
707
708
709
710
711
712
713
714

715
716
717

718
719
720
721

722
723
724
725
726
727
728
729
730
731
732
733
734
735

736
737
738

739
740
741
742

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

760
761
762
763


764
765
766
767
768
769
770
707
708
709
710
711
712
713

714
715
716

717
718
719
720

721
722
723
724
725
726
727
728
729
730
731
732
733
734

735
736
737

738
739
740
741

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

759
760
761


762
763
764
765
766
767
768
769
770







-
+


-
+



-
+













-
+


-
+



-
+
















-
+


-
-
+
+







		free(_s->cString);

	[super dealloc];
}

- (size_t)getCString: (char *)cString
	   maxLength: (size_t)maxLength
	    encoding: (of_string_encoding_t)encoding
	    encoding: (OFStringEncoding)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_ASCII:
	case OFStringEncodingASCII:
		if (_s->isUTF8)
			@throw [OFInvalidEncodingException exception];
		/* intentional fall-through */
	case OF_STRING_ENCODING_UTF_8:
	case OFStringEncodingUTF8:
		if (_s->cStringLength + 1 > maxLength)
			@throw [OFOutOfRangeException exception];

		memcpy(cString, _s->cString, _s->cStringLength + 1);

		return _s->cStringLength;
	default:
		return [super getCString: cString
			       maxLength: maxLength
				encoding: encoding];
	}
}

- (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
- (const char *)cStringWithEncoding: (OFStringEncoding)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_ASCII:
	case OFStringEncodingASCII:
		if (_s->isUTF8)
			@throw [OFInvalidEncodingException exception];
		/* intentional fall-through */
	case OF_STRING_ENCODING_UTF_8:
	case OFStringEncodingUTF8:
		return _s->cString;
	default:
		return [super cStringWithEncoding: encoding];
	}
}

- (const char *)UTF8String
{
	return _s->cString;
}

- (size_t)length
{
	return _s->length;
}

- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
- (size_t)cStringLengthWithEncoding: (OFStringEncoding)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
	case OF_STRING_ENCODING_ASCII:
	case OFStringEncodingUTF8:
	case OFStringEncodingASCII:
		return _s->cStringLength;
	default:
		return [super cStringLengthWithEncoding: encoding];
	}
}

- (size_t)UTF8StringLength
795
796
797
798
799
800
801
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
842

843
844
845
846
847
848
849
850
851
852
853
854
855
856

857
858
859


860
861
862
863

864
865

866
867
868
869
870
871
872

873
874
875
876
877
878
879
880
881
882
883
884

885
886
887
888
889
890
891
892

893
894
895
896
897
898
899
900

901
902

903
904
905
906
907
908
909

910
911

912
913
914

915
916
917
918
919
920
921
922
923
924
925
926
927

928
929
930
931
932
933
934
795
796
797
798
799
800
801

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

842
843
844
845
846
847
848
849
850
851
852
853
854
855

856
857


858
859
860
861
862

863
864

865
866
867
868
869
870
871

872
873
874
875
876
877
878
879
880
881
882
883

884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
899

900
901

902
903
904
905
906
907
908

909
910

911
912
913

914
915
916
917
918
919
920
921
922
923
924
925
926

927
928
929
930
931
932
933
934







-
+





-
+











-
+

-
-
+
+



-
+

-
+


-
+









-
+













-
+

-
-
+
+



-
+

-
+






-
+











-
+







-
+







-
+

-
+






-
+

-
+


-
+












-
+








	if (strcmp(_s->cString, string.UTF8String) != 0)
		return false;

	return true;
}

- (of_comparison_result_t)compare: (OFString *)string
- (OFComparisonResult)compare: (OFString *)string
{
	size_t otherCStringLength, minimumCStringLength;
	int compare;

	if (string == self)
		return OF_ORDERED_SAME;
		return OFOrderedSame;

	if (![string isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException exception];

	otherCStringLength = string.UTF8StringLength;
	minimumCStringLength = (_s->cStringLength > otherCStringLength
	    ? otherCStringLength : _s->cStringLength);

	if ((compare = memcmp(_s->cString, string.UTF8String,
	    minimumCStringLength)) == 0) {
		if (_s->cStringLength > otherCStringLength)
			return OF_ORDERED_DESCENDING;
			return OFOrderedDescending;
		if (_s->cStringLength < otherCStringLength)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
			return OFOrderedAscending;
		return OFOrderedSame;
	}

	if (compare > 0)
		return OF_ORDERED_DESCENDING;
		return OFOrderedDescending;
	else
		return OF_ORDERED_ASCENDING;
		return OFOrderedAscending;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString *)string
- (OFComparisonResult)caseInsensitiveCompare: (OFString *)string
{
	const char *otherCString;
	size_t otherCStringLength, minimumCStringLength;
#ifdef OF_HAVE_UNICODE_TABLES
	size_t i, j;
#endif
	int compare;

	if (string == self)
		return OF_ORDERED_SAME;
		return OFOrderedSame;

	otherCString = string.UTF8String;
	otherCStringLength = string.UTF8StringLength;

#ifdef OF_HAVE_UNICODE_TABLES
	if (!_s->isUTF8) {
#endif
		minimumCStringLength = (_s->cStringLength > otherCStringLength
		    ? otherCStringLength : _s->cStringLength);

		if ((compare = memcasecmp(_s->cString, otherCString,
		    minimumCStringLength)) == 0) {
			if (_s->cStringLength > otherCStringLength)
				return OF_ORDERED_DESCENDING;
				return OFOrderedDescending;
			if (_s->cStringLength < otherCStringLength)
				return OF_ORDERED_ASCENDING;
			return OF_ORDERED_SAME;
				return OFOrderedAscending;
			return OFOrderedSame;
		}

		if (compare > 0)
			return OF_ORDERED_DESCENDING;
			return OFOrderedDescending;
		else
			return OF_ORDERED_ASCENDING;
			return OFOrderedAscending;
#ifdef OF_HAVE_UNICODE_TABLES
	}

	i = j = 0;

	while (i < _s->cStringLength && j < otherCStringLength) {
		of_unichar_t c1, c2;
		OFUnichar c1, c2;
		ssize_t l1, l2;

		l1 = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c1);
		l2 = of_string_utf8_decode(otherCString + j,
		    otherCStringLength - j, &c2);

		if (l1 <= 0 || l2 <= 0 || c1 > 0x10FFFF || c2 > 0x10FFFF)
			@throw [OFInvalidEncodingException exception];

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			OFUnichar tc =
			    of_unicode_casefolding_table[c1 >> 8][c1 & 0xFF];

			if (tc)
				c1 = tc;
		}

		if (c2 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			OFUnichar tc =
			    of_unicode_casefolding_table[c2 >> 8][c2 & 0xFF];

			if (tc)
				c2 = tc;
		}

		if (c1 > c2)
			return OF_ORDERED_DESCENDING;
			return OFOrderedDescending;
		if (c1 < c2)
			return OF_ORDERED_ASCENDING;
			return OFOrderedAscending;

		i += l1;
		j += l2;
	}

	if (_s->cStringLength - i > otherCStringLength - j)
		return OF_ORDERED_DESCENDING;
		return OFOrderedDescending;
	else if (_s->cStringLength - i < otherCStringLength - j)
		return OF_ORDERED_ASCENDING;
		return OFOrderedAscending;
#endif

	return OF_ORDERED_SAME;
	return OFOrderedSame;
}

- (unsigned long)hash
{
	uint32_t hash;

	if (_s->hashed)
		return _s->hash;

	OF_HASH_INIT(hash);

	for (size_t i = 0; i < _s->cStringLength; i++) {
		of_unichar_t c;
		OFUnichar c;
		ssize_t length;

		if ((length = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c)) <= 0)
			@throw [OFInvalidEncodingException exception];

		OF_HASH_ADD(hash, (c & 0xFF0000) >> 16);
942
943
944
945
946
947
948
949

950
951

952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968

969
970
971
972

973
974
975
976
977
978
979

980
981
982
983
984
985
986



987
988
989
990
991
992
993
942
943
944
945
946
947
948

949
950

951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967

968
969
970
971

972
973
974
975
976
977
978

979
980
981
982
983



984
985
986
987
988
989
990
991
992
993







-
+

-
+
















-
+



-
+






-
+




-
-
-
+
+
+








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

	return hash;
}

- (of_unichar_t)characterAtIndex: (size_t)idx
- (OFUnichar)characterAtIndex: (size_t)idx
{
	of_unichar_t character;
	OFUnichar character;

	if (idx >= _s->length)
		@throw [OFOutOfRangeException exception];

	if (!_s->isUTF8)
		return _s->cString[idx];

	idx = of_string_utf8_get_position(_s->cString, idx, _s->cStringLength);

	if (of_string_utf8_decode(_s->cString + idx,
	    _s->cStringLength - idx, &character) <= 0)
		@throw [OFInvalidEncodingException exception];

	return character;
}

- (void)getCharacters: (of_unichar_t *)buffer inRange: (of_range_t)range
- (void)getCharacters: (OFUnichar *)buffer inRange: (OFRange)range
{
	/* TODO: Could be slightly optimized */
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = self.characters;
	const OFUnichar *characters = self.characters;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _s->length)
		@throw [OFOutOfRangeException exception];

	memcpy(buffer, characters + range.location,
	    range.length * sizeof(of_unichar_t));
	    range.length * sizeof(OFUnichar));

	objc_autoreleasePoolPop(pool);
}

- (of_range_t)rangeOfString: (OFString *)string
		    options: (int)options
		      range: (of_range_t)range
- (OFRange)rangeOfString: (OFString *)string
		 options: (int)options
		   range: (OFRange)range
{
	const char *cString = string.UTF8String;
	size_t cStringLength = string.UTF8StringLength;
	size_t rangeLocation, rangeLength;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _s->length)
1001
1002
1003
1004
1005
1006
1007
1008

1009
1010
1011

1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041

1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

1063
1064
1065
1066
1067
1068
1069
1001
1002
1003
1004
1005
1006
1007

1008
1009
1010

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061

1062
1063
1064
1065
1066
1067
1068
1069







-
+


-
+














-
+














-
+




















-
+







		    _s->cStringLength - rangeLocation);
	} else {
		rangeLocation = range.location;
		rangeLength = range.length;
	}

	if (cStringLength == 0)
		return of_range(0, 0);
		return OFMakeRange(0, 0);

	if (cStringLength > rangeLength)
		return of_range(OF_NOT_FOUND, 0);
		return OFMakeRange(OFNotFound, 0);

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (size_t i = rangeLength - cStringLength;; i--) {
			if (memcmp(_s->cString + rangeLocation + i, cString,
			    cStringLength) == 0) {
				range.location += of_string_utf8_get_index(
				    _s->cString + rangeLocation, i);
				range.length = string.length;

				return range;
			}

			/* Did not match and we're at the last char */
			if (i == 0)
				return of_range(OF_NOT_FOUND, 0);
				return OFMakeRange(OFNotFound, 0);
		}
	} else {
		for (size_t i = 0; i <= rangeLength - cStringLength; i++) {
			if (memcmp(_s->cString + rangeLocation + i, cString,
			    cStringLength) == 0) {
				range.location += of_string_utf8_get_index(
				    _s->cString + rangeLocation, i);
				range.length = string.length;

				return range;
			}
		}
	}

	return of_range(OF_NOT_FOUND, 0);
	return OFMakeRange(OFNotFound, 0);
}

- (bool)containsString: (OFString *)string
{
	const char *cString = string.UTF8String;
	size_t cStringLength = string.UTF8StringLength;

	if (cStringLength == 0)
		return true;

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

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

	return false;
}

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

	if (range.length > SIZE_MAX - range.location || end > _s->length)
		@throw [OFOutOfRangeException exception];

1148
1149
1150
1151
1152
1153
1154
1155

1156
1157

1158
1159
1160
1161

1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178

1179
1180
1181
1182

1183
1184

1185
1186
1187
1188

1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210

1211
1212
1213
1214
1215
1216
1217
1148
1149
1150
1151
1152
1153
1154

1155
1156

1157
1158
1159
1160

1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177

1178
1179
1180
1181

1182
1183

1184
1185
1186
1187

1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198

1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209

1210
1211
1212
1213
1214
1215
1216
1217







-
+

-
+



-
+
















-
+



-
+

-
+



-
+










-
+










-
+







	[array makeImmutable];

	objc_autoreleasePoolPop(pool);

	return array;
}

- (const of_unichar_t *)characters
- (const OFUnichar *)characters
{
	of_unichar_t *buffer = of_alloc(_s->length, sizeof(of_unichar_t));
	OFUnichar *buffer = of_alloc(_s->length, sizeof(OFUnichar));
	size_t i = 0, j = 0;

	while (i < _s->cStringLength) {
		of_unichar_t c;
		OFUnichar c;
		ssize_t cLen;

		cLen = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c);

		if (cLen <= 0 || c > 0x10FFFF) {
			free(buffer);
			@throw [OFInvalidEncodingException exception];
		}

		buffer[j++] = c;
		i += cLen;
	}

	return [[OFData dataWithItemsNoCopy: buffer
				      count: _s->length
				   itemSize: sizeof(of_unichar_t)
				   itemSize: sizeof(OFUnichar)
			       freeWhenDone: true] items];
}

- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
- (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder
{
	of_char32_t *buffer = of_alloc(_s->length + 1, sizeof(of_char32_t));
	OFChar32 *buffer = of_alloc(_s->length + 1, sizeof(OFChar32));
	size_t i = 0, j = 0;

	while (i < _s->cStringLength) {
		of_char32_t c;
		OFChar32 c;
		ssize_t cLen;

		cLen = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c);

		if (cLen <= 0 || c > 0x10FFFF) {
			free(buffer);
			@throw [OFInvalidEncodingException exception];
		}

		if (byteOrder != OF_BYTE_ORDER_NATIVE)
		if (byteOrder != OFByteOrderNative)
			buffer[j++] = OF_BSWAP32(c);
		else
			buffer[j++] = c;

		i += cLen;
	}
	buffer[j] = 0;

	return [[OFData dataWithItemsNoCopy: buffer
				      count: _s->length + 1
				   itemSize: sizeof(of_char32_t)
				   itemSize: sizeof(OFChar32)
			       freeWhenDone: true] items];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool;