Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -13,11 +13,12 @@ #include #import "OFObject.h" #import "OFArray.h" -extern int of_string_check_utf8(const char *str, size_t len); +extern int of_string_check_utf8(const char*, size_t); +extern size_t of_string_unicode_to_utf8(uint32_t, uint8_t*); /** * A class for managing strings. */ @interface OFString: OFObject Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -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];