Commit e7f2b4c8 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

objstrunicode: Revamp len() handling for unicode, and optimize bool().

parent 86d3898e
...@@ -354,7 +354,12 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, ...@@ -354,7 +354,12 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index,
// may return MP_OBJ_NULL // may return MP_OBJ_NULL
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) { if (
#if !MICROPY_PY_BUILTINS_STR_UNICODE
// It's simple - unicode is slow, non-unicode is fast
MP_OBJ_IS_STR(o_in) ||
#endif
MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in)); return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
} else { } else {
mp_obj_type_t *type = mp_obj_get_type(o_in); mp_obj_type_t *type = mp_obj_get_type(o_in);
......
...@@ -100,6 +100,18 @@ STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, ...@@ -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) { 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 MICROPY_CPYTHON_COMPAT
if (n_kw != 0) { if (n_kw != 0) {
...@@ -297,6 +309,7 @@ const mp_obj_type_t mp_type_str = { ...@@ -297,6 +309,7 @@ const mp_obj_type_t mp_type_str = {
.name = MP_QSTR_str, .name = MP_QSTR_str,
.print = uni_print, .print = uni_print,
.make_new = str_make_new, .make_new = str_make_new,
.unary_op = uni_unary_op,
.binary_op = str_binary_op, .binary_op = str_binary_op,
.subscr = str_subscr, .subscr = str_subscr,
.getiter = mp_obj_new_str_iterator, .getiter = mp_obj_new_str_iterator,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment