Lichen

lib/__builtins__/character.py

932:c07b0dd14f85
10 months ago Paul Boddie Moved integer instantiation support to library functions.
     1 #!/usr/bin/env python     2      3 """     4 Character-related functions.     5      6 Copyright (C) 2015, 2016, 2017 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    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.types import check_int, check_string    23 from native import str_chr, unicode_unichr    24     25 def chr(i):    26     27     "Return a string containing a character having the value 'i'."    28     29     check_int(i)    30     31     if 0 <= i <= 255:    32         return str_chr(i)    33     else:    34         raise ValueError, i    35     36 _hexdigits = "0123456789abcdef"    37     38 def _base(number, base, prefix=""):    39     40     """    41     Return 'number' encoded in the given 'base', prefixed with 'prefix'.    42     """    43     44     if number < 0:    45         number = -number    46         sign = "-"    47     else:    48         sign = ""    49     50     digits = []    51     52     if number:    53         while number:    54             digits.append(_hexdigits[number % base])    55             number = number / base    56     else:    57         digits.append("0")    58     59     digits.append(prefix)    60     61     if sign:    62         digits.append(sign)    63     64     return "".join(reversed(digits))    65     66 def bin(number, prefix="0b"):    67     68     """    69     Return 'number' encoded as a binary (base 2) value, prefixed with 'prefix'    70     ("0b" by default).    71     """    72     73     return _base(number, 2, prefix)    74     75 def hex(number, prefix="0x"):    76     77     """    78     Return 'number' encoded as a hexadecimal (base 16) value, prefixed with    79     'prefix' ("0x" by default).    80     """    81     82     return _base(number, 16, prefix)    83     84 def oct(number, prefix="0"):    85     86     """    87     Return 'number' encoded as an octal (base 8) value, prefixed with 'prefix'    88     ("0" by default).    89     """    90     91     return _base(number, 8, prefix)    92     93 def ord(c):    94     95     "Return the value of the given character 'c'."    96     97     return c.__ord__()    98     99 def unichr(i):   100    101     "Return the given character value 'i' encoded as a character."   102    103     check_int(i)   104    105     if 0 <= i <= 2097151:   106         return utf8string(unicode_unichr(i))   107     else:   108         raise ValueError, i   109    110 # vim: tabstop=4 expandtab shiftwidth=4