1.1 --- a/lib/__builtins__/character.py Thu Dec 01 23:27:23 2016 +0100
1.2 +++ b/lib/__builtins__/character.py Thu Dec 01 23:48:24 2016 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Character-related functions.
1.6
1.7 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -19,10 +19,21 @@
1.13 this program. If not, see <http://www.gnu.org/licenses/>.
1.14 """
1.15
1.16 +from native import _str_ord
1.17 +
1.18 def chr(i): pass
1.19 def hex(number): pass
1.20 def oct(number): pass
1.21 -def ord(c): pass
1.22 +
1.23 +def ord(c):
1.24 +
1.25 + "Return the value of the given character 'c'."
1.26 +
1.27 + if isinstance(c, string) and len(c) == 1:
1.28 + return _str_ord(c)
1.29 + else:
1.30 + raise ValueError(c)
1.31 +
1.32 def unichr(i): pass
1.33
1.34 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/native.py Thu Dec 01 23:27:23 2016 +0100
2.2 +++ b/lib/native.py Thu Dec 01 23:48:24 2016 +0100
2.3 @@ -55,11 +55,12 @@
2.4 # String operations.
2.5
2.6 def _str_add(self, other): pass
2.7 -def _str_lt(self, other): pass
2.8 +def _str_eq(self, other): pass
2.9 def _str_gt(self, other): pass
2.10 -def _str_eq(self, other): pass
2.11 +def _str_lt(self, other): pass
2.12 def _str_len(self): pass
2.13 def _str_nonempty(self): pass
2.14 +def _str_ord(self): pass
2.15 def _str_substr(self, start, size): pass
2.16
2.17 # List operations.
3.1 --- a/templates/native.c Thu Dec 01 23:27:23 2016 +0100
3.2 +++ b/templates/native.c Thu Dec 01 23:48:24 2016 +0100
3.3 @@ -363,6 +363,15 @@
3.4 return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False;
3.5 }
3.6
3.7 +__attr __fn_native__str_ord(__attr __args[])
3.8 +{
3.9 + __attr * const self = &__args[1];
3.10 + /* self.__data__ interpreted as string */
3.11 + char *s = __load_via_object(self->value, __pos___data__).strvalue;
3.12 +
3.13 + return __new_int((unsigned int) s[0]);
3.14 +}
3.15 +
3.16 __attr __fn_native__str_substr(__attr __args[])
3.17 {
3.18 __attr * const self = &__args[1];
4.1 --- a/templates/native.h Thu Dec 01 23:27:23 2016 +0100
4.2 +++ b/templates/native.h Thu Dec 01 23:48:24 2016 +0100
4.3 @@ -40,6 +40,7 @@
4.4 __attr __fn_native__str_eq(__attr __args[]);
4.5 __attr __fn_native__str_len(__attr __args[]);
4.6 __attr __fn_native__str_nonempty(__attr __args[]);
4.7 +__attr __fn_native__str_ord(__attr __args[]);
4.8 __attr __fn_native__str_substr(__attr __args[]);
4.9
4.10 __attr __fn_native__list_init(__attr __args[]);
5.1 --- a/tests/string.py Thu Dec 01 23:27:23 2016 +0100
5.2 +++ b/tests/string.py Thu Dec 01 23:48:24 2016 +0100
5.3 @@ -13,3 +13,10 @@
5.4
5.5 print s[0] # H
5.6 print s[-1] # !
5.7 +
5.8 +print ord(s[0]) # 72
5.9 +
5.10 +try:
5.11 + print ord(s) # should raise an exception
5.12 +except ValueError, exc:
5.13 + print "ord(s): value is not appropriate", exc.value