ObjFW  Diff

Differences From Artifact [781415eae9]:

To Artifact [0b49a22c9f]:


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
	}

	return self;
}

- initFromCString: (const char*)str
{


	if ((self = [super init])) {
		if (str != NULL) {
			length = strlen(str);

			switch (check_utf8(str, length)) {
			case 1:
				is_utf8 = YES;
				break;
			case -1:

				[super free];
				@throw [OFInvalidEncodingException
				    newWithObject: self];
			}

			string = [self getMemWithSize: length + 1];
			memcpy(string, str, length + 1);
		}
	}








>
>









>


|







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
	}

	return self;
}

- initFromCString: (const char*)str
{
	Class c;

	if ((self = [super init])) {
		if (str != NULL) {
			length = strlen(str);

			switch (check_utf8(str, length)) {
			case 1:
				is_utf8 = YES;
				break;
			case -1:
				c = [self class];
				[super free];
				@throw [OFInvalidEncodingException
				    newWithClass: c];
			}

			string = [self getMemWithSize: length + 1];
			memcpy(string, str, length + 1);
		}
	}

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
	return ret;
}

- initFromFormatCString: (const char*)fmt
	  withArguments: (va_list)args
{
	int t;


	if ((self = [super init])) {
		if (fmt == NULL)


			@throw [OFInvalidFormatException newWithObject: self];


		if ((t = vasprintf(&string, fmt, args)) == -1)
			/*
			 * This is only the most likely error to happen.
			 * Unfortunately, as errno isn't always thread-safe,
			 * there's no good way for us to find out what really

			 * happened.
			 */
			@throw [OFNoMemException newWithObject: self];


		length = t;

		switch (check_utf8(string, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			free(string);

			[super free];
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		@try {
			[self addToMemoryPool: string];
		} @catch (OFException *e) {
			free(string);
			@throw e;







>


|
>
>
|
|
>
|
<
<
<
<
>
|
<
|
>
>








>

|







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
	return ret;
}

- initFromFormatCString: (const char*)fmt
	  withArguments: (va_list)args
{
	int t;
	Class c;

	if ((self = [super init])) {
		if (fmt == NULL) {
			c = [self class];
			[super free];
			@throw [OFInvalidFormatException newWithClass: c];
		}

		if ((t = vasprintf(&string, fmt, args)) == -1) {




			c = [self class];
			[super free];

			@throw [OFInitializationFailedException
			    newWithClass: c];
		}
		length = t;

		switch (check_utf8(string, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			free(string);
			c = [self class];
			[super free];
			@throw [OFInvalidEncodingException newWithClass: c];
		}

		@try {
			[self addToMemoryPool: string];
		} @catch (OFException *e) {
			free(string);
			@throw e;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	strlength = strlen(str);

	switch (check_utf8(str, strlength)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithObject: self];
	}

	newlen = length + strlength;
	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);







|







264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	strlength = strlen(str);

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

	newlen = length + strlength;
	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);
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

- appendWithFormatCString: (const char*)fmt
	     andArguments: (va_list)args
{
	char *t;

	if (fmt == NULL)
		@throw [OFInvalidFormatException newWithObject: self];

	if ((vasprintf(&t, fmt, args)) == -1)
		/*
		 * This is only the most likely error to happen.
		 * Unfortunately, as errno isn't always thread-safe, there's
		 * no good way for us to find out what really happened.
		 */
		@throw [OFNoMemException newWithObject: self];


	[self appendCString: t];

	free(t);


	return self;
}

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







|







|

>
|
>
|
>







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

- appendWithFormatCString: (const char*)fmt
	     andArguments: (va_list)args
{
	char *t;

	if (fmt == NULL)
		@throw [OFInvalidFormatException newWithClass: [self class]];

	if ((vasprintf(&t, fmt, args)) == -1)
		/*
		 * This is only the most likely error to happen.
		 * Unfortunately, as errno isn't always thread-safe, there's
		 * no good way for us to find out what really happened.
		 */
		@throw [OFNoMemException newWithClass: [self class]];

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

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;
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
		/* ASCII */
		if (OF_LIKELY(!(string[i] & 0x80)))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];

		}

		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 1 || !(string[i + 1] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];

		}

		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];

		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithObject: self];

		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

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

	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{
	size_t i = length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithObject: self];

	while (i--)
		string[i] = toupper(string[i]);

	return self;
}

- lower
{
	size_t i = length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithObject: self];

	while (i--)
		string[i] = tolower(string[i]);

	return self;
}
@end







|
>





|
>















|
>















|
>


















|












|












|







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
		/* ASCII */
		if (OF_LIKELY(!(string[i] & 0x80)))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40)) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			    newWithClass: [self class]];
		}

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

		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			    newWithClass: [self class]];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) {
			madvise(string, len, MADV_NORMAL);
			@throw [OFInvalidEncodingException
			    newWithClass: [self class]];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

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

	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{
	size_t i = length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: [self class]];

	while (i--)
		string[i] = toupper(string[i]);

	return self;
}

- lower
{
	size_t i = length;

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: [self class]];

	while (i--)
		string[i] = tolower(string[i]);

	return self;
}
@end