ObjFW  Diff

Differences From Artifact [50da202bb8]:

To Artifact [7fb8de4d28]:

  • File src/OFMutableString_UTF8.m — part of check-in [3d16a30f41] at 2013-06-22 12:12:36 on branch trunk — Rework exceptions.

    This mostly removes the argument for the class in which the exception
    occurred. As backtraces were recently added for all platforms, the
    passed class does not give any extra information on where the exception
    occurred anymore.

    This also removes a few other arguments which were not too helpful. In
    the past, the idea was to pass as many arguments as possible so that it
    is easier to find the origin of the exception. However, as backtraces
    are a much better way to find the origin, those are not useful anymore
    and just make the exception more cumbersome to use. The rule is now to
    only pass arguments that might help in recovering from the exception or
    provide information that is otherwise not easily accessible. (user: js, size: 19565) [annotate] [blame] [check-ins using]


123
124
125
126
127
128
129
130

131
132
133
134
135
136
137
138
123
124
125
126
127
128
129

130

131
132
133
134
135
136
137







-
+
-







		}

		cLen = of_string_utf8_decode(_s->cString + i,
		    _s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF) {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];
		}

		switch (c) {
		case ' ':
		case '\t':
		case '\n':
		case '\r':
157
158
159
160
161
162
163
164

165
166
167
168
169
170
171
172
156
157
158
159
160
161
162

163

164
165
166
167
168
169
170







-
+
-







			newCStringLength += 2;
		else if (c < 0x10000)
			newCStringLength += 3;
		else if (c < 0x110000)
			newCStringLength += 4;
		else {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];
		}

		i += cLen;
	}

	@try {
		newCString = [self allocMemoryWithSize: newCStringLength + 1];
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
195
178
179
180
181
182
183
184

185

186
187
188
189
190
191
192







-
+
-







	for (i = 0; i < unicodeLen; i++) {
		size_t d;

		if ((d = of_string_utf8_encode(unicodeString[i],
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];
		}
		j += d;
	}

	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];
213
214
215
216
217
218
219
220

221
222
223
224
225
226
227
228
229
230

231
232
233
234
235

236
237
238
239
240
241
242
243
210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
225
226

227

228
229
230

231

232
233
234
235
236
237
238







-
+









-
+
-



-
+
-







	size_t lenNew, lenOld;

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

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

	/* Shortcut if old and new character both are ASCII */
	if (!(character & 0x80) && !(_s->cString[index] & 0x80)) {
		_s->hashed = false;
		_s->cString[index] = character;
		return;
	}

	if ((lenNew = of_string_utf8_encode(character, buffer)) == 0)
		@throw [OFInvalidEncodingException
		@throw [OFInvalidEncodingException exception];
		    exceptionWithClass: [self class]];

	if ((lenOld = of_string_utf8_decode(_s->cString + index,
	    _s->cStringLength - index, &c)) == 0)
		@throw [OFInvalidEncodingException
		@throw [OFInvalidEncodingException exception];
		    exceptionWithClass: [self class]];

	_s->hashed = false;

	if (lenNew == lenOld)
		memcpy(_s->cString + index, buffer, lenNew);
	else if (lenNew > lenOld) {
		_s->cString = [self resizeMemory: _s->cString
287
288
289
290
291
292
293
294

295
296
297
298
299
300
301
302
282
283
284
285
286
287
288

289

290
291
292
293
294
295
296







-
+
-







	}

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

	_s->hashed = false;
	_s->cString = [self resizeMemory: _s->cString
				    size: _s->cStringLength +
					  UTF8StringLength + 1];
	memcpy(_s->cString + _s->cStringLength, UTF8String,
317
318
319
320
321
322
323
324

325
326
327
328
329
330
331
332
311
312
313
314
315
316
317

318

319
320
321
322
323
324
325







-
+
-







	}

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

	_s->hashed = false;
	_s->cString = [self resizeMemory: _s->cString
				    size: _s->cStringLength +
					  UTF8StringLength + 1];
	memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength);
363
364
365
366
367
368
369
370

371
372
373
374
375
376
377
378
379
356
357
358
359
360
361
362

363


364
365
366
367
368
369
370







-
+
-
-







}

- (void)appendString: (OFString*)string
{
	size_t UTF8StringLength;

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

	UTF8StringLength = [string UTF8StringLength];

	_s->hashed = false;
	_s->cString = [self resizeMemory: _s->cString
				    size: _s->cStringLength +
					  UTF8StringLength + 1];
428
429
430
431
432
433
434
435

436
437
438
439
440
441
442
443
419
420
421
422
423
424
425

426

427
428
429
430
431
432
433







-
+
-







				isUTF8 = true;

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

				break;
			default:
				@throw [OFInvalidEncodingException
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];
			}
		}

		tmp[j] = '\0';

		_s->hashed = false;
		_s->cString = [self resizeMemory: _s->cString
457
458
459
460
461
462
463
464

465
466
467
468
469
470

471
472
473
474
475
476
477
478
447
448
449
450
451
452
453

454


455
456
457

458

459
460
461
462
463
464
465







-
+
-
-



-
+
-







- (void)appendFormat: (OFConstantString*)format
	   arguments: (va_list)arguments
{
	char *UTF8String;
	int UTF8StringLength;

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

	if ((UTF8StringLength = of_vasprintf(&UTF8String, [format UTF8String],
	    arguments)) == -1)
		@throw [OFInvalidFormatException
		@throw [OFInvalidFormatException exception];
		    exceptionWithClass: [self class]];

	@try {
		[self appendUTF8String: UTF8String
				length: UTF8StringLength];
	} @finally {
		free(UTF8String);
	}
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
550
551
552
553
554
555
556
557
558
559
560
561

562
563
564
565
566
567
568
569
570
571
572

573
574
575
576
577
578
579
580
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

544

545
546
547
548
549
550
551
552
553

554

555
556
557
558
559
560
561







-
+
-




-
+
-














-
+
-














-
+
-
















-
+
-









-
+
-







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

		/* A start byte can't happen first as we reversed everything */
		if OF_UNLIKELY (_s->cString[i] & 0x40)
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];

		/* Next byte must not be ASCII */
		if OF_UNLIKELY (_s->cStringLength < i + 1 ||
		    !(_s->cString[i + 1] & 0x80))
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];

		/* Next byte is the start byte */
		if OF_LIKELY (_s->cString[i + 1] & 0x40) {
			_s->cString[i] ^= _s->cString[i + 1];
			_s->cString[i + 1] ^= _s->cString[i];
			_s->cString[i] ^= _s->cString[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if OF_UNLIKELY (_s->cStringLength < i + 2 ||
		    !(_s->cString[i + 2] & 0x80))
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];

		/* Second next byte is the start byte */
		if OF_LIKELY (_s->cString[i + 2] & 0x40) {
			_s->cString[i] ^= _s->cString[i + 2];
			_s->cString[i + 2] ^= _s->cString[i];
			_s->cString[i] ^= _s->cString[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if OF_UNLIKELY (_s->cStringLength < i + 3 ||
		    !(_s->cString[i + 3] & 0x80))
			@throw [OFInvalidEncodingException
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];

		/* Third next byte is the start byte */
		if OF_LIKELY (_s->cString[i + 3] & 0x40) {
			_s->cString[i] ^= _s->cString[i + 3];
			_s->cString[i + 3] ^= _s->cString[i];
			_s->cString[i] ^= _s->cString[i + 3];

			_s->cString[i + 1] ^= _s->cString[i + 2];
			_s->cString[i + 2] ^= _s->cString[i + 1];
			_s->cString[i + 1] ^= _s->cString[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */
		@throw [OFInvalidEncodingException
		@throw [OFInvalidEncodingException exception];
		    exceptionWithClass: [self class]];
	}
}

- (void)insertString: (OFString*)string
	     atIndex: (size_t)index
{
	size_t newCStringLength;

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

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

	newCStringLength = _s->cStringLength + [string UTF8StringLength];
	_s->hashed = false;
600
601
602
603
604
605
606
607

608
609
610
611
612
613
614
581
582
583
584
585
586
587

588
589
590
591
592
593
594
595







-
+








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

	if (range.length > SIZE_MAX - range.location || end > _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];
		@throw [OFOutOfRangeException exception];

	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);
	}
632
633
634
635
636
637
638
639

640
641
642
643
644
645
646
613
614
615
616
617
618
619

620
621
622
623
624
625
626
627







-
+







		      withString: (OFString*)replacement
{
	size_t start = range.location;
	size_t end = range.location + range.length;
	size_t newCStringLength, newLength;

	if (range.length > SIZE_MAX - range.location || end > _s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];
		@throw [OFOutOfRangeException exception];

	newLength = _s->length - range.length + [replacement length];

	if (_s->isUTF8) {
		start = of_string_utf8_get_position(_s->cString, start,
		    _s->cStringLength);
		end = of_string_utf8_get_position(_s->cString, end,
673
674
675
676
677
678
679
680

681
682
683
684
685
686
687
654
655
656
657
658
659
660

661
662
663
664
665
666
667
668







-
+







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

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > [self length])
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];
		@throw [OFOutOfRangeException exception];

	if (_s->isUTF8) {
		range.location = of_string_utf8_get_position(_s->cString,
		    range.location, _s->cStringLength);
		range.length = of_string_utf8_get_position(
		    _s->cString + range.location, range.length,
		    _s->cStringLength - range.location);