blob: 1cf4ed4743db73ed814c61b26ef3f3ad67892919 [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"
36#include "py/pfenv.h"
Paul Sokolovsky83865342014-06-13 00:51:34 +030037
Paul Sokolovsky97319122014-06-13 22:01:26 +030038#if MICROPY_PY_BUILTINS_STR_UNICODE
Paul Sokolovsky83865342014-06-13 00:51:34 +030039
40STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
Paul Sokolovsky83865342014-06-13 00:51:34 +030041
42/******************************************************************************/
43/* str */
44
Paul Sokolovsky86d38982014-06-13 23:00:15 +030045STATIC void uni_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030046 // 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 }
Damien George2e2e4042015-03-19 00:21:29 +000056 unichar quote_char = '\'';
Paul Sokolovsky83865342014-06-13 00:51:34 +030057 if (has_single_quote && !has_double_quote) {
58 quote_char = '"';
59 }
60 print(env, "%c", quote_char);
Paul Sokolovsky00c904b2014-06-14 17:48:40 +030061 const byte *s = str_data, *top = str_data + str_len;
Chris Angelico64b468d2014-06-04 05:28:12 +100062 while (s < top) {
63 unichar ch;
Paul Sokolovsky86d38982014-06-13 23:00:15 +030064 ch = utf8_get_char(s);
65 s = utf8_next_char(s);
Chris Angelico64b468d2014-06-04 05:28:12 +100066 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 Sokolovsky83865342014-06-13 00:51:34 +030084 }
85 }
86 print(env, "%c", quote_char);
87}
88
Paul Sokolovsky86d38982014-06-13 23:00:15 +030089STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030090 GET_STR_DATA_LEN(self_in, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010091 #if MICROPY_PY_UJSON
92 if (kind == PRINT_JSON) {
Damien Georgecde0ca22014-09-25 17:35:56 +010093 mp_str_print_json(print, env, str_data, str_len);
Damien George612045f2014-09-17 22:56:34 +010094 return;
95 }
96 #endif
Paul Sokolovsky86d38982014-06-13 23:00:15 +030097 if (kind == PRINT_STR) {
Paul Sokolovsky83865342014-06-13 00:51:34 +030098 print(env, "%.*s", str_len, str_data);
99 } else {
Paul Sokolovsky86d38982014-06-13 23:00:15 +0300100 uni_print_quoted(print, env, str_data, str_len);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300101 }
102}
103
Damien Georgeecc88e92014-08-30 00:35:11 +0100104STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300105 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 Sokolovsky9e215fa2014-06-28 23:14:30 +0300110 return MP_OBJ_NEW_SMALL_INT(unichar_charlen((const char *)str_data, str_len));
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300111 default:
112 return MP_OBJ_NULL; // op not supported
113 }
114}
115
Chris Angelico64b468d2014-06-04 05:28:12 +1000116// Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or
117// be capped to the first/last character of the string, depending on is_slice.
Damien George4abff752014-08-30 14:59:21 +0100118const 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 +0300119 mp_obj_t index, bool is_slice) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000120 (void)type;
Damien George40f3c022014-07-03 13:25:24 +0100121 mp_int_t i;
Chris Angelico64b468d2014-06-04 05:28:12 +1000122 // Copied from mp_get_index; I don't want bounds checking, just give me
123 // the integer as-is. (I can't bounds-check without scanning the whole
124 // string; an out-of-bounds index will be caught in the loops below.)
125 if (MP_OBJ_IS_SMALL_INT(index)) {
126 i = MP_OBJ_SMALL_INT_VALUE(index);
127 } else if (!mp_obj_get_int_maybe(index, &i)) {
128 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index)));
129 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300130 const byte *s, *top = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000131 if (i < 0)
132 {
133 // Negative indexing is performed by counting from the end of the string.
134 for (s = top - 1; i; --s) {
135 if (s < self_data) {
136 if (is_slice) {
137 return self_data;
138 }
139 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range"));
140 }
141 if (!UTF8_IS_CONT(*s)) {
142 ++i;
143 }
144 }
145 ++s;
146 } else if (!i) {
147 return self_data; // Shortcut - str[0] is its base pointer
148 } else {
149 // Positive indexing, correspondingly, counts from the start of the string.
150 // It's assumed that negative indexing will generally be used with small
151 // absolute values (eg str[-1], not str[-1000000]), which means it'll be
152 // more efficient this way.
153 for (s = self_data; true; ++s) {
154 if (s >= top) {
155 if (is_slice) {
156 return top;
157 }
158 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range"));
159 }
160 while (UTF8_IS_CONT(*s)) {
161 ++s;
162 }
163 if (!i--) {
164 return s;
165 }
166 }
167 }
168 return s;
169}
170
Paul Sokolovsky83865342014-06-13 00:51:34 +0300171STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
172 mp_obj_type_t *type = mp_obj_get_type(self_in);
173 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
185 if (type == &mp_type_bytes) {
Damien George40f3c022014-07-03 13:25:24 +0100186 mp_int_t start = 0, stop = self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000187 if (ostart != mp_const_none) {
188 start = MP_OBJ_SMALL_INT_VALUE(ostart);
189 if (start < 0) {
190 start = self_len + start;
191 }
192 }
193 if (ostop != mp_const_none) {
194 stop = MP_OBJ_SMALL_INT_VALUE(ostop);
195 if (stop < 0) {
196 stop = self_len + stop;
197 }
198 }
199 return mp_obj_new_str_of_type(type, self_data + start, stop - start);
200 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300201 const byte *pstart, *pstop;
Chris Angelico64b468d2014-06-04 05:28:12 +1000202 if (ostart != mp_const_none) {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300203 pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000204 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300205 pstart = self_data;
Chris Angelico64b468d2014-06-04 05:28:12 +1000206 }
207 if (ostop != mp_const_none) {
208 // pstop will point just after the stop character. This depends on
209 // the \0 at the end of the string.
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300210 pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000211 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300212 pstop = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000213 }
214 if (pstop < pstart) {
215 return MP_OBJ_NEW_QSTR(MP_QSTR_);
216 }
217 return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300218 }
219#endif
Paul Sokolovsky83865342014-06-13 00:51:34 +0300220 if (type == &mp_type_bytes) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000221 uint index_val = mp_get_index(type, self_len, index, false);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100222 return MP_OBJ_NEW_SMALL_INT(self_data[index_val]);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300223 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300224 const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
Chris Angelico64b468d2014-06-04 05:28:12 +1000225 int len = 1;
226 if (UTF8_IS_NONASCII(*s)) {
227 // Count the number of 1 bits (after the first)
228 for (char mask = 0x40; *s & mask; mask >>= 1) {
229 ++len;
230 }
231 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300232 return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
Paul Sokolovsky83865342014-06-13 00:51:34 +0300233 } else {
234 return MP_OBJ_NULL; // op not supported
235 }
236}
237
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200238STATIC const mp_map_elem_t struni_locals_dict_table[] = {
Paul Sokolovsky83865342014-06-13 00:51:34 +0300239#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky83865342014-06-13 00:51:34 +0300240 { MP_OBJ_NEW_QSTR(MP_QSTR_encode), (mp_obj_t)&str_encode_obj },
241#endif
242 { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
243 { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
244 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&str_index_obj },
245 { MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj },
246 { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
247 { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
248 { MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj },
249 { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
250 { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj },
251 { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
252 { MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj },
253 { MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj },
254 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
255 { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
256 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
257 { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj },
258 { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj },
259 { MP_OBJ_NEW_QSTR(MP_QSTR_lower), (mp_obj_t)&str_lower_obj },
260 { MP_OBJ_NEW_QSTR(MP_QSTR_upper), (mp_obj_t)&str_upper_obj },
261 { MP_OBJ_NEW_QSTR(MP_QSTR_isspace), (mp_obj_t)&str_isspace_obj },
262 { MP_OBJ_NEW_QSTR(MP_QSTR_isalpha), (mp_obj_t)&str_isalpha_obj },
263 { MP_OBJ_NEW_QSTR(MP_QSTR_isdigit), (mp_obj_t)&str_isdigit_obj },
264 { MP_OBJ_NEW_QSTR(MP_QSTR_isupper), (mp_obj_t)&str_isupper_obj },
265 { MP_OBJ_NEW_QSTR(MP_QSTR_islower), (mp_obj_t)&str_islower_obj },
266};
267
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200268STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300269
270const mp_obj_type_t mp_type_str = {
271 { &mp_type_type },
272 .name = MP_QSTR_str,
Paul Sokolovsky86d38982014-06-13 23:00:15 +0300273 .print = uni_print,
Paul Sokolovsky344e15b2015-01-23 02:15:56 +0200274 .make_new = mp_obj_str_make_new,
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300275 .unary_op = uni_unary_op,
Damien Georgee04a44e2014-06-28 10:27:23 +0100276 .binary_op = mp_obj_str_binary_op,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300277 .subscr = str_subscr,
278 .getiter = mp_obj_new_str_iterator,
Damien Georgee04a44e2014-06-28 10:27:23 +0100279 .buffer_p = { .get_buffer = mp_obj_str_get_buffer },
Paul Sokolovsky6113eb22015-01-23 02:05:58 +0200280 .locals_dict = (mp_obj_t)&struni_locals_dict,
Paul Sokolovsky83865342014-06-13 00:51:34 +0300281};
282
Paul Sokolovsky83865342014-06-13 00:51:34 +0300283/******************************************************************************/
284/* str iterator */
285
286typedef struct _mp_obj_str_it_t {
287 mp_obj_base_t base;
288 mp_obj_t str;
Damien George40f3c022014-07-03 13:25:24 +0100289 mp_uint_t cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300290} mp_obj_str_it_t;
291
292STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
293 mp_obj_str_it_t *self = self_in;
294 GET_STR_DATA_LEN(self->str, str, len);
295 if (self->cur < len) {
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300296 const byte *cur = str + self->cur;
297 const byte *end = utf8_next_char(str + self->cur);
298 mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true);
299 self->cur += end - cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300300 return o_out;
301 } else {
302 return MP_OBJ_STOP_ITERATION;
303 }
304}
305
306STATIC const mp_obj_type_t mp_type_str_it = {
307 { &mp_type_type },
308 .name = MP_QSTR_iterator,
309 .getiter = mp_identity,
310 .iternext = str_it_iternext,
311};
312
Paul Sokolovsky83865342014-06-13 00:51:34 +0300313mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
314 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
315 o->base.type = &mp_type_str_it;
316 o->str = str;
317 o->cur = 0;
318 return o;
319}
320
Paul Sokolovsky97319122014-06-13 22:01:26 +0300321#endif // MICROPY_PY_BUILTINS_STR_UNICODE