Index: Include/longintrepr.h =================================================================== --- Include/longintrepr.h (revision 74768) +++ Include/longintrepr.h (working copy) @@ -50,12 +50,16 @@ typedef PY_UINT64_T twodigits; typedef PY_INT64_T stwodigits; /* signed variant of twodigits */ #define PyLong_SHIFT 30 +#define _PyLong_DECIMAL_SHIFT 9 /* max(e such that 10**e fits in a digit) */ +#define _PyLong_DECIMAL_BASE ((digit)1000000000) /* 10 ** DECIMAL_SHIFT */ #elif PYLONG_BITS_IN_DIGIT == 15 typedef unsigned short digit; typedef short sdigit; /* signed variant of digit */ typedef unsigned long twodigits; typedef long stwodigits; /* signed variant of twodigits */ #define PyLong_SHIFT 15 +#define _PyLong_DECIMAL_SHIFT 4 /* max(e such that 10**e fits in a digit) */ +#define _PyLong_DECIMAL_BASE ((digit)10000) /* 10 ** DECIMAL_SHIFT */ #else #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" #endif Index: Objects/longobject.c =================================================================== --- Objects/longobject.c (revision 74768) +++ Objects/longobject.c (working copy) @@ -1650,6 +1650,125 @@ return long_normalize(z); } +/* Convert a long integer to a base 10 string. Returns a new non-shared + string. (Return value is non-shared so that callers can modify the + returned value if necessary.) */ + +static PyObject * +_PyLong_ToDecimal(PyLongObject *a) +{ + PyLongObject *scratch; + PyObject *str; + Py_ssize_t size, strlen, size_a, i, j; + digit *pout, *pin, rem, tenpow; + Py_UNICODE *p; + int negative; + + size_a = ABS(Py_SIZE(a)); + if (size_a == 0) { + str = PyUnicode_FromUnicode(NULL, 1); + if (str == NULL) + return NULL; + p = PyUnicode_AS_UNICODE(str); + *p++ = '0'; + *p++ = '\0'; + return (PyObject *)str; + } + negative = Py_SIZE(a) < 0; + + /* Make sure size_a*PyLong_SHIFT isn't going to overflow */ + if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { + PyErr_SetString(PyExc_OverflowError, + "long is too large to format"); + return NULL; + } + + /* quick and dirty upper bound for number of digits + required to express a in base _PyLong_DECIMAL_BASE: + + #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) + + But log2(a) < size_a*PyLong_SHIFT, and + log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT + > 3 * _PyLong_DECIMAL_SHIFT + */ + size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); + scratch = _PyLong_New(size); + if (scratch == NULL) { + Py_DECREF(str); + return NULL; + } + pin = a->ob_digit; + pout = scratch->ob_digit; + + /* convert array of base _PyLong_BASE digits in pin to an array of + base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, + Volume 2 (3rd edn), section 4.4, Method 1b). */ + size = 0; + for (i=size_a; --i >= 0; ) { + rem = pin[i]; + for (j = 0; j < size; j++) { + twodigits z = (twodigits)pout[j] << PyLong_SHIFT | rem; + rem = z / _PyLong_DECIMAL_BASE; + pout[j] = z - (twodigits)rem * _PyLong_DECIMAL_BASE; + } + while (rem != 0) { + digit q = rem / _PyLong_DECIMAL_BASE; + pout[size++] = rem - q * _PyLong_DECIMAL_BASE; + rem = q; + } + /* check for keyboard interrupt */ + SIGCHECK({ + Py_DECREF(scratch); + return NULL; + }) + } + assert(size > 0); + + /* how large a string do we need? */ + strlen = negative + (size - 1) * _PyLong_DECIMAL_SHIFT; + tenpow = 1; + rem = pout[size-1]; + while (rem >= tenpow) { + tenpow *= 10; + strlen++; + } + str = PyUnicode_FromUnicode(NULL, strlen); + if (str == NULL) { + Py_DECREF(scratch); + return NULL; + } + + /* fill the string right-to-left */ + p = PyUnicode_AS_UNICODE(str) + strlen; + *p = '\0'; + /* pout[0] through pout[size-2] contribute exactly + _PyLong_DECIMAL_SHIFT digits each */ + for (i=0; i < size - 1; i++) { + rem = pout[i]; + for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { + digit q = rem / 10; + *--p = '0' + (rem - 10 * q); + rem = q; + } + } + /* top digit */ + rem = pout[i]; + while (rem != 0) { + digit q = rem / 10; + *--p = '0' + (rem - 10 * q); + rem = q; + } + /* and sign */ + if (negative) + *--p = '-'; + + /* check we've counted correctly */ + assert (p == PyUnicode_AS_UNICODE(str)); + Py_DECREF(scratch); + return (PyObject *)str; +} + /* Convert a long int object to a string, using a given conversion base. Return a string object. If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ @@ -1669,6 +1788,10 @@ PyErr_BadInternalCall(); return NULL; } + + if (base == 10) + return _PyLong_ToDecimal(a); + assert(base >= 2 && base <= 36); size_a = ABS(Py_SIZE(a));