1.1 --- a/lib/__builtins__/character.py Thu Feb 02 23:15:24 2017 +0100
1.2 +++ b/lib/__builtins__/character.py Thu Feb 02 23:17:08 2017 +0100
1.3 @@ -19,9 +19,19 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from native import str_ord
1.8 +from __builtins__.types import check_int, check_string
1.9 +from native import str_chr, str_ord
1.10 +
1.11 +def chr(i):
1.12 +
1.13 + "Return a string containing a character having the value 'i'."
1.14
1.15 -def chr(i): pass
1.16 + check_int(i)
1.17 +
1.18 + if 0 <= i <= 255:
1.19 + return str_chr(i.__data__)
1.20 + else:
1.21 + raise ValueError, i
1.22
1.23 _hexdigits = "0123456789abcdef"
1.24
1.25 @@ -75,10 +85,12 @@
1.26
1.27 "Return the value of the given character 'c'."
1.28
1.29 - if isinstance(c, string) and len(c) == 1:
1.30 + check_string(c)
1.31 +
1.32 + if c.__len__() == 1:
1.33 return str_ord(c.__data__)
1.34 else:
1.35 - raise ValueError(c)
1.36 + raise ValueError, c
1.37
1.38 def unichr(i): pass
1.39
2.1 --- a/lib/native/__init__.py Thu Feb 02 23:15:24 2017 +0100
2.2 +++ b/lib/native/__init__.py Thu Feb 02 23:17:08 2017 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Native library functions.
2.6
2.7 -Copyright (C) 2011, 2015, 2016 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2011, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -42,8 +42,8 @@
2.13
2.14 from native.program import get_using
2.15
2.16 -from native.str import str_add, str_eq, str_gt, str_lt, str_len, str_nonempty, \
2.17 - str_ord, str_substr
2.18 +from native.str import str_add, str_chr, str_eq, str_gt, str_lt, str_len, \
2.19 + str_nonempty, str_ord, str_substr
2.20
2.21 from native.system import exit, get_argv, get_path
2.22
3.1 --- a/lib/native/str.py Thu Feb 02 23:15:24 2017 +0100
3.2 +++ b/lib/native/str.py Thu Feb 02 23:17:08 2017 +0100
3.3 @@ -8,7 +8,7 @@
3.4 non-core exceptions used by the native functions because they need to be
3.5 identified as being needed by the program.
3.6
3.7 -Copyright (C) 2011, 2015, 2016 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2011, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -27,6 +27,7 @@
3.13 # String operations.
3.14
3.15 def str_add(data, other_data): pass
3.16 +def str_chr(data): pass
3.17 def str_eq(data, other_data): pass
3.18 def str_gt(data, other_data): pass
3.19 def str_lt(data, other_data): pass
4.1 --- a/templates/native/str.c Thu Feb 02 23:15:24 2017 +0100
4.2 +++ b/templates/native/str.c Thu Feb 02 23:17:08 2017 +0100
4.3 @@ -1,6 +1,6 @@
4.4 /* Native functions for string 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 @@ -45,6 +45,17 @@
4.12 return __new_str(r, n);
4.13 }
4.14
4.15 +__attr __fn_native_str_str_chr(__attr __args[])
4.16 +{
4.17 + __attr * const _data = &__args[1];
4.18 + /* _data interpreted as int */
4.19 + int n = _data->intvalue;
4.20 + char *s = (char *) __ALLOCATE(2, sizeof(char));
4.21 +
4.22 + s[0] = (char) n;
4.23 + return __new_str(s, 1);
4.24 +}
4.25 +
4.26 __attr __fn_native_str_str_lt(__attr __args[])
4.27 {
4.28 __attr * const _data = &__args[1];
5.1 --- a/templates/native/str.h Thu Feb 02 23:15:24 2017 +0100
5.2 +++ b/templates/native/str.h Thu Feb 02 23:17:08 2017 +0100
5.3 @@ -1,6 +1,6 @@
5.4 /* Native functions for string operations.
5.5
5.6 -Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
5.7 +Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
5.8
5.9 This program is free software; you can redistribute it and/or modify it under
5.10 the terms of the GNU General Public License as published by the Free Software
5.11 @@ -22,6 +22,7 @@
5.12 /* String operations. */
5.13
5.14 __attr __fn_native_str_str_add(__attr __args[]);
5.15 +__attr __fn_native_str_str_chr(__attr __args[]);
5.16 __attr __fn_native_str_str_lt(__attr __args[]);
5.17 __attr __fn_native_str_str_gt(__attr __args[]);
5.18 __attr __fn_native_str_str_eq(__attr __args[]);
6.1 --- a/tests/string.py Thu Feb 02 23:15:24 2017 +0100
6.2 +++ b/tests/string.py Thu Feb 02 23:17:08 2017 +0100
6.3 @@ -49,6 +49,14 @@
6.4 except ValueError, exc:
6.5 print "ord(s): value is not appropriate", repr(exc.value)
6.6
6.7 +print chr(72) # H
6.8 +print repr(chr(0)) # "\x00"
6.9 +
6.10 +try:
6.11 + print repr(chr(-1)) # should raise an exception
6.12 +except ValueError, exc:
6.13 + print "chr(-1): value is not appropriate", exc.value
6.14 +
6.15 l = ["Hello", "world!"]
6.16 s3 = " ".join(l)
6.17 print s3 # Hello world!