Rewrite PyUnicode_TransformDecimalToASCII() to use the new Unicode API

This commit is contained in:
Victor Stinner 2011-11-21 23:12:56 +01:00
parent 2d718f39a5
commit f01245067a
1 changed files with 27 additions and 18 deletions

View File

@ -8779,32 +8779,41 @@ PyObject *
PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
Py_ssize_t length) Py_ssize_t length)
{ {
PyObject *result; PyObject *decimal;
Py_UNICODE *p; /* write pointer into result */
Py_ssize_t i; Py_ssize_t i;
/* Copy to a new string */ Py_UCS4 maxchar;
result = (PyObject *)_PyUnicode_New(length); enum PyUnicode_Kind kind;
Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); void *data;
if (result == NULL)
return result; maxchar = 0;
p = PyUnicode_AS_UNICODE(result);
/* Iterate over code points */
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
Py_UNICODE ch =s[i]; Py_UNICODE ch = s[i];
if (ch > 127) { if (ch > 127) {
int decimal = Py_UNICODE_TODECIMAL(ch); int decimal = Py_UNICODE_TODECIMAL(ch);
if (decimal >= 0) if (decimal >= 0)
p[i] = '0' + decimal; ch = '0' + decimal;
} }
maxchar = Py_MAX(maxchar, ch);
} }
#ifndef DONT_MAKE_RESULT_READY
if (_PyUnicode_READY_REPLACE(&result)) { /* Copy to a new string */
Py_DECREF(result); decimal = PyUnicode_New(length, maxchar);
return NULL; if (decimal == NULL)
return decimal;
kind = PyUnicode_KIND(decimal);
data = PyUnicode_DATA(decimal);
/* Iterate over code points */
for (i = 0; i < length; i++) {
Py_UNICODE ch = s[i];
if (ch > 127) {
int decimal = Py_UNICODE_TODECIMAL(ch);
if (decimal >= 0)
ch = '0' + decimal;
}
PyUnicode_WRITE(kind, data, i, ch);
} }
#endif assert(_PyUnicode_CheckConsistency(decimal, 1));
assert(_PyUnicode_CheckConsistency(result, 1)); return decimal;
return result;
} }
/* --- Decimal Encoder ---------------------------------------------------- */ /* --- Decimal Encoder ---------------------------------------------------- */