@@ -102,10 +102,39 @@ madvise((void*)str, len, MADV_NORMAL); return utf8; } + +size_t +of_string_unicode_to_utf8(uint32_t c, uint8_t *buf) +{ + if (c < 0x80) { + buf[0] = c; + return 1; + } + if (c < 0x800) { + buf[0] = 0xC0 | (c >> 6); + buf[1] = 0x80 | (c & 0x3F); + return 2; + } + if (c < 0x10000) { + buf[0] = 0xE0 | (c >> 12); + buf[1] = 0x80 | (c >> 6 & 0x3F); + buf[2] = 0x80 | (c & 0x3F); + return 3; + } + if (c < 0x110000) { + buf[0] = 0xF0 | (c >> 18); + buf[1] = 0x80 | (c >> 12 & 0x3F); + buf[2] = 0x80 | (c >> 6 & 0x3F); + buf[3] = 0x80 | (c & 0x3F); + return 4; + } + + return 0; +} @implementation OFString + string { return [[[self alloc] init] autorelease];