static PyObject * icu_normalize(PyObject *self, PyObject *args) { UErrorCode status = U_ZERO_ERROR; int32_t sz = 0, cap = 0, rsz = 0; NORM_MODES mode; UChar *dest = NULL, *source = NULL; PyObject *ret = NULL, *src = NULL; if (!PyArg_ParseTuple(args, "iO", &mode, &src)) return NULL; const UNormalizer2 *n = NULL; switch (mode) { case NFC: n = unorm2_getNFCInstance(&status); break; case NFKC: n = unorm2_getNFKCInstance(&status); break; case NFD: n = unorm2_getNFDInstance(&status); break; case NFKD: n = unorm2_getNFKDInstance(&status); break; } if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, u_errorName(status)); goto end; } source = python_to_icu(src, &sz); if (source == NULL) goto end; cap = 2 * sz; dest = (UChar*) calloc(cap, sizeof(UChar)); if (dest == NULL) { PyErr_NoMemory(); goto end; } while (1) { rsz = unorm2_normalize(n, source, sz, dest, cap, &status); if (status == U_BUFFER_OVERFLOW_ERROR) { cap *= 2; dest = (UChar*) realloc(dest, cap*sizeof(UChar)); if (dest == NULL) { PyErr_NoMemory(); goto end; } continue; } break; } if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, u_errorName(status)); goto end; } ret = icu_to_python(dest, rsz); end: if (source != NULL) free(source); if (dest != NULL) free(dest); return ret; } // }}}
const UNormalizer2* GetNormalizerForForm(NormalizationForm normalizationForm, UErrorCode* pErrorCode) { switch (normalizationForm) { case NormalizationForm::C: return unorm2_getNFCInstance(pErrorCode); case NormalizationForm::D: return unorm2_getNFDInstance(pErrorCode); case NormalizationForm::KC: return unorm2_getNFKCInstance(pErrorCode); case NormalizationForm::KD: return unorm2_getNFKDInstance(pErrorCode); } *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; return nullptr; }