paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Character-related functions. |
paul@6 | 5 | |
paul@530 | 6 | Copyright (C) 2015, 2016, 2017 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@531 | 22 | from __builtins__.types import check_int, check_string |
paul@607 | 23 | from native import str_chr, unicode_unichr |
paul@531 | 24 | |
paul@531 | 25 | def chr(i): |
paul@531 | 26 | |
paul@531 | 27 | "Return a string containing a character having the value 'i'." |
paul@296 | 28 | |
paul@531 | 29 | check_int(i) |
paul@531 | 30 | |
paul@531 | 31 | if 0 <= i <= 255: |
paul@758 | 32 | return str_chr(i) |
paul@531 | 33 | else: |
paul@531 | 34 | raise ValueError, i |
paul@374 | 35 | |
paul@374 | 36 | _hexdigits = "0123456789abcdef" |
paul@374 | 37 | |
paul@374 | 38 | def _base(number, base, prefix=""): |
paul@374 | 39 | |
paul@374 | 40 | """ |
paul@374 | 41 | Return 'number' encoded in the given 'base', prefixed with 'prefix'. |
paul@374 | 42 | """ |
paul@374 | 43 | |
paul@374 | 44 | if number < 0: |
paul@374 | 45 | number = -number |
paul@374 | 46 | sign = "-" |
paul@374 | 47 | else: |
paul@374 | 48 | sign = "" |
paul@374 | 49 | |
paul@374 | 50 | digits = [] |
paul@374 | 51 | |
paul@530 | 52 | if number: |
paul@530 | 53 | while number: |
paul@530 | 54 | digits.append(_hexdigits[number % base]) |
paul@530 | 55 | number = number / base |
paul@530 | 56 | else: |
paul@530 | 57 | digits.append("0") |
paul@374 | 58 | |
paul@374 | 59 | digits.append(prefix) |
paul@374 | 60 | |
paul@374 | 61 | if sign: |
paul@374 | 62 | digits.append(sign) |
paul@374 | 63 | |
paul@374 | 64 | return "".join(reversed(digits)) |
paul@374 | 65 | |
paul@805 | 66 | def bin(number, prefix="0b"): |
paul@805 | 67 | |
paul@805 | 68 | """ |
paul@805 | 69 | Return 'number' encoded as a binary (base 2) value, prefixed with 'prefix' |
paul@805 | 70 | ("0b" by default). |
paul@805 | 71 | """ |
paul@805 | 72 | |
paul@805 | 73 | return _base(number, 2, prefix) |
paul@805 | 74 | |
paul@374 | 75 | def hex(number, prefix="0x"): |
paul@374 | 76 | |
paul@374 | 77 | """ |
paul@374 | 78 | Return 'number' encoded as a hexadecimal (base 16) value, prefixed with |
paul@374 | 79 | 'prefix' ("0x" by default). |
paul@374 | 80 | """ |
paul@374 | 81 | |
paul@374 | 82 | return _base(number, 16, prefix) |
paul@374 | 83 | |
paul@374 | 84 | def oct(number, prefix="0"): |
paul@374 | 85 | |
paul@374 | 86 | """ |
paul@374 | 87 | Return 'number' encoded as an octal (base 8) value, prefixed with 'prefix' |
paul@374 | 88 | ("0" by default). |
paul@374 | 89 | """ |
paul@374 | 90 | |
paul@374 | 91 | return _base(number, 8, prefix) |
paul@296 | 92 | |
paul@296 | 93 | def ord(c): |
paul@296 | 94 | |
paul@296 | 95 | "Return the value of the given character 'c'." |
paul@296 | 96 | |
paul@534 | 97 | return c.__ord__() |
paul@296 | 98 | |
paul@607 | 99 | def unichr(i): |
paul@607 | 100 | |
paul@607 | 101 | "Return the given character value 'i' encoded as a character." |
paul@607 | 102 | |
paul@607 | 103 | check_int(i) |
paul@607 | 104 | |
paul@607 | 105 | if 0 <= i <= 2097151: |
paul@758 | 106 | return utf8string(unicode_unichr(i)) |
paul@607 | 107 | else: |
paul@607 | 108 | raise ValueError, i |
paul@6 | 109 | |
paul@6 | 110 | # vim: tabstop=4 expandtab shiftwidth=4 |