summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-07-15 09:11:14 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-07-15 09:11:14 (GMT)
commit586b2e3f3df6134064af4231215c13d221087f3a (patch)
tree47ba8c7a063a2fbb759d889473a1c2baf90d65e8 /Objects/longobject.c
parent881fd90fd62a1fa113efefae94bf744101fa39de (diff)
downloadcpython-586b2e3f3df6134064af4231215c13d221087f3a.zip
cpython-586b2e3f3df6134064af4231215c13d221087f3a.tar.gz
cpython-586b2e3f3df6134064af4231215c13d221087f3a.tar.bz2
long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c44
1 files changed, 16 insertions, 28 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index ad13b07..6e337d0 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -792,38 +792,26 @@ long_format(PyObject *aa, int base, int addL)
}
else if ((base & (base - 1)) == 0) {
/* JRH: special case for power-of-2 bases */
- twodigits temp = a->ob_digit[0];
- int bitsleft = SHIFT;
- int rem;
- int last = size_a;
- int basebits = 1;
+ twodigits accum = 0;
+ int accumbits = 0; /* # of bits in accum */
+ int basebits = 1; /* # of bits in base-1 */
i = base;
while ((i >>= 1) > 1)
++basebits;
-
- i = 0;
- for (;;) {
- while (bitsleft >= basebits) {
- if ((temp == 0) && (i >= last - 1)) break;
- rem = temp & (base - 1);
- if (rem < 10)
- rem += '0';
- else
- rem += 'A' - 10;
+
+ for (i = 0; i < size_a; ++i) {
+ accum |= a->ob_digit[i] << accumbits;
+ accumbits += SHIFT;
+ assert(accumbits >= basebits);
+ do {
+ char digit = (char)(accum & (base - 1));
+ digit += (digit < 10) ? '0' : 'A'-10;
assert(p > PyString_AS_STRING(str));
- *--p = (char) rem;
- bitsleft -= basebits;
- temp >>= basebits;
- }
- if (++i >= last) {
- if (temp == 0) break;
- bitsleft = 99;
- /* loop again to pick up final digits */
- }
- else {
- temp = (a->ob_digit[i] << bitsleft) | temp;
- bitsleft += SHIFT;
- }
+ *--p = digit;
+ accumbits -= basebits;
+ accum >>= basebits;
+ } while (i < size_a-1 ? accumbits >= basebits :
+ accum > 0);
}
}
else {