blob: 0bb6e045ee89de5304a58ff0da33c241daccc498 [file] [log] [blame]
Paul Sokolovsky83865342014-06-13 00:51:34 +03001/*
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 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
39STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
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 Georgeecc88e92014-08-30 00:35:11 +0100103STATIC mp_obj_t uni_unary_op(mp_uint_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:
107 return MP_BOOL(str_len != 0);
108 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 George4abff752014-08-30 14:59:21 +0100117const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, mp_uint_t self_len,
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300118 mp_obj_t index, bool is_slice) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000119 (void)type;
Damien George40f3c022014-07-03 13:25:24 +0100120 mp_int_t i;
Chris Angelico64b468d2014-06-04 05:28:12 +1000121 // Copied from mp_get_index; I don't want bounds checking, just give me
122 // the integer as-is. (I can't bounds-check without scanning the whole
123 // string; an out-of-bounds index will be caught in the loops below.)
124 if (MP_OBJ_IS_SMALL_INT(index)) {
125 i = MP_OBJ_SMALL_INT_VALUE(index);
126 } else if (!mp_obj_get_int_maybe(index, &i)) {
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)));
128 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300129 const byte *s, *top = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000130 if (i < 0)
131 {
132 // Negative indexing is performed by counting from the end of the string.
133 for (s = top - 1; i; --s) {
134 if (s < self_data) {
135 if (is_slice) {
136 return self_data;
137 }
138 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range"));
139 }
140 if (!UTF8_IS_CONT(*s)) {
141 ++i;
142 }
143 }
144 ++s;
145 } else if (!i) {
146 return self_data; // Shortcut - str[0] is its base pointer
147 } else {
148 // Positive indexing, correspondingly, counts from the start of the string.
149 // It's assumed that negative indexing will generally be used with small
150 // absolute values (eg str[-1], not str[-1000000]), which means it'll be
151 // more efficient this way.
152 for (s = self_data; true; ++s) {
153 if (s >= top) {
154 if (is_slice) {
155 return top;
156 }
157 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range"));
158 }
159 while (UTF8_IS_CONT(*s)) {
160 ++s;
161 }
162 if (!i--) {
163 return s;
164 }
165 }
166 }
167 return s;
168}
169
Paul Sokolovsky83865342014-06-13 00:51:34 +0300170STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
171 mp_obj_type_t *type = mp_obj_get_type(self_in);
Damien George0528c5a2015-04-04 19:42:03 +0100172 assert(type == &mp_type_str);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300173 GET_STR_DATA_LEN(self_in, self_data, self_len);
174 if (value == MP_OBJ_SENTINEL) {
175 // load
176#if MICROPY_PY_BUILTINS_SLICE
177 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000178 mp_obj_t ostart, ostop, ostep;
179 mp_obj_slice_get(index, &ostart, &ostop, &ostep);
180 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300181 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
182 "only slices with step=1 (aka None) are supported"));
183 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000184
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300185 const byte *pstart, *pstop;
Chris Angelico64b468d2014-06-04 05:28:12 +1000186 if (ostart != mp_const_none) {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300187 pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000188 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300189 pstart = self_data;
Chris Angelico64b468d2014-06-04 05:28:12 +1000190 }
191 if (ostop != mp_const_none) {
192 // pstop will point just after the stop character. This depends on
193 // the \0 at the end of the string.
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300194 pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000195 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300196 pstop = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000197 }
198 if (pstop < pstart) {
199 return MP_OBJ_NEW_QSTR(MP_QSTR_);
200 }
201 return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300202 }
203#endif
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300204 const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
Chris Angelico64b468d2014-06-04 05:28:12 +1000205 int len = 1;
206 if (UTF8_IS_NONASCII(*s)) {
207 // Count the number of 1 bits (after the first)
208 for (char mask = 0x40; *s & mask; mask >>= 1) {
209 ++len;
210 }
211 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300212 return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
Paul Sokolovsky83865342014-06-13 00:51:34 +0300213 } else {
214 return MP_OBJ_NULL; // op not supported
215 }
216}
217
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200218STATIC const mp_map_elem_t struni_locals_dict_table[] = {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300219#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky83865342014-06-13 00:51:34 +0300220 { MP_OBJ_NEW_QSTR(MP_QSTR_encode), (mp_obj_t)&str_encode_obj },
221#endif
222 { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
223 { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
224 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&str_index_obj },
225 { MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj },
226 { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
227 { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
Paul Sokolovskyac2f7a72015-04-04 00:09:23 +0300228 #if MICROPY_PY_BUILTINS_STR_SPLITLINES
229 { MP_OBJ_NEW_QSTR(MP_QSTR_splitlines), (mp_obj_t)&str_splitlines_obj },
230 #endif
Paul Sokolovsky83865342014-06-13 00:51:34 +0300231 { MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj },
232 { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
233 { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj },
234 { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
235 { MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj },
236 { MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj },
237 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
238 { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
239 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
240 { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj },
241 { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj },
242 { MP_OBJ_NEW_QSTR(MP_QSTR_lower), (mp_obj_t)&str_lower_obj },
243 { MP_OBJ_NEW_QSTR(MP_QSTR_upper), (mp_obj_t)&str_upper_obj },
244 { MP_OBJ_NEW_QSTR(MP_QSTR_isspace), (mp_obj_t)&str_isspace_obj },
245 { MP_OBJ_NEW_QSTR(MP_QSTR_isalpha), (mp_obj_t)&str_isalpha_obj },
246 { MP_OBJ_NEW_QSTR(MP_QSTR_isdigit), (mp_obj_t)&str_isdigit_obj },
247 { MP_OBJ_NEW_QSTR(MP_QSTR_isupper), (mp_obj_t)&str_isupper_obj },
248 { MP_OBJ_NEW_QSTR(MP_QSTR_islower), (mp_obj_t)&str_islower_obj },
249};
250
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200251STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300252
253const mp_obj_type_t mp_type_str = {
254 { &mp_type_type },
255 .name = MP_QSTR_str,
Paul Sokolovsky86d38982014-06-13 23:00:15 +0300256 .print = uni_print,
Paul Sokolovsky344e15b2015-01-23 02:15:56 +0200257 .make_new = mp_obj_str_make_new,
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300258 .unary_op = uni_unary_op,
Damien Georgee04a44e2014-06-28 10:27:23 +0100259 .binary_op = mp_obj_str_binary_op,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300260 .subscr = str_subscr,
261 .getiter = mp_obj_new_str_iterator,
Damien Georgee04a44e2014-06-28 10:27:23 +0100262 .buffer_p = { .get_buffer = mp_obj_str_get_buffer },
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200263 .locals_dict = (mp_obj_t)&struni_locals_dict,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300264};
265
Paul Sokolovsky83865342014-06-13 00:51:34 +0300266/******************************************************************************/
267/* str iterator */
268
269typedef struct _mp_obj_str_it_t {
270 mp_obj_base_t base;
271 mp_obj_t str;
Damien George40f3c022014-07-03 13:25:24 +0100272 mp_uint_t cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300273} mp_obj_str_it_t;
274
275STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
276 mp_obj_str_it_t *self = self_in;
277 GET_STR_DATA_LEN(self->str, str, len);
278 if (self->cur < len) {
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300279 const byte *cur = str + self->cur;
280 const byte *end = utf8_next_char(str + self->cur);
281 mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true);
282 self->cur += end - cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300283 return o_out;
284 } else {
285 return MP_OBJ_STOP_ITERATION;
286 }
287}
288
289STATIC const mp_obj_type_t mp_type_str_it = {
290 { &mp_type_type },
291 .name = MP_QSTR_iterator,
292 .getiter = mp_identity,
293 .iternext = str_it_iternext,
294};
295
Damien George44e7cbf2015-05-17 16:44:24 +0100296STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300297 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
298 o->base.type = &mp_type_str_it;
299 o->str = str;
300 o->cur = 0;
301 return o;
302}
303
Paul Sokolovsky97319122014-06-13 22:01:26 +0300304#endif // MICROPY_PY_BUILTINS_STR_UNICODE