ObjFW  Diff

Differences From Artifact [b5d78abf98]:

To Artifact [4a1023ff16]:


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51






















52
53
54
55
56
57
58
34
35
36
37
38
39
40











41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69







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







#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "of_asprintf.h"
#import "unicode.h"

extern const of_char16_t of_iso_8859_2[128];
extern const of_char16_t of_iso_8859_3[128];
extern const of_char16_t of_iso_8859_15[128];
extern const of_char16_t of_windows_1251[128];
extern const of_char16_t of_windows_1252[128];
extern const of_char16_t of_codepage_437[128];
extern const of_char16_t of_codepage_850[128];
extern const of_char16_t of_codepage_858[128];
extern const of_char16_t of_mac_roman[128];
extern const of_char16_t of_koi8_r[128];
extern const of_char16_t of_koi8_u[128];
extern const of_char16_t of_iso_8859_2_table[];
extern const size_t of_iso_8859_2_table_offset;
extern const of_char16_t of_iso_8859_3_table[];
extern const size_t of_iso_8859_3_table_offset;
extern const of_char16_t of_iso_8859_15_table[];
extern const size_t of_iso_8859_15_table_offset;
extern const of_char16_t of_windows_1251_table[];
extern const size_t of_windows_1251_table_offset;
extern const of_char16_t of_windows_1252_table[];
extern const size_t of_windows_1252_table_offset;
extern const of_char16_t of_codepage_437_table[];
extern const size_t of_codepage_437_table_offset;
extern const of_char16_t of_codepage_850_table[];
extern const size_t of_codepage_850_table_offset;
extern const of_char16_t of_codepage_858_table[];
extern const size_t of_codepage_858_table_offset;
extern const of_char16_t of_mac_roman_table[];
extern const size_t of_mac_roman_table_offset;
extern const of_char16_t of_koi8_r_table[];
extern const size_t of_koi8_r_table_offset;
extern const of_char16_t of_koi8_u_table[];
extern const size_t of_koi8_u_table_offset;

static inline int
memcasecmp(const char *first, const char *second, size_t length)
{
	for (size_t i = 0; i < length; i++) {
		unsigned char f = first[i];
		unsigned char s = second[i];
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
229
230
231
232
233
234
235

236
237
238
239
240
241
242
243







-
+







	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength
{
	self = [super init];

	@try {
		const of_char16_t *table;
		size_t j;
		size_t tableOffset, j;

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    cStringLength >= 3 &&
		    memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
			cString += 3;
			cStringLength -= 3;
		}
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
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
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
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







-
-
-
-
+
+
+
+
+
+
+


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-


-
+
-
-

+






+
+

-
-
+

-
+




-
+

-
+



-
+
-

-
+


-
+




-
-
+
+








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

			return self;
		}

		switch (encoding) {
#ifdef HAVE_ISO_8859_2
		case OF_STRING_ENCODING_ISO_8859_2:
			table = of_iso_8859_2;
			break;
#define CASE(encoding, var)			\
	case encoding:				\
		table = var;			\
		tableOffset = var##_offset;	\
		break;
#ifdef HAVE_ISO_8859_2
		CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table)
#endif
#ifdef HAVE_ISO_8859_3
		case OF_STRING_ENCODING_ISO_8859_3:
		CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table)
			table = of_iso_8859_3;
			break;
#endif
#ifdef HAVE_ISO_8859_15
		case OF_STRING_ENCODING_ISO_8859_15:
		CASE(OF_STRING_ENCODING_ISO_8859_15, of_iso_8859_15_table)
			table = of_iso_8859_15;
			break;
#endif
#ifdef HAVE_WINDOWS_1251
		case OF_STRING_ENCODING_WINDOWS_1251:
		CASE(OF_STRING_ENCODING_WINDOWS_1251, of_windows_1251_table)
			table = of_windows_1251;
			break;
#endif
#ifdef HAVE_WINDOWS_1252
		case OF_STRING_ENCODING_WINDOWS_1252:
		CASE(OF_STRING_ENCODING_WINDOWS_1252, of_windows_1252_table)
			table = of_windows_1252;
			break;
#endif
#ifdef HAVE_CODEPAGE_437
		case OF_STRING_ENCODING_CODEPAGE_437:
		CASE(OF_STRING_ENCODING_CODEPAGE_437, of_codepage_437_table)
			table = of_codepage_437;
			break;
#endif
#ifdef HAVE_CODEPAGE_850
		case OF_STRING_ENCODING_CODEPAGE_850:
		CASE(OF_STRING_ENCODING_CODEPAGE_850, of_codepage_850_table)
			table = of_codepage_850;
			break;
#endif
#ifdef HAVE_CODEPAGE_858
		case OF_STRING_ENCODING_CODEPAGE_858:
		CASE(OF_STRING_ENCODING_CODEPAGE_858, of_codepage_858_table)
			table = of_codepage_858;
			break;
#endif
#ifdef HAVE_MAC_ROMAN
		case OF_STRING_ENCODING_MAC_ROMAN:
		CASE(OF_STRING_ENCODING_MAC_ROMAN, of_mac_roman_table)
			table = of_mac_roman;
			break;
#endif
#ifdef HAVE_KOI8_R
		case OF_STRING_ENCODING_KOI8_R:
		CASE(OF_STRING_ENCODING_KOI8_R, of_koi8_r_table)
			table = of_koi8_r;
			break;
#endif
#ifdef HAVE_KOI8_U
		case OF_STRING_ENCODING_KOI8_U:
		CASE(OF_STRING_ENCODING_KOI8_U, of_koi8_u_table)
			table = of_koi8_u;
			break;
#endif
#undef CASE
		default:
			@throw [OFInvalidEncodingException exception];
		}

		j = 0;
		for (size_t i = 0; i < cStringLength; i++) {
			unsigned char character = (unsigned char)cString[i];
			of_unichar_t unichar;
			char buffer[4];
			of_unichar_t character;
			size_t characterBytes;
			size_t byteLength;

			if (!(cString[i] & 0x80)) {
			if (character < tableOffset) {
				_s->cString[j++] = cString[i];
				continue;
			}

			character = table[(uint8_t)cString[i] - 128];
			unichar = table[character - tableOffset];

			if (character == 0xFFFF)
			if (unichar == 0xFFFF)
				@throw [OFInvalidEncodingException exception];

			_s->isUTF8 = true;
			characterBytes = of_string_utf8_encode(character,
			byteLength = of_string_utf8_encode(unichar, buffer);
			    buffer);

			if (characterBytes == 0)
			if (byteLength == 0)
				@throw [OFInvalidEncodingException exception];

			_s->cStringLength += characterBytes - 1;
			_s->cStringLength += byteLength - 1;
			_s->cString = [self
			    resizeMemory: _s->cString
				    size: _s->cStringLength + 1];

			memcpy(_s->cString + j, buffer, characterBytes);
			j += characterBytes;
			memcpy(_s->cString + j, buffer, byteLength);
			j += byteLength;
		}

		_s->cString[_s->cStringLength] = 0;
	} @catch (id e) {
		[self release];
		@throw e;
	}