blob: d7ce4fca0e58ef81ae39073ba3ac44def0e1eb6b [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
Paul Sokolovsky8fea8332019-01-31 11:55:21 +03007 * Copyright (c) 2014-2016 Paul Sokolovsky
Paul Sokolovsky83865342014-06-13 00:51:34 +03008 *
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
Angus Grattondecf8e62024-02-27 15:32:29 +110037static 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
Angus Grattondecf8e62024-02-27 15:32:29 +110042static 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
Angus Grattondecf8e62024-02-27 15:32:29 +110086static 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);
Jim Mussaredf5f9edf2022-08-18 15:01:26 +100088 #if MICROPY_PY_JSON
Damien George612045f2014-09-17 22:56:34 +010089 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) {
Joris Peeraer5020b142020-10-22 10:38:03 +020095 print->print_strn(print->data, (const char *)str_data, str_len);
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
Angus Grattondecf8e62024-02-27 15:32:29 +1100101static 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:
Damien George19aee942018-02-14 18:19:22 +1100107 return MP_OBJ_NEW_SMALL_INT(utf8_charlen(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,
Damien George69661f32020-02-27 15:36:53 +1100116 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.
Andrew Leechf7f56d42022-08-10 14:13:17 +1000119 if (type == &mp_type_bytes
120 #if MICROPY_PY_BUILTINS_BYTEARRAY
121 || type == &mp_type_bytearray
122 #endif
123 ) {
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300124 // Taken from objstr.c:str_index_to_ptr()
Damien Georgec88cfe12017-03-23 16:17:40 +1100125 size_t index_val = mp_get_index(type, self_len, index, is_slice);
Paul Sokolovsky6af90b22016-07-25 14:43:04 +0300126 return self_data + index_val;
127 }
128
Damien George40f3c022014-07-03 13:25:24 +0100129 mp_int_t i;
Chris Angelico64b468d2014-06-04 05:28:12 +1000130 // Copied from mp_get_index; I don't want bounds checking, just give me
131 // the integer as-is. (I can't bounds-check without scanning the whole
132 // string; an out-of-bounds index will be caught in the loops below.)
Damien Georgeeee1e882019-01-30 18:49:52 +1100133 if (mp_obj_is_small_int(index)) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000134 i = MP_OBJ_SMALL_INT_VALUE(index);
135 } else if (!mp_obj_get_int_maybe(index, &i)) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100136 mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("string indices must be integers, not %s"), mp_obj_get_type_str(index));
Chris Angelico64b468d2014-06-04 05:28:12 +1000137 }
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300138 const byte *s, *top = self_data + self_len;
Damien George69661f32020-02-27 15:36:53 +1100139 if (i < 0) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000140 // Negative indexing is performed by counting from the end of the string.
141 for (s = top - 1; i; --s) {
142 if (s < self_data) {
143 if (is_slice) {
144 return self_data;
145 }
Jim Mussareddef76fe2020-03-02 22:35:22 +1100146 mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("string index out of range"));
Chris Angelico64b468d2014-06-04 05:28:12 +1000147 }
148 if (!UTF8_IS_CONT(*s)) {
149 ++i;
150 }
151 }
152 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000153 } else {
154 // Positive indexing, correspondingly, counts from the start of the string.
155 // It's assumed that negative indexing will generally be used with small
156 // absolute values (eg str[-1], not str[-1000000]), which means it'll be
157 // more efficient this way.
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300158 s = self_data;
159 while (1) {
160 // First check out-of-bounds
Chris Angelico64b468d2014-06-04 05:28:12 +1000161 if (s >= top) {
162 if (is_slice) {
163 return top;
164 }
Jim Mussareddef76fe2020-03-02 22:35:22 +1100165 mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("string index out of range"));
Chris Angelico64b468d2014-06-04 05:28:12 +1000166 }
Paul Sokolovskyed1c1942016-07-25 19:02:51 +0300167 // Then check completion
168 if (i-- == 0) {
169 break;
170 }
171 // Then skip UTF-8 char
172 ++s;
Chris Angelico64b468d2014-06-04 05:28:12 +1000173 while (UTF8_IS_CONT(*s)) {
174 ++s;
175 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000176 }
177 }
178 return s;
179}
180
Angus Grattondecf8e62024-02-27 15:32:29 +1100181static mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien Georgebfbd9442020-01-09 11:01:14 +1100182 const mp_obj_type_t *type = mp_obj_get_type(self_in);
Damien George0528c5a2015-04-04 19:42:03 +0100183 assert(type == &mp_type_str);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300184 GET_STR_DATA_LEN(self_in, self_data, self_len);
185 if (value == MP_OBJ_SENTINEL) {
186 // load
Damien George69661f32020-02-27 15:36:53 +1100187 #if MICROPY_PY_BUILTINS_SLICE
Damien Georgeeee1e882019-01-30 18:49:52 +1100188 if (mp_obj_is_type(index, &mp_type_slice)) {
Chris Angelico64b468d2014-06-04 05:28:12 +1000189 mp_obj_t ostart, ostop, ostep;
Nicko van Someren10709842019-11-20 18:53:07 -0700190 mp_obj_slice_t *slice = MP_OBJ_TO_PTR(index);
191 ostart = slice->start;
192 ostop = slice->stop;
193 ostep = slice->step;
194
Chris Angelico64b468d2014-06-04 05:28:12 +1000195 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100196 mp_raise_NotImplementedError(MP_ERROR_TEXT("only slices with step=1 (aka None) are supported"));
Paul Sokolovsky83865342014-06-13 00:51:34 +0300197 }
Chris Angelico64b468d2014-06-04 05:28:12 +1000198
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300199 const byte *pstart, *pstop;
Chris Angelico64b468d2014-06-04 05:28:12 +1000200 if (ostart != mp_const_none) {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300201 pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000202 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300203 pstart = self_data;
Chris Angelico64b468d2014-06-04 05:28:12 +1000204 }
205 if (ostop != mp_const_none) {
206 // pstop will point just after the stop character. This depends on
207 // the \0 at the end of the string.
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300208 pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
Chris Angelico64b468d2014-06-04 05:28:12 +1000209 } else {
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300210 pstop = self_data + self_len;
Chris Angelico64b468d2014-06-04 05:28:12 +1000211 }
212 if (pstop < pstart) {
213 return MP_OBJ_NEW_QSTR(MP_QSTR_);
214 }
215 return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300216 }
Damien George69661f32020-02-27 15:36:53 +1100217 #endif
Paul Sokolovskyea2c9362014-06-15 00:35:09 +0300218 const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
Chris Angelico64b468d2014-06-04 05:28:12 +1000219 int len = 1;
220 if (UTF8_IS_NONASCII(*s)) {
221 // Count the number of 1 bits (after the first)
222 for (char mask = 0x40; *s & mask; mask >>= 1) {
223 ++len;
224 }
225 }
Damien George69661f32020-02-27 15:36:53 +1100226 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 +0300227 } else {
228 return MP_OBJ_NULL; // op not supported
229 }
230}
231
Jim Mussared662b9762021-07-14 14:38:38 +1000232MP_DEFINE_CONST_OBJ_TYPE(
233 mp_type_str,
234 MP_QSTR_str,
Jim Mussared6da41b52022-09-16 23:57:38 +1000235 MP_TYPE_FLAG_ITER_IS_GETITER,
Jim Mussared94beeab2022-09-17 00:31:23 +1000236 make_new, mp_obj_str_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000237 print, uni_print,
238 unary_op, uni_unary_op,
239 binary_op, mp_obj_str_binary_op,
240 subscr, str_subscr,
Jim Mussared6da41b52022-09-16 23:57:38 +1000241 iter, mp_obj_new_str_iterator,
Jim Mussared662b9762021-07-14 14:38:38 +1000242 buffer, mp_obj_str_get_buffer,
Jim Mussared9dce8272022-06-24 16:27:46 +1000243 locals_dict, &mp_obj_str_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000244 );
Paul Sokolovsky83865342014-06-13 00:51:34 +0300245
Paul Sokolovsky83865342014-06-13 00:51:34 +0300246/******************************************************************************/
247/* str iterator */
248
249typedef struct _mp_obj_str_it_t {
250 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000251 mp_fun_1_t iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300252 mp_obj_t str;
Damien Georgec0d95002017-02-16 16:26:48 +1100253 size_t cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300254} mp_obj_str_it_t;
255
Angus Grattondecf8e62024-02-27 15:32:29 +1100256static mp_obj_t str_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000257 mp_obj_str_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300258 GET_STR_DATA_LEN(self->str, str, len);
259 if (self->cur < len) {
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300260 const byte *cur = str + self->cur;
261 const byte *end = utf8_next_char(str + self->cur);
Damien George69661f32020-02-27 15:36:53 +1100262 mp_obj_t o_out = mp_obj_new_str_via_qstr((const char *)cur, end - cur);
Paul Sokolovsky79b7fe22014-06-14 02:07:25 +0300263 self->cur += end - cur;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300264 return o_out;
265 } else {
266 return MP_OBJ_STOP_ITERATION;
267 }
268}
269
Angus Grattondecf8e62024-02-27 15:32:29 +1100270static mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) {
Damien Georgeae8d8672016-01-09 23:14:54 +0000271 assert(sizeof(mp_obj_str_it_t) <= sizeof(mp_obj_iter_buf_t));
Damien George69661f32020-02-27 15:36:53 +1100272 mp_obj_str_it_t *o = (mp_obj_str_it_t *)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000273 o->base.type = &mp_type_polymorph_iter;
274 o->iternext = str_it_iternext;
Paul Sokolovsky83865342014-06-13 00:51:34 +0300275 o->str = str;
276 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000277 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky83865342014-06-13 00:51:34 +0300278}
279
Paul Sokolovsky97319122014-06-13 22:01:26 +0300280#endif // MICROPY_PY_BUILTINS_STR_UNICODE