@@ -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;