ObjFW  Diff

Differences From Artifact [8acc3c73fe]:

To Artifact [0a6f9138dd]:


45
46
47
48
49
50
51

52
53
54
55
56
57
58
# import "OFFileManager.h"
#endif
#import "OFIRI.h"
#import "OFIRIHandler.h"
#import "OFLocale.h"
#import "OFStream.h"
#import "OFSystemInfo.h"

#import "OFUTF8String.h"
#import "OFUTF8String+Private.h"

#import "OFGetItemAttributesFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"







>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# import "OFFileManager.h"
#endif
#import "OFIRI.h"
#import "OFIRIHandler.h"
#import "OFLocale.h"
#import "OFStream.h"
#import "OFSystemInfo.h"
#import "OFTaggedPointerString.h"
#import "OFUTF8String.h"
#import "OFUTF8String+Private.h"

#import "OFGetItemAttributesFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
85
86
87
88
89
90
91








92
93
94
95
96
97
98
static struct {
	Class isa;
} placeholder;

#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L) || defined(HAVE_USELOCALE)
static locale_t cLocale;
#endif









@interface OFString ()
- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (OFStringEncoding)encoding
		  lossy: (bool)lossy OF_DIRECT;
- (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding







>
>
>
>
>
>
>
>







86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static struct {
	Class isa;
} placeholder;

#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L) || defined(HAVE_USELOCALE)
static locale_t cLocale;
#endif

#ifdef OF_OBJFW_RUNTIME
# if UINTPTR_MAX == UINT64_MAX
#  define MAX_TAGGED_POINTER_LENGTH 8
# else
#  define MAX_TAGGED_POINTER_LENGTH 4
# endif
#endif

@interface OFString ()
- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (OFStringEncoding)encoding
		  lossy: (bool)lossy OF_DIRECT;
- (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding
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
{
	size_t length = strlen(string);
	char *copy = (char *)OFAllocMemory(1, length + 1);
	memcpy(copy, string, length + 1);

	return copy;
}














@implementation OFPlaceholderString
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-pragmas"
# pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#endif
- (instancetype)init
{
	return (id)[[OFUTF8String alloc] init];
}

- (instancetype)initWithUTF8String: (const char *)UTF8String
{

	OFUTF8String *string;
	size_t length;
	void *storage;






	length = strlen(UTF8String);









	string = OFAllocObject([OFUTF8String class], length + 1, 1, &storage);

	return (id)[string of_initWithUTF8String: UTF8String
					  length: length
					 storage: storage];
}

- (instancetype)initWithUTF8String: (const char *)UTF8String
			    length: (size_t)UTF8StringLength
{
	OFUTF8String *string;
	void *storage;
















	string = OFAllocObject([OFUTF8String class], UTF8StringLength + 1, 1,
	    &storage);

	return (id)[string of_initWithUTF8String: UTF8String
					  length: UTF8StringLength
					 storage: storage];







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










|




>

<


>
>
>
>
>
|
>
>
>
>
>
>
>
>
>












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







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
456
457
{
	size_t length = strlen(string);
	char *copy = (char *)OFAllocMemory(1, length + 1);
	memcpy(copy, string, length + 1);

	return copy;
}

#ifdef OF_OBJFW_RUNTIME
static bool
isASCII(const char *string, size_t length)
{
	uint8_t combined = 0;

	for (size_t i = 0; i < length; i++)
		combined |= string[i];

	return !(combined & ~0x7F);
}
#endif

@implementation OFPlaceholderString
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-pragmas"
# pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#endif
- (instancetype)init
{
	return (id)@"";
}

- (instancetype)initWithUTF8String: (const char *)UTF8String
{
	size_t length = strlen(UTF8String);
	OFUTF8String *string;

	void *storage;

	if (length == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (length <= MAX_TAGGED_POINTER_LENGTH &&
	    isASCII(UTF8String, length)) {
		id ret = [OFTaggedPointerString
		    stringWithASCIIString: UTF8String
				   length: length];

		if (ret != nil)
			return ret;
	}
#endif

	string = OFAllocObject([OFUTF8String class], length + 1, 1, &storage);

	return (id)[string of_initWithUTF8String: UTF8String
					  length: length
					 storage: storage];
}

- (instancetype)initWithUTF8String: (const char *)UTF8String
			    length: (size_t)UTF8StringLength
{
	OFUTF8String *string;
	void *storage;

	if (UTF8StringLength == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (UTF8StringLength <= MAX_TAGGED_POINTER_LENGTH &&
	    isASCII(UTF8String, UTF8StringLength)) {
		id ret = [OFTaggedPointerString
		    stringWithASCIIString: UTF8String
				   length: UTF8StringLength];

		if (ret != nil)
			return ret;
	}
#endif

	string = OFAllocObject([OFUTF8String class], UTF8StringLength + 1, 1,
	    &storage);

	return (id)[string of_initWithUTF8String: UTF8String
					  length: UTF8StringLength
					 storage: storage];
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
456
457
458
459
460
461
			      length: UTF8StringLength
			freeWhenDone: freeWhenDone];
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (OFStringEncoding)encoding
{
	if (encoding == OFStringEncodingUTF8) {
		OFUTF8String *string;
		size_t length;

		void *storage;

		length = strlen(cString);














		string = OFAllocObject([OFUTF8String class], length + 1, 1,
		    &storage);

		return (id)[string of_initWithUTF8String: cString
						  length: length
						 storage: storage];
	}

	return (id)[[OFUTF8String alloc] initWithCString: cString
						encoding: encoding];
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (OFStringEncoding)encoding
			 length: (size_t)cStringLength
{
	if (encoding == OFStringEncodingUTF8) {

		OFUTF8String *string;
		void *storage;
















		string = OFAllocObject([OFUTF8String class], cStringLength + 1,
		    1, &storage);

		return (id)[string of_initWithUTF8String: cString
						  length: cStringLength
						 storage: storage];







|
|
|
>


|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
















|
>


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







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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
			      length: UTF8StringLength
			freeWhenDone: freeWhenDone];
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (OFStringEncoding)encoding
{
	if (encoding == OFStringEncodingUTF8 ||
	    encoding == OFStringEncodingASCII) {
		size_t length = strlen(cString);
		OFUTF8String *string;
		void *storage;

		if (length == 0)
			return (id)@"";

#ifdef OF_OBJFW_RUNTIME
		if (length <= MAX_TAGGED_POINTER_LENGTH &&
		    isASCII(cString, length)) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: cString
					   length: length];

			if (ret != nil)
				return ret;
		}
#endif

		string = OFAllocObject([OFUTF8String class], length + 1, 1,
		    &storage);

		return (id)[string of_initWithUTF8String: cString
						  length: length
						 storage: storage];
	}

	return (id)[[OFUTF8String alloc] initWithCString: cString
						encoding: encoding];
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (OFStringEncoding)encoding
			 length: (size_t)cStringLength
{
	if (encoding == OFStringEncodingUTF8 ||
	    encoding == OFStringEncodingASCII) {
		OFUTF8String *string;
		void *storage;

		if (cStringLength == 0)
			return (id)@"";

#ifdef OF_OBJFW_RUNTIME
		if (cStringLength <= MAX_TAGGED_POINTER_LENGTH &&
		    isASCII(cString, cStringLength)) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: cString
					   length: cStringLength];

			if (ret != nil)
				return ret;
		}
#endif

		string = OFAllocObject([OFUTF8String class], cStringLength + 1,
		    1, &storage);

		return (id)[string of_initWithUTF8String: cString
						  length: cStringLength
						 storage: storage];
477
478
479
480
481
482
483




























484
485
486
487
488
489
490
{
	return (id)[[OFUTF8String alloc] initWithString: string];
}

- (instancetype)initWithCharacters: (const OFUnichar *)string
			    length: (size_t)length
{




























	return (id)[[OFUTF8String alloc] initWithCharacters: string
						     length: length];
}

- (instancetype)initWithUTF16String: (const OFChar16 *)string
{
	return (id)[[OFUTF8String alloc] initWithUTF16String: string];







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







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
{
	return (id)[[OFUTF8String alloc] initWithString: string];
}

- (instancetype)initWithCharacters: (const OFUnichar *)string
			    length: (size_t)length
{
	if (length == 0)
		return (id)@"";

#ifdef OF_OBJFW_RUNTIME
	if (length <= MAX_TAGGED_POINTER_LENGTH) {
		char buffer[MAX_TAGGED_POINTER_LENGTH];
		bool isUnicode = false;

		for (size_t i = 0; i < length; i++) {
			if (string[i] >= 0x80) {
				isUnicode = true;
				break;
			}

			buffer[i] = (char)string[i];
		}

		if (!isUnicode) {
			id ret = [OFTaggedPointerString
			    stringWithASCIIString: buffer
					   length: length];

			if (ret != nil)
				return ret;
		}
	}
#endif

	return (id)[[OFUTF8String alloc] initWithCharacters: string
						     length: length];
}

- (instancetype)initWithUTF16String: (const OFChar16 *)string
{
	return (id)[[OFUTF8String alloc] initWithUTF16String: string];