ObjFW  Check-in [9817eab3be]

Overview
Comment:Rename s->UTF8 to s->isUTF8.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9817eab3be57fe38df25a566b6b92b19f639b44418a676fed6c86c5203cc828d
User & Date: js on 2012-10-13 21:29:30
Other Links: manifest | tags
Context
2012-10-13
22:21
Add -[OFString rangeOfString:options:range:]. check-in: 20dddc7345 user: js tags: trunk
21:29
Rename s->UTF8 to s->isUTF8. check-in: 9817eab3be user: js tags: trunk
21:06
of_bswap* -> OF_BSWAP*. check-in: 1cb8fee5c3 user: js tags: trunk
Changes

Modified src/OFConstantString.m from [0925478785] to [f72b46effc].

154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168







-
+








	ivars->cString = cString;
	ivars->cStringLength = cStringLength;

	switch (of_string_utf8_check(ivars->cString, ivars->cStringLength,
	    &ivars->length)) {
	case 1:
		ivars->UTF8 = YES;
		ivars->isUTF8 = YES;
		break;
	case -1:
		free(ivars);
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}

Modified src/OFMutableString_UTF8.m from [e8d0af35fc] to [34bbb5a839].

57
58
59
60
61
62
63
64

65
66
67
68
69
70
71
57
58
59
60
61
62
63

64
65
66
67
68
69
70
71







-
+







{
	of_unichar_t *unicodeString;
	size_t unicodeLen, newCStringLength;
	size_t i, j;
	char *newCString;
	BOOL isStart = YES;

	if (!s->UTF8) {
	if (!s->isUTF8) {
		uint8_t t;
		const of_unichar_t *const *table;

		assert(startTableSize >= 1 && middleTableSize >= 1);

		s->hashed = NO;

201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
201
202
203
204
205
206
207

208
209
210
211
212
213
214
215







-
+







- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index
{
	char buffer[4];
	of_unichar_t c;
	size_t lenNew, lenOld;

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

	if (index > s->cStringLength)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	/* Shortcut if old and new character both are ASCII */
243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
243
244
245
246
247
248
249

250
251
252
253
254
255
256
257







-
+







		memcpy(s->cString + index, buffer, lenNew);

		s->cStringLength -= lenOld;
		s->cStringLength += lenNew;
		s->cString[s->cStringLength] = '\0';

		if (character & 0x80)
			s->UTF8 = YES;
			s->isUTF8 = YES;
	} else if (lenNew < lenOld) {
		memmove(s->cString + index + lenNew,
		    s->cString + index + lenOld,
		    s->cStringLength - index - lenOld);
		memcpy(s->cString + index, buffer, lenNew);

		s->cStringLength -= lenOld;
276
277
278
279
280
281
282
283

284
285
286
287
288
289
290
276
277
278
279
280
281
282

283
284
285
286
287
288
289
290







-
+







	if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
		UTF8String += 3;
		UTF8StringLength -= 3;
	}

	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		s->isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}

	s->hashed = NO;
305
306
307
308
309
310
311
312

313
314
315
316
317
318
319
305
306
307
308
309
310
311

312
313
314
315
316
317
318
319







-
+







	if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
		UTF8String += 3;
		UTF8StringLength -= 3;
	}

	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		s->isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}

	s->hashed = NO;
374
375
376
377
378
379
380
381
382


383
384

385
386
387
388
389
390
391
374
375
376
377
378
379
380


381
382
383

384
385
386
387
388
389
390
391







-
-
+
+

-
+







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

	s->cString[s->cStringLength] = 0;

	if ([string isKindOfClass: [OFString_UTF8 class]] ||
	    [string isKindOfClass: [OFMutableString_UTF8 class]]) {
		if (((OFString_UTF8*)string)->s->UTF8)
			s->UTF8 = YES;
		if (((OFString_UTF8*)string)->s->isUTF8)
			s->isUTF8 = YES;
	} else
		s->UTF8 = YES;
		s->isUTF8 = YES;
}

- (void)appendFormat: (OFConstantString*)format
       withArguments: (va_list)arguments
{
	char *UTF8String;
	int UTF8StringLength;
418
419
420
421
422
423
424
425

426
427
428
429
430
431
432
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432







-
+







	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];
	}

	if (!s->UTF8)
	if (!s->isUTF8)
		return;

	for (i = 0; i < s->cStringLength; i++) {
		/* ASCII */
		if OF_LIKELY (!(s->cString[i] & 0x80))
			continue;

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
544
545
546
547
548
549
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
544
545
546
547
548
549







-
+



















-
-
+
+

-
+













-
+







{
	size_t newCStringLength;

	if (index > s->length)
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

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

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

	memmove(s->cString + index + [string UTF8StringLength],
	    s->cString + index, s->cStringLength - index);
	memcpy(s->cString + index, [string UTF8String],
	    [string UTF8StringLength]);
	s->cString[newCStringLength] = '\0';

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

	if ([string isKindOfClass: [OFString_UTF8 class]] ||
	    [string isKindOfClass: [OFMutableString_UTF8 class]]) {
		if (((OFString_UTF8*)string)->s->UTF8)
			s->UTF8 = YES;
		if (((OFString_UTF8*)string)->s->isUTF8)
			s->isUTF8 = YES;
	} else
		s->UTF8 = YES;
		s->isUTF8 = YES;
}

- (void)deleteCharactersInRange: (of_range_t)range
{
	size_t start = range.start;
	size_t end = range.start + range.length;

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

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

	if (s->UTF8) {
	if (s->isUTF8) {
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	memmove(s->cString + start, s->cString + end, s->cStringLength - end);
566
567
568
569
570
571
572
573

574
575
576
577
578
579
580
566
567
568
569
570
571
572

573
574
575
576
577
578
579
580







-
+







	size_t newCStringLength, newLength;

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

	newLength = s->length - (end - start) + [replacement length];

	if (s->UTF8) {
	if (s->isUTF8) {
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	newCStringLength = s->cStringLength - (end - start) +
600
601
602
603
604
605
606
607

608
609
610
611
612
613
614
600
601
602
603
604
605
606

607
608
609
610
611
612
613
614







-
+







	const char *searchString = [string UTF8String];
	const char *replacementString = [replacement UTF8String];
	size_t searchLength = [string UTF8StringLength];
	size_t replacementLength = [replacement UTF8StringLength];
	size_t i, last, newCStringLength, newLength;
	char *newCString;

	if (s->UTF8) {
	if (s->isUTF8) {
		range.start = of_string_utf8_get_position(s->cString,
		    range.start, s->cStringLength);
		range.length = of_string_utf8_get_position(s->cString,
		    range.start + range.length, s->cStringLength) - range.start;
	}

	if (range.start + range.length > [self UTF8StringLength])

Modified src/OFString.h from [505bd888a0] to [2f006f43b3].

623
624
625
626
627
628
629
630

631
632
633
634
635
636
637
623
624
625
626
627
628
629

630
631
632
633
634
635
636
637







-
+







 */
- (of_range_t)rangeOfString: (OFString*)string;

/**
 * \brief Returns the range of the string.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour.
 * \param options Options modifying search behaviour
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options;

/**

Modified src/OFString_UTF8.h from [a124fe7f59] to [cbba34b9c4].

25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
40
41
42
43
44
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44







-
+












	 * Since constant strings don't have s_store, they have to malloc it on
	 * the first access. Strings created at runtime just set the pointer to
	 * &s_store.
	 */
	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		BOOL	 UTF8;
		BOOL	 isUTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
		char	 *freeWhenDone;
	} *restrict s;
	struct of_string_utf8_ivars s_store;
}

- OF_initWithUTF8String: (const char*)UTF8String
		 length: (size_t)UTF8StringLength
		storage: (char*)storage;
@end

Modified src/OFString_UTF8.m from [2e4dae0bbe] to [dd6ca8a27b].

252
253
254
255
256
257
258
259

260
261
262
263
264
265
266
252
253
254
255
256
257
258

259
260
261
262
263
264
265
266







-
+








		s->cString = storage;
		s->cStringLength = UTF8StringLength;

		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &s->length)) {
		case 1:
			s->UTF8 = YES;
			s->isUTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}

		memcpy(s->cString, UTF8String, UTF8StringLength);
299
300
301
302
303
304
305
306

307
308
309
310
311
312
313
299
300
301
302
303
304
305

306
307
308
309
310
311
312
313







-
+







			switch (of_string_utf8_check(cString, cStringLength,
			    &s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				s->UTF8 = YES;
				s->isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}

			memcpy(s->cString, cString, cStringLength);
325
326
327
328
329
330
331
332

333
334
335
336
337
338
339
325
326
327
328
329
330
331

332
333
334
335
336
337
338
339







-
+







				size_t bytes;

				if (!(cString[i] & 0x80)) {
					s->cString[j++] = cString[i];
					continue;
				}

				s->UTF8 = YES;
				s->isUTF8 = YES;
				bytes = of_string_utf8_encode(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

375
376
377
378
379
380
381
382

383
384
385
386
387
388
389
375
376
377
378
379
380
381

382
383
384
385
386
387
388
389







-
+








			character = table[(uint8_t)cString[i]];

			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			s->UTF8 = YES;
			s->isUTF8 = YES;
			characterBytes = of_string_utf8_encode(character,
			    buffer);

			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

425
426
427
428
429
430
431
432

433
434
435
436
437
438
439
425
426
427
428
429
430
431

432
433
434
435
436
437
438
439







-
+








		s->cString = (char*)UTF8String;
		s->cStringLength = UTF8StringLength;

		switch (of_string_utf8_check(UTF8String, UTF8StringLength,
		    &s->length)) {
		case 1:
			s->UTF8 = YES;
			s->isUTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
	} @catch (id e) {
		[self release];
450
451
452
453
454
455
456
457

458
459

460
461
462
463
464
465
466
450
451
452
453
454
455
456

457
458

459
460
461
462
463
464
465
466







-
+

-
+







	@try {
		s = &s_store;

		s->cStringLength = [string UTF8StringLength];

		if ([string isKindOfClass: [OFString_UTF8 class]] ||
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)string)->s->UTF8;
			s->isUTF8 = ((OFString_UTF8*)string)->s->isUTF8;
		else
			s->UTF8 = YES;
			s->isUTF8 = YES;

		s->length = [string length];

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
		memcpy(s->cString, [string UTF8String], s->cStringLength + 1);
	} @catch (id e) {
		[self release];
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
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







-
+







-
+







-
+







			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
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
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







-
+







-
+







-
+







			characterLen = of_string_utf8_encode(character, buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
682
683
684
685
686
687
688
689

690
691
692
693
694
695
696
682
683
684
685
686
687
688

689
690
691
692
693
694
695
696







-
+








		s->cStringLength = cStringLength;

		@try {
			switch (of_string_utf8_check(tmp, cStringLength,
			    &s->length)) {
			case 1:
				s->UTF8 = YES;
				s->isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}

			s->cString = [self
720
721
722
723
724
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
720
721
722
723
724
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







-
+

-
+












+
-
+

-
+







		s = &s_store;

		s->cStringLength = [firstComponent UTF8StringLength];

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

		s->length = [firstComponent length];

		/* Calculate length and see if we need UTF-8 */
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			s->cStringLength += 1 + [component UTF8StringLength];
			s->length += 1 + [component length];

			if ([component isKindOfClass: [OFString_UTF8 class]] ||
			    [component isKindOfClass:
			    [OFMutableString_UTF8 class]])
				s->isUTF8 =
				s->UTF8 = ((OFString_UTF8*)component)->s->UTF8;
				    ((OFString_UTF8*)component)->s->isUTF8;
			else
				s->UTF8 = YES;
				s->isUTF8 = YES;
		}

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

		cStringLength = [firstComponent UTF8StringLength];
		memcpy(s->cString, [firstComponent UTF8String], cStringLength);
		i = cStringLength;
784
785
786
787
788
789
790
791

792
793
794
795
796
797
798
785
786
787
788
789
790
791

792
793
794
795
796
797
798
799







-
+








- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cString;
	case OF_STRING_ENCODING_ASCII:
		if (s->UTF8)
		if (s->isUTF8)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		return s->cString;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
812
813
814
815
816
817
818
819

820
821
822
823
824
825
826
813
814
815
816
817
818
819

820
821
822
823
824
825
826
827







-
+








- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cStringLength;
	case OF_STRING_ENCODING_ASCII:
		if (s->UTF8)
		if (s->isUTF8)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		return s->cStringLength;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
897
898
899
900
901
902
903
904

905
906
907
908
909
910
911
898
899
900
901
902
903
904

905
906
907
908
909
910
911
912







-
+







		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherCString = [otherString UTF8String];
	otherCStringLength = [otherString UTF8StringLength];

	if (!s->UTF8) {
	if (!s->isUTF8) {
		minimumCStringLength = (s->cStringLength > otherCStringLength
		    ? otherCStringLength : s->cStringLength);

		if ((compare = memcasecmp(s->cString, otherCString,
		    minimumCStringLength)) == 0) {
			if (s->cStringLength > otherCStringLength)
				return OF_ORDERED_DESCENDING;
1005
1006
1007
1008
1009
1010
1011
1012

1013
1014
1015
1016
1017
1018
1019
1006
1007
1008
1009
1010
1011
1012

1013
1014
1015
1016
1017
1018
1019
1020







-
+







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

	if (index >= s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (!s->UTF8)
	if (!s->isUTF8)
		return s->cString[index];

	index = of_string_utf8_get_position(s->cString, index,
	    s->cStringLength);

	if (!of_string_utf8_decode(s->cString + index, s->cStringLength - index,
	    &character))
1092
1093
1094
1095
1096
1097
1098
1099

1100
1101
1102
1103
1104
1105
1106
1093
1094
1095
1096
1097
1098
1099

1100
1101
1102
1103
1104
1105
1106
1107







-
+







{
	size_t start = range.start;
	size_t end = range.start + range.length;

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

	if (s->UTF8) {
	if (s->isUTF8) {
		start = of_string_utf8_get_position(s->cString, start,
		    s->cStringLength);
		end = of_string_utf8_get_position(s->cString, end,
		    s->cStringLength);
	}

	return [OFString stringWithUTF8String: s->cString + start