ObjFW  Diff

Differences From Artifact [0615761918]:

To Artifact [503640087e]:


151
152
153
154
155
156
157
158

159
160
161


162
163
164
165
166
167
168
151
152
153
154
155
156
157

158
159


160
161
162
163
164
165
166
167
168







-
+

-
-
+
+








@implementation OFString_UTF8
- init
{
	self = [super init];

	@try {
		s = &s_store;
		_s = &_storage;

		s->cString = [self allocMemoryWithSize: 1];
		s->cString[0] = '\0';
		_s->cString = [self allocMemoryWithSize: 1];
		_s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
176
177
178
179
180
181
182
183

184
185
186


187
188
189

190
191

192
193
194
195
196
197
198
199


200
201
202
203
204
205
206
176
177
178
179
180
181
182

183
184


185
186
187
188

189
190

191
192
193
194
195
196
197


198
199
200
201
202
203
204
205
206







-
+

-
-
+
+


-
+

-
+






-
-
+
+







	@try {
		if (UTF8StringLength >= 3 &&
		    !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
			UTF8String += 3;
			UTF8StringLength -= 3;
		}

		s = &s_store;
		_s = &_storage;

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

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

		memcpy(s->cString, UTF8String, UTF8StringLength);
		s->cString[UTF8StringLength] = 0;
		memcpy(_s->cString, UTF8String, UTF8StringLength);
		_s->cString[UTF8StringLength] = 0;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
217
218
219
220
221
222
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
284
285
286
287
288
217
218
219
220
221
222
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
284
285
286
287
288







-
+

-
-
+
+




-
+





-
+






-
-
+
+





-
+







-
+



-
+







-
-
-
-
+
+
+
+

-
+



-
+








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

		s = &s_store;
		_s = &_storage;

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

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

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

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

			return self;
		}

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

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

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

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

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

				s->cStringLength += bytes - 1;
				s->cString = [self
				    resizeMemory: s->cString
					    size: s->cStringLength + 1];
				_s->cStringLength += bytes - 1;
				_s->cString = [self
				    resizeMemory: _s->cString
					    size: _s->cStringLength + 1];

				memcpy(s->cString + j, buffer, bytes);
				memcpy(_s->cString + j, buffer, bytes);
				j += bytes;
			}

			s->cString[s->cStringLength] = 0;
			_s->cString[_s->cStringLength] = 0;

			return self;
		}

		switch (encoding) {
		case OF_STRING_ENCODING_ISO_8859_15:
			table = of_iso_8859_15;
297
298
299
300
301
302
303
304

305
306
307
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
297
298
299
300
301
302
303

304
305
306
307
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







-
+









-
+







-
-
-
+
+
+
+

-
+



-
+








		for (i = j = 0; i < cStringLength; i++) {
			char buffer[4];
			of_unichar_t character;
			size_t characterBytes;

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

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

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

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

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

			s->cStringLength += characterBytes - 1;
			s->cString = [self resizeMemory: s->cString
						   size: s->cStringLength + 1];
			_s->cStringLength += characterBytes - 1;
			_s->cString = [self
			    resizeMemory: _s->cString
				    size: _s->cStringLength + 1];

			memcpy(s->cString + j, buffer, characterBytes);
			memcpy(_s->cString + j, buffer, characterBytes);
			j += characterBytes;
		}

		s->cString[s->cStringLength] = 0;
		_s->cString[_s->cStringLength] = 0;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
346
347
348
349
350
351
352
353

354
355
356


357
358
359

360
361
362

363
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

384
385

386
387
388
389

390
391

392
393

394
395
396


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

414
415
416


417
418
419
420
421
422
423
424
425

426
427
428
429
430

431
432

433
434
435
436
437
438
439
440
441
442
443


444
445
446
447


448
449
450
451
452
453
454
347
348
349
350
351
352
353

354
355


356
357
358
359

360
361
362

363
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

384
385

386
387
388
389

390
391

392
393

394
395


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

414
415


416
417
418
419
420
421
422
423
424
425

426
427
428
429
430

431
432

433
434
435
436
437
438
439
440
441
442


443
444
445
446


447
448
449
450
451
452
453
454
455







-
+

-
-
+
+


-
+


-
+

-
+


















-
+

-
+



-
+

-
+

-
+

-
-
+
+
















-
+

-
-
+
+








-
+




-
+

-
+









-
-
+
+


-
-
+
+








		if (UTF8StringLength >= 3 &&
		    !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
			UTF8String += 3;
			UTF8StringLength -= 3;
		}

		s = &s_store;
		_s = &_storage;

		s->cString = (char*)UTF8String;
		s->cStringLength = UTF8StringLength;
		_s->cString = (char*)UTF8String;
		_s->cStringLength = UTF8StringLength;

		if (freeWhenDone)
			s->freeWhenDone = UTF8String;
			_s->freeWhenDone = UTF8String;

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

	return self;
}

- initWithString: (OFString*)string
{
	self = [super init];

	@try {
		s = &s_store;
		_s = &_storage;

		s->cStringLength = [string UTF8StringLength];
		_s->cStringLength = [string UTF8StringLength];

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

		s->length = [string length];
		_s->length = [string length];

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
		memcpy(s->cString, [string UTF8String], s->cStringLength + 1);
		_s->cString = [self allocMemoryWithSize: _s->cStringLength + 1];
		memcpy(_s->cString, [string UTF8String], _s->cStringLength + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	@try {
		size_t i, j = 0;

		s = &s_store;
		_s = &_storage;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;
		_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(characters[i],
			    buffer);

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

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

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		s->cString[j] = '\0';
		s->cStringLength = j;
		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: j + 1];
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
472
473
474
475
476
477
478
479

480
481
482


483
484
485
486
487
488
489
473
474
475
476
477
478
479

480
481


482
483
484
485
486
487
488
489
490







-
+

-
-
+
+







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

		s = &s_store;
		_s = &_storage;

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

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

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







-
+






-
+




-
+

-
+









-
-
+
+


-
-
+
+







					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
				s->length--;
				_s->length--;
			}

			len = of_string_utf8_encode(character, buffer);

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

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

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		s->cString[j] = '\0';
		s->cStringLength = j;
		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: j + 1];
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
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
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







-
+

-
-
+
+









-
+




-
+

-
+









-
-
+
+


-
-
+
+







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

		s = &s_store;
		_s = &_storage;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;
		_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];
				_s->cString[j++] = buffer[0];
				break;
			case 2:
			case 3:
			case 4:
				s->isUTF8 = YES;
				_s->isUTF8 = YES;

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

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		s->cString[j] = '\0';
		s->cStringLength = j;
		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: j + 1];
			_s->cString = [self resizeMemory: _s->cString
						    size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
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
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







-
+






-
+



-
+

-
+






-
+

-
+







		int cStringLength;

		if (format == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		s = &s_store;
		_s = &_storage;

		if ((cStringLength = of_vasprintf(&tmp, [format UTF8String],
		    arguments)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		s->cStringLength = cStringLength;
		_s->cStringLength = cStringLength;

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

			s->cString = [self
			_s->cString = [self
			    allocMemoryWithSize: cStringLength + 1];
			memcpy(s->cString, tmp, cStringLength + 1);
			memcpy(_s->cString, tmp, cStringLength + 1);
		} @finally {
			free(tmp);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
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
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

771
772
773
774
775

776
777
778
779
780
781
782
783

784
785
786
787
788

789
790
791
792
793
794
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
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

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
771

772
773
774
775
776

777
778
779
780
781
782
783
784

785
786
787
788
789

790
791
792
793
794
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







-
+

-
+




+
-
+

-
+

-
+




-
-
+
+




-
-
+
+

-
+


-
+


-
+





-
-
+
+





-
+










-
-
+
+










-
+




-
+



-
+

-
+











-
+




-
+







-
+




-
+







-
+







-
+














-
-
+
+




-
-
+
+


-
+







	self = [super init];

	@try {
		OFString *component;
		size_t i, cStringLength;
		va_list argumentsCopy;

		s = &s_store;
		_s = &_storage;

		s->cStringLength = [firstComponent UTF8StringLength];
		_s->cStringLength = [firstComponent UTF8StringLength];

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

		s->length = [firstComponent length];
		_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];
			_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;
				_s->isUTF8 =
				    ((OFString_UTF8*)component)->_s->isUTF8;
			else
				s->isUTF8 = YES;
				_s->isUTF8 = YES;
		}

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

		cStringLength = [firstComponent UTF8StringLength];
		memcpy(s->cString, [firstComponent UTF8String], cStringLength);
		memcpy(_s->cString, [firstComponent UTF8String], cStringLength);
		i = cStringLength;

		while ((component = va_arg(arguments, OFString*)) != nil) {
			cStringLength = [component UTF8StringLength];

			s->cString[i] = OF_PATH_DELIMITER;
			memcpy(s->cString + i + 1, [component UTF8String],
			_s->cString[i] = OF_PATH_DELIMITER;
			memcpy(_s->cString + i + 1, [component UTF8String],
			    cStringLength);

			i += 1 + cStringLength;
		}

		s->cString[i] = '\0';
		_s->cString[i] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	if (s != NULL && s->freeWhenDone != NULL)
		free(s->freeWhenDone);
	if (_s != NULL && _s->freeWhenDone != NULL)
		free(_s->freeWhenDone);

	[super dealloc];
}

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

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

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

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

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

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

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

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

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

	if (object == self)
		return YES;

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

	otherString = object;

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

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

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

	return YES;
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
{
844
845
846
847
848
849
850
851
852


853
854

855
856

857
858

859
860
861
862
863
864
865
846
847
848
849
850
851
852


853
854
855

856
857

858
859

860
861
862
863
864
865
866
867







-
-
+
+

-
+

-
+

-
+







	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = (OFString*)object;
	otherCStringLength = [otherString UTF8StringLength];
	minimumCStringLength = (s->cStringLength > otherCStringLength
	    ? otherCStringLength : s->cStringLength);
	minimumCStringLength = (_s->cStringLength > otherCStringLength
	    ? otherCStringLength : _s->cStringLength);

	if ((compare = memcmp(s->cString, [otherString UTF8String],
	if ((compare = memcmp(_s->cString, [otherString UTF8String],
	    minimumCStringLength)) == 0) {
		if (s->cStringLength > otherCStringLength)
		if (_s->cStringLength > otherCStringLength)
			return OF_ORDERED_DESCENDING;
		if (s->cStringLength < otherCStringLength)
		if (_s->cStringLength < otherCStringLength)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
	}

	if (compare > 0)
		return OF_ORDERED_DESCENDING;
	else
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
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







-
-
-
+
+
+

-
+

-
+

-
+












-
+



-
-
+
+







		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherCString = [otherString UTF8String];
	otherCStringLength = [otherString UTF8StringLength];

	if (!s->isUTF8) {
		minimumCStringLength = (s->cStringLength > otherCStringLength
		    ? otherCStringLength : s->cStringLength);
	if (!_s->isUTF8) {
		minimumCStringLength = (_s->cStringLength > otherCStringLength
		    ? otherCStringLength : _s->cStringLength);

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

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

	i = j = 0;

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

		l1 = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c1);
		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
			    exceptionWithClass: [self class]];

938
939
940
941
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
994
995


996
997
998


999
1000
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
1070
1071

1072
1073
1074

1075
1076
1077
1078
1079
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
1110
1111
1112
1113
1114
1115





1116
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
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
1218
1219
1220
1221

1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233

1234
1235
1236
1237
1238
1239
1240

1241
1242
1243


1244
1245
1246
1247
1248
1249

1250
1251

1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265

1266
1267
1268
1269
1270
1271

1272
1273
1274
1275
1276
1277

1278
1279
1280


1281
1282
1283
1284
1285

1286
1287
1288
1289
1290

1291
1292

1293
1294

1295
1296
1297
1298

1299
1300

1301
1302

1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315

1316
1317
1318
1319

1320
1321
1322
1323
1324


1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344

1345
1346
1347
1348

1349
1350
1351
1352
1353


1354
1355
1356
1357
1358
1359
1360
940
941
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
994
995


996
997
998


999
1000
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
1070
1071
1072

1073
1074
1075

1076
1077
1078
1079
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

1110
1111
1112





1113
1114
1115
1116
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
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
1218
1219
1220
1221
1222

1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234

1235
1236
1237
1238
1239
1240
1241

1242
1243


1244
1245
1246
1247
1248
1249
1250

1251
1252

1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266

1267
1268
1269
1270
1271
1272

1273
1274
1275
1276
1277
1278

1279
1280


1281
1282
1283
1284
1285
1286

1287
1288
1289
1290
1291

1292
1293

1294
1295

1296
1297
1298
1299

1300
1301

1302
1303

1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316

1317
1318
1319
1320

1321
1322
1323
1324


1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345

1346
1347
1348
1349

1350
1351
1352
1353


1354
1355
1356
1357
1358
1359
1360
1361
1362







-
+

-
+










-
-
+
+



-
+



-
-
+
+












-
-
+
+








-
+


-
-
+
+

-
-
+
+

-
-
+
+














-
+

















-
+


-
+

-
+

-
-
+
+













-
+


-
+











-
+


-
+


















-
+


-
-
+
+










-
+


-
-
-
-
-
+
+
+
+
+


-
+







-
+


-
+






-
+


-
+

















-
+






-
-
+
+


-
+







-
+














-
+









-
+

-
-
+
+





-
+

-
+


-
+





-
+











-
+






-
+

-
-
+
+





-
+

-
+













-
+





-
+





-
+

-
-
+
+




-
+




-
+

-
+

-
+



-
+

-
+

-
+












-
+



-
+



-
-
+
+



















-
+



-
+



-
-
+
+







		if (c1 < c2)
			return OF_ORDERED_ASCENDING;

		i += l1;
		j += l2;
	}

	if (s->cStringLength - i > otherCStringLength - j)
	if (_s->cStringLength - i > otherCStringLength - j)
		return OF_ORDERED_DESCENDING;
	else if (s->cStringLength - i < otherCStringLength - j)
	else if (_s->cStringLength - i < otherCStringLength - j)
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
	size_t i;
	uint32_t hash;

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

	OF_HASH_INIT(hash);

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

		if ((length = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c)) == 0)
		if ((length = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c)) == 0)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		OF_HASH_ADD(hash, (c & 0xFF0000) >> 16);
		OF_HASH_ADD(hash, (c & 0x00FF00) >>  8);
		OF_HASH_ADD(hash,  c & 0x0000FF);

		i += length - 1;
	}

	OF_HASH_FINALIZE(hash);

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

	return hash;
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t character;

	if (index >= s->length)
	if (index >= _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

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

	index = of_string_utf8_get_position(s->cString, index,
	    s->cStringLength);
	index = of_string_utf8_get_position(_s->cString, index,
	    _s->cStringLength);

	if (!of_string_utf8_decode(s->cString + index, s->cStringLength - index,
	    &character))
	if (!of_string_utf8_decode(_s->cString + index,
	    _s->cStringLength - index, &character))
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	return character;
}

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

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > s->length)
	    range.location + range.length > _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

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

	objc_autoreleasePoolPop(pool);
}

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

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > s->length)
	    range.location + range.length > _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (s->isUTF8) {
	if (_s->isUTF8) {
		rangeLocation = of_string_utf8_get_position(
		    s->cString, range.location, s->cStringLength);
		    _s->cString, range.location, _s->cStringLength);
		rangeLength = of_string_utf8_get_position(
		    s->cString + rangeLocation, range.length,
		    s->cStringLength - rangeLocation);
		    _s->cString + rangeLocation, range.length,
		    _s->cStringLength - rangeLocation);
	} else {
		rangeLocation = range.location;
		rangeLength = range.length;
	}

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

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

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (i = rangeLength - cStringLength;; i--) {
			if (!memcmp(s->cString + rangeLocation + i, cString,
			if (!memcmp(_s->cString + rangeLocation + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeLocation, i);
				    _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);
		}
	} else {
		for (i = 0; i <= rangeLength - cStringLength; i++) {
			if (!memcmp(s->cString + rangeLocation + i, cString,
			if (!memcmp(_s->cString + rangeLocation + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeLocation, i);
				    _s->cString + rangeLocation, i);
				range.length = [string length];

				return range;
			}
		}
	}

	return of_range(OF_NOT_FOUND, 0);
}

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

	if (cStringLength == 0)
		return YES;

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

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

	return NO;
}

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

	if (range.length > SIZE_MAX - range.location || end > s->length)
	if (range.length > SIZE_MAX - range.location || end > _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (s->isUTF8) {
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	if (_s->isUTF8) {
		start = of_string_utf8_get_position(_s->cString, start,
		    _s->cStringLength);
		end = of_string_utf8_get_position(_s->cString, end,
		    _s->cStringLength);
	}

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

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

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

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

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

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

	return !memcmp(s->cString + (s->cStringLength - cStringLength),
	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);
	size_t i, last;
	OFString *component;

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

	if (cStringLength > s->cStringLength) {
	if (cStringLength > _s->cStringLength) {
		[array addObject: [[self copy] autorelease]];
		objc_autoreleasePoolPop(pool);

		return array;
	}

	for (i = 0, last = 0; i <= s->cStringLength - cStringLength; i++) {
		if (memcmp(s->cString + i, cString, cStringLength))
	for (i = 0, last = 0; i <= _s->cStringLength - cStringLength; i++) {
		if (memcmp(_s->cString + i, cString, cStringLength))
			continue;

		component = [OFString stringWithUTF8String: s->cString + last
		component = [OFString stringWithUTF8String: _s->cString + last
						    length: i - last];
		if (!skipEmpty || [component length] > 0)
			[array addObject: component];

		i += cStringLength - 1;
		last = i + 1;
	}
	component = [OFString stringWithUTF8String: s->cString + last];
	component = [OFString stringWithUTF8String: _s->cString + last];
	if (!skipEmpty || [component length] > 0)
		[array addObject: component];

	[array makeImmutable];

	objc_autoreleasePoolPop(pool);

	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	void *pool;
	size_t i, last = 0, pathCStringLength = s->cStringLength;
	size_t i, last = 0, pathCStringLength = _s->cStringLength;

	ret = [OFMutableArray array];

	if (pathCStringLength == 0)
		return ret;

	pool = objc_autoreleasePoolPush();

#ifndef _WIN32
	if (s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
	if (_s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
#else
	if (s->cString[pathCStringLength - 1] == '/' ||
	    s->cString[pathCStringLength - 1] == '\\')
	if (_s->cString[pathCStringLength - 1] == '/' ||
	    _s->cString[pathCStringLength - 1] == '\\')
#endif
		pathCStringLength--;

	for (i = 0; i < pathCStringLength; i++) {
#ifndef _WIN32
		if (s->cString[i] == OF_PATH_DELIMITER) {
		if (_s->cString[i] == OF_PATH_DELIMITER) {
#else
		if (s->cString[i] == '/' || s->cString[i] == '\\') {
		if (_s->cString[i] == '/' || _s->cString[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithUTF8String: s->cString + last
			    [OFString stringWithUTF8String: _s->cString + last
						    length: i - last]];
			last = i + 1;
		}
	}

	[ret addObject: [OFString stringWithUTF8String: s->cString + last
	[ret addObject: [OFString stringWithUTF8String: _s->cString + last
						length: i - last]];

	[ret makeImmutable];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFString*)lastPathComponent
{
	size_t pathCStringLength = s->cStringLength;
	size_t pathCStringLength = _s->cStringLength;
	ssize_t i;

	if (pathCStringLength == 0)
		return @"";

#ifndef _WIN32
	if (s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
	if (_s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
#else
	if (s->cString[pathCStringLength - 1] == '/' ||
	    s->cString[pathCStringLength - 1] == '\\')
	if (_s->cString[pathCStringLength - 1] == '/' ||
	    _s->cString[pathCStringLength - 1] == '\\')
#endif
		pathCStringLength--;

	for (i = pathCStringLength - 1; i >= 0; i--) {
#ifndef _WIN32
		if (s->cString[i] == OF_PATH_DELIMITER) {
		if (_s->cString[i] == OF_PATH_DELIMITER) {
#else
		if (s->cString[i] == '/' || s->cString[i] == '\\') {
		if (_s->cString[i] == '/' || _s->cString[i] == '\\') {
#endif
			i++;
			break;
		}
	}

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i = 0;

	return [OFString stringWithUTF8String: s->cString + i
	return [OFString stringWithUTF8String: _s->cString + i
				       length: pathCStringLength - i];
}

- (OFString*)stringByDeletingLastPathComponent
{
	size_t i, pathCStringLength = s->cStringLength;
	size_t i, pathCStringLength = _s->cStringLength;

	if (pathCStringLength == 0)
		return @"";

#ifndef _WIN32
	if (s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
	if (_s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
#else
	if (s->cString[pathCStringLength - 1] == '/' ||
	    s->cString[pathCStringLength - 1] == '\\')
	if (_s->cString[pathCStringLength - 1] == '/' ||
	    _s->cString[pathCStringLength - 1] == '\\')
#endif
		pathCStringLength--;

	if (pathCStringLength == 0)
		return [OFString stringWithUTF8String: s->cString
		return [OFString stringWithUTF8String: _s->cString
					       length: 1];

	for (i = pathCStringLength - 1; i >= 1; i--)
#ifndef _WIN32
		if (s->cString[i] == OF_PATH_DELIMITER)
		if (_s->cString[i] == OF_PATH_DELIMITER)
#else
		if (s->cString[i] == '/' || s->cString[i] == '\\')
		if (_s->cString[i] == '/' || _s->cString[i] == '\\')
#endif
			return [OFString stringWithUTF8String: s->cString
			return [OFString stringWithUTF8String: _s->cString
						       length: i];

#ifndef _WIN32
	if (s->cString[0] == OF_PATH_DELIMITER)
	if (_s->cString[0] == OF_PATH_DELIMITER)
#else
	if (s->cString[0] == '/' || s->cString[0] == '\\')
	if (_s->cString[0] == '/' || _s->cString[0] == '\\')
#endif
		return [OFString stringWithUTF8String: s->cString
		return [OFString stringWithUTF8String: _s->cString
					       length: 1];

	return @".";
}

- (const of_unichar_t*)characters
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	of_unichar_t *ret;
	size_t i, j;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: s->length];
				    count: _s->length];

	i = j = 0;

	while (i < s->cStringLength) {
	while (i < _s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

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

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

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

	return ret;
}

- (const of_char32_t*)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	of_char32_t *ret;
	size_t i, j;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: s->length + 1];
				    count: _s->length + 1];

	i = j = 0;

	while (i < s->cStringLength) {
	while (i < _s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

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

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		if (byteOrder != OF_BYTE_ORDER_NATIVE)
			ret[j++] = OF_BSWAP32(c);
1368
1369
1370
1371
1372
1373
1374
1375

1376
1377
1378
1379
1380
1381
1382
1370
1371
1372
1373
1374
1375
1376

1377
1378
1379
1380
1381
1382
1383
1384







-
+







	return ret;
}

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

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