ObjFW  Diff

Differences From Artifact [8b9a1eb927]:

To Artifact [3c8f4c2bc5]:


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
252
253
254
255
256
257
258
259
260
261
	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: s->cString];
	s->cString = newCString;
	s->cStringLength = newCStringLength;





}

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


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

	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->cString];

	s->cStringLength = newCStringLength;


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

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


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

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

	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*)cString
	   withLength: (size_t)cStringLength
{


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

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

	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*)cString
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)cStringLength
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCString: cString
			 withLength: cStringLength];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString:
		    [OFString stringWithCString: cString
				       encoding: encoding
					 length: cStringLength]];
		[pool release];
	}
}

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

	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*)cString
				  length: (size_t)cStringLength
{
	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];

	[self appendCStringWithoutUTF8Checking: [string cString]
					length: [string cStringLength]];

	if (string->s->isUTF8)
		s->isUTF8 = YES;
}

- (void)appendFormat: (OFConstantString*)format, ...
{







>
>
>
>
>





>






|
>













>
>







>






|










>

>





>
>





|










>

>
>




















|



|
|
|
<
<
|
<
<
|
|


|
|
<
|
|
<
<
|
<
<

<
|







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
252
253
254
255
256

257
258


259


260

261
262
263
264
265
266
267
268
	assert(j == newCStringLength);
	newCString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: s->cString];
	s->cString = newCString;
	s->cStringLength = newCStringLength;

	/*
	 * Even though cStringLength can change, length can not, therefore no
	 * need to change it.
	 */
}

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

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

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

	[self freeMemory: s->cString];

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

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

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

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

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

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

	s->cStringLength += cStringLength;
	s->length += length;
}

- (void)appendCString: (const char*)cString
	   withLength: (size_t)cStringLength
{
	size_t length;

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

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

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

	s->cStringLength += cStringLength;
	s->length += length;

	s->cString[s->cStringLength] = 0;
}

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

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

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





	cStringLength = [string cStringLength];

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


	s->cStringLength += cStringLength;


	s->length += string->s->length;




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

	if (string->s->isUTF8)
		s->isUTF8 = YES;
}

- (void)appendFormat: (OFConstantString*)format, ...
{
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
}

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




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

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

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

	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->cStringLength = newCStringLength;

}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{









	if (s->isUTF8) {
		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->cStringLength)
		@throw [OFOutOfRangeException newWithClass: isa];

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

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];







>
>
>




<
<
<




|
|
|
>



>





>
>
>
>
>
>
>
>
>







<
<
<
<
<
<
<







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
}

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

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

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




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

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

	s->cStringLength = newCStringLength;
	s->length += string->s->length;
}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

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

	s->length -= end - start;

	if (s->isUTF8) {
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_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;

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
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
				toIndex: range.start + range.length];
}

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










	if (s->isUTF8) {
		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->cStringLength)
		@throw [OFOutOfRangeException newWithClass: isa];

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

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

	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 cStringLength = [string cStringLength];
	size_t replacementCStringLength = [replacement cStringLength];
	size_t i, last, newCStringLength;
	char *newCString;

	if (cStringLength > s->cStringLength)
		return;

	newCString = NULL;
	newCStringLength = 0;


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

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

		newCStringLength += i - last + replacementCStringLength;



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

	@try {
		newCString = [self
		    resizeMemory: newCString







|
>
>
>
>
>
>
>
>
>








<
<
<
<
<
<
<

|




|
|
|



>















|
|
|







>


















>

>
>
>







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
				toIndex: range.start + range.length];
}

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

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

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

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

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








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

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

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

- (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 cStringLength = string->s->cStringLength;
	size_t replacementCStringLength = replacement->s->cStringLength;
	size_t i, last, newCStringLength, newLength;
	char *newCString;

	if (cStringLength > s->cStringLength)
		return;

	newCString = NULL;
	newCStringLength = 0;
	newLength = s->length;

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

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

		newCStringLength += i - last + replacementCStringLength;
		newLength = newLength - string->s->length +
		    replacement->s->length;

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

	@try {
		newCString = [self
		    resizeMemory: newCString
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
	    s->cStringLength - last);
	newCStringLength += s->cStringLength - last;
	newCString[newCStringLength] = 0;

	[self freeMemory: s->cString];
	s->cString = newCString;
	s->cStringLength = newCStringLength;

}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	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->cStringLength -= i;


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

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {







>












>
>







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
	    s->cStringLength - last);
	newCStringLength += s->cStringLength - last;
	newCString[newCStringLength] = 0;

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

- (void)deleteLeadingWhitespaces
{
	size_t i;

	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->cStringLength -= i;
	s->length -= i;

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

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {
592
593
594
595
596
597
598

599
600
601
602
603
604
605
			break;

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

	s->cStringLength -= d;


	@try {
		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];







>







614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
			break;

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

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

	@try {
		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];
617
618
619
620
621
622
623

624
625
626
627
628
629
630


631
632
633
634
635
636
637
			break;

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

	s->cStringLength -= d;


	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->cStringLength -= i;


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

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {







>







>
>







640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
			break;

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

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

	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->cStringLength -= i;
	s->length -= i;

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

	@try {
		s->cString = [self resizeMemory: s->cString
					 toSize: s->cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {