ObjFW  Check-in [f177032b43]

Overview
Comment:OF(Mutable)String_UTF8: Code simplification.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f177032b43bea32a84498a790fdb35226c915e654b0acb195572075e317fd92d
User & Date: js on 2013-11-23 17:28:18
Other Links: manifest | tags
Context
2013-11-23
17:48
of_asprintf: Add %K format specifier. check-in: 945ff1043d user: js tags: trunk
17:28
OF(Mutable)String_UTF8: Code simplification. check-in: f177032b43 user: js tags: trunk
03:24
of_asprintf: Change %C to %k. check-in: 5eada9f7b0 user: js tags: trunk
Changes

Modified src/OFMutableString_UTF8.m from [9492a9e3d8] to [be6d727813].

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
391
392
393
394
395
396
397



398


399



400



401
402



403



404


405
406


407





408
409
410
411
412
413
414







-
-
-
+
-
-
+
-
-
-

-
-
-
+
+
-
-
-

-
-
-
+
-
-


-
-
+
-
-
-
-
-








	tmp = [self allocMemoryWithSize: (length * 4) + 1];
	@try {
		size_t i, j = 0;
		bool isUTF8 = false;

		for (i = 0; i < length; i++) {
			char buffer[4];

			switch (of_string_utf8_encode(characters[i], buffer)) {
			size_t len = of_string_utf8_encode(characters[i],
			case 1:
				tmp[j++] = buffer[0];
			    tmp + j);
				break;
			case 2:
				isUTF8 = true;

				memcpy(tmp + j, buffer, 2);
				j += 2;

			if (len == 0)
				@throw [OFInvalidEncodingException exception];
				break;
			case 3:
				isUTF8 = true;

				memcpy(tmp + j, buffer, 3);
				j += 3;

			if (len > 1)
				break;
			case 4:
				isUTF8 = true;

				memcpy(tmp + j, buffer, 4);
				j += 4;
			j += len;

				break;
			default:
				@throw [OFInvalidEncodingException exception];
			}
		}

		tmp[j] = '\0';

		_s->hashed = false;
		_s->cString = [self resizeMemory: _s->cString
					    size: _s->cStringLength + j + 1];

Modified src/OFString_UTF8.m from [33807e1aeb] to [7fb4904a02].

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
412
413
414
415
416
417
418

419

420
421


422
423
424


425



426
427


428





429
430
431
432
433
434
435







-

-
+

-
-
+
+
+
-
-
+
-
-
-


-
-
+
-
-
-
-
-








		_s = &_storage;

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

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);
			    _s->cString + j);

			switch (len) {
			case 1:
			if (len == 0)
				@throw [OFInvalidEncodingException exception];

				_s->cString[j++] = buffer[0];
				break;
			if (len > 1)
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = true;

				memcpy(_s->cString + j, buffer, len);
				j += len;
			j += len;

				break;
			default:
				@throw [OFInvalidEncodingException exception];
			}
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = [self resizeMemory: _s->cString
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
467
468
469
470
471
472
473

474
475
476
477
478
479
480







-








		_s = &_storage;

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

		for (i = 0; i < length; i++) {
			char buffer[4];
			of_unichar_t character =
			    (swap ? OF_BSWAP16(string[i]) : string[i]);
			size_t len;

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException exception];
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
497
498
499
500
501
502
503

504
505




506
507
508
509



510
511


512





513
514
515
516
517
518
519







-
+

-
-
-
-
+
+
+
+
-
-
-


-
-
+
-
-
-
-
-







				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
				_s->length--;
			}

			len = of_string_utf8_encode(character, buffer);
			len = of_string_utf8_encode(character, _s->cString + j);

			switch (len) {
			case 1:
				_s->cString[j++] = buffer[0];
				break;
			if (len == 0)
				@throw [OFInvalidEncodingException exception];

			if (len > 1)
			case 2:
			case 3:
			case 4:
				_s->isUTF8 = true;

				memcpy(_s->cString + j, buffer, len);
				j += len;
			j += len;

				break;
			default:
				@throw [OFInvalidEncodingException exception];
			}
		}

		_s->cString[j] = '\0';
		_s->cStringLength = j;

		@try {
			_s->cString = [self resizeMemory: _s->cString