blob: 785317406bb0a21dfc9c59d90ae38c3fd84208a2 [file] [log] [blame]
Paul Sokolovsky83865342014-06-13 00:51:34 +03001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Paul Sokolovsky83865342014-06-13 00:51:34 +03003 *
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 Sokolovsky83865342014-06-13 00:51:34 +030028#include <string.h>
29#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/objstr.h"
33#include "py/objlist.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
Paul Sokolovsky83865342014-06-13 00:51:34 +030036
Paul Sokolovsky97319122014-06-13 22:01:26 +030037#if MICROPY_PY_BUILTINS_STR_UNICODE
Paul Sokolovsky83865342014-06-13 00:51:34 +030038
Damien Georgeae8d8672016-01-09 23:14:54 +000039STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
Paul Sokolovsky83865342014-06-13 00:51:34 +030040
41/******************************************************************************/
42/* str */
43
Damien George7f9d1d62015-04-09 23:56:15 +010044STATIC void uni_print_quoted(const mp_print_t *print, const byte *str_data, uint str_len) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030045 // this escapes characters, but it will be very slow to print (calling print many times)
46 bool has_single_quote = false;
47 bool has_double_quote = false;
48 for (const byte *s = str_data, *top = str_data + str_len; !has_double_quote && s < top; s++) {
49 if (*s == '\'') {
50 has_single_quote = true;
51 } else if (*s == '"') {
52 has_double_quote = true;
53 }
54 }
Damien George2e2e4042015-03-19 00:21:29 +000055 unichar quote_char = '\'';
Paul Sokolovsky83865342014-06-13 00:51:34 +030056 if (has_single_quote && !has_double_quote) {
57 quote_char = '"';
58 }
Damien George7f9d1d62015-04-09 23:56:15 +010059 mp_printf(print, "%c", quote_char);
Paul Sokolovsky00c904b2014-06-14 17:48:40 +030060 const byte *s = str_data, *top = str_data + str_len;
Chris Angelico64b468d2014-06-04 05:28:12 +100061 while (s < top) {
62 unichar ch;
Paul Sokolovsky86d38982014-06-13 23:00:15 +030063 ch = utf8_get_char(s);
64 s = utf8_next_char(s);
Chris Angelico64b468d2014-06-04 05:28:12 +100065 if (ch == quote_char) {
Damien George7f9d1d62015-04-09 23:56:15 +010066 mp_printf(print, "\\%c", quote_char);
Chris Angelico64b468d2014-06-04 05:28:12 +100067 } else if (ch == '\\') {
Damien George7f9d1d62015-04-09 23:56:15 +010068 mp_print_str(print, "\\\\");
Chris Angelico64b468d2014-06-04 05:28:12 +100069 } else if (32 <= ch && ch <= 126) {
Damien George7f9d1d62015-04-09 23:56:15 +010070 mp_printf(print, "%c", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100071 } else if (ch == '\n') {
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_print_str(print, "\\n");
Chris Angelico64b468d2014-06-04 05:28:12 +100073 } else if (ch == '\r') {
Damien George7f9d1d62015-04-09 23:56:15 +010074 mp_print_str(print, "\\r");
Chris Angelico64b468d2014-06-04 05:28:12 +100075 } else if (ch == '\t') {
Damien George7f9d1d62015-04-09 23:56:15 +010076 mp_print_str(print, "\\t");
Chris Angelico64b468d2014-06-04 05:28:12 +100077 } else if (ch < 0x100) {
Damien George7f9d1d62015-04-09 23:56:15 +010078 mp_printf(print, "\\x%02x", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100079 } else if (ch < 0x10000) {
Damien George7f9d1d62015-04-09 23:56:15 +010080 mp_printf(print, "\\u%04x", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100081 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010082 mp_printf(print, "\\U%08x", ch);
Paul Sokolovsky83865342014-06-13 00:51:34 +030083 }
84 }
Damien George7f9d1d62015-04-09 23:56:15 +010085 mp_printf(print, "%c", quote_char);
Paul Sokolovsky83865342014-06-13 00:51:34 +030086}
87
Damien George7f9d1d62015-04-09 23:56:15 +010088STATIC void uni_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030089 GET_STR_DATA_LEN(self_in, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010090 #if MICROPY_PY_UJSON
91 if (kind == PRINT_JSON) {
Damien George7f9d1d62015-04-09 23:56:15 +010092 mp_str_print_json(print, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010093 return;
94 }
95 #endif
Paul Sokolovsky86d38982014-06-13 23:00:15 +030096 if (kind == PRINT_STR) {
Damien George7f9d1d62015-04-09 23:56:15 +010097 mp_printf(print, "%.*s", str_len, str_data);
Paul Sokolovsky83865342014-06-13 00:51:34 +030098 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010099 uni_print_quoted(print, str_data, str_len);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300100 }
101}
102
Damien George58321dd2017-08-29 13:04:01 +1000103STATIC mp_obj_t uni_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300104 GET_STR_DATA_LEN(self_in, str_data, str_len);
105 switch (op) {
106 case MP_UNARY_OP_BOOL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300107 return mp_obj_new_bool(str_len != 0);
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300108 case MP_UNARY_OP_LEN:
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300109 return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len));
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300110 default:
111 return MP_OBJ_NULL; // op not supported
112 }
113}
114
Chris Angelico64b468d2014-06-04 05:28:12 +1000115// Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or
116// be capped to the first/last character of the string, depending on is_slice.
Damien George999cedb2015-11-27 17:01:44 +0000117const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300118 mp_obj_t index, bool is_slice) {
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300119 // All str functions also handle bytes objects, and they call str_index_to_ptr(),
120 // so it must handle bytes.
121 if (type == &mp_type_bytes) {
122 // Taken from objstr.c:str_index_to_ptr()
Damien Georgec88cfe12017-03-23 16:17:40 +1100123 size_t index_val = mp_get_index(type, self_len, index, is_slice);
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300124 return self_data + index_val;
125 }
126
Damien George40f3c022014-07-03 13:25:24 +0100127 mp_int_t i;
Chris Angelico64b468d2014-06-04 05:28:12 +1000128 // Copied from mp_get_index; I don't want bounds checking, just give me
129 // the integer as-is. (I can't bounds-check without scanning the whole
130 // string; an out-of-bounds index will be caught in the loops below.)
131 if (MP_OBJ_IS_SMALL_INT(index)) {
132 i = MP_OBJ_SMALL_INT_VALUE(index);
133 } else if (!mp_obj_get_int_maybe(index, &i)) {
134 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index)));
135 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300136 const byte *s, *top = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000137 if (i < 0)
138 {
139 // Negative indexing is performed by counting from the end of the string.
140 for (s = top - 1; i; --s) {
141 if (s < self_data) {
142 if (is_slice) {
143 return self_data;
144 }
Damien George48d867b2017-06-15 11:54:41 +1000145 mp_raise_msg(&mp_type_IndexError, "string index out of range");
Chris Angelico64b468d2014-06-04 05:28:12 +1000146 }
147 if (!UTF8_IS_CONT(*s)) {
148 ++i;
149 }
150 }
151 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000152 } else {
153 // Positive indexing, correspondingly, counts from the start of the string.
154 // It's assumed that negative indexing will generally be used with small
155 // absolute values (eg str[-1], not str[-1000000]), which means it'll be
156 // more efficient this way.
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300157 s = self_data;
158 while (1) {
159 // First check out-of-bounds
Chris Angelico64b468d2014-06-04 05:28:12 +1000160 if (s >= top) {
161 if (is_slice) {
162 return top;
163 }
Damien George48d867b2017-06-15 11:54:41 +1000164 mp_raise_msg(&mp_type_IndexError, "string index out of range");
Chris Angelico64b468d2014-06-04 05:28:12 +1000165 }
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300166 // Then check completion
167 if (i-- == 0) {
168 break;
169 }
170 // Then skip UTF-8 char
171 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000172 while (UTF8_IS_CONT(*s)) {
173 ++s;
174 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000175 }
176 }
177 return s;
178}
179
Paul Sokolovsky83865342014-06-13 00:51:34 +0300180STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
181 mp_obj_type_t *type = mp_obj_get_type(self_in);
Damien George0528c5a2015-04-04 19:42:03 +0100182 assert(type == &mp_type_str);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300183 GET_STR_DATA_LEN(self_in, self_data, self_len);
184 if (value == MP_OBJ_SENTINEL) {
185 // load
186#if MICROPY_PY_BUILTINS_SLICE
187 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000188 mp_obj_t ostart, ostop, ostep;
189 mp_obj_slice_get(index, &ostart, &ostop, &ostep);
190 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Javier Candeira35a1fea2017-08-09 14:40:45 +1000191 mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
Paul Sokolovsky83865342014-06-13 00:51:34 +0300192 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000193
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300194 const byte *pstart, *pstop;
Chris Angelico64b468d2014-06-04 05:28:12 +1000195 if (ostart != mp_const_none) {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300196 pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000197 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300198 pstart = self_data;
Chris Angelico64b468d2014-06-04 05:28:12 +1000199 }
200 if (ostop != mp_const_none) {
201 // pstop will point just after the stop character. This depends on
202 // the \0 at the end of the string.
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300203 pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000204 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300205 pstop = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000206 }
207 if (pstop < pstart) {
208 return MP_OBJ_NEW_QSTR(MP_QSTR_);
209 }
210 return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300211 }
212#endif
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300213 const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
Chris Angelico64b468d2014-06-04 05:28:12 +1000214 int len = 1;
215 if (UTF8_IS_NONASCII(*s)) {
216 // Count the number of 1 bits (after the first)
217 for (char mask = 0x40; *s & mask; mask >>= 1) {
218 ++len;
219 }
220 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300221 return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
Paul Sokolovsky83865342014-06-13 00:51:34 +0300222 } else {
223 return MP_OBJ_NULL; // op not supported
224 }
225}
226
Damien Georgecbf76742015-11-27 13:38:15 +0000227STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300228#if MICROPY_CPYTHON_COMPAT
Damien Georgecbf76742015-11-27 13:38:15 +0000229 { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) },
Paul Sokolovsky83865342014-06-13 00:51:34 +0300230#endif
Damien Georgecbf76742015-11-27 13:38:15 +0000231 { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&str_find_obj) },
232 { MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&str_rfind_obj) },
233 { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&str_index_obj) },
234 { MP_ROM_QSTR(MP_QSTR_rindex), MP_ROM_PTR(&str_rindex_obj) },
235 { MP_ROM_QSTR(MP_QSTR_join), MP_ROM_PTR(&str_join_obj) },
236 { MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&str_split_obj) },
Paul Sokolovskyac2f7a72015-04-04 00:09:23 +0300237 #if MICROPY_PY_BUILTINS_STR_SPLITLINES
Damien Georgecbf76742015-11-27 13:38:15 +0000238 { MP_ROM_QSTR(MP_QSTR_splitlines), MP_ROM_PTR(&str_splitlines_obj) },
Paul Sokolovskyac2f7a72015-04-04 00:09:23 +0300239 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000240 { MP_ROM_QSTR(MP_QSTR_rsplit), MP_ROM_PTR(&str_rsplit_obj) },
241 { MP_ROM_QSTR(MP_QSTR_startswith), MP_ROM_PTR(&str_startswith_obj) },
242 { MP_ROM_QSTR(MP_QSTR_endswith), MP_ROM_PTR(&str_endswith_obj) },
243 { MP_ROM_QSTR(MP_QSTR_strip), MP_ROM_PTR(&str_strip_obj) },
244 { MP_ROM_QSTR(MP_QSTR_lstrip), MP_ROM_PTR(&str_lstrip_obj) },
245 { MP_ROM_QSTR(MP_QSTR_rstrip), MP_ROM_PTR(&str_rstrip_obj) },
246 { MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
247 { MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
248 { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
Paul Sokolovsky56eb25f2016-08-07 06:46:55 +0300249 #if MICROPY_PY_BUILTINS_STR_PARTITION
Damien Georgecbf76742015-11-27 13:38:15 +0000250 { MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
251 { MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
Paul Sokolovsky56eb25f2016-08-07 06:46:55 +0300252 #endif
Paul Sokolovsky15633882016-08-07 15:24:57 +0300253 #if MICROPY_PY_BUILTINS_STR_CENTER
Paul Sokolovsky1b5abfc2016-05-22 00:13:44 +0300254 { MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
Paul Sokolovsky15633882016-08-07 15:24:57 +0300255 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000256 { MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) },
257 { MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) },
258 { MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) },
259 { MP_ROM_QSTR(MP_QSTR_isalpha), MP_ROM_PTR(&str_isalpha_obj) },
260 { MP_ROM_QSTR(MP_QSTR_isdigit), MP_ROM_PTR(&str_isdigit_obj) },
261 { MP_ROM_QSTR(MP_QSTR_isupper), MP_ROM_PTR(&str_isupper_obj) },
262 { MP_ROM_QSTR(MP_QSTR_islower), MP_ROM_PTR(&str_islower_obj) },
Paul Sokolovsky83865342014-06-13 00:51:34 +0300263};
264
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200265STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300266
267const mp_obj_type_t mp_type_str = {
268 { &mp_type_type },
269 .name = MP_QSTR_str,
Paul Sokolovsky86d38982014-06-13 23:00:15 +0300270 .print = uni_print,
Paul Sokolovsky344e15b2015-01-23 02:15:56 +0200271 .make_new = mp_obj_str_make_new,
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300272 .unary_op = uni_unary_op,
Damien Georgee04a44e2014-06-28 10:27:23 +0100273 .binary_op = mp_obj_str_binary_op,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300274 .subscr = str_subscr,
275 .getiter = mp_obj_new_str_iterator,
Damien Georgee04a44e2014-06-28 10:27:23 +0100276 .buffer_p = { .get_buffer = mp_obj_str_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000277 .locals_dict = (mp_obj_dict_t*)&struni_locals_dict,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300278};
279
Paul Sokolovsky83865342014-06-13 00:51:34 +0300280/******************************************************************************/
281/* str iterator */
282
283typedef struct _mp_obj_str_it_t {
284 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000285 mp_fun_1_t iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300286 mp_obj_t str;
Damien Georgec0d95002017-02-16 16:26:48 +1100287 size_t cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300288} mp_obj_str_it_t;
289
290STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000291 mp_obj_str_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300292 GET_STR_DATA_LEN(self->str, str, len);
293 if (self->cur < len) {
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300294 const byte *cur = str + self->cur;
295 const byte *end = utf8_next_char(str + self->cur);
296 mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true);
297 self->cur += end - cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300298 return o_out;
299 } else {
300 return MP_OBJ_STOP_ITERATION;
301 }
302}
303
Damien Georgeae8d8672016-01-09 23:14:54 +0000304STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) {
305 assert(sizeof(mp_obj_str_it_t) <= sizeof(mp_obj_iter_buf_t));
306 mp_obj_str_it_t *o = (mp_obj_str_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000307 o->base.type = &mp_type_polymorph_iter;
308 o->iternext = str_it_iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300309 o->str = str;
310 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000311 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300312}
313
Paul Sokolovsky97319122014-06-13 22:01:26 +0300314#endif // MICROPY_PY_BUILTINS_STR_UNICODE