1.1 --- a/lib/__builtins__/character.py Mon Feb 20 12:59:17 2017 +0100
1.2 +++ b/lib/__builtins__/character.py Mon Feb 20 18:48:39 2017 +0100
1.3 @@ -20,7 +20,7 @@
1.4 """
1.5
1.6 from __builtins__.types import check_int, check_string
1.7 -from native import str_chr
1.8 +from native import str_chr, unicode_unichr
1.9
1.10 def chr(i):
1.11
1.12 @@ -87,6 +87,15 @@
1.13
1.14 return c.__ord__()
1.15
1.16 -def unichr(i): pass
1.17 +def unichr(i):
1.18 +
1.19 + "Return the given character value 'i' encoded as a character."
1.20 +
1.21 + check_int(i)
1.22 +
1.23 + if 0 <= i <= 2097151:
1.24 + return utf8string(unicode_unichr(i.__data__))
1.25 + else:
1.26 + raise ValueError, i
1.27
1.28 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/native/__init__.py Mon Feb 20 12:59:17 2017 +0100
2.2 +++ b/lib/native/__init__.py Mon Feb 20 18:48:39 2017 +0100
2.3 @@ -47,6 +47,6 @@
2.4
2.5 from native.system import exit, get_argv, get_path
2.6
2.7 -from native.unicode import unicode_len, unicode_ord, unicode_substr
2.8 +from native.unicode import unicode_len, unicode_ord, unicode_substr, unicode_unichr
2.9
2.10 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/lib/native/unicode.py Mon Feb 20 12:59:17 2017 +0100
3.2 +++ b/lib/native/unicode.py Mon Feb 20 18:48:39 2017 +0100
3.3 @@ -29,5 +29,6 @@
3.4 def unicode_len(data): pass
3.5 def unicode_ord(data): pass
3.6 def unicode_substr(data, start, end, step): pass
3.7 +def unicode_unichr(value): pass
3.8
3.9 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/templates/native/unicode.c Mon Feb 20 12:59:17 2017 +0100
4.2 +++ b/templates/native/unicode.c Mon Feb 20 18:48:39 2017 +0100
4.3 @@ -1,6 +1,6 @@
4.4 /* Native functions for Unicode operations.
4.5
4.6 -Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
4.7 +Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
4.8
4.9 This program is free software; you can redistribute it and/or modify it under
4.10 the terms of the GNU General Public License as published by the Free Software
4.11 @@ -193,6 +193,48 @@
4.12 return __new_str(sub, resultsize);
4.13 }
4.14
4.15 +__attr __fn_native_unicode_unicode_unichr(__attr __args[])
4.16 +{
4.17 + __attr * const value = &__args[1];
4.18 + /* value interpreted as int */
4.19 + int i = value->intvalue;
4.20 + unsigned int resultsize;
4.21 + char *s;
4.22 +
4.23 + if (i < 128) resultsize = 1;
4.24 + else if (i < 2048) resultsize = 2;
4.25 + else if (i < 65536) resultsize = 3;
4.26 + else resultsize = 4;
4.27 +
4.28 + /* Reserve space for a new string. */
4.29 +
4.30 + s = (char *) __ALLOCATE(resultsize + 1, sizeof(char));
4.31 +
4.32 + /* Populate the string. */
4.33 +
4.34 + if (i < 128) s[0] = (char) i;
4.35 + else if (i < 2048)
4.36 + {
4.37 + s[0] = 0b11000000 | (i >> 6);
4.38 + s[1] = 0b10000000 | (i & 0b00111111);
4.39 + }
4.40 + else if (i < 65536)
4.41 + {
4.42 + s[0] = 0b11100000 | (i >> 12);
4.43 + s[1] = 0b10000000 | ((i >> 6) & 0b00111111);
4.44 + s[2] = 0b10000000 | (i & 0b00111111);
4.45 + }
4.46 + else
4.47 + {
4.48 + s[0] = 0b11110000 | (i >> 18);
4.49 + s[1] = 0b10000000 | ((i >> 12) & 0b00111111);
4.50 + s[2] = 0b10000000 | ((i >> 6) & 0b00111111);
4.51 + s[3] = 0b10000000 | (i & 0b00111111);
4.52 + }
4.53 +
4.54 + return __new_str(s, resultsize);
4.55 +}
4.56 +
4.57 /* Module initialisation. */
4.58
4.59 void __main_native_unicode()
5.1 --- a/templates/native/unicode.h Mon Feb 20 12:59:17 2017 +0100
5.2 +++ b/templates/native/unicode.h Mon Feb 20 18:48:39 2017 +0100
5.3 @@ -24,6 +24,7 @@
5.4 __attr __fn_native_unicode_unicode_len(__attr __args[]);
5.5 __attr __fn_native_unicode_unicode_ord(__attr __args[]);
5.6 __attr __fn_native_unicode_unicode_substr(__attr __args[]);
5.7 +__attr __fn_native_unicode_unicode_unichr(__attr __args[]);
5.8
5.9 /* Module initialisation. */
5.10
6.1 --- a/tests/unicode.py Mon Feb 20 12:59:17 2017 +0100
6.2 +++ b/tests/unicode.py Mon Feb 20 18:48:39 2017 +0100
6.3 @@ -212,3 +212,5 @@
6.4 print repr(euro) # "\u20ac"
6.5 print ord(euro) # 8364
6.6 print "\u20ac" # ¤
6.7 +print unichr(ord(euro)) # ¤
6.8 +print unichr(ord(euro)) == euro # True