Commit b6fd9655 authored by Stefan Behnel's avatar Stefan Behnel

move utility function to a a better suited source file

parent 2b3f99e6
......@@ -732,6 +732,95 @@ static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* value
#endif
/////////////// BuildPyUnicode.proto ///////////////
static PyObject* __Pyx_PyUnicode_BuildFromAscii(Py_ssize_t ulength, char* chars, int clength,
int prepend_sign, char padding_char);
/////////////// BuildPyUnicode ///////////////
// Create a PyUnicode object from an ASCII char*, e.g. a formatted number.
static PyObject* __Pyx_PyUnicode_BuildFromAscii(Py_ssize_t ulength, char* chars, int clength,
int prepend_sign, char padding_char) {
PyObject *uval;
Py_ssize_t uoffset = ulength - clength;
#if CYTHON_USE_UNICODE_INTERNALS
Py_ssize_t i;
#if CYTHON_PEP393_ENABLED
// Py 3.3+ (post PEP-393)
void *udata;
uval = PyUnicode_New(ulength, 127);
if (unlikely(!uval)) return NULL;
udata = PyUnicode_DATA(uval);
#else
// Py 2.x/3.2 (pre PEP-393)
Py_UNICODE *udata;
uval = PyUnicode_FromUnicode(NULL, ulength);
if (unlikely(!uval)) return NULL;
udata = PyUnicode_AS_UNICODE(uval);
#endif
if (uoffset > 0) {
i = 0;
if (prepend_sign) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, 0, '-');
i++;
}
for (; i < uoffset; i++) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, i, padding_char);
}
}
for (i=0; i < clength; i++) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, uoffset+i, chars[i]);
}
#else
// non-CPython
{
uval = NULL;
PyObject *sign = NULL, *padding = NULL;
if (uoffset > 0) {
prepend_sign = !!prepend_sign;
if (uoffset > prepend_sign) {
padding = PyUnicode_FromOrdinal(padding_char);
if (likely(padding) && uoffset > prepend_sign + 1) {
PyObject *tmp;
PyObject *repeat = PyInt_FromSize_t(uoffset - prepend_sign);
if (unlikely(!repeat)) goto done_or_error;
tmp = PyNumber_Multiply(padding, repeat);
Py_DECREF(repeat);
Py_DECREF(padding);
padding = tmp;
}
if (unlikely(!padding)) goto done_or_error;
}
if (prepend_sign) {
sign = PyUnicode_FromOrdinal('-');
if (unlikely(!sign)) goto done_or_error;
}
}
uval = PyUnicode_DecodeASCII(chars, clength, NULL);
if (likely(uval) && padding) {
PyObject *tmp = PyNumber_Add(padding, uval);
Py_DECREF(uval);
uval = tmp;
}
if (likely(uval) && sign) {
PyObject *tmp = PyNumber_Add(sign, uval);
Py_DECREF(uval);
uval = tmp;
}
done_or_error:
Py_XDECREF(padding);
Py_XDECREF(sign);
}
#endif
return uval;
}
//////////////////// ByteArrayAppendObject.proto ////////////////////
static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyObject* value);
......
......@@ -579,7 +579,7 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t width, char padding_char, char format_char);
/////////////// CIntToPyUnicode ///////////////
//@requires: BuildPyUnicode
//@requires: StringTools.c::BuildPyUnicode
// NOTE: inlining because most arguments are constant, which collapses lots of code below
......@@ -642,94 +642,7 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if (width > ulength) {
ulength = width;
}
return __Pyx_PyUnicode_Build(ulength, dpos, length, prepend_sign, padding_char);
}
/////////////// BuildPyUnicode.proto ///////////////
static PyObject* __Pyx_PyUnicode_Build(Py_ssize_t ulength, char* chars, int clength,
int prepend_sign, char padding_char);
/////////////// BuildPyUnicode ///////////////
static PyObject* __Pyx_PyUnicode_Build(Py_ssize_t ulength, char* chars, int clength,
int prepend_sign, char padding_char) {
PyObject *uval;
Py_ssize_t uoffset = ulength - clength;
#if CYTHON_USE_UNICODE_INTERNALS
Py_ssize_t i;
#if CYTHON_PEP393_ENABLED
// Py 3.3+ (post PEP-393)
void *udata;
uval = PyUnicode_New(ulength, 127);
if (unlikely(!uval)) return NULL;
udata = PyUnicode_DATA(uval);
#else
// Py 2.x/3.2 (pre PEP-393)
Py_UNICODE *udata;
uval = PyUnicode_FromUnicode(NULL, ulength);
if (unlikely(!uval)) return NULL;
udata = PyUnicode_AS_UNICODE(uval);
#endif
if (uoffset > 0) {
i = 0;
if (prepend_sign) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, 0, '-');
i++;
}
for (; i < uoffset; i++) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, i, padding_char);
}
}
for (i=0; i < clength; i++) {
__Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, uoffset+i, chars[i]);
}
#else
// non-CPython
{
uval = NULL;
PyObject *sign = NULL, *padding = NULL;
if (uoffset > 0) {
prepend_sign = !!prepend_sign;
if (uoffset > prepend_sign) {
padding = PyUnicode_FromOrdinal(padding_char);
if (likely(padding) && uoffset > prepend_sign + 1) {
PyObject *tmp;
PyObject *repeat = PyInt_FromSize_t(uoffset - prepend_sign);
if (unlikely(!repeat)) goto done_or_error;
tmp = PyNumber_Multiply(padding, repeat);
Py_DECREF(repeat);
Py_DECREF(padding);
padding = tmp;
}
if (unlikely(!padding)) goto done_or_error;
}
if (prepend_sign) {
sign = PyUnicode_FromOrdinal('-');
if (unlikely(!sign)) goto done_or_error;
}
}
uval = PyUnicode_DecodeASCII(chars, clength, NULL);
if (likely(uval) && padding) {
PyObject *tmp = PyNumber_Add(padding, uval);
Py_DECREF(uval);
uval = tmp;
}
if (likely(uval) && sign) {
PyObject *tmp = PyNumber_Add(sign, uval);
Py_DECREF(uval);
uval = tmp;
}
done_or_error:
Py_XDECREF(padding);
Py_XDECREF(sign);
}
#endif
return uval;
return __Pyx_PyUnicode_BuildFromAscii(ulength, dpos, length, prepend_sign, padding_char);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment