ObjFW  Diff

Differences From Artifact [ead9a0ea73]:

To Artifact [7863930129]:


59
60
61
62
63
64
65
66

67
68
69
70
71

72
73

74
75
76
77
78
79
80
59
60
61
62
63
64
65

66
67
68
69
70

71
72

73
74
75
76
77
78
79
80







-
+




-
+

-
+







	_OFString_Hashing_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
};

static inline int
memcasecmp(const char *s1, const char *s2, size_t len)
memcasecmp(const char *first, const char *second, size_t len)
{
	size_t i;

	for (i = 0; i < len; i++) {
		if (tolower((int)s1[i]) > tolower((int)s2[i]))
		if (tolower((int)first[i]) > tolower((int)second[i]))
			return OF_ORDERED_DESCENDING;
		if (tolower((int)s1[i]) < tolower((int)s2[i]))
		if (tolower((int)first[i]) < tolower((int)second[i]))
			return OF_ORDERED_ASCENDING;
	}

	return OF_ORDERED_SAME;
}

int
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
289
290

291
292
293

294
295
296
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
339
340
341
342
343
344
345
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
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
289

290
291
292

293
294




295
296
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
339
340
341
342
343
344
345
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







-
+

-
+


-
+


-
+



-
+

-
+

-
+

-
+


-
-
+
+

-
-
+
+


-
+


-
+

-
-
-
-
+
+
+
+




-
+


-
+

-
-
-
-
+
+
+
+




-
+

-
+














-
+

-
+


-
+


-
+


















-
+

-
+

-
+


-
+


-
+

-
+


-
+

-
+








-
-
-
+
+
+


-
-
+
+


-
+








-
+






-
+



-
-
+
+





-
+








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

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
{
	return [[[self alloc] initWithCString: str] autorelease];
	return [[[self alloc] initWithCString: string] autorelease];
}

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithCString: str
	return [[[self alloc] initWithCString: string
				     encoding: encoding] autorelease];
}

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding
	     length: (size_t)len
	     length: (size_t)length
{
	return [[[self alloc] initWithCString: str
	return [[[self alloc] initWithCString: string
				     encoding: encoding
				       length: len] autorelease];
				       length: length] autorelease];
}

+ stringWithCString: (const char*)str
	     length: (size_t)len
+ stringWithCString: (const char*)string
	     length: (size_t)length
{
	return [[[self alloc] initWithCString: str
				       length: len] autorelease];
	return [[[self alloc] initWithCString: string
				       length: length] autorelease];
}

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

	va_start(args, fmt);
	ret = [[[self alloc] initWithFormat: fmt
				  arguments: args] autorelease];
	va_end(args);
	va_start(arguments, format);
	ret = [[[self alloc] initWithFormat: format
				  arguments: arguments] autorelease];
	va_end(arguments);

	return ret;
}

+ stringWithPath: (OFString*)first, ...
+ stringWithPath: (OFString*)firstComponent, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [[[self alloc] initWithPath: first
				arguments: args] autorelease];
	va_end(args);
	va_start(arguments, firstComponent);
	ret = [[[self alloc] initWithPath: firstComponent
				arguments: arguments] autorelease];
	va_end(arguments);

	return ret;
}

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

+ stringWithContentsOfFile: (OFString*)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ stringWithContentsOfFile: (OFString*)path
		  encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithContentsOfFile: path
					    encoding: encoding] autorelease];
}

+ stringWithContentsOfURL: (OFURL*)url
+ stringWithContentsOfURL: (OFURL*)URL
{
	return [[[self alloc] initWithContentsOfURL: url] autorelease];
	return [[[self alloc] initWithContentsOfURL: URL] autorelease];
}

+ stringWithContentsOfURL: (OFURL*)url
+ stringWithContentsOfURL: (OFURL*)URL
		 encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithContentsOfURL: url
	return [[[self alloc] initWithContentsOfURL: URL
					   encoding: encoding] autorelease];
}

- init
{
	self = [super init];

	@try {
		string = [self allocMemoryWithSize: 1];
		string[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: strlen(str)];
			      length: strlen(string_)];
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
	 encoding: (of_string_encoding_t)encoding
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: encoding
			      length: strlen(str)];
			      length: strlen(string_)];
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)len
	   length: (size_t)length_
{
	self = [super init];

	@try {
		size_t i, j;
		const uint16_t *table;

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    len >= 3 && !memcmp(str, "\xEF\xBB\xBF", 3)) {
			str += 3;
			len -= 3;
		    length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
			string_ += 3;
			length_ -= 3;
		}

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

		if (encoding == OF_STRING_ENCODING_UTF_8) {
			switch (of_string_check_utf8(str, length)) {
			switch (of_string_check_utf8(string_, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			memcpy(string, str, length);
			memcpy(string, string_, length);
			string[length] = 0;

			return self;
		}

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

				if (!(str[i] & 0x80)) {
					string[j++] = str[i];
				if (!(string_[i] & 0x80)) {
					string[j++] = string_[i];
					continue;
				}

				isUTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				    (uint8_t)str[i], buf);
				    (uint8_t)string_[i], buf);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];

				length += bytes - 1;
				string = [self resizeMemory: string
432
433
434
435
436
437
438
439

440
441
442


443
444
445


446
447
448
449

450
451

452
453
454
455
456


457
458

459
460
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
486
487

488
489
490

491
492
493
494
495




496
497
498
499
500
501


502
503
504
505
506
507
508

509
510
511
512


513
514
515
516
517
518
519
432
433
434
435
436
437
438

439
440


441
442
443


444
445
446
447
448

449
450

451
452
453
454
455

456
457
458

459
460
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
486
487

488
489
490

491
492




493
494
495
496
497
498
499
500


501
502
503
504
505
506
507
508

509
510
511
512

513
514
515
516
517
518
519
520
521







-
+

-
-
+
+

-
-
+
+



-
+

-
+




-
+
+

-
+



-
+



-
-
+
+











-
-
+
+

-
+

-
+


-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+






-
+



-
+
+







		case OF_STRING_ENCODING_WINDOWS_1252:
			table = of_windows_1252;
			break;
		default:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		for (i = j = 0; i < len; i++) {
		for (i = j = 0; i < length_; i++) {
			char buf[4];
			of_unichar_t chr;
			size_t chr_bytes;
			of_unichar_t character;
			size_t characterBytes;

			if (!(str[i] & 0x80)) {
				string[j++] = str[i];
			if (!(string_[i] & 0x80)) {
				string[j++] = string_[i];
				continue;
			}

			chr = table[(uint8_t)str[i]];
			character = table[(uint8_t)string_[i]];

			if (chr == 0xFFFD)
			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			isUTF8 = YES;
			chr_bytes = of_string_unicode_to_utf8(chr, buf);
			characterBytes = of_string_unicode_to_utf8(character,
			    buf);

			if (chr_bytes == 0)
			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

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

			memcpy(string + j, buf, chr_bytes);
			j += chr_bytes;
			memcpy(string + j, buf, characterBytes);
			j += characterBytes;
		}

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

	return self;
}

- initWithCString: (const char*)str
	   length: (size_t)len
- initWithCString: (const char*)string_
	   length: (size_t)length_
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: len];
			      length: length_];
}

- initWithFormat: (OFString*)fmt, ...
- initWithFormat: (OFString*)format, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, fmt);
	ret = [self initWithFormat: fmt
			 arguments: args];
	va_end(args);
	va_start(arguments, format);
	ret = [self initWithFormat: format
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
- initWithFormat: (OFString*)format
       arguments: (va_list)arguments
{
	self = [super init];

	@try {
		int len;

		if (fmt == nil)
		if (format == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((len = of_vasprintf(&string, [fmt cString], args)) == -1)
		if ((len = of_vasprintf(&string, [format cString],
		    arguments)) == -1)
			@throw [OFInvalidFormatException newWithClass: isa];

		@try {
			length = len;

			switch (of_string_check_utf8(string, length)) {
			case 1:
532
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
625
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
625
626
627
628







-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+






-
+

-
+

-
+
+








-
-
+
+
















-
-
+
+


-
+















-
+





-
-
+
+







		[self release];
		@throw e;
	}

	return self;
}

- initWithPath: (OFString*)first, ...
- initWithPath: (OFString*)firstComponent, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [self initWithPath: first
		       arguments: args];
	va_end(args);
	va_start(arguments, firstComponent);
	ret = [self initWithPath: firstComponent
		       arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithPath: (OFString*)first
     arguments: (va_list)args
- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	self = [super init];

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

		length = [first cStringLength];
		length = [firstComponent cStringLength];

		switch (of_string_check_utf8([first cString], length)) {
		switch (of_string_check_utf8([firstComponent cString],
		    length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Calculate length */
		va_copy(args2, args);
		while ((component = va_arg(args2, OFString*)) != nil) {
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			len = [component cStringLength];
			length += 1 + len;

			switch (of_string_check_utf8([component cString],
			    len)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		string = [self allocMemoryWithSize: length + 1];

		len = [first cStringLength];
		memcpy(string, [first cString], len);
		len = [firstComponent cStringLength];
		memcpy(string, [firstComponent cString], len);
		i = len;

		while ((component = va_arg(args, OFString*)) != nil) {
		while ((component = va_arg(arguments, OFString*)) != nil) {
			len = [component cStringLength];
			string[i] = OF_PATH_DELIM;
			memcpy(string + i + 1, [component cString], len);
			i += len + 1;
		}

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

	return self;
}

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

	@try {
		/* We have no -[dealloc], so this is ok */
		string = (char*)[str cString];
		length = [str cStringLength];
		string = (char*)[string_ cString];
		length = [string_ cStringLength];

		switch (of_string_check_utf8(string, length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:;
			@throw [OFInvalidEncodingException newWithClass: isa];
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
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







-
+

-
+



-
+



-
-
+
+








-
-
+
+





-
-
+
+

-
+

-
-
-
+
+
+

-
+

-
+







			    encoding: encoding
			      length: s.st_size];
	[self freeMemory: tmp];

	return self;
}

- initWithContentsOfURL: (OFURL*)url
- initWithContentsOfURL: (OFURL*)URL
{
	return [self initWithContentsOfURL: url
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfURL: (OFURL*)url
- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *req;
	OFHTTPRequestResult *res;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	Class c;

	c = isa;
	[self release];
	self = nil;

	pool = [[OFAutoreleasePool alloc] init];

	if ([[url scheme] isEqual: @"file"]) {
		self = [[c alloc] initWithContentsOfFile: [url path]
	if ([[URL scheme] isEqual: @"file"]) {
		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		[pool release];
		return self;
	}

	req = [OFHTTPRequest requestWithURL: url];
	res = [req perform];
	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([res statusCode] != 200)
	if ([result statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    newWithClass: [req class]
		     HTTPRequest: req
		      statusCode: [res statusCode]];
		    newWithClass: [request class]
		     HTTPRequest: request
		      statusCode: [result statusCode]];

	self = [[c alloc] initWithCString: (char*)[[res data] cArray]
	self = [[c alloc] initWithCString: (char*)[[result data] cArray]
				 encoding: encoding
				   length: [[res data] count]];
				   length: [[result data] count]];
	[pool release];
	return self;
}

- (const char*)cString
{
	return string;
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
839
840
841


842
843
844
845
846
847
848
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
839
840
841
842
843
844
845
846

847
848
849
850
851

852
853
854
855
856
857
858
859
860







-
+

+
+
-
+

+
+
+
-
+















-
+

+
-
+


-
+



-
-
+
+
+
+

-
-
+
+

-
+










-
+

-
-
+
+


-
+



-
-
+
+


-
+

-
-
+
+

-
+












-
+




-
+
+







}

- (BOOL)isUTF8
{
	return isUTF8;
}

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

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

	otherString = (OFString*)object;

	if (strcmp(string, [(OFString*)obj cString]))
	if (strcmp(string, [otherString cString]))
		return NO;

	return YES;
}

- copy
{
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id)obj
- (of_comparison_result_t)compare: (id)object
{
	OFString *otherString;
	size_t str_len, min_len;
	size_t otherLen, minLen;
	int cmp;

	if (![obj isKindOfClass: [OFString class]])
	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	str_len = [(OFString*)obj cStringLength];
	min_len = (length > str_len ? str_len : length);
	otherString = (OFString*)object;

	otherLen = [otherString cStringLength];
	minLen = (length > otherLen ? otherLen : length);

	if ((cmp = memcmp(string, [(OFString*)obj cString], min_len)) == 0) {
		if (length > str_len)
	if ((cmp = memcmp(string, [otherString cString], minLen)) == 0) {
		if (length > otherLen)
			return OF_ORDERED_DESCENDING;
		if (length < str_len)
		if (length < otherLen)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
	}

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

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)str
- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString;
{
	const char *str_cstr;
	size_t i, j, str_len, min_len;
	const char *otherCString;
	size_t i, j, otherLen, minLen;
	int cmp;

	if (![str isKindOfClass: [OFString class]])
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	str_cstr = [str cString];
	str_len = [str cStringLength];
	otherCString = [otherString cString];
	otherLen = [otherString cStringLength];

	if (![self isUTF8]) {
		min_len = (length > str_len ? str_len : length);
		minLen = (length > otherLen ? otherLen : length);

		if ((cmp = memcasecmp(string, [str cString], min_len)) == 0) {
			if (length > str_len)
		if ((cmp = memcasecmp(string, otherCString, minLen)) == 0) {
			if (length > otherLen)
				return OF_ORDERED_DESCENDING;
			if (length < str_len)
			if (length < otherLen)
				return OF_ORDERED_ASCENDING;
			return OF_ORDERED_SAME;
		}

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

	i = j = 0;

	while (i < length && j < str_len) {
	while (i < length && j < otherLen) {
		of_unichar_t c1, c2;
		size_t l1, l2;

		l1 = of_string_utf8_to_unicode(string + i, length - i, &c1);
		l2 = of_string_utf8_to_unicode(str_cstr + j, str_len - j, &c2);
		l2 = of_string_utf8_to_unicode(otherCString + j, otherLen - j,
		    &c2);

		if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF)
			@throw [OFInvalidEncodingException newWithClass: isa];

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			    of_unicode_casefolding_table[c1 >> 8][c1 & 0xFF];
864
865
866
867
868
869
870
871

872
873

874
875
876
877
878
879
880
876
877
878
879
880
881
882

883
884

885
886
887
888
889
890
891
892







-
+

-
+







		if (c1 < c2)
			return OF_ORDERED_ASCENDING;

		i += l1;
		j += l2;
	}

	if (length - i > str_len - j)
	if (length - i > otherLen - j)
		return OF_ORDERED_DESCENDING;
	else if (length - i < str_len - j)
	else if (length - i < otherLen - j)
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
912
913
914
915
916
917
918
919

920
921
922


923
924
925

926
927
928

929
930
931
932


933
934
935
936
937
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
924
925
926
927
928
929
930

931
932


933
934
935
936

937
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







-
+

-
-
+
+


-
+


-
+


-
-
+
+





-
+

-
-
+
+


-
+


-
+


-
-
+
+








-
+

-
-
+
+


-
+


-
+


-
-
+
+








	if (!of_string_utf8_to_unicode(string + index, length - index, &c))
		@throw [OFInvalidEncodingException newWithClass: isa];

	return c;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)str
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return 0;

	if (str_len > length)
	if (stringLen > length)
		return OF_INVALID_INDEX;

	for (i = 0; i <= length - str_len; i++)
		if (!memcmp(string + i, str_c, str_len))
	for (i = 0; i <= length - stringLen; i++)
		if (!memcmp(string + i, cString, stringLen))
			return of_string_position_to_index(string, i);

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)str
- (size_t)indexOfLastOccurrenceOfString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return of_string_position_to_index(string, length);

	if (str_len > length)
	if (stringLen > length)
		return OF_INVALID_INDEX;

	for (i = length - str_len;; i--) {
		if (!memcmp(string + i, str_c, str_len))
	for (i = length - stringLen;; i--) {
		if (!memcmp(string + i, cString, stringLen))
			return of_string_position_to_index(string, i);

		/* Did not match and we're at the last char */
		if (i == 0)
			return OF_INVALID_INDEX;
	}
}

- (BOOL)containsString: (OFString*)str
- (BOOL)containsString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return YES;

	if (str_len > length)
	if (stringLen > length)
		return NO;

	for (i = 0; i <= length - str_len; i++)
		if (!memcmp(string + i, str_c, str_len))
	for (i = 0; i <= length - stringLen; i++)
		if (!memcmp(string + i, cString, stringLen))
			return YES;

	return NO;
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
997
998
999
1000
1001
1002
1003
1004

1005
1006
1007
1008
1009

1010
1011
1012
1013
1014
1015
1016
1009
1010
1011
1012
1013
1014
1015

1016
1017
1018
1019
1020

1021
1022
1023
1024
1025
1026
1027
1028







-
+




-
+








- (OFString*)substringWithRange: (of_range_t)range
{
	return [self substringFromIndex: range.start
				toIndex: range.start + range.length];
}

- (OFString*)stringByAppendingString: (OFString*)str
- (OFString*)stringByAppendingString: (OFString*)string_
{
	OFMutableString *new;

	new = [OFMutableString stringWithString: self];
	[new appendString: str];
	[new appendString: string_];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	new->isa = [OFString class];
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
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







-
+





-
+






-
-
+
+




-
+



















-
+



-
+





-
+

-
+

-
+

-
+







}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
	OFAutoreleasePool *pool;
	OFMutableArray *array;
	const char *delim = [delimiter cString];
	size_t delim_len = [delimiter cStringLength];
	size_t delimLen = [delimiter cStringLength];
	size_t i, last;

	array = [OFMutableArray array];
	pool = [[OFAutoreleasePool alloc] init];

	if (delim_len > length) {
	if (delimLen > length) {
		[array addObject: [[self copy] autorelease]];
		[pool release];

		return array;
	}

	for (i = 0, last = 0; i <= length - delim_len; i++) {
		if (memcmp(string + i, delim, delim_len))
	for (i = 0, last = 0; i <= length - delimLen; i++) {
		if (memcmp(string + i, delim, delimLen))
			continue;

		[array addObject: [OFString stringWithCString: string + last
						       length: i - last]];
		i += delim_len - 1;
		i += delimLen - 1;
		last = i + 1;
	}
	[array addObject: [OFString stringWithCString: string + last]];

	[pool release];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	array->isa = [OFArray class];
	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	OFAutoreleasePool *pool;
	size_t i, last = 0, path_len = length;
	size_t i, last = 0, pathLen = length;

	ret = [OFMutableArray array];

	if (path_len == 0)
	if (pathLen == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	for (i = 0; i < path_len; i++) {
	for (i = 0; i < pathLen; i++) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithCString: string + last
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
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







-
+


-
+



-
+

-
+

-
+

-
+


















-
+




-
+

-
+



-
+

-
+

-
+

-
+



-
+







	 */
	ret->isa = [OFArray class];
	return ret;
}

- (OFString*)lastPathComponent
{
	size_t path_len = length;
	size_t pathLen = length;
	ssize_t i;

	if (path_len == 0)
	if (pathLen == 0)
		return @"";

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	for (i = path_len - 1; i >= 0; i--) {
	for (i = pathLen - 1; i >= 0; i--) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM) {
#else
		if (string[i] == '/' || string[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 stringWithCString: string + i
				    length: path_len - i];
				    length: pathLen - i];
}

- (OFString*)stringByDeletingLastPathComponent;
{
	size_t i, path_len = length;
	size_t i, pathLen = length;

	if (path_len == 0)
	if (pathLen == 0)
		return @"";

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	if (path_len == 0)
	if (pathLen == 0)
		return [OFString stringWithCString: string
					    length: 1];

	for (i = path_len - 1; i >= 1; i--)
	for (i = pathLen - 1; i >= 1; i--)
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM)
#else
		if (string[i] == '/' || string[i] == '\\')
#endif
			return [OFString stringWithCString: string
						    length: i];
1351
1352
1353
1354
1355
1356
1357
1358

1359
1360
1361
1362
1363
1364
1365

1366
1367
1368
1369
1370
1371




1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382

1383
1384
1385
1386
1387
1388
1389

1390
1391
1392
1393
1394
1395




1396
1397
1398
1399
1400
1401
1402
1363
1364
1365
1366
1367
1368
1369

1370
1371
1372
1373
1374
1375
1376

1377
1378
1379




1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393

1394
1395
1396
1397
1398
1399
1400

1401
1402
1403




1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414







-
+






-
+


-
-
-
-
+
+
+
+










-
+






-
+


-
-
-
-
+
+
+
+








	return num;
}

- (float)floatValue
{
	const char *str = string;
	char *endptr;
	char *endPtr;
	float value;

	/* Don't depend on isspace and thus the used locale */
	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r')
		str++;

	value = strtof(str, &endptr);
	value = strtof(str, &endPtr);

	/* Check if there are any invalid chars left */
	if (endptr != NULL) {
		for (; *endptr != '\0'; endptr++)
			if (*endptr != ' ' && *endptr != '\t' &&
			    *endptr != '\n' && *endptr != '\r')
	if (endPtr != NULL) {
		for (; *endPtr != '\0'; endPtr++)
			if (*endPtr != ' ' && *endPtr != '\t' &&
			    *endPtr != '\n' && *endPtr != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
	}

	return value;
}

- (double)doubleValue
{
	const char *str = string;
	char *endptr;
	char *endPtr;
	double value;

	/* Don't depend on isspace and thus the used locale */
	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r')
		str++;

	value = strtod(str, &endptr);
	value = strtod(str, &endPtr);

	/* Check if there are any invalid chars left */
	if (endptr != NULL) {
		for (; *endptr != '\0'; endptr++)
			if (*endptr != ' ' && *endptr != '\t' &&
			    *endptr != '\n' && *endptr != '\r')
	if (endPtr != NULL) {
		for (; *endPtr != '\0'; endPtr++)
			if (*endPtr != ' ' && *endPtr != '\t' &&
			    *endPtr != '\n' && *endPtr != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
	}

	return value;
}

1410
1411
1412
1413
1414
1415
1416
1417

1418
1419

1420
1421

1422
1423
1424
1425
1426
1427

1428
1429
1430
1431
1432
1433
1434
1422
1423
1424
1425
1426
1427
1428

1429
1430

1431
1432

1433
1434
1435
1436
1437
1438

1439
1440
1441
1442
1443
1444
1445
1446







-
+

-
+

-
+





-
+







	if ((ret = malloc((len + 1) * sizeof(of_unichar_t))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa];

	i = j = 0;

	while (i < length) {
		of_unichar_t c;
		size_t clen;
		size_t cLen;

		clen = of_string_utf8_to_unicode(string + i, length - 1, &c);
		cLen = of_string_utf8_to_unicode(string + i, length - 1, &c);

		if (clen == 0 || c > 0x10FFFF) {
		if (cLen == 0 || c > 0x10FFFF) {
			free(ret);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

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

	ret[j] = 0;

	return ret;
}