paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Character-related functions. |
paul@6 | 5 | |
paul@296 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@354 | 22 | from native import str_ord |
paul@296 | 23 | |
paul@6 | 24 | def chr(i): pass |
paul@374 | 25 | |
paul@374 | 26 | _hexdigits = "0123456789abcdef" |
paul@374 | 27 | |
paul@374 | 28 | def _base(number, base, prefix=""): |
paul@374 | 29 | |
paul@374 | 30 | """ |
paul@374 | 31 | Return 'number' encoded in the given 'base', prefixed with 'prefix'. |
paul@374 | 32 | """ |
paul@374 | 33 | |
paul@374 | 34 | if number < 0: |
paul@374 | 35 | number = -number |
paul@374 | 36 | sign = "-" |
paul@374 | 37 | else: |
paul@374 | 38 | sign = "" |
paul@374 | 39 | |
paul@374 | 40 | digits = [] |
paul@374 | 41 | |
paul@374 | 42 | while number: |
paul@374 | 43 | digits.append(_hexdigits[number % base]) |
paul@374 | 44 | number = number / base |
paul@374 | 45 | |
paul@374 | 46 | digits.append(prefix) |
paul@374 | 47 | |
paul@374 | 48 | if sign: |
paul@374 | 49 | digits.append(sign) |
paul@374 | 50 | |
paul@374 | 51 | return "".join(reversed(digits)) |
paul@374 | 52 | |
paul@374 | 53 | def hex(number, prefix="0x"): |
paul@374 | 54 | |
paul@374 | 55 | """ |
paul@374 | 56 | Return 'number' encoded as a hexadecimal (base 16) value, prefixed with |
paul@374 | 57 | 'prefix' ("0x" by default). |
paul@374 | 58 | """ |
paul@374 | 59 | |
paul@374 | 60 | return _base(number, 16, prefix) |
paul@374 | 61 | |
paul@374 | 62 | def oct(number, prefix="0"): |
paul@374 | 63 | |
paul@374 | 64 | """ |
paul@374 | 65 | Return 'number' encoded as an octal (base 8) value, prefixed with 'prefix' |
paul@374 | 66 | ("0" by default). |
paul@374 | 67 | """ |
paul@374 | 68 | |
paul@374 | 69 | return _base(number, 8, prefix) |
paul@296 | 70 | |
paul@296 | 71 | def ord(c): |
paul@296 | 72 | |
paul@296 | 73 | "Return the value of the given character 'c'." |
paul@296 | 74 | |
paul@296 | 75 | if isinstance(c, string) and len(c) == 1: |
paul@354 | 76 | return str_ord(c.__data__) |
paul@296 | 77 | else: |
paul@296 | 78 | raise ValueError(c) |
paul@296 | 79 | |
paul@6 | 80 | def unichr(i): pass |
paul@6 | 81 | |
paul@6 | 82 | # vim: tabstop=4 expandtab shiftwidth=4 |