mirror of https://github.com/python/cpython
long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
This commit is contained in:
parent
881fd90fd6
commit
586b2e3f3d
|
@ -792,38 +792,26 @@ long_format(PyObject *aa, int base, int addL)
|
||||||
}
|
}
|
||||||
else if ((base & (base - 1)) == 0) {
|
else if ((base & (base - 1)) == 0) {
|
||||||
/* JRH: special case for power-of-2 bases */
|
/* JRH: special case for power-of-2 bases */
|
||||||
twodigits temp = a->ob_digit[0];
|
twodigits accum = 0;
|
||||||
int bitsleft = SHIFT;
|
int accumbits = 0; /* # of bits in accum */
|
||||||
int rem;
|
int basebits = 1; /* # of bits in base-1 */
|
||||||
int last = size_a;
|
|
||||||
int basebits = 1;
|
|
||||||
i = base;
|
i = base;
|
||||||
while ((i >>= 1) > 1)
|
while ((i >>= 1) > 1)
|
||||||
++basebits;
|
++basebits;
|
||||||
|
|
||||||
i = 0;
|
for (i = 0; i < size_a; ++i) {
|
||||||
for (;;) {
|
accum |= a->ob_digit[i] << accumbits;
|
||||||
while (bitsleft >= basebits) {
|
accumbits += SHIFT;
|
||||||
if ((temp == 0) && (i >= last - 1)) break;
|
assert(accumbits >= basebits);
|
||||||
rem = temp & (base - 1);
|
do {
|
||||||
if (rem < 10)
|
char digit = (char)(accum & (base - 1));
|
||||||
rem += '0';
|
digit += (digit < 10) ? '0' : 'A'-10;
|
||||||
else
|
|
||||||
rem += 'A' - 10;
|
|
||||||
assert(p > PyString_AS_STRING(str));
|
assert(p > PyString_AS_STRING(str));
|
||||||
*--p = (char) rem;
|
*--p = digit;
|
||||||
bitsleft -= basebits;
|
accumbits -= basebits;
|
||||||
temp >>= basebits;
|
accum >>= basebits;
|
||||||
}
|
} while (i < size_a-1 ? accumbits >= basebits :
|
||||||
if (++i >= last) {
|
accum > 0);
|
||||||
if (temp == 0) break;
|
|
||||||
bitsleft = 99;
|
|
||||||
/* loop again to pick up final digits */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
temp = (a->ob_digit[i] << bitsleft) | temp;
|
|
||||||
bitsleft += SHIFT;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in New Issue