ObjFW  Check-in [a0d967e8dd]

Overview
Comment:Cache hashes of strings.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a0d967e8dd048e7904c780ad3ca7eaf406003507803637d53227c55b9ceda2d0
User & Date: js on 2012-01-24 12:01:50
Other Links: manifest | tags
Context
2012-01-25
20:00
Update Xcode project. check-in: 7d548c1262 user: js tags: trunk
2012-01-24
12:01
Cache hashes of strings. check-in: a0d967e8dd user: js tags: trunk
11:57
Compare pointers of strings before comparing the string content. check-in: af56ce47e0 user: js tags: trunk
Changes

Modified src/OFMutableString_UTF8.m from [eed6c4692d] to [a5af572702].

54
55
56
57
58
59
60


61
62
63
64
65
66
67
	char *newCString;

	if (!s->UTF8) {
		assert(tableSize >= 1);

		uint8_t *p = (uint8_t*)s->cString + s->cStringLength;
		uint8_t t;



		while (--p >= (uint8_t*)s->cString)
			if ((t = table[0][*p]) != 0)
				*p = t;

		return;
	}







>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	char *newCString;

	if (!s->UTF8) {
		assert(tableSize >= 1);

		uint8_t *p = (uint8_t*)s->cString + s->cStringLength;
		uint8_t t;

		s->hashed = NO;

		while (--p >= (uint8_t*)s->cString)
			if ((t = table[0][*p]) != 0)
				*p = t;

		return;
	}
129
130
131
132
133
134
135

136
137
138
139
140
141
142
	}

	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: s->cString];

	s->cString = newCString;
	s->cStringLength = newCStringLength;

	/*
	 * Even though cStringLength can change, length cannot, therefore no
	 * need to change it.
	 */







>







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	}

	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: s->cString];
	s->hashed = NO;
	s->cString = newCString;
	s->cStringLength = newCStringLength;

	/*
	 * Even though cStringLength can change, length cannot, therefore no
	 * need to change it.
	 */
150
151
152
153
154
155
156

157
158
159
160
161
162
163
164
165
166


167
168
169
170
171
172
173
	size_t length, oldLength;

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		    s->cStringLength);

	if (!(character & 0x80) && !(s->cString[index] & 0x80)) {

		s->cString[index] = character;
		return;
	}

	if ((length = of_string_unicode_to_utf8(character, buffer)) == 0)
		@throw [OFInvalidEncodingException exceptionWithClass: isa];

	if ((oldLength = of_string_utf8_to_unicode(s->cString + index,
	    s->cStringLength - index, &c)) == 0)
		@throw [OFInvalidEncodingException exceptionWithClass: isa];



	if (length == oldLength) {
		memcpy(s->cString + index, buffer, length);
		return;
	}

	if (length > oldLength) {







>










>
>







153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	size_t length, oldLength;

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		    s->cStringLength);

	if (!(character & 0x80) && !(s->cString[index] & 0x80)) {
		s->hashed = NO;
		s->cString[index] = character;
		return;
	}

	if ((length = of_string_unicode_to_utf8(character, buffer)) == 0)
		@throw [OFInvalidEncodingException exceptionWithClass: isa];

	if ((oldLength = of_string_utf8_to_unicode(s->cString + index,
	    s->cStringLength - index, &c)) == 0)
		@throw [OFInvalidEncodingException exceptionWithClass: isa];

	s->hashed = NO;

	if (length == oldLength) {
		memcpy(s->cString + index, buffer, length);
		return;
	}

	if (length > oldLength) {
227
228
229
230
231
232
233

234
235
236
237
238
239
240
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException exceptionWithClass: isa];
	}


	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1);

	s->cStringLength += UTF8StringLength;
	s->length += length;







>







233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException exceptionWithClass: isa];
	}

	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1);

	s->cStringLength += UTF8StringLength;
	s->length += length;
254
255
256
257
258
259
260

261
262
263
264
265
266
267
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException exceptionWithClass: isa];
	}


	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength);

	s->cStringLength += UTF8StringLength;
	s->length += length;







>







261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException exceptionWithClass: isa];
	}

	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength);

	s->cStringLength += UTF8StringLength;
	s->length += length;
300
301
302
303
304
305
306

307
308
309
310
311
312
313

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

	UTF8StringLength = [string UTF8StringLength];


	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, [string UTF8String],
	    UTF8StringLength);

	s->cStringLength += UTF8StringLength;







>







308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

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

	UTF8StringLength = [string UTF8StringLength];

	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, [string UTF8String],
	    UTF8StringLength);

	s->cStringLength += UTF8StringLength;
344
345
346
347
348
349
350


351
352
353
354
355
356
357
		free(UTF8String);
	}
}

- (void)reverse
{
	size_t i, j;



	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = s->cStringLength - 1; i < s->cStringLength / 2;
	    i++, j--) {
		s->cString[i] ^= s->cString[j];
		s->cString[j] ^= s->cString[i];
		s->cString[i] ^= s->cString[j];







>
>







353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
		free(UTF8String);
	}
}

- (void)reverse
{
	size_t i, j;

	s->hashed = NO;

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = s->cStringLength - 1; i < s->cStringLength / 2;
	    i++, j--) {
		s->cString[i] ^= s->cString[j];
		s->cString[j] ^= s->cString[i];
		s->cString[i] ^= s->cString[j];
436
437
438
439
440
441
442

443
444
445
446
447
448
449
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		    s->cStringLength);

	newCStringLength = s->cStringLength + [string UTF8StringLength];

	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->cString + index + [string UTF8StringLength],
	    s->cString + index, s->cStringLength - index);
	memcpy(s->cString + index, [string UTF8String],
	    [string UTF8StringLength]);







>







447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		    s->cStringLength);

	newCStringLength = s->cStringLength + [string UTF8StringLength];
	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->cString + index + [string UTF8StringLength],
	    s->cString + index, s->cStringLength - index);
	memcpy(s->cString + index, [string UTF8String],
	    [string UTF8StringLength]);
468
469
470
471
472
473
474

475
476
477
478
479
480
481
	if (start > end)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	if (end > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: isa];


	s->length -= end - start;

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);







>







480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
	if (start > end)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	if (end > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	s->hashed = NO;
	s->length -= end - start;

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
514
515
516
517
518
519
520

521
522
523
524
525
526
527
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	newCStringLength = s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];

	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->cString + end, s->cString + start +
	    [replacement UTF8StringLength], s->cStringLength - end);
	memcpy(s->cString + start, [replacement UTF8String],
	    [replacement UTF8StringLength]);







>







527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	newCStringLength = s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];
	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->cString + end, s->cString + start +
	    [replacement UTF8StringLength], s->cStringLength - end);
	memcpy(s->cString + start, [replacement UTF8String],
	    [replacement UTF8StringLength]);
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
629
630
	}
	memcpy(newCString + newCStringLength, s->cString + last,
	    s->cStringLength - last);
	newCStringLength += s->cStringLength - last;
	newCString[newCStringLength] = 0;

	[self freeMemory: s->cString];

	s->cString = newCString;
	s->cStringLength = newCStringLength;
	s->length = newLength;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < s->cStringLength; i++)
		if (s->cString[i] != ' '  && s->cString[i] != '\t' &&
		    s->cString[i] != '\n' && s->cString[i] != '\r' &&
		    s->cString[i] != '\f')
			break;


	s->cStringLength -= i;
	s->length -= i;

	memmove(s->cString, s->cString + i, s->cStringLength);
	s->cString[s->cStringLength] = '\0';

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

- (void)deleteTrailingWhitespaces
{
	size_t d;
	char *p;



	d = 0;
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
		    *p != '\f')
			break;








>















>


















>
>







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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
	}
	memcpy(newCString + newCStringLength, s->cString + last,
	    s->cStringLength - last);
	newCStringLength += s->cStringLength - last;
	newCString[newCStringLength] = 0;

	[self freeMemory: s->cString];
	s->hashed = NO;
	s->cString = newCString;
	s->cStringLength = newCStringLength;
	s->length = newLength;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < s->cStringLength; i++)
		if (s->cString[i] != ' '  && s->cString[i] != '\t' &&
		    s->cString[i] != '\n' && s->cString[i] != '\r' &&
		    s->cString[i] != '\f')
			break;

	s->hashed = NO;
	s->cStringLength -= i;
	s->length -= i;

	memmove(s->cString, s->cString + i, s->cStringLength);
	s->cString[s->cStringLength] = '\0';

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

- (void)deleteTrailingWhitespaces
{
	size_t d;
	char *p;

	s->hashed = NO;

	d = 0;
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
		    *p != '\f')
			break;

643
644
645
646
647
648
649


650
651
652
653
654
655
656
	}
}

- (void)deleteEnclosingWhitespaces
{
	size_t d, i;
	char *p;



	d = 0;
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
		    *p != '\f')
			break;








>
>







661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
	}
}

- (void)deleteEnclosingWhitespaces
{
	size_t d, i;
	char *p;

	s->hashed = NO;

	d = 0;
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
		    *p != '\f')
			break;

Modified src/OFString_UTF8.h from [820eccb843] to [958573e473].

16
17
18
19
20
21
22
23
24
25
26


27
28
29

#import "OFString.h"

@interface OFString_UTF8: OFString
{
@public
	struct of_string_utf8_ivars {
		char   *cString;
		size_t cStringLength;
		BOOL   UTF8;
		size_t length;


	} *restrict s;
}
@end







|
|
|
|
>
>



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#import "OFString.h"

@interface OFString_UTF8: OFString
{
@public
	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
	} *restrict s;
}
@end

Modified src/OFString_UTF8.m from [53d2bab608] to [2b9e572a06].

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
}

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




	OF_HASH_INIT(hash);

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

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

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




	return hash;
}

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








>
>
>




















>
>
>







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
}

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

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

	OF_HASH_INIT(hash);

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

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

		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;

	return hash;
}

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