ObjFW  Diff

Differences From Artifact [f3abcc1339]:

To Artifact [2744e572a4]:


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







-
+

-
+


-
-
-
+
+
+


-
+








-
-
+
+
+

-
+



-
-
+
+



-
-
-
+
+
+


-
+








-
-
+
+
+

-
+







+
+
+
+
+
+
+
+



-
-
+
+












-
+





-
+


-
+
+

-
+

-
+








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

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

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

	switch (of_string_check_utf8(cString, cStringLength, &length)) {
	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &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);
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1);

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

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

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

	switch (of_string_check_utf8(cString, cStringLength, &length)) {
	switch (of_string_check_utf8(UTF8String, UTF8StringLength, &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);
				 toSize: s->cStringLength +
					 UTF8StringLength + 1];
	memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength);

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

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

- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding
{
	return [self appendCString: cString
		      withEncoding: encoding
			    length: strlen(cString)];
}

- (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];
		[self appendUTF8String: 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;
	size_t UTF8StringLength;

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

	cStringLength = [string cStringLength];
	UTF8StringLength = [string UTF8StringLength];

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

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

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

	if (string->s->isUTF8)
		s->isUTF8 = YES;
}
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
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







-
-
+
+





-
+




-
-
+
+

-
+







	     withArguments: arguments];
	va_end(arguments);
}

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

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

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

	@try {
		[self appendCString: cString
			 withLength: cStringLength];
		[self appendUTF8String: UTF8String
			    withLength: UTF8StringLength];
	} @finally {
		free(cString);
		free(UTF8String);
	}
}

- (void)prependString: (OFString*)string
{
	return [self insertString: string
			  atIndex: 0];
384
385
386
387
388
389
390
391

392
393
394
395
396
397
398
395
396
397
398
399
400
401

402
403
404
405
406
407
408
409







-
+







	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];
	newCStringLength = s->cStringLength + [string UTF8StringLength];
	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);
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
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







-
-
-
-
+
+
+
+



-
+






-
-
+
+






-
+







-
+

-
+



-
+







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

- (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;
	const char *UTF8String = [string UTF8String];
	const char *replacementUTF8String = [replacement UTF8String];
	size_t UTF8StringLength = string->s->cStringLength;
	size_t replacementUTF8StringLength = replacement->s->cStringLength;
	size_t i, last, newCStringLength, newLength;
	char *newCString;

	if (cStringLength > s->cStringLength)
	if (UTF8StringLength > 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))
	for (i = 0, last = 0; i <= s->cStringLength - UTF8StringLength; i++) {
		if (memcmp(s->cString + i, UTF8String, UTF8StringLength))
			continue;

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

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

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

	@try {
		newCString = [self
		    resizeMemory: newCString
			  toSize: newCStringLength +