ObjFW  Diff

Differences From Artifact [0d95785aef]:

To Artifact [8129fb7234]:


77
78
79
80
81
82
83
84

85
86
87
88
89
90
91
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91







-
+








	if (!_s->isUTF8) {
		uint8_t t;
		const OFUnichar *const *table;

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

		_s->hashed = false;
		_s->hasHash = false;

		for (i = 0; i < _s->cStringLength; i++) {
			if (isStart)
				table = startTable;
			else
				table = middleTable;

174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
174
175
176
177
178
179
180

181
182
183
184
185
186
187
188







-
+







	}

	assert(j == newCStringLength);
	newCString[j] = 0;
	OFFreeMemory(unicodeString);

	OFFreeMemory(_s->cString);
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = newCString;
	_s->cStringLength = newCStringLength;

	/*
	 * Even though cStringLength can change, length cannot, therefore no
	 * need to change it.
	 */
201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
216
217
218
219
220

221
222
223
224
225
226
227
201
202
203
204
205
206
207

208
209
210
211
212
213
214
215
216
217
218
219

220
221
222
223
224
225
226
227







-
+











-
+







		    _s->cStringLength);

	if (idx >= _s->cStringLength)
		@throw [OFOutOfRangeException exception];

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

	if ((lenNew = OFUTF8StringEncode(character, buffer)) == 0)
		@throw [OFInvalidEncodingException exception];

	if ((lenOld = OFUTF8StringDecode(_s->cString + idx,
	    _s->cStringLength - idx, &c)) <= 0)
		@throw [OFInvalidEncodingException exception];

	_s->hashed = false;
	_s->hasHash = false;

	if (lenNew == (size_t)lenOld)
		memcpy(_s->cString + idx, buffer, lenNew);
	else if (lenNew > (size_t)lenOld) {
		_s->cString = OFResizeMemory(_s->cString,
		    _s->cStringLength - lenOld + lenNew + 1, 1);

271
272
273
274
275
276
277
278

279
280
281
282
283
284
285
271
272
273
274
275
276
277

278
279
280
281
282
283
284
285







-
+







	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, UTF8String,
	    UTF8StringLength + 1);

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

308
309
310
311
312
313
314
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314







-
+







	case 1:
		_s->isUTF8 = true;
		break;
	case -1:
		@throw [OFInvalidEncodingException exception];
	}

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength);

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

346
347
348
349
350
351
352
353

354
355
356
357
358
359
360
346
347
348
349
350
351
352

353
354
355
356
357
358
359
360







-
+







	size_t UTF8StringLength;

	if (string == nil)
		@throw [OFInvalidArgumentException exception];

	UTF8StringLength = string.UTF8StringLength;

	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString,
	    _s->cStringLength + UTF8StringLength + 1, 1);
	memcpy(_s->cString + _s->cStringLength, string.UTF8String,
	    UTF8StringLength);

	_s->cStringLength += UTF8StringLength;
	_s->length += string.length;
387
388
389
390
391
392
393
394

395
396
397
398
399
400
401
387
388
389
390
391
392
393

394
395
396
397
398
399
400
401







-
+







				isUTF8 = true;

			j += len;
		}

		tmp[j] = '\0';

		_s->hashed = false;
		_s->hasHash = false;
		_s->cString = OFResizeMemory(_s->cString,
		    _s->cStringLength + j + 1, 1);
		memcpy(_s->cString + _s->cStringLength, tmp, j + 1);

		_s->cStringLength += j;
		_s->length += length;

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







-
+







	}
}

- (void)reverse
{
	size_t i, j;

	_s->hashed = false;
	_s->hasHash = false;

	/* 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];
513
514
515
516
517
518
519
520

521
522
523
524
525
526
527
513
514
515
516
517
518
519

520
521
522
523
524
525
526
527







-
+







		@throw [OFOutOfRangeException exception];

	if (_s->isUTF8)
		idx = OFUTF8StringIndexToPosition(_s->cString, idx,
		    _s->cStringLength);

	newCStringLength = _s->cStringLength + string.UTF8StringLength;
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, 1);

	memmove(_s->cString + idx + string.UTF8StringLength,
	    _s->cString + idx, _s->cStringLength - idx);
	memcpy(_s->cString + idx, string.UTF8String,
	    string.UTF8StringLength);
	_s->cString[newCStringLength] = '\0';
550
551
552
553
554
555
556
557

558
559
560
561
562
563
564
550
551
552
553
554
555
556

557
558
559
560
561
562
563
564







-
+







		    _s->cStringLength);
		end = OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	memmove(_s->cString + start, _s->cString + end,
	    _s->cStringLength - end);
	_s->hashed = false;
	_s->hasHash = false;
	_s->length -= range.length;
	_s->cStringLength -= end - start;
	_s->cString[_s->cStringLength] = 0;

	@try {
		_s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1,
		    1);
587
588
589
590
591
592
593
594

595
596
597
598
599
600
601
587
588
589
590
591
592
593

594
595
596
597
598
599
600
601







-
+







		    _s->cStringLength);
		end = OFUTF8StringIndexToPosition(_s->cString, end,
		    _s->cStringLength);
	}

	newCStringLength = _s->cStringLength - (end - start) +
	    replacement.UTF8StringLength;
	_s->hashed = false;
	_s->hasHash = false;

	/*
	 * If the new string is bigger, we need to resize it first so we can
	 * memmove() the rest of the string to the end.
	 *
	 * We must not resize the string if the new string is smaller, because
	 * then we can't memmove() the rest of the string forward as the rest is
698
699
700
701
702
703
704
705

706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
753
698
699
700
701
702
703
704

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
753







-
+




















-
+



















-
+







	}
	memcpy(newCString + newCStringLength, _s->cString + last,
	    _s->cStringLength - last);
	newCStringLength += _s->cStringLength - last;
	newCString[newCStringLength] = 0;

	OFFreeMemory(_s->cString);
	_s->hashed = false;
	_s->hasHash = false;
	_s->cString = newCString;
	_s->cStringLength = newCStringLength;
	_s->length = newLength;

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
			_s->isUTF8 = true;
	} else
		_s->isUTF8 = true;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < _s->cStringLength; i++)
		if (!OFASCIIIsSpace(_s->cString[i]))
			break;

	_s->hashed = false;
	_s->hasHash = false;
	_s->cStringLength -= i;
	_s->length -= i;

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

	@try {
		_s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1,
		    1);
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

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

	_s->hashed = false;
	_s->hasHash = false;

	d = 0;
	for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) {
		if (!OFASCIIIsSpace(*p))
			break;

		*p = '\0';
766
767
768
769
770
771
772
773

774
775
776
777
778
779
780
766
767
768
769
770
771
772

773
774
775
776
777
778
779
780







-
+







}

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

	_s->hashed = false;
	_s->hasHash = false;

	d = 0;
	for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) {
		if (!OFASCIIIsSpace(*p))
			break;

		*p = '\0';