ObjFW  Check-in [a51b3cb390]

Overview
Comment:Add -[UTF16String] to OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a51b3cb39018e2b7e2b9c0af94de1348c05365dee3630879c43b6112df5c896d
User & Date: js on 2011-10-31 22:02:09
Other Links: manifest | tags
Context
2011-11-02
11:20
Fix parsing of struct tm. check-in: 5d102d382e user: js tags: trunk
2011-10-31
22:02
Add -[UTF16String] to OFString. check-in: a51b3cb390 user: js tags: trunk
22:01
Assume big endian encoding for UTF16 if none is specified and no BOM. check-in: f412995e6a user: js tags: trunk
Changes

Modified src/OFConstantString.m from [e406c9d41b] to [31cc01efd6].

633
634
635
636
637
638
639








640
641
642
643
644
645
646
- (const of_unichar_t*)unicodeString
{
	if (initialized != SIZE_MAX)
		[self finishInitialization];

	return [super unicodeString];
}









- (void)writeToFile: (OFString*)path
{
	if (initialized != SIZE_MAX)
		[self finishInitialization];

	return [super writeToFile: path];







>
>
>
>
>
>
>
>







633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
- (const of_unichar_t*)unicodeString
{
	if (initialized != SIZE_MAX)
		[self finishInitialization];

	return [super unicodeString];
}

- (const uint16_t*)UTF16String
{
	if (initialized != SIZE_MAX)
		[self finishInitialization];

	return [super UTF16String];
}

- (void)writeToFile: (OFString*)path
{
	if (initialized != SIZE_MAX)
		[self finishInitialization];

	return [super writeToFile: path];

Modified src/OFString.h from [2bb04bd0ef] to [bf385a3e5c].

793
794
795
796
797
798
799











800
801
802
803
804
805
806
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * \return The string as an array of Unicode characters
 */
- (const of_unichar_t*)unicodeString;












/**
 * \brief Writes the string into the specified file using UTF-8 encoding.
 *
 * \param path The path of the file to write to
 */
- (void)writeToFile: (OFString*)path;








>
>
>
>
>
>
>
>
>
>
>







793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * \return The string as an array of Unicode characters
 */
- (const of_unichar_t*)unicodeString;

/**
 * \brief Returns the string in big endian UTF-16 encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * \return The string in big endian UTF-16 encoding
 */
- (const uint16_t*)UTF16String;

/**
 * \brief Writes the string into the specified file using UTF-8 encoding.
 *
 * \param path The path of the file to write to
 */
- (void)writeToFile: (OFString*)path;

Modified src/OFString.m from [1d69719094] to [e38e775f82].

1895
1896
1897
1898
1899
1900
1901















































1902
1903
1904
1905
1906
1907
1908
			    exceptionWithClass: isa];

		ret[j++] = c;
		i += cLen;
	}

	ret[j] = 0;
















































	return ret;
}

- (void)writeToFile: (OFString*)path
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
			    exceptionWithClass: isa];

		ret[j++] = c;
		i += cLen;
	}

	ret[j] = 0;

	return ret;
}

- (const uint16_t*)UTF16String
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	uint16_t *ret;
	size_t i, j;

	/* Allocate memory for the worst case */
	ret = [object allocMemoryForNItems: s->length * 2 + 1
				    ofSize: sizeof(uint16_t)];

	i = 0;
	j = 0;

	while (i < s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

		cLen = of_string_utf8_to_unicode(s->cString + i,
		    s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];

		if (c > 0xFFFF) {
			c -= 0x10000;
			ret[j++] = of_bswap16_if_le(0xD800 | (c >> 10));
			ret[j++] = of_bswap16_if_le(0xDC00 | (c & 0x3FF));
		} else
			ret[j++] = of_bswap16_if_le(c);

		i += cLen;
	}

	ret[j] = 0;

	@try {
		ret = [object resizeMemory: ret
				  toNItems: j + 1
				    ofSize: sizeof(uint16_t)];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	return ret;
}

- (void)writeToFile: (OFString*)path
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

Modified tests/OFStringTests.m from [4c3630eafa] to [9ef8e91ce4].

70
71
72
73
74
75
76

77
78
79
80
81
82
83
- (void)stringTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableString *s[3];
	OFArray *a;
	int i;
	const of_unichar_t *ua;

	EntityHandler *h;
#ifdef OF_HAVE_BLOCKS
	__block BOOL ok;
#endif

	s[0] = [OFMutableString stringWithString: @"täs€"];
	s[1] = [OFMutableString string];







>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
- (void)stringTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableString *s[3];
	OFArray *a;
	int i;
	const of_unichar_t *ua;
	const uint16_t *u16a;
	EntityHandler *h;
#ifdef OF_HAVE_BLOCKS
	__block BOOL ok;
#endif

	s[0] = [OFMutableString stringWithString: @"täs€"];
	s[1] = [OFMutableString string];
378
379
380
381
382
383
384







385
386
387
388
389
390
391
	    OFOutOfRangeException,
	    [@"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	     @"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	    hexadecimalValue])

	TEST(@"-[unicodeString]", (ua = [@"fööbär🀺" unicodeString]) &&
	    !memcmp(ua, ucstr + 1, sizeof(ucstr) - sizeof(of_unichar_t)))








	TEST(@"-[MD5Hash]", [[@"asdfoobar" MD5Hash]
	    isEqual: @"184dce2ec49b5422c7cfd8728864db4c"])

	TEST(@"-[SHA1Hash]", [[@"asdfoobar" SHA1Hash]
	    isEqual: @"f5f81ac0a8b5cbfdc4585ec1ad32e7b3a12b9b49"])








>
>
>
>
>
>
>







379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
	    OFOutOfRangeException,
	    [@"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	     @"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	    hexadecimalValue])

	TEST(@"-[unicodeString]", (ua = [@"fööbär🀺" unicodeString]) &&
	    !memcmp(ua, ucstr + 1, sizeof(ucstr) - sizeof(of_unichar_t)))

	TEST(@"-[UTF16String]", (u16a = [@"fööbär🀺" UTF16String]) &&
#ifdef OF_BIG_ENDIAN
	    !memcmp(u16a, utf16str + 1, sizeof(utf16str) - sizeof(uint16_t)))
#else
	    !memcmp(u16a, sutf16str + 1, sizeof(sutf16str) - sizeof(uint16_t)))
#endif

	TEST(@"-[MD5Hash]", [[@"asdfoobar" MD5Hash]
	    isEqual: @"184dce2ec49b5422c7cfd8728864db4c"])

	TEST(@"-[SHA1Hash]", [[@"asdfoobar" SHA1Hash]
	    isEqual: @"f5f81ac0a8b5cbfdc4585ec1ad32e7b3a12b9b49"])