Index: src/OFMutableString_UTF8.m ================================================================== --- src/OFMutableString_UTF8.m +++ src/OFMutableString_UTF8.m @@ -393,40 +393,20 @@ @try { size_t i, j = 0; bool isUTF8 = false; for (i = 0; i < length; i++) { - char buffer[4]; - - switch (of_string_utf8_encode(characters[i], buffer)) { - case 1: - tmp[j++] = buffer[0]; - break; - case 2: - isUTF8 = true; - - memcpy(tmp + j, buffer, 2); - j += 2; - - break; - case 3: - isUTF8 = true; - - memcpy(tmp + j, buffer, 3); - j += 3; - - break; - case 4: - isUTF8 = true; - - memcpy(tmp + j, buffer, 4); - j += 4; - - break; - default: - @throw [OFInvalidEncodingException exception]; - } + size_t len = of_string_utf8_encode(characters[i], + tmp + j); + + if (len == 0) + @throw [OFInvalidEncodingException exception]; + + if (len > 1) + isUTF8 = true; + + j += len; } tmp[j] = '\0'; _s->hashed = false; Index: src/OFString_UTF8.m ================================================================== --- src/OFString_UTF8.m +++ src/OFString_UTF8.m @@ -414,30 +414,20 @@ _s->cString = [self allocMemoryWithSize: (length * 4) + 1]; _s->length = length; for (i = 0; i < length; i++) { - char buffer[4]; size_t len = of_string_utf8_encode(characters[i], - buffer); - - switch (len) { - case 1: - _s->cString[j++] = buffer[0]; - break; - case 2: - case 3: - case 4: + _s->cString + j); + + if (len == 0) + @throw [OFInvalidEncodingException exception]; + + if (len > 1) _s->isUTF8 = true; - memcpy(_s->cString + j, buffer, len); - j += len; - - break; - default: - @throw [OFInvalidEncodingException exception]; - } + j += len; } _s->cString[j] = '\0'; _s->cStringLength = j; @@ -479,11 +469,10 @@ _s->cString = [self allocMemoryWithSize: (length * 4) + 1]; _s->length = length; for (i = 0; i < length; i++) { - char buffer[4]; of_unichar_t character = (swap ? OF_BSWAP16(string[i]) : string[i]); size_t len; /* Missing high surrogate */ @@ -510,28 +499,19 @@ i++; _s->length--; } - len = of_string_utf8_encode(character, buffer); - - switch (len) { - case 1: - _s->cString[j++] = buffer[0]; - break; - case 2: - case 3: - case 4: + len = of_string_utf8_encode(character, _s->cString + j); + + if (len == 0) + @throw [OFInvalidEncodingException exception]; + + if (len > 1) _s->isUTF8 = true; - memcpy(_s->cString + j, buffer, len); - j += len; - - break; - default: - @throw [OFInvalidEncodingException exception]; - } + j += len; } _s->cString[j] = '\0'; _s->cStringLength = j;