paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Variable-length integer functions. |
paul@44 | 5 | |
paul@70 | 6 | Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@44 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@44 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@44 | 16 | |
paul@44 | 17 | You should have received a copy of the GNU General Public License along |
paul@44 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 19 | """ |
paul@44 | 20 | |
paul@55 | 21 | from array import array |
paul@55 | 22 | |
paul@51 | 23 | vint_cache = {} |
paul@56 | 24 | vint_bytes_cache = {} |
paul@44 | 25 | |
paul@51 | 26 | def vint(number): |
paul@44 | 27 | |
paul@51 | 28 | "Write 'number' as a variable-length integer." |
paul@44 | 29 | |
paul@51 | 30 | try: |
paul@51 | 31 | return vint_cache[number] |
paul@51 | 32 | except KeyError: |
paul@44 | 33 | if number >= 0: |
paul@55 | 34 | bytes = array('B') |
paul@56 | 35 | _vint_to_array(number, bytes) |
paul@55 | 36 | return bytes.tostring() |
paul@44 | 37 | |
paul@44 | 38 | # Negative numbers are not supported. |
paul@44 | 39 | |
paul@44 | 40 | else: |
paul@44 | 41 | raise ValueError, "Number %r is negative." % number |
paul@44 | 42 | |
paul@56 | 43 | def vint_to_array(number, bytes): |
paul@56 | 44 | |
paul@56 | 45 | "Write 'number' as a variable-length integer to 'bytes'." |
paul@56 | 46 | |
paul@56 | 47 | try: |
paul@56 | 48 | bytes += vint_bytes_cache[number] |
paul@56 | 49 | except KeyError: |
paul@56 | 50 | if number >= 0: |
paul@56 | 51 | _vint_to_array(number, bytes) |
paul@56 | 52 | |
paul@56 | 53 | # Negative numbers are not supported. |
paul@56 | 54 | |
paul@56 | 55 | else: |
paul@56 | 56 | raise ValueError, "Number %r is negative." % number |
paul@56 | 57 | |
paul@56 | 58 | def _vint_to_array(number, bytes): |
paul@56 | 59 | |
paul@56 | 60 | "Write the 'number' to 'bytes' from least to most significant digits." |
paul@56 | 61 | |
paul@56 | 62 | while number > 127: |
paul@56 | 63 | bytes.append(number & 127 | 128) |
paul@56 | 64 | number = number >> 7 |
paul@56 | 65 | else: |
paul@56 | 66 | bytes.append(number) |
paul@56 | 67 | |
paul@70 | 68 | def string_to_array(s, bytes): |
paul@70 | 69 | |
paul@70 | 70 | "Write the given string 's' to 'bytes'." |
paul@70 | 71 | |
paul@70 | 72 | vint_to_array(len(s), bytes) |
paul@70 | 73 | bytes.fromstring(s.encode("utf-8")) |
paul@70 | 74 | |
paul@55 | 75 | for i in xrange(0, 65536): |
paul@56 | 76 | bytes = array('B') |
paul@56 | 77 | _vint_to_array(i, bytes) |
paul@56 | 78 | vint_bytes_cache[i] = bytes |
paul@56 | 79 | vint_cache[i] = bytes.tostring() |
paul@51 | 80 | |
paul@44 | 81 | # vim: tabstop=4 expandtab shiftwidth=4 |