# HG changeset patch # User Paul Boddie # Date 1487612919 -3600 # Node ID 8325575e2d240b528ef4c4380c7c53665f407f5b # Parent b12c85bb0a4ca52b15afa26dfba10eefcf094e1e Implemented the unichr function. diff -r b12c85bb0a4c -r 8325575e2d24 lib/__builtins__/character.py --- a/lib/__builtins__/character.py Mon Feb 20 12:59:17 2017 +0100 +++ b/lib/__builtins__/character.py Mon Feb 20 18:48:39 2017 +0100 @@ -20,7 +20,7 @@ """ from __builtins__.types import check_int, check_string -from native import str_chr +from native import str_chr, unicode_unichr def chr(i): @@ -87,6 +87,15 @@ return c.__ord__() -def unichr(i): pass +def unichr(i): + + "Return the given character value 'i' encoded as a character." + + check_int(i) + + if 0 <= i <= 2097151: + return utf8string(unicode_unichr(i.__data__)) + else: + raise ValueError, i # vim: tabstop=4 expandtab shiftwidth=4 diff -r b12c85bb0a4c -r 8325575e2d24 lib/native/__init__.py --- a/lib/native/__init__.py Mon Feb 20 12:59:17 2017 +0100 +++ b/lib/native/__init__.py Mon Feb 20 18:48:39 2017 +0100 @@ -47,6 +47,6 @@ from native.system import exit, get_argv, get_path -from native.unicode import unicode_len, unicode_ord, unicode_substr +from native.unicode import unicode_len, unicode_ord, unicode_substr, unicode_unichr # vim: tabstop=4 expandtab shiftwidth=4 diff -r b12c85bb0a4c -r 8325575e2d24 lib/native/unicode.py --- a/lib/native/unicode.py Mon Feb 20 12:59:17 2017 +0100 +++ b/lib/native/unicode.py Mon Feb 20 18:48:39 2017 +0100 @@ -29,5 +29,6 @@ def unicode_len(data): pass def unicode_ord(data): pass def unicode_substr(data, start, end, step): pass +def unicode_unichr(value): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r b12c85bb0a4c -r 8325575e2d24 templates/native/unicode.c --- a/templates/native/unicode.c Mon Feb 20 12:59:17 2017 +0100 +++ b/templates/native/unicode.c Mon Feb 20 18:48:39 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for Unicode operations. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -193,6 +193,48 @@ return __new_str(sub, resultsize); } +__attr __fn_native_unicode_unicode_unichr(__attr __args[]) +{ + __attr * const value = &__args[1]; + /* value interpreted as int */ + int i = value->intvalue; + unsigned int resultsize; + char *s; + + if (i < 128) resultsize = 1; + else if (i < 2048) resultsize = 2; + else if (i < 65536) resultsize = 3; + else resultsize = 4; + + /* Reserve space for a new string. */ + + s = (char *) __ALLOCATE(resultsize + 1, sizeof(char)); + + /* Populate the string. */ + + if (i < 128) s[0] = (char) i; + else if (i < 2048) + { + s[0] = 0b11000000 | (i >> 6); + s[1] = 0b10000000 | (i & 0b00111111); + } + else if (i < 65536) + { + s[0] = 0b11100000 | (i >> 12); + s[1] = 0b10000000 | ((i >> 6) & 0b00111111); + s[2] = 0b10000000 | (i & 0b00111111); + } + else + { + s[0] = 0b11110000 | (i >> 18); + s[1] = 0b10000000 | ((i >> 12) & 0b00111111); + s[2] = 0b10000000 | ((i >> 6) & 0b00111111); + s[3] = 0b10000000 | (i & 0b00111111); + } + + return __new_str(s, resultsize); +} + /* Module initialisation. */ void __main_native_unicode() diff -r b12c85bb0a4c -r 8325575e2d24 templates/native/unicode.h --- a/templates/native/unicode.h Mon Feb 20 12:59:17 2017 +0100 +++ b/templates/native/unicode.h Mon Feb 20 18:48:39 2017 +0100 @@ -24,6 +24,7 @@ __attr __fn_native_unicode_unicode_len(__attr __args[]); __attr __fn_native_unicode_unicode_ord(__attr __args[]); __attr __fn_native_unicode_unicode_substr(__attr __args[]); +__attr __fn_native_unicode_unicode_unichr(__attr __args[]); /* Module initialisation. */ diff -r b12c85bb0a4c -r 8325575e2d24 tests/unicode.py --- a/tests/unicode.py Mon Feb 20 12:59:17 2017 +0100 +++ b/tests/unicode.py Mon Feb 20 18:48:39 2017 +0100 @@ -212,3 +212,5 @@ print repr(euro) # "\u20ac" print ord(euro) # 8364 print "\u20ac" # ¤ +print unichr(ord(euro)) # ¤ +print unichr(ord(euro)) == euro # True