Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
py/objstrunicode.c@0:5868e8752d44, 2016-04-16 (annotated)
- Committer:
- pythontech
- Date:
- Sat Apr 16 17:11:56 2016 +0000
- Revision:
- 0:5868e8752d44
Split off library from repl
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pythontech | 0:5868e8752d44 | 1 | /* |
| pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| pythontech | 0:5868e8752d44 | 3 | * |
| pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
| pythontech | 0:5868e8752d44 | 5 | * |
| pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
| pythontech | 0:5868e8752d44 | 7 | * Copyright (c) 2014 Paul Sokolovsky |
| pythontech | 0:5868e8752d44 | 8 | * |
| pythontech | 0:5868e8752d44 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| pythontech | 0:5868e8752d44 | 10 | * of this software and associated documentation files (the "Software"), to deal |
| pythontech | 0:5868e8752d44 | 11 | * in the Software without restriction, including without limitation the rights |
| pythontech | 0:5868e8752d44 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| pythontech | 0:5868e8752d44 | 13 | * copies of the Software, and to permit persons to whom the Software is |
| pythontech | 0:5868e8752d44 | 14 | * furnished to do so, subject to the following conditions: |
| pythontech | 0:5868e8752d44 | 15 | * |
| pythontech | 0:5868e8752d44 | 16 | * The above copyright notice and this permission notice shall be included in |
| pythontech | 0:5868e8752d44 | 17 | * all copies or substantial portions of the Software. |
| pythontech | 0:5868e8752d44 | 18 | * |
| pythontech | 0:5868e8752d44 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| pythontech | 0:5868e8752d44 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| pythontech | 0:5868e8752d44 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| pythontech | 0:5868e8752d44 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| pythontech | 0:5868e8752d44 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| pythontech | 0:5868e8752d44 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| pythontech | 0:5868e8752d44 | 25 | * THE SOFTWARE. |
| pythontech | 0:5868e8752d44 | 26 | */ |
| pythontech | 0:5868e8752d44 | 27 | |
| pythontech | 0:5868e8752d44 | 28 | #include <string.h> |
| pythontech | 0:5868e8752d44 | 29 | #include <assert.h> |
| pythontech | 0:5868e8752d44 | 30 | |
| pythontech | 0:5868e8752d44 | 31 | #include "py/nlr.h" |
| pythontech | 0:5868e8752d44 | 32 | #include "py/objstr.h" |
| pythontech | 0:5868e8752d44 | 33 | #include "py/objlist.h" |
| pythontech | 0:5868e8752d44 | 34 | #include "py/runtime0.h" |
| pythontech | 0:5868e8752d44 | 35 | #include "py/runtime.h" |
| pythontech | 0:5868e8752d44 | 36 | |
| pythontech | 0:5868e8752d44 | 37 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
| pythontech | 0:5868e8752d44 | 38 | |
| pythontech | 0:5868e8752d44 | 39 | STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); |
| pythontech | 0:5868e8752d44 | 40 | |
| pythontech | 0:5868e8752d44 | 41 | /******************************************************************************/ |
| pythontech | 0:5868e8752d44 | 42 | /* str */ |
| pythontech | 0:5868e8752d44 | 43 | |
| pythontech | 0:5868e8752d44 | 44 | STATIC void uni_print_quoted(const mp_print_t *print, const byte *str_data, uint str_len) { |
| pythontech | 0:5868e8752d44 | 45 | // this escapes characters, but it will be very slow to print (calling print many times) |
| pythontech | 0:5868e8752d44 | 46 | bool has_single_quote = false; |
| pythontech | 0:5868e8752d44 | 47 | bool has_double_quote = false; |
| pythontech | 0:5868e8752d44 | 48 | for (const byte *s = str_data, *top = str_data + str_len; !has_double_quote && s < top; s++) { |
| pythontech | 0:5868e8752d44 | 49 | if (*s == '\'') { |
| pythontech | 0:5868e8752d44 | 50 | has_single_quote = true; |
| pythontech | 0:5868e8752d44 | 51 | } else if (*s == '"') { |
| pythontech | 0:5868e8752d44 | 52 | has_double_quote = true; |
| pythontech | 0:5868e8752d44 | 53 | } |
| pythontech | 0:5868e8752d44 | 54 | } |
| pythontech | 0:5868e8752d44 | 55 | unichar quote_char = '\''; |
| pythontech | 0:5868e8752d44 | 56 | if (has_single_quote && !has_double_quote) { |
| pythontech | 0:5868e8752d44 | 57 | quote_char = '"'; |
| pythontech | 0:5868e8752d44 | 58 | } |
| pythontech | 0:5868e8752d44 | 59 | mp_printf(print, "%c", quote_char); |
| pythontech | 0:5868e8752d44 | 60 | const byte *s = str_data, *top = str_data + str_len; |
| pythontech | 0:5868e8752d44 | 61 | while (s < top) { |
| pythontech | 0:5868e8752d44 | 62 | unichar ch; |
| pythontech | 0:5868e8752d44 | 63 | ch = utf8_get_char(s); |
| pythontech | 0:5868e8752d44 | 64 | s = utf8_next_char(s); |
| pythontech | 0:5868e8752d44 | 65 | if (ch == quote_char) { |
| pythontech | 0:5868e8752d44 | 66 | mp_printf(print, "\\%c", quote_char); |
| pythontech | 0:5868e8752d44 | 67 | } else if (ch == '\\') { |
| pythontech | 0:5868e8752d44 | 68 | mp_print_str(print, "\\\\"); |
| pythontech | 0:5868e8752d44 | 69 | } else if (32 <= ch && ch <= 126) { |
| pythontech | 0:5868e8752d44 | 70 | mp_printf(print, "%c", ch); |
| pythontech | 0:5868e8752d44 | 71 | } else if (ch == '\n') { |
| pythontech | 0:5868e8752d44 | 72 | mp_print_str(print, "\\n"); |
| pythontech | 0:5868e8752d44 | 73 | } else if (ch == '\r') { |
| pythontech | 0:5868e8752d44 | 74 | mp_print_str(print, "\\r"); |
| pythontech | 0:5868e8752d44 | 75 | } else if (ch == '\t') { |
| pythontech | 0:5868e8752d44 | 76 | mp_print_str(print, "\\t"); |
| pythontech | 0:5868e8752d44 | 77 | } else if (ch < 0x100) { |
| pythontech | 0:5868e8752d44 | 78 | mp_printf(print, "\\x%02x", ch); |
| pythontech | 0:5868e8752d44 | 79 | } else if (ch < 0x10000) { |
| pythontech | 0:5868e8752d44 | 80 | mp_printf(print, "\\u%04x", ch); |
| pythontech | 0:5868e8752d44 | 81 | } else { |
| pythontech | 0:5868e8752d44 | 82 | mp_printf(print, "\\U%08x", ch); |
| pythontech | 0:5868e8752d44 | 83 | } |
| pythontech | 0:5868e8752d44 | 84 | } |
| pythontech | 0:5868e8752d44 | 85 | mp_printf(print, "%c", quote_char); |
| pythontech | 0:5868e8752d44 | 86 | } |
| pythontech | 0:5868e8752d44 | 87 | |
| pythontech | 0:5868e8752d44 | 88 | STATIC void uni_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| pythontech | 0:5868e8752d44 | 89 | GET_STR_DATA_LEN(self_in, str_data, str_len); |
| pythontech | 0:5868e8752d44 | 90 | #if MICROPY_PY_UJSON |
| pythontech | 0:5868e8752d44 | 91 | if (kind == PRINT_JSON) { |
| pythontech | 0:5868e8752d44 | 92 | mp_str_print_json(print, str_data, str_len); |
| pythontech | 0:5868e8752d44 | 93 | return; |
| pythontech | 0:5868e8752d44 | 94 | } |
| pythontech | 0:5868e8752d44 | 95 | #endif |
| pythontech | 0:5868e8752d44 | 96 | if (kind == PRINT_STR) { |
| pythontech | 0:5868e8752d44 | 97 | mp_printf(print, "%.*s", str_len, str_data); |
| pythontech | 0:5868e8752d44 | 98 | } else { |
| pythontech | 0:5868e8752d44 | 99 | uni_print_quoted(print, str_data, str_len); |
| pythontech | 0:5868e8752d44 | 100 | } |
| pythontech | 0:5868e8752d44 | 101 | } |
| pythontech | 0:5868e8752d44 | 102 | |
| pythontech | 0:5868e8752d44 | 103 | STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 104 | GET_STR_DATA_LEN(self_in, str_data, str_len); |
| pythontech | 0:5868e8752d44 | 105 | switch (op) { |
| pythontech | 0:5868e8752d44 | 106 | case MP_UNARY_OP_BOOL: |
| pythontech | 0:5868e8752d44 | 107 | return mp_obj_new_bool(str_len != 0); |
| pythontech | 0:5868e8752d44 | 108 | case MP_UNARY_OP_LEN: |
| pythontech | 0:5868e8752d44 | 109 | return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len)); |
| pythontech | 0:5868e8752d44 | 110 | default: |
| pythontech | 0:5868e8752d44 | 111 | return MP_OBJ_NULL; // op not supported |
| pythontech | 0:5868e8752d44 | 112 | } |
| pythontech | 0:5868e8752d44 | 113 | } |
| pythontech | 0:5868e8752d44 | 114 | |
| pythontech | 0:5868e8752d44 | 115 | // Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or |
| pythontech | 0:5868e8752d44 | 116 | // be capped to the first/last character of the string, depending on is_slice. |
| pythontech | 0:5868e8752d44 | 117 | const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, |
| pythontech | 0:5868e8752d44 | 118 | mp_obj_t index, bool is_slice) { |
| pythontech | 0:5868e8752d44 | 119 | (void)type; |
| pythontech | 0:5868e8752d44 | 120 | mp_int_t i; |
| pythontech | 0:5868e8752d44 | 121 | // Copied from mp_get_index; I don't want bounds checking, just give me |
| pythontech | 0:5868e8752d44 | 122 | // the integer as-is. (I can't bounds-check without scanning the whole |
| pythontech | 0:5868e8752d44 | 123 | // string; an out-of-bounds index will be caught in the loops below.) |
| pythontech | 0:5868e8752d44 | 124 | if (MP_OBJ_IS_SMALL_INT(index)) { |
| pythontech | 0:5868e8752d44 | 125 | i = MP_OBJ_SMALL_INT_VALUE(index); |
| pythontech | 0:5868e8752d44 | 126 | } else if (!mp_obj_get_int_maybe(index, &i)) { |
| pythontech | 0:5868e8752d44 | 127 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index))); |
| pythontech | 0:5868e8752d44 | 128 | } |
| pythontech | 0:5868e8752d44 | 129 | const byte *s, *top = self_data + self_len; |
| pythontech | 0:5868e8752d44 | 130 | if (i < 0) |
| pythontech | 0:5868e8752d44 | 131 | { |
| pythontech | 0:5868e8752d44 | 132 | // Negative indexing is performed by counting from the end of the string. |
| pythontech | 0:5868e8752d44 | 133 | for (s = top - 1; i; --s) { |
| pythontech | 0:5868e8752d44 | 134 | if (s < self_data) { |
| pythontech | 0:5868e8752d44 | 135 | if (is_slice) { |
| pythontech | 0:5868e8752d44 | 136 | return self_data; |
| pythontech | 0:5868e8752d44 | 137 | } |
| pythontech | 0:5868e8752d44 | 138 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); |
| pythontech | 0:5868e8752d44 | 139 | } |
| pythontech | 0:5868e8752d44 | 140 | if (!UTF8_IS_CONT(*s)) { |
| pythontech | 0:5868e8752d44 | 141 | ++i; |
| pythontech | 0:5868e8752d44 | 142 | } |
| pythontech | 0:5868e8752d44 | 143 | } |
| pythontech | 0:5868e8752d44 | 144 | ++s; |
| pythontech | 0:5868e8752d44 | 145 | } else if (!i) { |
| pythontech | 0:5868e8752d44 | 146 | return self_data; // Shortcut - str[0] is its base pointer |
| pythontech | 0:5868e8752d44 | 147 | } else { |
| pythontech | 0:5868e8752d44 | 148 | // Positive indexing, correspondingly, counts from the start of the string. |
| pythontech | 0:5868e8752d44 | 149 | // It's assumed that negative indexing will generally be used with small |
| pythontech | 0:5868e8752d44 | 150 | // absolute values (eg str[-1], not str[-1000000]), which means it'll be |
| pythontech | 0:5868e8752d44 | 151 | // more efficient this way. |
| pythontech | 0:5868e8752d44 | 152 | for (s = self_data; true; ++s) { |
| pythontech | 0:5868e8752d44 | 153 | if (s >= top) { |
| pythontech | 0:5868e8752d44 | 154 | if (is_slice) { |
| pythontech | 0:5868e8752d44 | 155 | return top; |
| pythontech | 0:5868e8752d44 | 156 | } |
| pythontech | 0:5868e8752d44 | 157 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); |
| pythontech | 0:5868e8752d44 | 158 | } |
| pythontech | 0:5868e8752d44 | 159 | while (UTF8_IS_CONT(*s)) { |
| pythontech | 0:5868e8752d44 | 160 | ++s; |
| pythontech | 0:5868e8752d44 | 161 | } |
| pythontech | 0:5868e8752d44 | 162 | if (!i--) { |
| pythontech | 0:5868e8752d44 | 163 | return s; |
| pythontech | 0:5868e8752d44 | 164 | } |
| pythontech | 0:5868e8752d44 | 165 | } |
| pythontech | 0:5868e8752d44 | 166 | } |
| pythontech | 0:5868e8752d44 | 167 | return s; |
| pythontech | 0:5868e8752d44 | 168 | } |
| pythontech | 0:5868e8752d44 | 169 | |
| pythontech | 0:5868e8752d44 | 170 | STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { |
| pythontech | 0:5868e8752d44 | 171 | mp_obj_type_t *type = mp_obj_get_type(self_in); |
| pythontech | 0:5868e8752d44 | 172 | assert(type == &mp_type_str); |
| pythontech | 0:5868e8752d44 | 173 | GET_STR_DATA_LEN(self_in, self_data, self_len); |
| pythontech | 0:5868e8752d44 | 174 | if (value == MP_OBJ_SENTINEL) { |
| pythontech | 0:5868e8752d44 | 175 | // load |
| pythontech | 0:5868e8752d44 | 176 | #if MICROPY_PY_BUILTINS_SLICE |
| pythontech | 0:5868e8752d44 | 177 | if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) { |
| pythontech | 0:5868e8752d44 | 178 | mp_obj_t ostart, ostop, ostep; |
| pythontech | 0:5868e8752d44 | 179 | mp_obj_slice_get(index, &ostart, &ostop, &ostep); |
| pythontech | 0:5868e8752d44 | 180 | if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) { |
| pythontech | 0:5868e8752d44 | 181 | mp_not_implemented("only slices with step=1 (aka None) are supported"); |
| pythontech | 0:5868e8752d44 | 182 | } |
| pythontech | 0:5868e8752d44 | 183 | |
| pythontech | 0:5868e8752d44 | 184 | const byte *pstart, *pstop; |
| pythontech | 0:5868e8752d44 | 185 | if (ostart != mp_const_none) { |
| pythontech | 0:5868e8752d44 | 186 | pstart = str_index_to_ptr(type, self_data, self_len, ostart, true); |
| pythontech | 0:5868e8752d44 | 187 | } else { |
| pythontech | 0:5868e8752d44 | 188 | pstart = self_data; |
| pythontech | 0:5868e8752d44 | 189 | } |
| pythontech | 0:5868e8752d44 | 190 | if (ostop != mp_const_none) { |
| pythontech | 0:5868e8752d44 | 191 | // pstop will point just after the stop character. This depends on |
| pythontech | 0:5868e8752d44 | 192 | // the \0 at the end of the string. |
| pythontech | 0:5868e8752d44 | 193 | pstop = str_index_to_ptr(type, self_data, self_len, ostop, true); |
| pythontech | 0:5868e8752d44 | 194 | } else { |
| pythontech | 0:5868e8752d44 | 195 | pstop = self_data + self_len; |
| pythontech | 0:5868e8752d44 | 196 | } |
| pythontech | 0:5868e8752d44 | 197 | if (pstop < pstart) { |
| pythontech | 0:5868e8752d44 | 198 | return MP_OBJ_NEW_QSTR(MP_QSTR_); |
| pythontech | 0:5868e8752d44 | 199 | } |
| pythontech | 0:5868e8752d44 | 200 | return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart); |
| pythontech | 0:5868e8752d44 | 201 | } |
| pythontech | 0:5868e8752d44 | 202 | #endif |
| pythontech | 0:5868e8752d44 | 203 | const byte *s = str_index_to_ptr(type, self_data, self_len, index, false); |
| pythontech | 0:5868e8752d44 | 204 | int len = 1; |
| pythontech | 0:5868e8752d44 | 205 | if (UTF8_IS_NONASCII(*s)) { |
| pythontech | 0:5868e8752d44 | 206 | // Count the number of 1 bits (after the first) |
| pythontech | 0:5868e8752d44 | 207 | for (char mask = 0x40; *s & mask; mask >>= 1) { |
| pythontech | 0:5868e8752d44 | 208 | ++len; |
| pythontech | 0:5868e8752d44 | 209 | } |
| pythontech | 0:5868e8752d44 | 210 | } |
| pythontech | 0:5868e8752d44 | 211 | return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string |
| pythontech | 0:5868e8752d44 | 212 | } else { |
| pythontech | 0:5868e8752d44 | 213 | return MP_OBJ_NULL; // op not supported |
| pythontech | 0:5868e8752d44 | 214 | } |
| pythontech | 0:5868e8752d44 | 215 | } |
| pythontech | 0:5868e8752d44 | 216 | |
| pythontech | 0:5868e8752d44 | 217 | STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = { |
| pythontech | 0:5868e8752d44 | 218 | #if MICROPY_CPYTHON_COMPAT |
| pythontech | 0:5868e8752d44 | 219 | { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) }, |
| pythontech | 0:5868e8752d44 | 220 | #endif |
| pythontech | 0:5868e8752d44 | 221 | { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&str_find_obj) }, |
| pythontech | 0:5868e8752d44 | 222 | { MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&str_rfind_obj) }, |
| pythontech | 0:5868e8752d44 | 223 | { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&str_index_obj) }, |
| pythontech | 0:5868e8752d44 | 224 | { MP_ROM_QSTR(MP_QSTR_rindex), MP_ROM_PTR(&str_rindex_obj) }, |
| pythontech | 0:5868e8752d44 | 225 | { MP_ROM_QSTR(MP_QSTR_join), MP_ROM_PTR(&str_join_obj) }, |
| pythontech | 0:5868e8752d44 | 226 | { MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&str_split_obj) }, |
| pythontech | 0:5868e8752d44 | 227 | #if MICROPY_PY_BUILTINS_STR_SPLITLINES |
| pythontech | 0:5868e8752d44 | 228 | { MP_ROM_QSTR(MP_QSTR_splitlines), MP_ROM_PTR(&str_splitlines_obj) }, |
| pythontech | 0:5868e8752d44 | 229 | #endif |
| pythontech | 0:5868e8752d44 | 230 | { MP_ROM_QSTR(MP_QSTR_rsplit), MP_ROM_PTR(&str_rsplit_obj) }, |
| pythontech | 0:5868e8752d44 | 231 | { MP_ROM_QSTR(MP_QSTR_startswith), MP_ROM_PTR(&str_startswith_obj) }, |
| pythontech | 0:5868e8752d44 | 232 | { MP_ROM_QSTR(MP_QSTR_endswith), MP_ROM_PTR(&str_endswith_obj) }, |
| pythontech | 0:5868e8752d44 | 233 | { MP_ROM_QSTR(MP_QSTR_strip), MP_ROM_PTR(&str_strip_obj) }, |
| pythontech | 0:5868e8752d44 | 234 | { MP_ROM_QSTR(MP_QSTR_lstrip), MP_ROM_PTR(&str_lstrip_obj) }, |
| pythontech | 0:5868e8752d44 | 235 | { MP_ROM_QSTR(MP_QSTR_rstrip), MP_ROM_PTR(&str_rstrip_obj) }, |
| pythontech | 0:5868e8752d44 | 236 | { MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) }, |
| pythontech | 0:5868e8752d44 | 237 | { MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) }, |
| pythontech | 0:5868e8752d44 | 238 | { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) }, |
| pythontech | 0:5868e8752d44 | 239 | { MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) }, |
| pythontech | 0:5868e8752d44 | 240 | { MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) }, |
| pythontech | 0:5868e8752d44 | 241 | { MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) }, |
| pythontech | 0:5868e8752d44 | 242 | { MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) }, |
| pythontech | 0:5868e8752d44 | 243 | { MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) }, |
| pythontech | 0:5868e8752d44 | 244 | { MP_ROM_QSTR(MP_QSTR_isalpha), MP_ROM_PTR(&str_isalpha_obj) }, |
| pythontech | 0:5868e8752d44 | 245 | { MP_ROM_QSTR(MP_QSTR_isdigit), MP_ROM_PTR(&str_isdigit_obj) }, |
| pythontech | 0:5868e8752d44 | 246 | { MP_ROM_QSTR(MP_QSTR_isupper), MP_ROM_PTR(&str_isupper_obj) }, |
| pythontech | 0:5868e8752d44 | 247 | { MP_ROM_QSTR(MP_QSTR_islower), MP_ROM_PTR(&str_islower_obj) }, |
| pythontech | 0:5868e8752d44 | 248 | }; |
| pythontech | 0:5868e8752d44 | 249 | |
| pythontech | 0:5868e8752d44 | 250 | STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table); |
| pythontech | 0:5868e8752d44 | 251 | |
| pythontech | 0:5868e8752d44 | 252 | const mp_obj_type_t mp_type_str = { |
| pythontech | 0:5868e8752d44 | 253 | { &mp_type_type }, |
| pythontech | 0:5868e8752d44 | 254 | .name = MP_QSTR_str, |
| pythontech | 0:5868e8752d44 | 255 | .print = uni_print, |
| pythontech | 0:5868e8752d44 | 256 | .make_new = mp_obj_str_make_new, |
| pythontech | 0:5868e8752d44 | 257 | .unary_op = uni_unary_op, |
| pythontech | 0:5868e8752d44 | 258 | .binary_op = mp_obj_str_binary_op, |
| pythontech | 0:5868e8752d44 | 259 | .subscr = str_subscr, |
| pythontech | 0:5868e8752d44 | 260 | .getiter = mp_obj_new_str_iterator, |
| pythontech | 0:5868e8752d44 | 261 | .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, |
| pythontech | 0:5868e8752d44 | 262 | .locals_dict = (mp_obj_dict_t*)&struni_locals_dict, |
| pythontech | 0:5868e8752d44 | 263 | }; |
| pythontech | 0:5868e8752d44 | 264 | |
| pythontech | 0:5868e8752d44 | 265 | /******************************************************************************/ |
| pythontech | 0:5868e8752d44 | 266 | /* str iterator */ |
| pythontech | 0:5868e8752d44 | 267 | |
| pythontech | 0:5868e8752d44 | 268 | typedef struct _mp_obj_str_it_t { |
| pythontech | 0:5868e8752d44 | 269 | mp_obj_base_t base; |
| pythontech | 0:5868e8752d44 | 270 | mp_fun_1_t iternext; |
| pythontech | 0:5868e8752d44 | 271 | mp_obj_t str; |
| pythontech | 0:5868e8752d44 | 272 | mp_uint_t cur; |
| pythontech | 0:5868e8752d44 | 273 | } mp_obj_str_it_t; |
| pythontech | 0:5868e8752d44 | 274 | |
| pythontech | 0:5868e8752d44 | 275 | STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 276 | mp_obj_str_it_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 277 | GET_STR_DATA_LEN(self->str, str, len); |
| pythontech | 0:5868e8752d44 | 278 | if (self->cur < len) { |
| pythontech | 0:5868e8752d44 | 279 | const byte *cur = str + self->cur; |
| pythontech | 0:5868e8752d44 | 280 | const byte *end = utf8_next_char(str + self->cur); |
| pythontech | 0:5868e8752d44 | 281 | mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true); |
| pythontech | 0:5868e8752d44 | 282 | self->cur += end - cur; |
| pythontech | 0:5868e8752d44 | 283 | return o_out; |
| pythontech | 0:5868e8752d44 | 284 | } else { |
| pythontech | 0:5868e8752d44 | 285 | return MP_OBJ_STOP_ITERATION; |
| pythontech | 0:5868e8752d44 | 286 | } |
| pythontech | 0:5868e8752d44 | 287 | } |
| pythontech | 0:5868e8752d44 | 288 | |
| pythontech | 0:5868e8752d44 | 289 | STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) { |
| pythontech | 0:5868e8752d44 | 290 | mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t); |
| pythontech | 0:5868e8752d44 | 291 | o->base.type = &mp_type_polymorph_iter; |
| pythontech | 0:5868e8752d44 | 292 | o->iternext = str_it_iternext; |
| pythontech | 0:5868e8752d44 | 293 | o->str = str; |
| pythontech | 0:5868e8752d44 | 294 | o->cur = 0; |
| pythontech | 0:5868e8752d44 | 295 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 296 | } |
| pythontech | 0:5868e8752d44 | 297 | |
| pythontech | 0:5868e8752d44 | 298 | #endif // MICROPY_PY_BUILTINS_STR_UNICODE |