# HG changeset patch # User Paul Boddie # Date 1252625238 -7200 # Node ID 1293ffb9e43b1d38a6ad7686fe8cd728312d39f7 # Parent f0655c38ee18341a62731f8e2076f7b82f313361 Fixed Pyrex implementation for numbers from 0 to 127 inclusive. diff -r f0655c38ee18 -r 1293ffb9e43b iixr.py --- a/iixr.py Fri Sep 11 01:21:09 2009 +0200 +++ b/iixr.py Fri Sep 11 01:27:18 2009 +0200 @@ -48,7 +48,17 @@ # Utility functions. try: - from vint import vint + from vint import vint as _vint + + def vint(number): + + "Write 'number' as a variable-length integer." + + if number >= 0: + return _vint(number) + else: + raise ValueError, "Number %r is negative." % number + except ImportError: def vint(number): diff -r f0655c38ee18 -r 1293ffb9e43b vint.c --- a/vint.c Fri Sep 11 01:21:09 2009 +0200 +++ b/vint.c Fri Sep 11 01:27:18 2009 +0200 @@ -1,4 +1,4 @@ -/* Generated by Pyrex 0.9.6.4 on Fri Sep 11 01:17:16 2009 */ +/* Generated by Pyrex 0.9.6.4 on Fri Sep 11 01:26:01 2009 */ #define PY_SSIZE_T_CLEAN #include "Python.h" @@ -61,41 +61,62 @@ __pyx_v_s = Py_None; Py_INCREF(Py_None); /* "/home/paulb/Software/Python/iixr/vint.pyx":11 */ + __pyx_1 = (__pyx_v_number < 128); + if (__pyx_1) { + + /* "/home/paulb/Software/Python/iixr/vint.pyx":12 */ + (__pyx_v_bytes[0]) = __pyx_v_number; + + /* "/home/paulb/Software/Python/iixr/vint.pyx":13 */ + __pyx_2 = PyString_FromStringAndSize(__pyx_v_bytes,1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; goto __pyx_L1;} + Py_DECREF(__pyx_v_s); + __pyx_v_s = __pyx_2; + __pyx_2 = 0; + + /* "/home/paulb/Software/Python/iixr/vint.pyx":14 */ + Py_INCREF(__pyx_v_s); + __pyx_r = __pyx_v_s; + goto __pyx_L0; + goto __pyx_L2; + } + __pyx_L2:; + + /* "/home/paulb/Software/Python/iixr/vint.pyx":16 */ __pyx_v_i = 0; - /* "/home/paulb/Software/Python/iixr/vint.pyx":12 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":17 */ while (1) { __pyx_1 = (__pyx_v_number != 0); if (!__pyx_1) break; - /* "/home/paulb/Software/Python/iixr/vint.pyx":13 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":18 */ __pyx_v_lsd = (__pyx_v_number & 127); - /* "/home/paulb/Software/Python/iixr/vint.pyx":14 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":19 */ __pyx_v_number = (__pyx_v_number >> 7); - /* "/home/paulb/Software/Python/iixr/vint.pyx":15 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":20 */ __pyx_1 = (__pyx_v_number != 0); if (__pyx_1) { __pyx_v_lsd = (__pyx_v_lsd | 128); - goto __pyx_L4; + goto __pyx_L5; } - __pyx_L4:; + __pyx_L5:; - /* "/home/paulb/Software/Python/iixr/vint.pyx":17 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":22 */ (__pyx_v_bytes[__pyx_v_i]) = __pyx_v_lsd; - /* "/home/paulb/Software/Python/iixr/vint.pyx":18 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":23 */ __pyx_v_i = (__pyx_v_i + 1); } - /* "/home/paulb/Software/Python/iixr/vint.pyx":20 */ - __pyx_2 = PyString_FromStringAndSize(__pyx_v_bytes,__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; goto __pyx_L1;} + /* "/home/paulb/Software/Python/iixr/vint.pyx":25 */ + __pyx_2 = PyString_FromStringAndSize(__pyx_v_bytes,__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; goto __pyx_L1;} Py_DECREF(__pyx_v_s); __pyx_v_s = __pyx_2; __pyx_2 = 0; - /* "/home/paulb/Software/Python/iixr/vint.pyx":21 */ + /* "/home/paulb/Software/Python/iixr/vint.pyx":26 */ Py_INCREF(__pyx_v_s); __pyx_r = __pyx_v_s; goto __pyx_L0; diff -r f0655c38ee18 -r 1293ffb9e43b vint.pyx --- a/vint.pyx Fri Sep 11 01:21:09 2009 +0200 +++ b/vint.pyx Fri Sep 11 01:27:18 2009 +0200 @@ -8,6 +8,11 @@ cdef char bytes[40] # NOTE: Arbitrary limit. cdef int lsd, i + if number < 128: + bytes[0] = number + s = PyString_FromStringAndSize(bytes, 1) + return s + i = 0 while number != 0: lsd = number & 127