summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-11-21 22:12:56 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-11-21 22:12:56 (GMT)
commitf01245067a54e3863b7db5e2eebd9f7ca45b29b5 (patch)
tree5a41bbbf78c2789ba460346191b3591352bd6ce5 /Objects
parent2d718f39a528009d84341a91555ffe66f9acc480 (diff)
downloadcpython-f01245067a54e3863b7db5e2eebd9f7ca45b29b5.zip
cpython-f01245067a54e3863b7db5e2eebd9f7ca45b29b5.tar.gz
cpython-f01245067a54e3863b7db5e2eebd9f7ca45b29b5.tar.bz2
Rewrite PyUnicode_TransformDecimalToASCII() to use the new Unicode API
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 1c276d1..4c6868f 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8779,32 +8779,41 @@ PyObject *
PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
Py_ssize_t length)
{
- PyObject *result;
- Py_UNICODE *p; /* write pointer into result */
+ PyObject *decimal;
Py_ssize_t i;
+ Py_UCS4 maxchar;
+ enum PyUnicode_Kind kind;
+ void *data;
+
+ maxchar = 0;
+ 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;
+ }
+ maxchar = Py_MAX(maxchar, ch);
+ }
+
/* Copy to a new string */
- result = (PyObject *)_PyUnicode_New(length);
- Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length);
- if (result == NULL)
- return result;
- p = PyUnicode_AS_UNICODE(result);
+ decimal = PyUnicode_New(length, maxchar);
+ 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];
+ Py_UNICODE ch = s[i];
if (ch > 127) {
int decimal = Py_UNICODE_TODECIMAL(ch);
if (decimal >= 0)
- p[i] = '0' + decimal;
+ ch = '0' + decimal;
}
+ PyUnicode_WRITE(kind, data, i, ch);
}
-#ifndef DONT_MAKE_RESULT_READY
- if (_PyUnicode_READY_REPLACE(&result)) {
- Py_DECREF(result);
- return NULL;
- }
-#endif
- assert(_PyUnicode_CheckConsistency(result, 1));
- return result;
+ assert(_PyUnicode_CheckConsistency(decimal, 1));
+ return decimal;
}
/* --- Decimal Encoder ---------------------------------------------------- */