blob: a1f54b8a211a5468d5d21cbd431250915239c0d0 [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/objstr.h"
32#include "py/objlist.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/runtime.h"
Paul Sokolovsky83865342014-06-13 00:51:34 +030034
Paul Sokolovsky97319122014-06-13 22:01:26 +030035#if MICROPY_PY_BUILTINS_STR_UNICODE
Paul Sokolovsky83865342014-06-13 00:51:34 +030036
Damien Georgeae8d8672016-01-09 23:14:54 +000037STATIC 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 +030038
39/******************************************************************************/
40/* str */
41
Damien George7f9d1d62015-04-09 23:56:15 +010042STATIC void uni_print_quoted(const mp_print_t *print, const byte *str_data, uint str_len) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030043 // this escapes characters, but it will be very slow to print (calling print many times)
44 bool has_single_quote = false;
45 bool has_double_quote = false;
46 for (const byte *s = str_data, *top = str_data + str_len; !has_double_quote && s < top; s++) {
47 if (*s == '\'') {
48 has_single_quote = true;
49 } else if (*s == '"') {
50 has_double_quote = true;
51 }
52 }
Damien George2e2e4042015-03-19 00:21:29 +000053 unichar quote_char = '\'';
Paul Sokolovsky83865342014-06-13 00:51:34 +030054 if (has_single_quote && !has_double_quote) {
55 quote_char = '"';
56 }
Damien George7f9d1d62015-04-09 23:56:15 +010057 mp_printf(print, "%c", quote_char);
Paul Sokolovsky00c904b2014-06-14 17:48:40 +030058 const byte *s = str_data, *top = str_data + str_len;
Chris Angelico64b468d2014-06-04 05:28:12 +100059 while (s < top) {
60 unichar ch;
Paul Sokolovsky86d38982014-06-13 23:00:15 +030061 ch = utf8_get_char(s);
62 s = utf8_next_char(s);
Chris Angelico64b468d2014-06-04 05:28:12 +100063 if (ch == quote_char) {
Damien George7f9d1d62015-04-09 23:56:15 +010064 mp_printf(print, "\\%c", quote_char);
Chris Angelico64b468d2014-06-04 05:28:12 +100065 } else if (ch == '\\') {
Damien George7f9d1d62015-04-09 23:56:15 +010066 mp_print_str(print, "\\\\");
Chris Angelico64b468d2014-06-04 05:28:12 +100067 } else if (32 <= ch && ch <= 126) {
Damien George7f9d1d62015-04-09 23:56:15 +010068 mp_printf(print, "%c", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100069 } else if (ch == '\n') {
Damien George7f9d1d62015-04-09 23:56:15 +010070 mp_print_str(print, "\\n");
Chris Angelico64b468d2014-06-04 05:28:12 +100071 } else if (ch == '\r') {
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_print_str(print, "\\r");
Chris Angelico64b468d2014-06-04 05:28:12 +100073 } else if (ch == '\t') {
Damien George7f9d1d62015-04-09 23:56:15 +010074 mp_print_str(print, "\\t");
Chris Angelico64b468d2014-06-04 05:28:12 +100075 } else if (ch < 0x100) {
Damien George7f9d1d62015-04-09 23:56:15 +010076 mp_printf(print, "\\x%02x", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100077 } else if (ch < 0x10000) {
Damien George7f9d1d62015-04-09 23:56:15 +010078 mp_printf(print, "\\u%04x", ch);
Chris Angelico64b468d2014-06-04 05:28:12 +100079 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010080 mp_printf(print, "\\U%08x", ch);
Paul Sokolovsky83865342014-06-13 00:51:34 +030081 }
82 }
Damien George7f9d1d62015-04-09 23:56:15 +010083 mp_printf(print, "%c", quote_char);
Paul Sokolovsky83865342014-06-13 00:51:34 +030084}
85
Damien George7f9d1d62015-04-09 23:56:15 +010086STATIC 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 +030087 GET_STR_DATA_LEN(self_in, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010088 #if MICROPY_PY_UJSON
89 if (kind == PRINT_JSON) {
Damien George7f9d1d62015-04-09 23:56:15 +010090 mp_str_print_json(print, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010091 return;
92 }
93 #endif
Paul Sokolovsky86d38982014-06-13 23:00:15 +030094 if (kind == PRINT_STR) {
Damien George7f9d1d62015-04-09 23:56:15 +010095 mp_printf(print, "%.*s", str_len, str_data);
Paul Sokolovsky83865342014-06-13 00:51:34 +030096 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010097 uni_print_quoted(print, str_data, str_len);
Paul Sokolovsky83865342014-06-13 00:51:34 +030098 }
99}
100
Damien George58321dd2017-08-29 13:04:01 +1000101STATIC mp_obj_t uni_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300102 GET_STR_DATA_LEN(self_in, str_data, str_len);
103 switch (op) {
104 case MP_UNARY_OP_BOOL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300105 return mp_obj_new_bool(str_len != 0);
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300106 case MP_UNARY_OP_LEN:
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300107 return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len));
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300108 default:
109 return MP_OBJ_NULL; // op not supported
110 }
111}
112
Chris Angelico64b468d2014-06-04 05:28:12 +1000113// Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or
114// be capped to the first/last character of the string, depending on is_slice.
Damien George999cedb2015-11-27 17:01:44 +0000115const 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 +0300116 mp_obj_t index, bool is_slice) {
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300117 // All str functions also handle bytes objects, and they call str_index_to_ptr(),
118 // so it must handle bytes.
119 if (type == &mp_type_bytes) {
120 // Taken from objstr.c:str_index_to_ptr()
Damien Georgec88cfe12017-03-23 16:17:40 +1100121 size_t index_val = mp_get_index(type, self_len, index, is_slice);
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300122 return self_data + index_val;
123 }
124
Damien George40f3c022014-07-03 13:25:24 +0100125 mp_int_t i;
Chris Angelico64b468d2014-06-04 05:28:12 +1000126 // Copied from mp_get_index; I don't want bounds checking, just give me
127 // the integer as-is. (I can't bounds-check without scanning the whole
128 // string; an out-of-bounds index will be caught in the loops below.)
129 if (MP_OBJ_IS_SMALL_INT(index)) {
130 i = MP_OBJ_SMALL_INT_VALUE(index);
131 } else if (!mp_obj_get_int_maybe(index, &i)) {
132 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index)));
133 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300134 const byte *s, *top = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000135 if (i < 0)
136 {
137 // Negative indexing is performed by counting from the end of the string.
138 for (s = top - 1; i; --s) {
139 if (s < self_data) {
140 if (is_slice) {
141 return self_data;
142 }
Damien George48d867b2017-06-15 11:54:41 +1000143 mp_raise_msg(&mp_type_IndexError, "string index out of range");
Chris Angelico64b468d2014-06-04 05:28:12 +1000144 }
145 if (!UTF8_IS_CONT(*s)) {
146 ++i;
147 }
148 }
149 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000150 } else {
151 // Positive indexing, correspondingly, counts from the start of the string.
152 // It's assumed that negative indexing will generally be used with small
153 // absolute values (eg str[-1], not str[-1000000]), which means it'll be
154 // more efficient this way.
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300155 s = self_data;
156 while (1) {
157 // First check out-of-bounds
Chris Angelico64b468d2014-06-04 05:28:12 +1000158 if (s >= top) {
159 if (is_slice) {
160 return top;
161 }
Damien George48d867b2017-06-15 11:54:41 +1000162 mp_raise_msg(&mp_type_IndexError, "string index out of range");
Chris Angelico64b468d2014-06-04 05:28:12 +1000163 }
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300164 // Then check completion
165 if (i-- == 0) {
166 break;
167 }
168 // Then skip UTF-8 char
169 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000170 while (UTF8_IS_CONT(*s)) {
171 ++s;
172 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000173 }
174 }
175 return s;
176}
177
Paul Sokolovsky83865342014-06-13 00:51:34 +0300178STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
179 mp_obj_type_t *type = mp_obj_get_type(self_in);
Damien George0528c5a2015-04-04 19:42:03 +0100180 assert(type == &mp_type_str);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300181 GET_STR_DATA_LEN(self_in, self_data, self_len);
182 if (value == MP_OBJ_SENTINEL) {
183 // load
184#if MICROPY_PY_BUILTINS_SLICE
185 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000186 mp_obj_t ostart, ostop, ostep;
187 mp_obj_slice_get(index, &ostart, &ostop, &ostep);
188 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Javier Candeira35a1fea2017-08-09 14:40:45 +1000189 mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
Paul Sokolovsky83865342014-06-13 00:51:34 +0300190 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000191
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300192 const byte *pstart, *pstop;
Chris Angelico64b468d2014-06-04 05:28:12 +1000193 if (ostart != mp_const_none) {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300194 pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000195 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300196 pstart = self_data;
Chris Angelico64b468d2014-06-04 05:28:12 +1000197 }
198 if (ostop != mp_const_none) {
199 // pstop will point just after the stop character. This depends on
200 // the \0 at the end of the string.
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300201 pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000202 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300203 pstop = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000204 }
205 if (pstop < pstart) {
206 return MP_OBJ_NEW_QSTR(MP_QSTR_);
207 }
208 return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300209 }
210#endif
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300211 const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
Chris Angelico64b468d2014-06-04 05:28:12 +1000212 int len = 1;
213 if (UTF8_IS_NONASCII(*s)) {
214 // Count the number of 1 bits (after the first)
215 for (char mask = 0x40; *s & mask; mask >>= 1) {
216 ++len;
217 }
218 }
Damien George46017592017-11-16 13:17:51 +1100219 return mp_obj_new_str_via_qstr((const char*)s, len); // This will create a one-character string
Paul Sokolovsky83865342014-06-13 00:51:34 +0300220 } else {
221 return MP_OBJ_NULL; // op not supported
222 }
223}
224
Damien Georgecbf76742015-11-27 13:38:15 +0000225STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300226#if MICROPY_CPYTHON_COMPAT
Damien Georgecbf76742015-11-27 13:38:15 +0000227 { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) },
Paul Sokolovsky83865342014-06-13 00:51:34 +0300228#endif
Damien Georgecbf76742015-11-27 13:38:15 +0000229 { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&str_find_obj) },
230 { MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&str_rfind_obj) },
231 { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&str_index_obj) },
232 { MP_ROM_QSTR(MP_QSTR_rindex), MP_ROM_PTR(&str_rindex_obj) },
233 { MP_ROM_QSTR(MP_QSTR_join), MP_ROM_PTR(&str_join_obj) },
234 { MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&str_split_obj) },
Paul Sokolovskyac2f7a72015-04-04 00:09:23 +0300235 #if MICROPY_PY_BUILTINS_STR_SPLITLINES
Damien Georgecbf76742015-11-27 13:38:15 +0000236 { MP_ROM_QSTR(MP_QSTR_splitlines), MP_ROM_PTR(&str_splitlines_obj) },
Paul Sokolovskyac2f7a72015-04-04 00:09:23 +0300237 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000238 { MP_ROM_QSTR(MP_QSTR_rsplit), MP_ROM_PTR(&str_rsplit_obj) },
239 { MP_ROM_QSTR(MP_QSTR_startswith), MP_ROM_PTR(&str_startswith_obj) },
240 { MP_ROM_QSTR(MP_QSTR_endswith), MP_ROM_PTR(&str_endswith_obj) },
241 { MP_ROM_QSTR(MP_QSTR_strip), MP_ROM_PTR(&str_strip_obj) },
242 { MP_ROM_QSTR(MP_QSTR_lstrip), MP_ROM_PTR(&str_lstrip_obj) },
243 { MP_ROM_QSTR(MP_QSTR_rstrip), MP_ROM_PTR(&str_rstrip_obj) },
244 { MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
245 { MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
246 { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
Paul Sokolovsky56eb25f2016-08-07 06:46:55 +0300247 #if MICROPY_PY_BUILTINS_STR_PARTITION
Damien Georgecbf76742015-11-27 13:38:15 +0000248 { MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
249 { MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
Paul Sokolovsky56eb25f2016-08-07 06:46:55 +0300250 #endif
Paul Sokolovsky15633882016-08-07 15:24:57 +0300251 #if MICROPY_PY_BUILTINS_STR_CENTER
Paul Sokolovsky1b5abfc2016-05-22 00:13:44 +0300252 { MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
Paul Sokolovsky15633882016-08-07 15:24:57 +0300253 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000254 { MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) },
255 { MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) },
256 { MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) },
257 { MP_ROM_QSTR(MP_QSTR_isalpha), MP_ROM_PTR(&str_isalpha_obj) },
258 { MP_ROM_QSTR(MP_QSTR_isdigit), MP_ROM_PTR(&str_isdigit_obj) },
259 { MP_ROM_QSTR(MP_QSTR_isupper), MP_ROM_PTR(&str_isupper_obj) },
260 { MP_ROM_QSTR(MP_QSTR_islower), MP_ROM_PTR(&str_islower_obj) },
Paul Sokolovsky83865342014-06-13 00:51:34 +0300261};
262
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200263STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300264
265const mp_obj_type_t mp_type_str = {
266 { &mp_type_type },
267 .name = MP_QSTR_str,
Paul Sokolovsky86d38982014-06-13 23:00:15 +0300268 .print = uni_print,
Paul Sokolovsky344e15b2015-01-23 02:15:56 +0200269 .make_new = mp_obj_str_make_new,
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300270 .unary_op = uni_unary_op,
Damien Georgee04a44e2014-06-28 10:27:23 +0100271 .binary_op = mp_obj_str_binary_op,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300272 .subscr = str_subscr,
273 .getiter = mp_obj_new_str_iterator,
Damien Georgee04a44e2014-06-28 10:27:23 +0100274 .buffer_p = { .get_buffer = mp_obj_str_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000275 .locals_dict = (mp_obj_dict_t*)&struni_locals_dict,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300276};
277
Paul Sokolovsky83865342014-06-13 00:51:34 +0300278/******************************************************************************/
279/* str iterator */
280
281typedef struct _mp_obj_str_it_t {
282 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000283 mp_fun_1_t iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300284 mp_obj_t str;
Damien Georgec0d95002017-02-16 16:26:48 +1100285 size_t cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300286} mp_obj_str_it_t;
287
288STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000289 mp_obj_str_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300290 GET_STR_DATA_LEN(self->str, str, len);
291 if (self->cur < len) {
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300292 const byte *cur = str + self->cur;
293 const byte *end = utf8_next_char(str + self->cur);
Damien George46017592017-11-16 13:17:51 +1100294 mp_obj_t o_out = mp_obj_new_str_via_qstr((const char*)cur, end - cur);
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300295 self->cur += end - cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300296 return o_out;
297 } else {
298 return MP_OBJ_STOP_ITERATION;
299 }
300}
301
Damien Georgeae8d8672016-01-09 23:14:54 +0000302STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) {
303 assert(sizeof(mp_obj_str_it_t) <= sizeof(mp_obj_iter_buf_t));
304 mp_obj_str_it_t *o = (mp_obj_str_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000305 o->base.type = &mp_type_polymorph_iter;
306 o->iternext = str_it_iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300307 o->str = str;
308 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000309 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300310}
311
Paul Sokolovsky97319122014-06-13 22:01:26 +0300312#endif // MICROPY_PY_BUILTINS_STR_UNICODE