/*---------------------------------------------------------------------------------------------- This method uses an ICU function to convert a string from UTF-16 to UTF-32. Assumptions: If sourceLen is -1, it will be computed (by ICU) Exit conditions: <text> Parameters: <text> Return value: The number of characters needed to store the fully-converted result (which may be greater than targetLen) ----------------------------------------------------------------------------------------------*/ int UnicodeConverter::Convert(const UChar* source, int sourceLen, wchar_t* target, int targetLen) { UErrorCode status = U_ZERO_ERROR; int32_t spaceRequiredForData; u_strToWCS(target, targetLen, &spaceRequiredForData, source, sourceLen, &status); if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) throw std::runtime_error("Unable to convert from UTF-16 to UTF-32"); return spaceRequiredForData; }
static PyObject* uchar_to_unicode(const UChar *src, int32_t len) { wchar_t *buf = NULL; PyObject *ans = NULL; UErrorCode status = U_ZERO_ERROR; if (len < 0) { len = u_strlen(src); } buf = (wchar_t *)calloc(4*len, sizeof(wchar_t)); if (buf == NULL) return PyErr_NoMemory(); u_strToWCS(buf, 4*len, NULL, src, len, &status); if (U_SUCCESS(status)) { ans = PyUnicode_FromWideChar(buf, wcslen(buf)); if (ans == NULL) PyErr_NoMemory(); } else PyErr_SetString(PyExc_TypeError, "Failed to convert UChar* to wchar_t*"); free(buf); return ans; }
// Collator.contractions {{{ static PyObject * icu_Collator_contractions(icu_Collator *self, PyObject *args, PyObject *kwargs) { UErrorCode status = U_ZERO_ERROR; UChar *str; UChar32 start=0, end=0; int32_t count = 0, len = 0, dlen = 0, i; PyObject *ans = Py_None, *pbuf; wchar_t *buf; if (self->contractions == NULL) { self->contractions = uset_open(1, 0); if (self->contractions == NULL) return PyErr_NoMemory(); self->contractions = ucol_getTailoredSet(self->collator, &status); } status = U_ZERO_ERROR; str = (UChar*)calloc(100, sizeof(UChar)); buf = (wchar_t*)calloc(4*100+2, sizeof(wchar_t)); if (str == NULL || buf == NULL) return PyErr_NoMemory(); count = uset_getItemCount(self->contractions); ans = PyTuple_New(count); if (ans != NULL) { for (i = 0; i < count; i++) { len = uset_getItem(self->contractions, i, &start, &end, str, 1000, &status); if (len >= 2) { // We have a string status = U_ZERO_ERROR; u_strToWCS(buf, 4*100 + 1, &dlen, str, len, &status); pbuf = PyUnicode_FromWideChar(buf, dlen); if (pbuf == NULL) return PyErr_NoMemory(); PyTuple_SetItem(ans, i, pbuf); } else { // Ranges dont make sense for contractions, ignore them PyTuple_SetItem(ans, i, Py_None); } } } free(str); free(buf); return Py_BuildValue("O", ans); } // }}}