Lichen

lib/__builtins__/character.py

794:7dd8e4815848
2017-03-31 Paul Boddie Introduced a multiple outcome result which can be used to provide a choice of return values to supply the initialiser and alias details.
     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 hex(number, prefix="0x"):    67     68     """    69     Return 'number' encoded as a hexadecimal (base 16) value, prefixed with    70     'prefix' ("0x" by default).    71     """    72     73     return _base(number, 16, prefix)    74     75 def oct(number, prefix="0"):    76     77     """    78     Return 'number' encoded as an octal (base 8) value, prefixed with 'prefix'    79     ("0" by default).    80     """    81     82     return _base(number, 8, prefix)    83     84 def ord(c):    85     86     "Return the value of the given character 'c'."    87     88     return c.__ord__()    89     90 def unichr(i):    91     92     "Return the given character value 'i' encoded as a character."    93     94     check_int(i)    95     96     if 0 <= i <= 2097151:    97         return utf8string(unicode_unichr(i))    98     else:    99         raise ValueError, i   100    101 # vim: tabstop=4 expandtab shiftwidth=4