Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -514,13 +514,15 @@ * \return A double with the value of the string */ - (double)doubleValue; /** - * Returns the string as an array of of_unichar_t. The result needs to be - * free()'d by the caller, as the memory is not marked as belonging to the - * object. + * Returns the string as an array of of_unichar_t. + * + * 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 as an array of Unicode characters */ - (of_unichar_t*)unicodeString; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -1576,19 +1576,21 @@ return value; } - (of_unichar_t*)unicodeString { + OFObject *object = [[[OFObject alloc] init] autorelease]; of_unichar_t *ret; - size_t i, j, len; - - len = [self length]; - - if ((ret = malloc((len + 1) * sizeof(of_unichar_t))) == NULL) - @throw [OFOutOfMemoryException newWithClass: isa]; - - i = j = 0; + size_t i, j; + + ret = [object allocMemoryForNItems: [self length] + 2 + withSize: sizeof(of_unichar_t)]; + + i = 0; + j = 0; + + ret[j++] = 0xFEFF; while (i < length) { of_unichar_t c; size_t cLen; Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -35,11 +35,13 @@ static OFString *module = @"OFString"; static OFString* whitespace[] = { @" \r \t\n\t \tasd \t \t\t\r\n", @" \t\t \t\t \t \t" }; -static of_unichar_t ucstr[] = { 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0x1F03A, 0 }; +static of_unichar_t ucstr[] = { + 0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0x1F03A, 0 +}; static of_unichar_t sucstr[] = { 0xFFFE0000, 0x66000000, 0xF6000000, 0xF6000000, 0x62000000, 0xE4000000, 0x72000000, 0x3AF00100, 0 }; @@ -368,11 +370,11 @@ [@"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" @"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" hexadecimalValue]) TEST(@"-[unicodeString]", (ua = [@"fööbär🀺" unicodeString]) && - !memcmp(ua, ucstr, 8 * sizeof(of_unichar_t)) && R(free(ua))) + !memcmp(ua, ucstr, 9 * sizeof(of_unichar_t))) TEST(@"-[MD5Hash]", [[@"asdfoobar" MD5Hash] isEqual: @"184dce2ec49b5422c7cfd8728864db4c"]) TEST(@"-[SHA1Hash]", [[@"asdfoobar" SHA1Hash]