ObjFW  Diff

Differences From Artifact [2df9fbd0df]:

  • File src/OFString_UTF8.m — part of check-in [28170f5f65] at 2012-03-15 11:29:24 on branch trunk — Greatly improve OFObject's memory handling and performance.

    A linked-list is put before each memory chunk allocated instead of
    having an array of all memory chunks. This means only one malloc now
    instead of one malloc and one realloc. This also means that when
    checking reallocs and frees, it's no longer necessary to iterate through
    all memory chunks, as the linked list also contains the owner, meaning
    realloc and free are no longer O(n), but O(1) now.

    As allocating bigger chunks seems to be a little bit slower than smaller
    chunks, it seems that this is slightly slower in benchmarks if only very small
    chunks are allocated. However, measuring real world usage, it's a lot faster. (user: js, size: 24829) [annotate] [blame] [check-ins using]

To Artifact [2441b116ae]:


60
61
62
63
64
65
66
67

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

67

68
69
70
71
72
73
74







-
+
-








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

	@try {
		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

		s->cString = [self allocMemoryWithSize: 1];
		s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}
89
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104
88
89
90
91
92
93
94

95

96
97
98
99
100
101
102







-
+
-








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

		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

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

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_check_utf8(cString, cStringLength,
210
211
212
213
214
215
216
217

218
219
220
221
222
223
224
225
208
209
210
211
212
213
214

215

216
217
218
219
220
221
222







-
+
-







}

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

	@try {
		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

		s->cStringLength = [string UTF8StringLength];

		if ([string isKindOfClass: [OFString_UTF8 class]] ||
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)string)->s->UTF8;
		else
253
254
255
256
257
258
259
260

261
262
263
264
265
266
267
268
250
251
252
253
254
255
256

257

258
259
260
261
262
263
264







-
+
-







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

		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

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

		for (i = 0; i < length; i++) {
			char buffer[4];
336
337
338
339
340
341
342
343

344
345
346
347
348
349
350
351
332
333
334
335
336
337
338

339

340
341
342
343
344
345
346







-
+
-







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

		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

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

		for (i = 0; i < length; i++) {
			char buffer[4];
439
440
441
442
443
444
445
446

447
448
449
450
451
452
453
454
434
435
436
437
438
439
440

441

442
443
444
445
446
447
448







-
+
-







		int cStringLength;

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

		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

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

		s->cStringLength = cStringLength;
484
485
486
487
488
489
490
491

492
493
494
495
496
497
498
499
478
479
480
481
482
483
484

485

486
487
488
489
490
491
492







-
+
-







	self = [super init];

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

		s = [self allocMemoryWithSize: sizeof(*s)];
		s = &s_store;
		memset(s, 0, sizeof(*s));

		s->cStringLength = [firstComponent UTF8StringLength];

		if ([firstComponent isKindOfClass: [OFString_UTF8 class]] ||
		    [firstComponent isKindOfClass:
		    [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)firstComponent)->s->UTF8;