| 213 | } |
| 214 | |
| 215 | UArray *UArray_asUCS2(const UArray *self) { |
| 216 | int convertToUtf8First = !UArray_isUTF8Compatible(self); |
| 217 | const UArray *utf8Array = convertToUtf8First ? UArray_asUTF8(self) : self; |
| 218 | size_t countedChars = UArray_numberOfCharacters(self); |
| 219 | size_t numChars; |
| 220 | |
| 221 | UArray *out = UArray_new(); |
| 222 | UArray_setItemType_(out, CTYPE_uint16_t); |
| 223 | UArray_setEncoding_(out, CENCODING_UCS2); |
| 224 | UArray_setSize_(out, countedChars * 2); |
| 225 | |
| 226 | numChars = ucs2decode((ucs2 *)(out->data), out->size, utf8Array->data); |
| 227 | |
| 228 | if ((numChars > 0) && (numChars > countedChars * 2)) { |
| 229 | printf("UArray_asUCS2 error: numChars (%i) > countedChars (2*%i)\n", |
| 230 | (int)numChars, (int)countedChars); |
| 231 | printf("Exiting because we may have overwritten the usc2 decode output " |
| 232 | "buffer."); |
| 233 | exit(-1); |
| 234 | } |
| 235 | |
| 236 | UArray_setSize_(out, numChars); |
| 237 | |
| 238 | if (convertToUtf8First) |
| 239 | UArray_free((UArray *)utf8Array); |
| 240 | return out; |
| 241 | } |
| 242 | |
| 243 | UArray *UArray_asUCS4(const UArray *self) { |
| 244 | int convertToUtf8First = !UArray_isUTF8Compatible(self); |
no test coverage detected