ObjFW  Diff

Differences From Artifact [bfc8f3ea93]:

To Artifact [de80a71b10]:


42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
42
43
44
45
46
47
48

49
50
51
52
53
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68







-
+











-
+








- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
			    freeWhenDone: (bool)freeWhenDone
{
	self = [self initWithUTF8String: UTF8String];

	if (freeWhenDone)
		free(UTF8String);
		OFFreeMemory(UTF8String);

	return self;
}

- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
				  length: (size_t)UTF8StringLength
			    freeWhenDone: (bool)freeWhenDone
{
	self = [self initWithUTF8String: UTF8String length: UTF8StringLength];

	if (freeWhenDone)
		free(UTF8String);
		OFFreeMemory(UTF8String);

	return self;
}

#ifdef OF_HAVE_UNICODE_TABLES
- (void)of_convertWithWordStartTable: (const OFUnichar *const [])startTable
		     wordMiddleTable: (const OFUnichar *const [])middleTable
95
96
97
98
99
100
101
102

103
104
105
106
107
108
109
95
96
97
98
99
100
101

102
103
104
105
106
107
108
109







-
+







				_s->cString[i] = t;
		}

		return;
	}

	unicodeLen = self.length;
	unicodeString = of_alloc(unicodeLen, sizeof(OFUnichar));
	unicodeString = OFAllocMemory(unicodeLen, sizeof(OFUnichar));

	i = j = 0;
	newCStringLength = 0;

	while (i < _s->cStringLength) {
		const OFUnichar *const *table;
		size_t tableSize;
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132







-
+







			tableSize = middleTableSize;
		}

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

		if (cLen <= 0 || c > 0x10FFFF) {
			free(unicodeString);
			OFFreeMemory(unicodeString);
			@throw [OFInvalidEncodingException exception];
		}

		isStart = of_ascii_isspace(c);

		if (c >> 8 < tableSize) {
			OFUnichar tc = table[c >> 8][c & 0xFF];
141
142
143
144
145
146
147
148

149
150
151
152
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
180

181
182
183
184
185
186
187
141
142
143
144
145
146
147

148
149
150
151
152
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

180
181
182
183
184
185
186
187







-
+







-
+

-
+










-
-
+
+







-
+

-
+







		else if (c < 0x800)
			newCStringLength += 2;
		else if (c < 0x10000)
			newCStringLength += 3;
		else if (c < 0x110000)
			newCStringLength += 4;
		else {
			free(unicodeString);
			OFFreeMemory(unicodeString);
			@throw [OFInvalidEncodingException exception];
		}

		i += cLen;
	}

	@try {
		newCString = of_alloc(newCStringLength + 1, 1);
		newCString = OFAllocMemory(newCStringLength + 1, 1);
	} @catch (id e) {
		free(unicodeString);
		OFFreeMemory(unicodeString);
		@throw e;
	}

	j = 0;

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

		if ((d = of_string_utf8_encode(unicodeString[i],
		    newCString + j)) == 0) {
			free(unicodeString);
			free(newCString);
			OFFreeMemory(unicodeString);
			OFFreeMemory(newCString);
			@throw [OFInvalidEncodingException exception];
		}
		j += d;
	}

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

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

	/*
	 * Even though cStringLength can change, length cannot, therefore no
	 * need to change it.
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
218
219
220
221
222
223
224

225
226
227
228
229
230
231
232







-
+







		@throw [OFInvalidEncodingException exception];

	_s->hashed = false;

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

		memmove(_s->cString + idx + lenNew, _s->cString + idx + lenOld,
		    _s->cStringLength - idx - lenOld);
		memcpy(_s->cString + idx, buffer, lenNew);

		_s->cStringLength -= lenOld;
244
245
246
247
248
249
250
251

252
253
254
255
256
257
258
244
245
246
247
248
249
250

251
252
253
254
255
256
257
258







-
+







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

		if (character >= 0x80)
			_s->isUTF8 = true;

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

272
273
274
275
276
277
278
279

280
281
282
283
284
285
286
272
273
274
275
276
277
278

279
280
281
282
283
284
285
286







-
+







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

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

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

309
310
311
312
313
314
315
301
302
303
304
305
306
307

308
309
310
311
312
313
314
315







-
+







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

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

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

	_s->cString[_s->cStringLength] = 0;
347
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

373
374
375

376
377
378
379
380
381
382
347
348
349
350
351
352
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

372

373

374
375
376
377
378
379
380
381







-
+

















-
+
-

-
+








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

	UTF8StringLength = string.UTF8StringLength;

	_s->hashed = false;
	_s->cString = of_realloc(_s->cString,
	_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;

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

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

- (void)appendCharacters: (const OFUnichar *)characters
- (void)appendCharacters: (const OFUnichar *)characters length: (size_t)length
		  length: (size_t)length
{
	char *tmp = of_alloc((length * 4) + 1, 1);
	char *tmp = OFAllocMemory((length * 4) + 1, 1);

	@try {
		size_t j = 0;
		bool isUTF8 = false;

		for (size_t i = 0; i < length; i++) {
			size_t len = of_string_utf8_encode(characters[i],
390
391
392
393
394
395
396
397

398
399
400
401
402
403
404
405
406
407

408
409
410
411
412
413
414
389
390
391
392
393
394
395

396
397
398
399
400
401
402
403
404
405

406
407
408
409
410
411
412
413







-
+









-
+








			j += len;
		}

		tmp[j] = '\0';

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

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

		if (isUTF8)
			_s->isUTF8 = true;
	} @finally {
		free(tmp);
		OFFreeMemory(tmp);
	}
}

- (void)appendFormat: (OFConstantString *)format arguments: (va_list)arguments
{
	char *UTF8String;
	int UTF8StringLength;
516
517
518
519
520
521
522
523

524
525
526
527
528
529
530
515
516
517
518
519
520
521

522
523
524
525
526
527
528
529







-
+








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

	newCStringLength = _s->cStringLength + string.UTF8StringLength;
	_s->hashed = false;
	_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
	_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';

558
559
560
561
562
563
564
565


566
567
568
569
570
571
572
557
558
559
560
561
562
563

564
565
566
567
568
569
570
571
572







-
+
+







	    _s->cStringLength - end);
	_s->hashed = false;
	_s->length -= range.length;
	_s->cStringLength -= end - start;
	_s->cString[_s->cStringLength] = 0;

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

- (void)replaceCharactersInRange: (OFRange)range
		      withString: (OFString *)replacement
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
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







-
+
+












-
+
+







	 * 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
	 * lost due to the resize!
	 */
	if (newCStringLength > _s->cStringLength)
		_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
		_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1,
		    1);

	memmove(_s->cString + start + replacement.UTF8StringLength,
	    _s->cString + end, _s->cStringLength - end);
	memcpy(_s->cString + start, replacement.UTF8String,
	    replacement.UTF8StringLength);
	_s->cString[newCStringLength] = '\0';

	/*
	 * If the new string is smaller, we can safely resize it now as we're
	 * done with memmove().
	 */
	if (newCStringLength < _s->cStringLength)
		_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
		_s->cString = OFResizeMemory(_s->cString, newCStringLength + 1,
		    1);

	_s->cStringLength = newCStringLength;
	_s->length = newLength;

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
		if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
665
666
667
668
669
670
671
672

673
674
675
676

677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692

693
694
695

696
697
698
699
700
701
702
703

704
705
706
707
708
709
710
667
668
669
670
671
672
673

674
675
676
677

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

694
695
696

697
698
699
700
701
702
703
704

705
706
707
708
709
710
711
712







-
+



-
+















-
+


-
+







-
+







	last = 0;

	for (size_t i = range.location; i <= range.length - searchLength; i++) {
		if (memcmp(_s->cString + i, searchString, searchLength) != 0)
			continue;

		@try {
			newCString = of_realloc(newCString,
			newCString = OFResizeMemory(newCString,
			    newCStringLength + i - last + replacementLength + 1,
			    1);
		} @catch (id e) {
			free(newCString);
			OFFreeMemory(newCString);
			@throw e;
		}
		memcpy(newCString + newCStringLength, _s->cString + last,
		    i - last);
		memcpy(newCString + newCStringLength + i - last,
		    replacementString, replacementLength);

		newCStringLength += i - last + replacementLength;
		newLength = newLength - string.length + replacement.length;

		i += searchLength - 1;
		last = i + 1;
	}

	@try {
		newCString = of_realloc(newCString,
		newCString = OFResizeMemory(newCString,
		    newCStringLength + _s->cStringLength - last + 1, 1);
	} @catch (id e) {
		free(newCString);
		OFFreeMemory(newCString);
		@throw e;
	}
	memcpy(newCString + newCStringLength, _s->cString + last,
	    _s->cStringLength - last);
	newCStringLength += _s->cStringLength - last;
	newCString[newCStringLength] = 0;

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

	if ([replacement isKindOfClass: [OFUTF8String class]] ||
	    [replacement isKindOfClass: [OFMutableUTF8String class]]) {
726
727
728
729
730
731
732
733


734
735
736
737
738
739
740
728
729
730
731
732
733
734

735
736
737
738
739
740
741
742
743







-
+
+







	_s->cStringLength -= i;
	_s->length -= i;

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

	@try {
		_s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1);
		_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
{
752
753
754
755
756
757
758
759


760
761
762
763
764
765
766
755
756
757
758
759
760
761

762
763
764
765
766
767
768
769
770







-
+
+







		d++;
	}

	_s->cStringLength -= d;
	_s->length -= d;

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

- (void)deleteEnclosingWhitespaces
{
788
789
790
791
792
793
794
795


796
797
798
799
800
801
802
803
804
805
792
793
794
795
796
797
798

799
800
801
802
803
804
805
806
807
808
809
810







-
+
+










	_s->cStringLength -= i;
	_s->length -= i;

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

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

- (void)makeImmutable
{
	object_setClass(self, [OFUTF8String class]);
}
@end