ObjFW  Diff

Differences From Artifact [2a8be01528]:

To Artifact [472df8f045]:


24
25
26
27
28
29
30


31
32
33
34
35
36
37
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39







+
+








#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#import "asprintf.h"

#import "encodings/iso_8859_15.h"

/* References for static linking */
void _references_to_categories_of_OFString()
{
	_OFHashing_reference = 1;
	_OFURLEncoding_reference = 1;
	_OFXMLElement_reference = 1;
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
297
298
299
300


301
302
303
304
305
306
307





308
309

310
311
312
313
314
315
316
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
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







+
+
+
+
+

-
+








+
-
+



+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

+

-
-
+
+
+
+
+
+
+

-
+




-
+















-
-
+
+







+
+
+
+
+

-
+







	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		switch (of_string_check_utf8(str, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			c = isa;
			[super dealloc];
			[self dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];
		}

		memcpy(string, str, length);
		string[length] = 0;

		break;
	case OF_STRING_ENCODING_ISO_8859_1:
	case OF_STRING_ENCODING_ISO_8859_15:
		for (i = j = 0; i < length; i++) {
		for (i = j = 0; i < len; i++) {
			if ((uint8_t)str[i] < 0x80)
				string[j++] = str[i];
			else {
				char buf[4];
				uint32_t chr;
				size_t chr_bytes;

				switch (encoding) {
				case OF_STRING_ENCODING_ISO_8859_1:
					chr = (uint8_t)str[i];
					break;
				case OF_STRING_ENCODING_ISO_8859_15:
					chr = iso_8859_15_to_unicode[
					    (uint8_t)str[i]];
					break;
				default:
				/*
					/*
				 * ISO 8859-1 can only have 2 bytes when encoded
				 * as UTF-8, nevertheless, let's be on the safe
				 * side.
				 */
				char buf[4];

					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				is_utf8 = YES;
				chr_bytes = of_string_unicode_to_utf8(chr, buf);

				if (of_string_unicode_to_utf8(
				    (uint8_t)str[i], buf) == 0) {
				if (chr_bytes == 0) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					c = isa;
					[super dealloc];
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				length++;
				length += chr_bytes - 1;
				@try {
					string = [self resizeMemory: string
							     toSize: length +
								     1];
				} @catch (OFException *e) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					[self dealloc];
					@throw e;
				}

				string[j++] = buf[0];
				string[j++] = buf[1];
				memcpy(string + j, buf, chr_bytes);
				j += chr_bytes;
			}
		}

		string[length] = 0;

		break;
	default:
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		c = isa;
		[super dealloc];
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	return self;
}

- initWithCString: (const char*)str