Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * Copyright (c) 2014 Paul Sokolovsky |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <assert.h> |
| 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/nlr.h" |
| 32 | #include "py/objstr.h" |
| 33 | #include "py/objlist.h" |
| 34 | #include "py/runtime0.h" |
| 35 | #include "py/runtime.h" |
| 36 | #include "py/pfenv.h" |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 37 | |
Paul Sokolovsky | 9731912 | 2014-06-13 22:01:26 +0300 | [diff] [blame] | 38 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 39 | |
| 40 | STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 41 | |
| 42 | /******************************************************************************/ |
| 43 | /* str */ |
| 44 | |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 45 | STATIC void uni_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 46 | // this escapes characters, but it will be very slow to print (calling print many times) |
| 47 | bool has_single_quote = false; |
| 48 | bool has_double_quote = false; |
| 49 | for (const byte *s = str_data, *top = str_data + str_len; !has_double_quote && s < top; s++) { |
| 50 | if (*s == '\'') { |
| 51 | has_single_quote = true; |
| 52 | } else if (*s == '"') { |
| 53 | has_double_quote = true; |
| 54 | } |
| 55 | } |
| 56 | int quote_char = '\''; |
| 57 | if (has_single_quote && !has_double_quote) { |
| 58 | quote_char = '"'; |
| 59 | } |
| 60 | print(env, "%c", quote_char); |
Paul Sokolovsky | 00c904b | 2014-06-14 17:48:40 +0300 | [diff] [blame] | 61 | const byte *s = str_data, *top = str_data + str_len; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 62 | while (s < top) { |
| 63 | unichar ch; |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 64 | ch = utf8_get_char(s); |
| 65 | s = utf8_next_char(s); |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 66 | if (ch == quote_char) { |
| 67 | print(env, "\\%c", quote_char); |
| 68 | } else if (ch == '\\') { |
| 69 | print(env, "\\\\"); |
| 70 | } else if (32 <= ch && ch <= 126) { |
| 71 | print(env, "%c", ch); |
| 72 | } else if (ch == '\n') { |
| 73 | print(env, "\\n"); |
| 74 | } else if (ch == '\r') { |
| 75 | print(env, "\\r"); |
| 76 | } else if (ch == '\t') { |
| 77 | print(env, "\\t"); |
| 78 | } else if (ch < 0x100) { |
| 79 | print(env, "\\x%02x", ch); |
| 80 | } else if (ch < 0x10000) { |
| 81 | print(env, "\\u%04x", ch); |
| 82 | } else { |
| 83 | print(env, "\\U%08x", ch); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | print(env, "%c", quote_char); |
| 87 | } |
| 88 | |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 89 | STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 90 | GET_STR_DATA_LEN(self_in, str_data, str_len); |
Damien George | 612045f | 2014-09-17 22:56:34 +0100 | [diff] [blame] | 91 | #if MICROPY_PY_UJSON |
| 92 | if (kind == PRINT_JSON) { |
Damien George | cde0ca2 | 2014-09-25 17:35:56 +0100 | [diff] [blame] | 93 | mp_str_print_json(print, env, str_data, str_len); |
Damien George | 612045f | 2014-09-17 22:56:34 +0100 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | #endif |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 97 | if (kind == PRINT_STR) { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 98 | print(env, "%.*s", str_len, str_data); |
| 99 | } else { |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 100 | uni_print_quoted(print, env, str_data, str_len); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 104 | STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) { |
Paul Sokolovsky | e7f2b4c | 2014-06-13 23:37:18 +0300 | [diff] [blame] | 105 | GET_STR_DATA_LEN(self_in, str_data, str_len); |
| 106 | switch (op) { |
| 107 | case MP_UNARY_OP_BOOL: |
| 108 | return MP_BOOL(str_len != 0); |
| 109 | case MP_UNARY_OP_LEN: |
Paul Sokolovsky | 9e215fa | 2014-06-28 23:14:30 +0300 | [diff] [blame] | 110 | return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len)); |
Paul Sokolovsky | e7f2b4c | 2014-06-13 23:37:18 +0300 | [diff] [blame] | 111 | default: |
| 112 | return MP_OBJ_NULL; // op not supported |
| 113 | } |
| 114 | } |
| 115 | |
Paul Sokolovsky | 344e15b | 2015-01-23 02:15:56 +0200 | [diff] [blame^] | 116 | mp_obj_t mp_obj_str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 117 | #if MICROPY_CPYTHON_COMPAT |
| 118 | if (n_kw != 0) { |
| 119 | mp_arg_error_unimpl_kw(); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | switch (n_args) { |
| 124 | case 0: |
| 125 | return MP_OBJ_NEW_QSTR(MP_QSTR_); |
| 126 | |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 127 | case 1: { |
| 128 | vstr_t vstr; |
| 129 | vstr_init(&vstr, 16); |
| 130 | mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, &vstr, args[0], PRINT_STR); |
| 131 | return mp_obj_new_str_from_vstr(type_in, &vstr); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | case 2: |
| 135 | case 3: |
| 136 | { |
| 137 | // TODO: validate 2nd/3rd args |
Paul Sokolovsky | e62a0fe | 2014-10-30 23:58:08 +0200 | [diff] [blame] | 138 | if (MP_OBJ_IS_TYPE(args[0], &mp_type_bytes)) { |
| 139 | GET_STR_DATA_LEN(args[0], str_data, str_len); |
| 140 | GET_STR_HASH(args[0], str_hash); |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 141 | mp_obj_str_t *o = mp_obj_new_str_of_type(type_in, NULL, str_len); |
Paul Sokolovsky | e62a0fe | 2014-10-30 23:58:08 +0200 | [diff] [blame] | 142 | o->data = str_data; |
| 143 | o->hash = str_hash; |
| 144 | return o; |
| 145 | } else { |
| 146 | mp_buffer_info_t bufinfo; |
| 147 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 148 | return mp_obj_new_str(bufinfo.buf, bufinfo.len, false); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 149 | } |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | default: |
| 153 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "str takes at most 3 arguments")); |
| 154 | } |
| 155 | } |
| 156 | |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 157 | // Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or |
| 158 | // be capped to the first/last character of the string, depending on is_slice. |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 159 | const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, mp_uint_t self_len, |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 160 | mp_obj_t index, bool is_slice) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 161 | (void)type; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 162 | mp_int_t i; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 163 | // Copied from mp_get_index; I don't want bounds checking, just give me |
| 164 | // the integer as-is. (I can't bounds-check without scanning the whole |
| 165 | // string; an out-of-bounds index will be caught in the loops below.) |
| 166 | if (MP_OBJ_IS_SMALL_INT(index)) { |
| 167 | i = MP_OBJ_SMALL_INT_VALUE(index); |
| 168 | } else if (!mp_obj_get_int_maybe(index, &i)) { |
| 169 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index))); |
| 170 | } |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 171 | const byte *s, *top = self_data + self_len; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 172 | if (i < 0) |
| 173 | { |
| 174 | // Negative indexing is performed by counting from the end of the string. |
| 175 | for (s = top - 1; i; --s) { |
| 176 | if (s < self_data) { |
| 177 | if (is_slice) { |
| 178 | return self_data; |
| 179 | } |
| 180 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); |
| 181 | } |
| 182 | if (!UTF8_IS_CONT(*s)) { |
| 183 | ++i; |
| 184 | } |
| 185 | } |
| 186 | ++s; |
| 187 | } else if (!i) { |
| 188 | return self_data; // Shortcut - str[0] is its base pointer |
| 189 | } else { |
| 190 | // Positive indexing, correspondingly, counts from the start of the string. |
| 191 | // It's assumed that negative indexing will generally be used with small |
| 192 | // absolute values (eg str[-1], not str[-1000000]), which means it'll be |
| 193 | // more efficient this way. |
| 194 | for (s = self_data; true; ++s) { |
| 195 | if (s >= top) { |
| 196 | if (is_slice) { |
| 197 | return top; |
| 198 | } |
| 199 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range")); |
| 200 | } |
| 201 | while (UTF8_IS_CONT(*s)) { |
| 202 | ++s; |
| 203 | } |
| 204 | if (!i--) { |
| 205 | return s; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | return s; |
| 210 | } |
| 211 | |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 212 | STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { |
| 213 | mp_obj_type_t *type = mp_obj_get_type(self_in); |
| 214 | GET_STR_DATA_LEN(self_in, self_data, self_len); |
| 215 | if (value == MP_OBJ_SENTINEL) { |
| 216 | // load |
| 217 | #if MICROPY_PY_BUILTINS_SLICE |
| 218 | if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) { |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 219 | mp_obj_t ostart, ostop, ostep; |
| 220 | mp_obj_slice_get(index, &ostart, &ostop, &ostep); |
| 221 | if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 222 | nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, |
| 223 | "only slices with step=1 (aka None) are supported")); |
| 224 | } |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 225 | |
| 226 | if (type == &mp_type_bytes) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 227 | mp_int_t start = 0, stop = self_len; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 228 | if (ostart != mp_const_none) { |
| 229 | start = MP_OBJ_SMALL_INT_VALUE(ostart); |
| 230 | if (start < 0) { |
| 231 | start = self_len + start; |
| 232 | } |
| 233 | } |
| 234 | if (ostop != mp_const_none) { |
| 235 | stop = MP_OBJ_SMALL_INT_VALUE(ostop); |
| 236 | if (stop < 0) { |
| 237 | stop = self_len + stop; |
| 238 | } |
| 239 | } |
| 240 | return mp_obj_new_str_of_type(type, self_data + start, stop - start); |
| 241 | } |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 242 | const byte *pstart, *pstop; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 243 | if (ostart != mp_const_none) { |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 244 | pstart = str_index_to_ptr(type, self_data, self_len, ostart, true); |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 245 | } else { |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 246 | pstart = self_data; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 247 | } |
| 248 | if (ostop != mp_const_none) { |
| 249 | // pstop will point just after the stop character. This depends on |
| 250 | // the \0 at the end of the string. |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 251 | pstop = str_index_to_ptr(type, self_data, self_len, ostop, true); |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 252 | } else { |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 253 | pstop = self_data + self_len; |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 254 | } |
| 255 | if (pstop < pstart) { |
| 256 | return MP_OBJ_NEW_QSTR(MP_QSTR_); |
| 257 | } |
| 258 | return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 259 | } |
| 260 | #endif |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 261 | if (type == &mp_type_bytes) { |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 262 | uint index_val = mp_get_index(type, self_len, index, false); |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 263 | return MP_OBJ_NEW_SMALL_INT(self_data[index_val]); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 264 | } |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 265 | const byte *s = str_index_to_ptr(type, self_data, self_len, index, false); |
Chris Angelico | 64b468d | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 266 | int len = 1; |
| 267 | if (UTF8_IS_NONASCII(*s)) { |
| 268 | // Count the number of 1 bits (after the first) |
| 269 | for (char mask = 0x40; *s & mask; mask >>= 1) { |
| 270 | ++len; |
| 271 | } |
| 272 | } |
Paul Sokolovsky | ea2c936 | 2014-06-15 00:35:09 +0300 | [diff] [blame] | 273 | return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 274 | } else { |
| 275 | return MP_OBJ_NULL; // op not supported |
| 276 | } |
| 277 | } |
| 278 | |
Paul Sokolovsky | 6113eb2 | 2015-01-23 02:05:58 +0200 | [diff] [blame] | 279 | STATIC const mp_map_elem_t struni_locals_dict_table[] = { |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 280 | #if MICROPY_CPYTHON_COMPAT |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 281 | { MP_OBJ_NEW_QSTR(MP_QSTR_encode), (mp_obj_t)&str_encode_obj }, |
| 282 | #endif |
| 283 | { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj }, |
| 284 | { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj }, |
| 285 | { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&str_index_obj }, |
| 286 | { MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj }, |
| 287 | { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj }, |
| 288 | { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj }, |
| 289 | { MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj }, |
| 290 | { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj }, |
| 291 | { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj }, |
| 292 | { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj }, |
| 293 | { MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj }, |
| 294 | { MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj }, |
| 295 | { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj }, |
| 296 | { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj }, |
| 297 | { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj }, |
| 298 | { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj }, |
| 299 | { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj }, |
| 300 | { MP_OBJ_NEW_QSTR(MP_QSTR_lower), (mp_obj_t)&str_lower_obj }, |
| 301 | { MP_OBJ_NEW_QSTR(MP_QSTR_upper), (mp_obj_t)&str_upper_obj }, |
| 302 | { MP_OBJ_NEW_QSTR(MP_QSTR_isspace), (mp_obj_t)&str_isspace_obj }, |
| 303 | { MP_OBJ_NEW_QSTR(MP_QSTR_isalpha), (mp_obj_t)&str_isalpha_obj }, |
| 304 | { MP_OBJ_NEW_QSTR(MP_QSTR_isdigit), (mp_obj_t)&str_isdigit_obj }, |
| 305 | { MP_OBJ_NEW_QSTR(MP_QSTR_isupper), (mp_obj_t)&str_isupper_obj }, |
| 306 | { MP_OBJ_NEW_QSTR(MP_QSTR_islower), (mp_obj_t)&str_islower_obj }, |
| 307 | }; |
| 308 | |
Paul Sokolovsky | 6113eb2 | 2015-01-23 02:05:58 +0200 | [diff] [blame] | 309 | STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table); |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 310 | |
| 311 | const mp_obj_type_t mp_type_str = { |
| 312 | { &mp_type_type }, |
| 313 | .name = MP_QSTR_str, |
Paul Sokolovsky | 86d3898 | 2014-06-13 23:00:15 +0300 | [diff] [blame] | 314 | .print = uni_print, |
Paul Sokolovsky | 344e15b | 2015-01-23 02:15:56 +0200 | [diff] [blame^] | 315 | .make_new = mp_obj_str_make_new, |
Paul Sokolovsky | e7f2b4c | 2014-06-13 23:37:18 +0300 | [diff] [blame] | 316 | .unary_op = uni_unary_op, |
Damien George | e04a44e | 2014-06-28 10:27:23 +0100 | [diff] [blame] | 317 | .binary_op = mp_obj_str_binary_op, |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 318 | .subscr = str_subscr, |
| 319 | .getiter = mp_obj_new_str_iterator, |
Damien George | e04a44e | 2014-06-28 10:27:23 +0100 | [diff] [blame] | 320 | .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, |
Paul Sokolovsky | 6113eb2 | 2015-01-23 02:05:58 +0200 | [diff] [blame] | 321 | .locals_dict = (mp_obj_t)&struni_locals_dict, |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 322 | }; |
| 323 | |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 324 | /******************************************************************************/ |
| 325 | /* str iterator */ |
| 326 | |
| 327 | typedef struct _mp_obj_str_it_t { |
| 328 | mp_obj_base_t base; |
| 329 | mp_obj_t str; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 330 | mp_uint_t cur; |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 331 | } mp_obj_str_it_t; |
| 332 | |
| 333 | STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) { |
| 334 | mp_obj_str_it_t *self = self_in; |
| 335 | GET_STR_DATA_LEN(self->str, str, len); |
| 336 | if (self->cur < len) { |
Paul Sokolovsky | 79b7fe2 | 2014-06-14 02:07:25 +0300 | [diff] [blame] | 337 | const byte *cur = str + self->cur; |
| 338 | const byte *end = utf8_next_char(str + self->cur); |
| 339 | mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true); |
| 340 | self->cur += end - cur; |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 341 | return o_out; |
| 342 | } else { |
| 343 | return MP_OBJ_STOP_ITERATION; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | STATIC const mp_obj_type_t mp_type_str_it = { |
| 348 | { &mp_type_type }, |
| 349 | .name = MP_QSTR_iterator, |
| 350 | .getiter = mp_identity, |
| 351 | .iternext = str_it_iternext, |
| 352 | }; |
| 353 | |
Paul Sokolovsky | 8386534 | 2014-06-13 00:51:34 +0300 | [diff] [blame] | 354 | mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) { |
| 355 | mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t); |
| 356 | o->base.type = &mp_type_str_it; |
| 357 | o->str = str; |
| 358 | o->cur = 0; |
| 359 | return o; |
| 360 | } |
| 361 | |
Paul Sokolovsky | 9731912 | 2014-06-13 22:01:26 +0300 | [diff] [blame] | 362 | #endif // MICROPY_PY_BUILTINS_STR_UNICODE |