# HG changeset patch # User Paul Boddie # Date 1480632504 -3600 # Node ID 3c7946627c2dc82acbdffbd510f459cc5e4d55a9 # Parent cb64d3e04c11a436e3d4a3386ae3dc3e529527d7 Added support for the ord built-in function. diff -r cb64d3e04c11 -r 3c7946627c2d lib/__builtins__/character.py --- a/lib/__builtins__/character.py Thu Dec 01 23:27:23 2016 +0100 +++ b/lib/__builtins__/character.py Thu Dec 01 23:48:24 2016 +0100 @@ -3,7 +3,7 @@ """ Character-related functions. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 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 @@ -19,10 +19,21 @@ this program. If not, see . """ +from native import _str_ord + def chr(i): pass def hex(number): pass def oct(number): pass -def ord(c): pass + +def ord(c): + + "Return the value of the given character 'c'." + + if isinstance(c, string) and len(c) == 1: + return _str_ord(c) + else: + raise ValueError(c) + def unichr(i): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r cb64d3e04c11 -r 3c7946627c2d lib/native.py --- a/lib/native.py Thu Dec 01 23:27:23 2016 +0100 +++ b/lib/native.py Thu Dec 01 23:48:24 2016 +0100 @@ -55,11 +55,12 @@ # String operations. def _str_add(self, other): pass -def _str_lt(self, other): pass +def _str_eq(self, other): pass def _str_gt(self, other): pass -def _str_eq(self, other): pass +def _str_lt(self, other): pass def _str_len(self): pass def _str_nonempty(self): pass +def _str_ord(self): pass def _str_substr(self, start, size): pass # List operations. diff -r cb64d3e04c11 -r 3c7946627c2d templates/native.c --- a/templates/native.c Thu Dec 01 23:27:23 2016 +0100 +++ b/templates/native.c Thu Dec 01 23:48:24 2016 +0100 @@ -363,6 +363,15 @@ return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False; } +__attr __fn_native__str_ord(__attr __args[]) +{ + __attr * const self = &__args[1]; + /* self.__data__ interpreted as string */ + char *s = __load_via_object(self->value, __pos___data__).strvalue; + + return __new_int((unsigned int) s[0]); +} + __attr __fn_native__str_substr(__attr __args[]) { __attr * const self = &__args[1]; diff -r cb64d3e04c11 -r 3c7946627c2d templates/native.h --- a/templates/native.h Thu Dec 01 23:27:23 2016 +0100 +++ b/templates/native.h Thu Dec 01 23:48:24 2016 +0100 @@ -40,6 +40,7 @@ __attr __fn_native__str_eq(__attr __args[]); __attr __fn_native__str_len(__attr __args[]); __attr __fn_native__str_nonempty(__attr __args[]); +__attr __fn_native__str_ord(__attr __args[]); __attr __fn_native__str_substr(__attr __args[]); __attr __fn_native__list_init(__attr __args[]); diff -r cb64d3e04c11 -r 3c7946627c2d tests/string.py --- a/tests/string.py Thu Dec 01 23:27:23 2016 +0100 +++ b/tests/string.py Thu Dec 01 23:48:24 2016 +0100 @@ -13,3 +13,10 @@ print s[0] # H print s[-1] # ! + +print ord(s[0]) # 72 + +try: + print ord(s) # should raise an exception +except ValueError, exc: + print "ord(s): value is not appropriate", exc.value