ObjFW  Diff

Differences From Artifact [7a74906b05]:

To Artifact [34b8bce34b]:


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
69
70
71
72
73

74
75
76
77



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

94
95

96
97

98
99

100
101
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
118
119

120
121

122
123
124
125
126
127
128


129
130
131
132
133



134
135
136

137
138

139
140
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


188
189
190
191



192
193
194

195
196
197
198
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
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
69
70
71
72

73
74



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

93
94

95
96

97
98

99
100
101
102
103
104
105
106
107
108

109
110
111
112
113
114
115
116
117
118

119
120

121
122
123
124
125
126


127
128
129
130



131
132
133
134
135

136
137

138
139



140
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
188



189
190
191
192
193

194
195
196
197
198
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







-
+

-
+




-
+


-
+











-
+

-
-
-
+
+
+















-
+

-
+

-
+

-
+









-
+









-
+

-
+





-
-
+
+


-
-
-
+
+
+


-
+

-
+

-
-
-
+
+
+


-
+










-
+

-
-
-
+
+
+


-
+

-
+

-
-
-
+
+
+


-
+







-
-
-
-
+
+
+
+


-
-
+
+

-
-
-
+
+
+


-
+







-
-
-
-
-
+
+
+
+
+


-
+

-
+


-
-
+
+


-
-
-
+
+
+
+




-
+

-
+

-
-
-
-
-
+
+
+
+
+


-
-
+
+

-
-
-
-
-
+
+
+
+
+








@implementation OFMutableString
- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)tableSize
{
	of_unichar_t c;
	of_unichar_t *unicodeString;
	size_t unicodeLen, newLength, cLen;
	size_t unicodeLen, newCStringLength, cLen;
	size_t i, j, d;
	char *newString;
	char *newCString;

	if (!s->isUTF8) {
		assert(tableSize >= 1);

		uint8_t *p = (uint8_t*)s->string + s->length;
		uint8_t *p = (uint8_t*)s->cString + s->cStringLength;
		uint8_t t;

		while (--p >= (uint8_t*)s->string)
		while (--p >= (uint8_t*)s->cString)
			if ((t = table[0][*p]) != 0)
				*p = t;

		return;
	}

	unicodeLen = [self length];
	unicodeString = [self allocMemoryForNItems: unicodeLen
					  withSize: sizeof(of_unichar_t)];

	i = j = 0;
	newLength = 0;
	newCStringLength = 0;

	while (i < s->length) {
		cLen = of_string_utf8_to_unicode(s->string + i, s->length - i,
		    &c);
	while (i < s->cStringLength) {
		cLen = of_string_utf8_to_unicode(s->cString + i,
		    s->cStringLength - i, &c);

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

		if (c >> 8 < tableSize) {
			of_unichar_t tc = table[c >> 8][c & 0xFF];

			if (tc)
				c = tc;
		}
		unicodeString[j++] = c;

		if (c < 0x80)
			newLength++;
			newCStringLength++;
		else if (c < 0x800)
			newLength += 2;
			newCStringLength += 2;
		else if (c < 0x10000)
			newLength += 3;
			newCStringLength += 3;
		else if (c < 0x110000)
			newLength += 4;
			newCStringLength += 4;
		else {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		i += cLen;
	}

	@try {
		newString = [self allocMemoryWithSize: newLength + 1];
		newCString = [self allocMemoryWithSize: newCStringLength + 1];
	} @catch (id e) {
		[self freeMemory: unicodeString];
		@throw e;
	}

	j = 0;

	for (i = 0; i < unicodeLen; i++) {
		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		    newString + j)) == 0) {
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException newWithClass: isa];
		}
		j += d;
	}

	assert(j == newLength);
	newString[j] = 0;
	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: s->string];
	s->string = newString;
	s->length = newLength;
	[self freeMemory: s->cString];
	s->cString = newCString;
	s->cStringLength = newCStringLength;
}

- (void)setToCString: (const char*)string
- (void)setToCString: (const char*)newCString
{
	size_t length = strlen(string);
	size_t newCStringLength = strlen(newCString);

	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	if (newCStringLength >= 3 && !memcmp(newCString, "\xEF\xBB\xBF", 3)) {
		newCString += 3;
		newCStringLength -= 3;
	}

	switch (of_string_check_utf8(string, length)) {
	switch (of_string_check_utf8(newCString, newCStringLength)) {
	case 0:
		s->isUTF8 = NO;
		break;
	case 1:
		s->isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	[self freeMemory: s->string];
	[self freeMemory: s->cString];

	s->length = length;
	s->string = [self allocMemoryWithSize: length + 1];
	memcpy(s->string, string, length + 1);
	s->cStringLength = newCStringLength;
	s->cString = [self allocMemoryWithSize: newCStringLength + 1];
	memcpy(s->cString, newCString, newCStringLength + 1);
}

- (void)appendCString: (const char*)string
- (void)appendCString: (const char*)cString
{
	size_t length = strlen(string);
	size_t cStringLength = strlen(cString);

	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
		cString += 3;
		cStringLength -= 3;
	}

	switch (of_string_check_utf8(string, length)) {
	switch (of_string_check_utf8(cString, cStringLength)) {
	case 1:
		s->isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length + 1);
	s->length += length;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength + cStringLength + 1];
	memcpy(s->cString + s->cStringLength, cString, cStringLength + 1);
	s->cStringLength += cStringLength;
}

- (void)appendCString: (const char*)string
	   withLength: (size_t)length
- (void)appendCString: (const char*)cString
	   withLength: (size_t)cStringLength
{
	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
		cString += 3;
		cStringLength -= 3;
	}

	switch (of_string_check_utf8(string, length)) {
	switch (of_string_check_utf8(cString, cStringLength)) {
	case 1:
		s->isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length);
	s->length += length;
	s->string[s->length] = 0;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength + cStringLength + 1];
	memcpy(s->cString + s->cStringLength, cString, cStringLength);
	s->cStringLength += cStringLength;
	s->cString[s->cStringLength] = 0;
}

- (void)appendCString: (const char*)string
- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)length
	       length: (size_t)cStringLength
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCString: string
			 withLength: length];
		[self appendCString: cString
			 withLength: cStringLength];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString: [OFString stringWithCString: string
						       encoding: encoding
							 length: length]];
		[self appendString:
		    [OFString stringWithCString: cString
				       encoding: encoding
					 length: cStringLength]];
		[pool release];
	}
}

- (void)appendCStringWithoutUTF8Checking: (const char*)string
- (void)appendCStringWithoutUTF8Checking: (const char*)cString
{
	size_t length;
	size_t cStringLength;

	length = strlen(string);
	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length + 1);
	s->length += length;
	cStringLength = strlen(cString);
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength + cStringLength + 1];
	memcpy(s->cString + s->cStringLength, cString, cStringLength + 1);
	s->cStringLength += cStringLength;
}

- (void)appendCStringWithoutUTF8Checking: (const char*)string
				  length: (size_t)length
- (void)appendCStringWithoutUTF8Checking: (const char*)cString
				  length: (size_t)cStringLength
{
	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length);
	s->length += length;
	s->string[s->length] = 0;
	s->cString = [self resizeMemory: s->cString
				 toSize: s->cStringLength + cStringLength + 1];
	memcpy(s->cString + s->cStringLength, cString, cStringLength);
	s->cStringLength += cStringLength;
	s->cString[s->cStringLength] = 0;
}

- (void)appendString: (OFString*)string
{
	if (string == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
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
298

299
300
301
302




303
304
305
306

307
308
309
310

311
312

313
314
315
316
317


318
319
320
321
322
323
324



325
326
327
328
329
330
331
332




333
334
335
336
337
338
339
340
341



342
343
344
345
346
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
383
384
385
386
387
388
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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459

460
461
462
463
464




465
466
467
468
469
470
471

472
473

474
475
476



477
478
479
480



481
482

483
484

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
562
563


564
565
566
567
568
569
570
571
572
573
574
575
576

577
578
579
580
581
582
583
584

585
586
587
588


589
590
591
592
593
594
595
596
597
598
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
629
630
631
632
633
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

298
299
300
301




302
303
304
305
306
307
308

309
310
311
312

313
314

315
316
317
318


319
320
321
322
323
324



325
326
327
328
329
330
331




332
333
334
335
336
337
338
339
340
341



342
343
344
345
346
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
383

384
385
386
387
388
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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

463
464
465



466
467
468
469
470
471
472
473
474
475

476
477
478
479



480
481
482
483



484
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



562
563
564
565
566



567
568
569
570
571


572
573
574
575
576
577
578
579
580
581
582
583
584
585

586
587
588
589
590
591
592
593

594
595
596


597
598
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
629
630


631
632
633
634
635
636
637
638
639
640
641
642
643







-
-
+
+





+
-
+



-
-
+
+

-
+











-
+

-
+


+
-
-
-
-
+
+
+
+



-
+



-
+

-
+



-
-
+
+




-
-
-
+
+
+




-
-
-
-
+
+
+
+






-
-
-
+
+
+




-
-
-
-
+
+
+
+






-
-
-
+
+
+




-
-
-
-
+
+
+
+

-
-
-
+
+
+






-
+



-
+

















-
+


-
-
+
+

-
+


-
-
-
+
+
+

-
-
-
-
+
+
+
+

-
+






-
-
-
+
+
+
+






-
+


-
-
-
+
+
+


-
-
+
+
















-
+


-
-
-
+
+
+
+






-
+


+
-
-
-
+
+
+

-
-
-
+
+
+

-
+

-
+















-
-
-
-
+
+
+
+

-
+


-
-
+
+

-
-
+
+



+
-
-
-
+
+
+

-
+


+
-
-
-
-
-
+
+
+
+
+




+
-
-
-
+
+
+

-
+


+
-
-
-
+
+
+

-
-
-
+
+
+






-
-
-
+
+
+


-
-
-
+
+
+


-
-
+
+












-
+







-
+


-
-
+
+












-
+







-
+

-
-
-
+
+
+


-
-
-
+
+
+


-
-
+
+











	     withArguments: arguments];
	va_end(arguments);
}

- (void)appendFormat: (OFConstantString*)format
       withArguments: (va_list)arguments
{
	char *t;
	int length;
	char *cString;
	int cStringLength;

	if (format == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((cStringLength = of_vasprintf(&cString, [format cString],
	if ((length = of_vasprintf(&t, [format cString], arguments)) == -1)
	    arguments)) == -1)
		@throw [OFInvalidFormatException newWithClass: isa];

	@try {
		[self appendCString: t
			 withLength: length];
		[self appendCString: cString
			 withLength: cStringLength];
	} @finally {
		free(t);
		free(cString);
	}
}

- (void)prependString: (OFString*)string
{
	return [self insertString: string
			  atIndex: 0];
}

- (void)reverse
{
	size_t i, j, length = s->length / 2;
	size_t i, j;

	madvise(s->string, s->length, MADV_SEQUENTIAL);
	madvise(s->cString, s->cStringLength, MADV_SEQUENTIAL);

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = s->cStringLength - 1; i < s->cStringLength / 2;
	for (i = 0, j = s->length - 1; i < length; i++, j--) {
		s->string[i] ^= s->string[j];
		s->string[j] ^= s->string[i];
		s->string[i] ^= s->string[j];
	    i++, j--) {
		s->cString[i] ^= s->cString[j];
		s->cString[j] ^= s->cString[i];
		s->cString[i] ^= s->cString[j];
	}

	if (!s->isUTF8) {
		madvise(s->string, s->length, MADV_NORMAL);
		madvise(s->cString, s->cStringLength, MADV_NORMAL);
		return;
	}

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

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(s->string[i] & 0x40)) {
			madvise(s->string, s->length, MADV_NORMAL);
		if (OF_UNLIKELY(s->cString[i] & 0x40)) {
			madvise(s->cString, s->cStringLength, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(s->length < i + 1 ||
		    !(s->string[i + 1] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
		if (OF_UNLIKELY(s->cStringLength < i + 1 ||
		    !(s->cString[i + 1] & 0x80))) {
			madvise(s->cString, s->cStringLength, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte is the start byte */
		if (OF_LIKELY(s->string[i + 1] & 0x40)) {
			s->string[i] ^= s->string[i + 1];
			s->string[i + 1] ^= s->string[i];
			s->string[i] ^= s->string[i + 1];
		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->length < i + 2 ||
		    !(s->string[i + 2] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
		if (OF_UNLIKELY(s->cStringLength < i + 2 ||
		    !(s->cString[i + 2] & 0x80))) {
			madvise(s->cString, s->cStringLength, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(s->string[i + 2] & 0x40)) {
			s->string[i] ^= s->string[i + 2];
			s->string[i + 2] ^= s->string[i];
			s->string[i] ^= s->string[i + 2];
		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->length < i + 3 ||
		    !(s->string[i + 3] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
		if (OF_UNLIKELY(s->cStringLength < i + 3 ||
		    !(s->cString[i + 3] & 0x80))) {
			madvise(s->cString, s->cStringLength, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(s->string[i + 3] & 0x40)) {
			s->string[i] ^= s->string[i + 3];
			s->string[i + 3] ^= s->string[i];
			s->string[i] ^= s->string[i + 3];
		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->string[i + 1] ^= s->string[i + 2];
			s->string[i + 2] ^= s->string[i + 1];
			s->string[i + 1] ^= s->string[i + 2];
			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 */
		madvise(s->string, s->length, MADV_NORMAL);
		madvise(s->cString, s->cStringLength, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	madvise(s->string, s->length, MADV_NORMAL);
	madvise(s->cString, s->cStringLength, MADV_NORMAL);
}

- (void)upper
{
	[self _applyTable: of_unicode_upper_table
		 withSize: OF_UNICODE_UPPER_TABLE_SIZE];
}

- (void)lower
{
	[self _applyTable: of_unicode_lower_table
		 withSize: OF_UNICODE_LOWER_TABLE_SIZE];
}

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

	if (s->isUTF8)
		index = of_string_index_to_position(s->string, index,
		    s->length);
		index = of_string_index_to_position(s->cString, index,
		    s->cStringLength);

	if (index > s->length)
	if (index > s->cStringLength)
		@throw [OFOutOfRangeException newWithClass: isa];

	newLength = s->length + [string cStringLength];
	s->string = [self resizeMemory: s->string
				toSize: newLength + 1];
	newCStringLength = s->cStringLength + [string cStringLength];
	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->string + index + [string cStringLength], s->string + index,
	    s->length - index);
	memcpy(s->string + index, [string cString], [string cStringLength]);
	s->string[newLength] = '\0';
	memmove(s->cString + index + [string cStringLength], s->cString + index,
	    s->cStringLength - index);
	memcpy(s->cString + index, [string cString], [string cStringLength]);
	s->cString[newCStringLength] = '\0';

	s->length = newLength;
	s->cStringLength = newCStringLength;
}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (s->isUTF8) {
		start = of_string_index_to_position(s->string, start,
		    s->length);
		end = of_string_index_to_position(s->string, end, s->length);
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (end > s->length)
	if (end > s->cStringLength)
		@throw [OFOutOfRangeException newWithClass: isa];

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

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

- (void)deleteCharactersInRange: (of_range_t)range
{
	[self deleteCharactersFromIndex: range.start
				toIndex: range.start + range.length];
}

- (void)replaceCharactersFromIndex: (size_t)start
			   toIndex: (size_t)end
			withString: (OFString*)replacement
{
	size_t newLength;
	size_t newCStringLength;

	if (s->isUTF8) {
		start = of_string_index_to_position(s->string, start,
		    s->length);
		end = of_string_index_to_position(s->string, end, s->length);
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (end > s->length)
	if (end > s->cStringLength)
		@throw [OFOutOfRangeException newWithClass: isa];

	newCStringLength = s->cStringLength - (end - start) +
	newLength = s->length - (end - start) + [replacement cStringLength];
	s->string = [self resizeMemory: s->string
				toSize: newLength + 1];
	    [replacement cStringLength];
	s->cString = [self resizeMemory: s->cString
				 toSize: newCStringLength + 1];

	memmove(s->string + end, s->string + start +
	    [replacement cStringLength], s->length - end);
	memcpy(s->string + start, [replacement cString],
	memmove(s->cString + end, s->cString + start +
	    [replacement cStringLength], s->cStringLength - end);
	memcpy(s->cString + start, [replacement cString],
	    [replacement cStringLength]);
	s->string[newLength] = '\0';
	s->cString[newCStringLength] = '\0';

	s->length = newLength;
	s->cStringLength = newCStringLength;
}

- (void)replaceCharactersInRange: (of_range_t)range
		      withString: (OFString*)replacement
{
	[self replaceCharactersFromIndex: range.start
				 toIndex: range.start + range.length
			      withString: replacement];
}

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
{
	const char *cString = [string cString];
	const char *replacementCString = [replacement cString];
	size_t stringLen = [string cStringLength];
	size_t replacementLen = [replacement cStringLength];
	size_t i, last, newLength;
	char *newString;
	size_t cStringLength = [string cStringLength];
	size_t replacementCStringLength = [replacement cStringLength];
	size_t i, last, newCStringLength;
	char *newCString;

	if (stringLen > s->length)
	if (cStringLength > s->cStringLength)
		return;

	newString = NULL;
	newLength = 0;
	newCString = NULL;
	newCStringLength = 0;

	for (i = 0, last = 0; i <= s->length - stringLen; i++) {
		if (memcmp(s->string + i, cString, stringLen))
	for (i = 0, last = 0; i <= s->cStringLength - cStringLength; i++) {
		if (memcmp(s->cString + i, cString, cStringLength))
			continue;

		@try {
			newCString = [self
			newString = [self resizeMemory: newString
						toSize: newLength + i - last +
							replacementLen + 1];
			    resizeMemory: newCString
				  toSize: newCStringLength + i - last +
					  replacementCStringLength + 1];
		} @catch (id e) {
			[self freeMemory: newString];
			[self freeMemory: newCString];
			@throw e;
		}
		memcpy(newCString + newCStringLength, s->cString + last,
		memcpy(newString + newLength, s->string + last, i - last);
		memcpy(newString + newLength + i - last, replacementCString,
		    replacementLen);
		newLength += i - last + replacementLen;
		i += stringLen - 1;
		    i - last);
		memcpy(newCString + newCStringLength + i - last,
		    replacementCString, replacementCStringLength);
		newCStringLength += i - last + replacementCStringLength;
		i += cStringLength - 1;
		last = i + 1;
	}

	@try {
		newCString = [self
		newString = [self resizeMemory: newString
					toSize: newLength + s->length - last +
						1];
		    resizeMemory: newCString
			  toSize: newCStringLength +
				  s->cStringLength - last + 1];
	} @catch (id e) {
		[self freeMemory: newString];
		[self freeMemory: newCString];
		@throw e;
	}
	memcpy(newCString + newCStringLength, s->cString + last,
	memcpy(newString + newLength, s->string + last, s->length - last);
	newLength += s->length - last;
	newString[newLength] = 0;
	    s->cStringLength - last);
	newCStringLength += s->cStringLength - last;
	newCString[newCStringLength] = 0;

	[self freeMemory: s->string];
	s->string = newString;
	s->length = newLength;
	[self freeMemory: s->cString];
	s->cString = newCString;
	s->cStringLength = newCStringLength;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < s->length; i++)
		if (s->string[i] != ' '  && s->string[i] != '\t' &&
		    s->string[i] != '\n' && s->string[i] != '\r')
	for (i = 0; i < s->cStringLength; i++)
		if (s->cString[i] != ' '  && s->cString[i] != '\t' &&
		    s->cString[i] != '\n' && s->cString[i] != '\r')
			break;

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

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

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

	d = 0;
	for (p = s->string + s->length - 1; p >= s->string; p--) {
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	s->length -= d;
	s->cStringLength -= d;

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

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

	d = 0;
	for (p = s->string + s->length - 1; p >= s->string; p--) {
	for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	s->length -= d;
	s->cStringLength -= d;

	for (i = 0; i < s->length; i++)
		if (s->string[i] != ' '  && s->string[i] != '\t' &&
		    s->string[i] != '\n' && s->string[i] != '\r')
	for (i = 0; i < s->cStringLength; i++)
		if (s->cString[i] != ' '  && s->cString[i] != '\t' &&
		    s->cString[i] != '\n' && s->cString[i] != '\r')
			break;

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

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

- copy
{
	return [[OFString alloc] initWithString: self];
}
@end