ObjFW  Diff

Differences From Artifact [e0a807cffb]:

To Artifact [e8d0af35fc]:


111
112
113
114
115
116
117
118

119
120
121
122
123
124
125
111
112
113
114
115
116
117

118
119
120
121
122
123
124
125







-
+







			table = startTable;
			tableSize = middleTableSize;
		} else {
			table = middleTable;
			tableSize = middleTableSize;
		}

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

		if (cLen == 0 || c > 0x10FFFF) {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
169
170
171
172
173
174
175

176
177
178
179
180
181
182
183







-
+







	}

	j = 0;

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

		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		if ((d = of_string_utf8_encode(unicodeString[i],
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
		j += d;
199
200
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
228
229
230
231
232
233


234
235
236

237
238
239
240


241
242
243
244
245




246
247
248


249
250
251
252
253

254
255
256
257
258
259
260
261




262
263
264


265
266
267
268
269
270
271
272
273

274
275
276
277

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

291
292
293
294
295
296
297
199
200
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
228
229
230
231
232


233
234



235

236


237
238
239




240
241
242
243
244


245
246
247
248
249
250

251








252
253
254
255
256


257
258
259
260
261
262
263
264
265
266

267




268
269
270
271
272
273
274
275
276
277
278
279
280

281
282
283
284
285
286
287
288







-
+


-
+





+






-
+



-
+






-
-
+
+
-
-
-
+
-

-
-
+
+

-
-
-
-
+
+
+
+

-
-
+
+




-
+
-
-
-
-
-
-
-
-
+
+
+
+

-
-
+
+








-
+
-
-
-
-
+












-
+







}

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

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		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 */
	if (!(character & 0x80) && !(s->cString[index] & 0x80)) {
		s->hashed = NO;
		s->cString[index] = character;
		return;
	}

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

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

	s->hashed = NO;

	if (length == oldLength) {
		memcpy(s->cString + index, buffer, length);
	if (lenNew == lenOld)
		memcpy(s->cString + index, buffer, lenNew);
		return;
	}

	else if (lenNew > lenOld) {
	if (length > oldLength) {
		s->cString = [self resizeMemory: s->cString
					   size: s->cStringLength - oldLength +
						 length + 1];
					   size: s->cStringLength -
						 lenOld + lenNew + 1];

		memmove(s->cString + index + length,
		    s->cString + index + oldLength,
		    s->cStringLength - index - oldLength);
		memcpy(s->cString + index, buffer, length);
		memmove(s->cString + index + lenNew,
		    s->cString + index + lenOld,
		    s->cStringLength - index - lenOld);
		memcpy(s->cString + index, buffer, lenNew);

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

		if (character & 0x80)
			s->UTF8 = YES;

	} else if (lenNew < lenOld) {
		return;
	}

	if (length < oldLength) {
		memmove(s->cString + index + length,
		    s->cString + index + oldLength,
		    s->cStringLength - index - oldLength);
		memcpy(s->cString + index, buffer, length);
		memmove(s->cString + index + lenNew,
		    s->cString + index + lenOld,
		    s->cStringLength - index - lenOld);
		memcpy(s->cString + index, buffer, lenNew);

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

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: s->cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't really care, as we only made it smaller */
		}

	} else
		return;
	}

	assert(0);
		assert(0);
}

- (void)appendUTF8String: (const char*)UTF8String
{
	size_t UTF8StringLength = strlen(UTF8String);
	size_t length;

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

	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}
312
313
314
315
316
317
318
319

320
321
322
323
324
325
326
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317







-
+







	size_t length;

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

	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
	switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) {
	case 1:
		s->UTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}
508
509
510
511
512
513
514
515

516
517
518
519
520
521
522
499
500
501
502
503
504
505

506
507
508
509
510
511
512
513







-
+







	size_t newCStringLength;

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

	if (s->UTF8)
		index = of_string_index_to_position(s->cString, index,
		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];

545
546
547
548
549
550
551
552

553
554

555
556
557
558
559
560
561
536
537
538
539
540
541
542

543
544

545
546
547
548
549
550
551
552







-
+

-
+







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

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

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

	memmove(s->cString + start, s->cString + end, s->cStringLength - end);
	s->cStringLength -= end - start;
	s->cString[s->cStringLength] = 0;

576
577
578
579
580
581
582
583

584
585

586
587
588
589
590
591
592
567
568
569
570
571
572
573

574
575

576
577
578
579
580
581
582
583







-
+

-
+








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

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

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

	newCStringLength = s->cStringLength - (end - start) +
	    [replacement UTF8StringLength];
	s->hashed = NO;
	s->cString = [self resizeMemory: s->cString
610
611
612
613
614
615
616
617

618
619

620
621
622
623
624
625
626
601
602
603
604
605
606
607

608
609

610
611
612
613
614
615
616
617







-
+

-
+







	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) {
		range.start = of_string_index_to_position(s->cString,
		range.start = of_string_utf8_get_position(s->cString,
		    range.start, s->cStringLength);
		range.length = of_string_index_to_position(s->cString,
		range.length = of_string_utf8_get_position(s->cString,
		    range.start + range.length, s->cStringLength) - range.start;
	}

	if (range.start + range.length > [self UTF8StringLength])
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if ([string UTF8StringLength] > range.length)