1 #!/usr/bin/env python 2 3 """ 4 A variable-length integer encoding implementation in Pyrex. 5 6 Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 cdef extern from "Python.h": 22 object PyString_FromStringAndSize(char *, int) 23 24 def vint(int number): 25 26 cdef char bytes[40] # NOTE: Arbitrary limit. 27 cdef int lsd, i 28 29 if number < 128: 30 bytes[0] = number 31 s = PyString_FromStringAndSize(bytes, 1) 32 return s 33 34 i = 0 35 while number != 0: 36 lsd = number & 127 37 number = number >> 7 38 if number != 0: 39 lsd = lsd | 128 40 bytes[i] = lsd 41 i = i + 1 42 43 s = PyString_FromStringAndSize(bytes, i) 44 return s 45 46 # vim: tabstop=4 expandtab shiftwidth=4