ObjFW  Check-in [8ca952eaa4]

Overview
Comment:Add support for tagged pointer strings
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8ca952eaa46d5f2070b0a507d52260380a05fbfd1418fae2c3f8403235dcbd08
User & Date: js on 2024-08-25 00:47:24
Other Links: manifest | tags
References
2024-08-25
00:53 Fixed ticket [31d1e19cd9]: Support for tagged pointer strings plus 4 other changes artifact: 016d95a5f0 user: js
Context
2024-08-25
21:27
OFINIFile: Fix parsing `=` within `"` check-in: 95fcd0f05b user: js tags: trunk
00:47
Add support for tagged pointer strings check-in: 8ca952eaa4 user: js tags: trunk
2024-08-24
23:41
Don't assume all custom string classes use Unicode check-in: 82c5846734 user: js tags: trunk
Changes

Modified src/Makefile from [4661138f18] to [65892fa6a5].

243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
		OFKernelEventObserver.m			\
		${OF_KQUEUE_KERNEL_EVENT_OBSERVER_M}	\
		${OF_POLL_KERNEL_EVENT_OBSERVER_M}	\
		${OF_SELECT_KERNEL_EVENT_OBSERVER_M}	\
		OFTCPSocketSOCKS5Connector.m
SRCS_TAGGED_POINTERS = OFTaggedPointerColor.m		\
		       OFTaggedPointerDate.m		\
		       OFTaggedPointerNumber.m

SRCS_WINDOWS += platform/Windows/OFWin32ConsoleStdIOStream.m	\
		versioninfo.rc

OBJS_EXTRA = exceptions/exceptions.a	\
	     encodings/encodings.a	\
	     forwarding/forwarding.a
LIB_OBJS_EXTRA = exceptions/exceptions.lib.a	\







|
>







243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
		OFKernelEventObserver.m			\
		${OF_KQUEUE_KERNEL_EVENT_OBSERVER_M}	\
		${OF_POLL_KERNEL_EVENT_OBSERVER_M}	\
		${OF_SELECT_KERNEL_EVENT_OBSERVER_M}	\
		OFTCPSocketSOCKS5Connector.m
SRCS_TAGGED_POINTERS = OFTaggedPointerColor.m		\
		       OFTaggedPointerDate.m		\
		       OFTaggedPointerNumber.m		\
		       OFTaggedPointerString.m
SRCS_WINDOWS += platform/Windows/OFWin32ConsoleStdIOStream.m	\
		versioninfo.rc

OBJS_EXTRA = exceptions/exceptions.a	\
	     encodings/encodings.a	\
	     forwarding/forwarding.a
LIB_OBJS_EXTRA = exceptions/exceptions.lib.a	\

Modified src/OFString.m from [8acc3c73fe] to [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];

Added src/OFTaggedPointerString.h version [b8ba961a0f].



























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFString.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFTaggedPointerString: OFString
+ (OFTaggedPointerString *)stringWithASCIIString: (const char *)ASCIIString
					  length: (size_t)length;
@end

OF_ASSUME_NONNULL_END

Added src/OFTaggedPointerString.m version [17da7c1b2d].

































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFTaggedPointerString.h"

#import "OFOutOfRangeException.h"

static int stringTag;

static OF_INLINE size_t
lengthForValue(uintptr_t value)
{
	if (value <= 0x7F)
		return 1;
	if (value <= 0x3FFF)
		return 2;
	if (value <= 0x1FFFFF)
		return 3;
	if (value <= 0xFFFFFFF)
		return 4;
#if UINTPTR_MAX == UINT64_MAX
	if (value <= 0x7FFFFFFFF)
		return 5;
	if (value <= 0x3FFFFFFFFFF)
		return 6;
	if (value <= 0x1FFFFFFFFFFFF)
		return 7;
	if (value <= 0xFFFFFFFFFFFFFF)
		return 8;
#endif

	@throw [OFOutOfRangeException exception];
}

@implementation OFTaggedPointerString
+ (void)initialize
{
	if (self == [OFTaggedPointerString class])
		stringTag = objc_registerTaggedPointerClass(self);
}

+ (OFTaggedPointerString *)stringWithASCIIString: (const char *)ASCIIString
					  length: (size_t)length
{
	uintptr_t value = 0;

	for (size_t i = 0; i < length; i++)
		value |= (uintptr_t)ASCIIString[i] << (i * 7);

	return objc_createTaggedPointer(stringTag, value);
}

- (size_t)length
{
	return lengthForValue(object_getTaggedPointerValue(self));
}

- (OFUnichar)characterAtIndex: (size_t)idx
{
	uintptr_t value = object_getTaggedPointerValue(self);

	if (idx >= lengthForValue(value))
		@throw [OFOutOfRangeException exception];

	return (value >> (idx * 7)) & 0x7F;
}

- (unsigned long)hash
{
	uintptr_t value = object_getTaggedPointerValue(self);
	unsigned long hash;

	OFHashInit(&hash);

	while (value > 0) {
		OFHashAddByte(&hash, 0);
		OFHashAddByte(&hash, 0);
		OFHashAddByte(&hash, value & 0x7F);
		value >>= 7;
	}

	OFHashFinalize(&hash);

	return hash;
}

- (size_t)UTF8StringLength
{
	return self.length;
}

- (size_t)cStringLengthWithEncoding: (OFStringEncoding)encoding
{
	return self.length;
}

- (void)getCharacters: (OFUnichar *)buffer inRange: (OFRange)range
{
	uintptr_t value = object_getTaggedPointerValue(self);

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

	for (size_t i = 0; i < range.length; i++)
		buffer[i] = (value >> ((i + range.location) * 7)) & 0x7F;
}

OF_SINGLETON_METHODS
@end