diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-13 23:37:18 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-27 00:04:18 +0300 |
commit | e7f2b4c875fa3130e4ad37721a7d231380456895 (patch) | |
tree | 11b053e49d893277e3522552ae5a16a1ab907c8f /py/objstrunicode.c | |
parent | 86d3898e709f32bef9e44dd558e7ea5569398011 (diff) | |
download | micropython-e7f2b4c875fa3130e4ad37721a7d231380456895.tar.gz micropython-e7f2b4c875fa3130e4ad37721a7d231380456895.zip |
objstrunicode: Revamp len() handling for unicode, and optimize bool().
Diffstat (limited to 'py/objstrunicode.c')
-rw-r--r-- | py/objstrunicode.c | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c index d41e92db4a..8fbe81532a 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -100,6 +100,18 @@ STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, } } +STATIC mp_obj_t uni_unary_op(int op, mp_obj_t self_in) { + GET_STR_DATA_LEN(self_in, str_data, str_len); + switch (op) { + case MP_UNARY_OP_BOOL: + return MP_BOOL(str_len != 0); + case MP_UNARY_OP_LEN: + return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len)); + default: + return MP_OBJ_NULL; // op not supported + } +} + STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { #if MICROPY_CPYTHON_COMPAT if (n_kw != 0) { @@ -297,6 +309,7 @@ const mp_obj_type_t mp_type_str = { .name = MP_QSTR_str, .print = uni_print, .make_new = str_make_new, + .unary_op = uni_unary_op, .binary_op = str_binary_op, .subscr = str_subscr, .getiter = mp_obj_new_str_iterator, |