ObjFW  Diff

Differences From Artifact [e8c7a3fc62]:

To Artifact [702fa32298]:


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







-
+








	[self freeMemory: *string];
	*string = nstr;
	*length = nlen;
}

@implementation OFMutableString
- setToCString: (const char*)str
- (void)setToCString: (const char*)str
{
	size_t len;

	[self freeMemory: string];

	len = strlen(str);

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
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
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
269
270
271

272
273
274
275
276
277
278
279







-
+
-
-
+
-
-
+

















-
+
-
-
+
-
-
-
+
+

















-
+
-
-
+
-
-
+








-
+
-
-
+
-
-
-
+
+









-
+
-
-
+
-
-
+


-
+
-
-
+
-
-
+

-



-
-
+
+

-
+
-
-
+
-
-
-
+
+




















-
+
-
-
+
-
-
+














-
+








		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	length = len;
	string = [self allocMemoryWithSize: length + 1];
	memcpy(string, str, length + 1);

}
	return self;
}


- appendCString: (const char*)str
- (void)appendCString: (const char*)str
{
	size_t strlength;

	strlength = strlen(str);

	switch (of_string_check_utf8(str, strlength)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;

}
	return self;
}


- appendCString: (const char*)str
     withLength: (size_t)len
- (void)appendCString: (const char*)str
	   withLength: (size_t)len
{
	if (len > strlen(str))
		@throw [OFOutOfRangeException newWithClass: isa];

	switch (of_string_check_utf8(str, len)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
	string[length] = 0;

}
	return self;
}


- appendCStringWithoutUTF8Checking: (const char*)str
- (void)appendCStringWithoutUTF8Checking: (const char*)str
{
	size_t strlength;

	strlength = strlen(str);
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;

}
	return self;
}


- appendCStringWithoutUTF8Checking: (const char*)str
			    length: (size_t)len
- (void)appendCStringWithoutUTF8Checking: (const char*)str
				  length: (size_t)len
{
	if (len > strlen(str))
		@throw [OFOutOfRangeException newWithClass: isa];

	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
	string[length] = 0;

}
	return self;
}


- appendString: (OFString*)str
- (void)appendString: (OFString*)str
{
	[self appendCString: [str cString]];

}
	return self;
}


- appendFormat: (OFString*)fmt, ...
- (void)appendFormat: (OFString*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);
	ret = [self appendFormat: fmt
		   withArguments: args];
	[self appendFormat: fmt
	     withArguments: args];
	va_end(args);

}
	return ret;
}


-  appendFormat: (OFString*)fmt
  withArguments: (va_list)args
- (void)appendFormat: (OFString*)fmt
       withArguments: (va_list)args
{
	char *t;

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

	if ((vasprintf(&t, [fmt cString], args)) == -1) {
		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: isa];
	}

	@try {
		[self appendCString: t];
	} @finally {
		free(t);
	}

}
	return self;
}


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

	madvise(string, len, MADV_SEQUENTIAL);

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

	if (!is_utf8) {
		madvise(string, len, MADV_NORMAL);
		return self;
		return;
	}

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

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







-
+
-
-
+
-
-
+



-
+
-
-
+
-
-
+



-
+
-
-
+
-
-
-
+
+








		/* UTF-8 does not allow more than 4 bytes per character */
		madvise(string, len, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	madvise(string, len, MADV_NORMAL);

}
	return self;
}


- upper
- (void)upper
{
	apply_table(self, isa, &string, &length, is_utf8,
	    of_unicode_upper_table, OF_UNICODE_UPPER_TABLE_SIZE);

}
	return self;
}


- lower
- (void)lower
{
	apply_table(self, isa, &string, &length, is_utf8,
	    of_unicode_lower_table, OF_UNICODE_LOWER_TABLE_SIZE);

}
	return self;
}


- removeCharactersFromIndex: (size_t)start
		    toIndex: (size_t)end
- (void)removeCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (is_utf8) {
		start = of_string_index_to_position(string, start, length);
		end = of_string_index_to_position(string, end, length);
	}

	if (start > end)
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
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







-
+
-
-
+
-
-
+

-
-
+
+


-
-
+
+









-
+







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

}
	return self;
}


- removeCharactersInRange: (of_range_t)range
- (void)removeCharactersInRange: (of_range_t)range
{
	return [self removeCharactersFromIndex: range.start
				       toIndex: range.start + range.length];
	[self removeCharactersFromIndex: range.start
				toIndex: range.start + range.length];
}

- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl
- (void)replaceOccurrencesOfString: (OFString*)str
			withString: (OFString*)repl
{
	const char *str_c = [str cString];
	const char *repl_c = [repl cString];
	size_t str_len = [str cStringLength];
	size_t repl_len = [repl cStringLength];
	size_t i, last, tmp_len;
	char *tmp;

	if (str_len > length)
		return self;
		return;

	tmp = NULL;
	tmp_len = 0;

	for (i = 0, last = 0; i <= length - str_len; i++) {
		if (memcmp(string + i, str_c, str_len))
			continue;
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
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







-
+
-
-
+
-
-
+



















-
+
-
-
+
-
-
+







	memcpy(tmp + tmp_len, string + last, length - last);
	tmp_len += length - last;
	tmp[tmp_len] = 0;

	[self freeMemory: string];
	string = tmp;
	length = tmp_len;

}
	return self;
}


- removeLeadingWhitespaces
- (void)removeLeadingWhitespaces
{
	size_t i;

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

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

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

}
	return self;
}


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

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
510
511
512
513
514
515
516
517

518
519

520
521

522
523
524
525
526
527
528
481
482
483
484
485
486
487

488


489


490
491
492
493
494
495
496
497







-
+
-
-
+
-
-
+







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

}
	return self;
}


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

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
515
516
517
518
519
520
521


522
523
524
525
526
527
528







-
-







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

	return self;
}

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