ObjFW  Check-in [6e79f005c4]

Overview
Comment:OFString: Fix converting from some encodings
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 1.1
Files: files | file ages | folders
SHA3-256: 6e79f005c455ded73d3e1537b60bd5478311cdab72219e2a2869914c5ec75a02
User & Date: js on 2024-08-24 22:20:09
Other Links: branch diff | manifest | tags
Context
2024-08-24
23:43
Don't assume all custom string classes use Unicode check-in: cacfcf1b9d user: js tags: 1.1
22:20
OFString: Fix converting from some encodings check-in: 6e79f005c4 user: js tags: 1.1
22:19
OFString: Fix converting from some encodings check-in: d6a8d57148 user: js tags: trunk
10:40
OFINIFile: Allow # for comments check-in: 55f5b88d26 user: js tags: 1.1
Changes

Modified src/OFUTF8String.m from [97ab3896de] to [2832b02ca4].

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
		j = 0;
		for (size_t i = 0; i < cStringLength; i++) {
			unsigned char character = (unsigned char)cString[i];
			OFUnichar unichar;
			char buffer[4];
			size_t byteLength;

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

			unichar = table[character - tableOffset];



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






			_s->isUTF8 = true;
			byteLength = _OFUTF8StringEncode(unichar, buffer);


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

			_s->cStringLength += byteLength - 1;
			_s->cString = OFResizeMemory(_s->cString,
			    _s->cStringLength + 1, 1);








|
<
<
<
<
|
>
>




>
>
>
>
>

<

>







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
		j = 0;
		for (size_t i = 0; i < cStringLength; i++) {
			unsigned char character = (unsigned char)cString[i];
			OFUnichar unichar;
			char buffer[4];
			size_t byteLength;

			if (character >= tableOffset)




				unichar = table[character - tableOffset];
			else
				unichar = character;

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

			if (unichar < 0x7F) {
				_s->cString[j++] = (char)unichar;
				continue;
			}

			_s->isUTF8 = true;


			byteLength = _OFUTF8StringEncode(unichar, buffer);
			if (byteLength == 0)
				@throw [OFInvalidEncodingException exception];

			_s->cStringLength += byteLength - 1;
			_s->cString = OFResizeMemory(_s->cString,
			    _s->cStringLength + 1, 1);

Modified tests/OFStringTests.m from [46bc1e7497] to [4b858a36c1].

252
253
254
255
256
257
258
259
260

261
262
263
264
265
266
267
		     encoding: OFStringEncodingISO8859_1], @"äöü");
}

#ifdef HAVE_ISO_8859_15
- (void)testStringWithCStringEncodingISO8859_15
{
	OTAssertEqualObjects([self.stringClass
	    stringWithCString: "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"
		     encoding: OFStringEncodingISO8859_15], @"€ŠšŽžŒœŸ");

}
#endif

#ifdef HAVE_WINDOWS_1252
- (void)testStringWithCStringEncodingWindows1252
{
	OTAssertEqualObjects([self.stringClass







|
|
>







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
		     encoding: OFStringEncodingISO8859_1], @"äöü");
}

#ifdef HAVE_ISO_8859_15
- (void)testStringWithCStringEncodingISO8859_15
{
	OTAssertEqualObjects([self.stringClass
	    stringWithCString: "a\x80\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"
		     encoding: OFStringEncodingISO8859_15],
	    @"a\xC2\x80€ŠšŽžŒœŸ");
}
#endif

#ifdef HAVE_WINDOWS_1252
- (void)testStringWithCStringEncodingWindows1252
{
	OTAssertEqualObjects([self.stringClass