blob: 03602b6ec7ac1c935e806559a288a5c4e0d32596 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <stdarg.h>
4#include <string.h>
5#include <assert.h>
6
7#include "nlr.h"
8#include "misc.h"
9#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "obj.h"
12#include "runtime0.h"
13#include "runtime.h"
14
15typedef struct _mp_obj_str_t {
16 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +000017 machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
18 machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
19 byte data[];
Damiend99b0522013-12-21 18:17:45 +000020} mp_obj_str_t;
21
Damien George5fa93b62014-01-22 14:35:10 +000022// use this macro to extract the string hash
23#define GET_STR_HASH(str_obj_in, str_hash) uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; }
24
25// use this macro to extract the string length
26#define GET_STR_LEN(str_obj_in, str_len) uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; }
27
28// use this macro to extract the string data and length
29#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) const byte *str_data; uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; str_data = ((mp_obj_str_t*)str_obj_in)->data; }
30
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020031static mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
32static mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
xyb8cfc9f02014-01-05 18:47:51 +080033
34/******************************************************************************/
35/* str */
36
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020037void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
38 // this escapes characters, but it will be very slow to print (calling print many times)
39 bool has_single_quote = false;
40 bool has_double_quote = false;
41 for (const byte *s = str_data, *top = str_data + str_len; (!has_single_quote || !has_double_quote) && s < top; s++) {
42 if (*s == '\'') {
43 has_single_quote = true;
44 } else if (*s == '"') {
45 has_double_quote = true;
46 }
47 }
48 int quote_char = '\'';
49 if (has_single_quote && !has_double_quote) {
50 quote_char = '"';
51 }
52 print(env, "%c", quote_char);
53 for (const byte *s = str_data, *top = str_data + str_len; s < top; s++) {
54 if (*s == quote_char) {
55 print(env, "\\%c", quote_char);
56 } else if (*s == '\\') {
57 print(env, "\\\\");
58 } else if (32 <= *s && *s <= 126) {
59 print(env, "%c", *s);
60 } else if (*s == '\n') {
61 print(env, "\\n");
62 // TODO add more escape codes here if we want to match CPython
63 } else {
64 print(env, "\\x%02x", *s);
65 }
66 }
67 print(env, "%c", quote_char);
68}
69
70static void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George5fa93b62014-01-22 14:35:10 +000071 GET_STR_DATA_LEN(self_in, str_data, str_len);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020072 bool is_bytes = MP_OBJ_IS_TYPE(self_in, &bytes_type);
73 if (kind == PRINT_STR && !is_bytes) {
Damien George5fa93b62014-01-22 14:35:10 +000074 print(env, "%.*s", str_len, str_data);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020075 } else {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020076 if (is_bytes) {
77 print(env, "b");
78 }
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020079 mp_str_print_quoted(print, env, str_data, str_len);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020080 }
Damiend99b0522013-12-21 18:17:45 +000081}
82
Damien George55baff42014-01-21 21:40:13 +000083// like strstr but with specified length and allows \0 bytes
84// TODO replace with something more efficient/standard
85static const byte *find_subbytes(const byte *haystack, uint hlen, const byte *needle, uint nlen) {
86 if (hlen >= nlen) {
87 for (uint i = 0; i <= hlen - nlen; i++) {
88 bool found = true;
89 for (uint j = 0; j < nlen; j++) {
90 if (haystack[i + j] != needle[j]) {
91 found = false;
92 break;
93 }
94 }
95 if (found) {
96 return haystack + i;
97 }
98 }
99 }
100 return NULL;
101}
102
Damiend99b0522013-12-21 18:17:45 +0000103mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000104 GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
Damiend99b0522013-12-21 18:17:45 +0000105 switch (op) {
106 case RT_BINARY_OP_SUBSCR:
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200107 // TODO: need predicate to check for int-like type (bools are such for example)
108 // ["no", "yes"][1 == 2] is common idiom
109 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George5fa93b62014-01-22 14:35:10 +0000110 uint index = mp_get_index(mp_obj_get_type(lhs_in), lhs_len, rhs_in);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200111 if (MP_OBJ_IS_TYPE(lhs_in, &bytes_type)) {
Damien George7c9c6672014-01-25 00:17:36 +0000112 return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200113 } else {
114 return mp_obj_new_str(lhs_data + index, 1, true);
115 }
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200116#if MICROPY_ENABLE_SLICE
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200117 } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200118 machine_uint_t start, stop;
Paul Sokolovskyea2509d2014-02-02 08:57:05 +0200119 if (!m_seq_get_fast_slice_indexes(lhs_len, rhs_in, &start, &stop)) {
120 assert(0);
121 }
Damien George5fa93b62014-01-22 14:35:10 +0000122 return mp_obj_new_str(lhs_data + start, stop - start, false);
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200123#endif
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200124 } else {
Paul Sokolovskyf8b9d3c2014-01-04 01:38:26 +0200125 // Message doesn't match CPython, but we don't have so much bytes as they
126 // to spend them on verbose wording
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000127 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "index must be int"));
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200128 }
Damiend99b0522013-12-21 18:17:45 +0000129
130 case RT_BINARY_OP_ADD:
131 case RT_BINARY_OP_INPLACE_ADD:
Damien George5fa93b62014-01-22 14:35:10 +0000132 if (MP_OBJ_IS_STR(rhs_in)) {
Damiend99b0522013-12-21 18:17:45 +0000133 // add 2 strings
Damien George5fa93b62014-01-22 14:35:10 +0000134
135 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000136 int alloc_len = lhs_len + rhs_len;
Damien George5fa93b62014-01-22 14:35:10 +0000137
138 /* code for making qstr
Damien George55baff42014-01-21 21:40:13 +0000139 byte *q_ptr;
140 byte *val = qstr_build_start(alloc_len, &q_ptr);
141 memcpy(val, lhs_data, lhs_len);
142 memcpy(val + lhs_len, rhs_data, rhs_len);
Damien George5fa93b62014-01-22 14:35:10 +0000143 return MP_OBJ_NEW_QSTR(qstr_build_end(q_ptr));
144 */
145
146 // code for non-qstr
147 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200148 mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(lhs_in), alloc_len, &data);
Damien George5fa93b62014-01-22 14:35:10 +0000149 memcpy(data, lhs_data, lhs_len);
150 memcpy(data + lhs_len, rhs_data, rhs_len);
151 return mp_obj_str_builder_end(s);
Damiend99b0522013-12-21 18:17:45 +0000152 }
153 break;
Damien George5fa93b62014-01-22 14:35:10 +0000154
Damien George9aa2a522014-02-01 23:04:09 +0000155 case RT_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +0000156 /* NOTE `a in b` is `b.__contains__(a)` */
Damien George5fa93b62014-01-22 14:35:10 +0000157 if (MP_OBJ_IS_STR(rhs_in)) {
158 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George9aa2a522014-02-01 23:04:09 +0000159 return MP_BOOL(find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len) != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000160 }
161 break;
Damien George5fa93b62014-01-22 14:35:10 +0000162
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200163 case RT_BINARY_OP_MULTIPLY:
164 {
165 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
166 return NULL;
167 }
168 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000169 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200170 mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(lhs_in), lhs_len * n, &data);
Damien George5fa93b62014-01-22 14:35:10 +0000171 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
172 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200173 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200174
175 // These 2 are never passed here, dealt with as a special case in rt_binary_op().
176 //case RT_BINARY_OP_EQUAL:
177 //case RT_BINARY_OP_NOT_EQUAL:
178 case RT_BINARY_OP_LESS:
179 case RT_BINARY_OP_LESS_EQUAL:
180 case RT_BINARY_OP_MORE:
181 case RT_BINARY_OP_MORE_EQUAL:
182 if (MP_OBJ_IS_STR(rhs_in)) {
183 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
184 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, rhs_data, rhs_len));
185 }
Damiend99b0522013-12-21 18:17:45 +0000186 }
187
188 return MP_OBJ_NULL; // op not supported
189}
190
191mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000192 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000193
Damien Georgefe8fb912014-01-02 16:36:09 +0000194 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000195 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000196
197 // process args
Damiend99b0522013-12-21 18:17:45 +0000198 uint seq_len;
199 mp_obj_t *seq_items;
200 if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
201 mp_obj_tuple_get(arg, &seq_len, &seq_items);
202 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
203 mp_obj_list_get(arg, &seq_len, &seq_items);
204 } else {
205 goto bad_arg;
206 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000207
208 // count required length
209 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000210 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000211 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000212 goto bad_arg;
213 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000214 if (i > 0) {
215 required_len += sep_len;
216 }
Damien George5fa93b62014-01-22 14:35:10 +0000217 GET_STR_LEN(seq_items[i], l);
218 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000219 }
220
221 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000222 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200223 mp_obj_t joined_str = mp_obj_str_builder_start(mp_obj_get_type(self_in), required_len, &data);
Damiend99b0522013-12-21 18:17:45 +0000224 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000225 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000226 memcpy(data, sep_str, sep_len);
227 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000228 }
Damien George5fa93b62014-01-22 14:35:10 +0000229 GET_STR_DATA_LEN(seq_items[i], s, l);
230 memcpy(data, s, l);
231 data += l;
Damiend99b0522013-12-21 18:17:45 +0000232 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000233
234 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000235 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000236
237bad_arg:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000238 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000239}
240
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200241#define is_ws(c) ((c) == ' ' || (c) == '\t')
242
243static mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
244 int splits = -1;
245 mp_obj_t sep = mp_const_none;
246 if (n_args > 1) {
247 sep = args[1];
248 if (n_args > 2) {
249 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
250 }
251 }
252 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000253 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200254 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000255 GET_STR_DATA_LEN(args[0], s, len);
256 const byte *top = s + len;
257 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200258
259 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000260 while (s < top && is_ws(*s)) s++;
261 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200262 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000263 while (s < top && !is_ws(*s)) s++;
264 rt_list_append(res, mp_obj_new_str(start, s - start, false));
265 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200266 break;
267 }
Damien George5fa93b62014-01-22 14:35:10 +0000268 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200269 if (splits > 0) {
270 splits--;
271 }
272 }
273
Damien George5fa93b62014-01-22 14:35:10 +0000274 if (s < top) {
275 rt_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200276 }
277
278 return res;
279}
280
Damien Georgea11ceca2014-01-19 16:02:09 +0000281static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
John R. Lentone8204912014-01-12 21:53:52 +0000282 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000283 assert(MP_OBJ_IS_STR(args[0]));
284 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000285
Damien George5fa93b62014-01-22 14:35:10 +0000286 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
287 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000288
289 size_t start = 0;
290 size_t end = haystack_len;
291 /* TODO use a non-exception-throwing mp_get_index */
292 if (n_args >= 3 && args[2] != mp_const_none) {
293 start = mp_get_index(&str_type, haystack_len, args[2]);
294 }
295 if (n_args >= 4 && args[3] != mp_const_none) {
296 end = mp_get_index(&str_type, haystack_len, args[3]);
297 }
298
Damien George5fa93b62014-01-22 14:35:10 +0000299 const byte *p = find_subbytes(haystack + start, haystack_len - start, needle, needle_len);
Damien George23005372014-01-13 19:39:01 +0000300 if (p == NULL) {
301 // not found
302 return MP_OBJ_NEW_SMALL_INT(-1);
303 } else {
304 // found
305 machine_int_t pos = p - haystack;
John R. Lentone8204912014-01-12 21:53:52 +0000306 if (pos + needle_len > end) {
307 pos = -1;
308 }
Damien George23005372014-01-13 19:39:01 +0000309 return MP_OBJ_NEW_SMALL_INT(pos);
John R. Lentone8204912014-01-12 21:53:52 +0000310 }
John R. Lentone8204912014-01-12 21:53:52 +0000311}
312
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200313// TODO: (Much) more variety in args
314static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
315 GET_STR_DATA_LEN(self_in, str, str_len);
316 GET_STR_DATA_LEN(arg, prefix, prefix_len);
317 if (prefix_len > str_len) {
318 return mp_const_false;
319 }
320 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
321}
322
Damien George5fa93b62014-01-22 14:35:10 +0000323static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
324 for (size_t i = 0; i < str_len; i++) {
325 if (str[i] == c) {
326 return true;
327 }
328 }
329 return false;
330}
331
Damien Georgea11ceca2014-01-19 16:02:09 +0000332mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800333 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000334 assert(MP_OBJ_IS_STR(args[0]));
335
336 const byte *chars_to_del;
337 uint chars_to_del_len;
338 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800339
340 if (n_args == 1) {
341 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000342 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800343 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000344 assert(MP_OBJ_IS_STR(args[1]));
345 GET_STR_DATA_LEN(args[1], s, l);
346 chars_to_del = s;
347 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800348 }
349
Damien George5fa93b62014-01-22 14:35:10 +0000350 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800351
352 size_t first_good_char_pos = 0;
353 bool first_good_char_pos_set = false;
354 size_t last_good_char_pos = 0;
355 for (size_t i = 0; i < orig_str_len; i++) {
356 if (!chr_in_str(chars_to_del, chars_to_del_len, orig_str[i])) {
357 last_good_char_pos = i;
358 if (!first_good_char_pos_set) {
359 first_good_char_pos = i;
360 first_good_char_pos_set = true;
361 }
362 }
363 }
364
365 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000366 // string is all whitespace, return ''
367 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800368 }
369
370 assert(last_good_char_pos >= first_good_char_pos);
371 //+1 to accomodate the last character
372 size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000373 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800374}
375
Damien Georgea11ceca2014-01-19 16:02:09 +0000376mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000377 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000378
Damien George5fa93b62014-01-22 14:35:10 +0000379 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000380 int arg_i = 1;
381 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000382 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000383 if (*str == '{') {
384 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000385 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000386 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200387 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000388 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000389 if (arg_i >= n_args) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000390 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000391 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200392 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000393 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000394 arg_i++;
395 }
396 } else {
397 vstr_add_char(vstr, *str);
398 }
399 }
400
Damien George5fa93b62014-01-22 14:35:10 +0000401 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
402 vstr_free(vstr);
403 return s;
Damiend99b0522013-12-21 18:17:45 +0000404}
405
xbe480c15a2014-01-30 22:17:30 -0800406mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
407 assert(MP_OBJ_IS_STR(args[0]));
408 assert(MP_OBJ_IS_STR(args[1]));
409 assert(MP_OBJ_IS_STR(args[2]));
410
Damien George94f68302014-01-31 23:45:12 +0000411 machine_int_t max_rep = 0;
xbe480c15a2014-01-30 22:17:30 -0800412 if (n_args == 4) {
413 assert(MP_OBJ_IS_SMALL_INT(args[3]));
414 max_rep = MP_OBJ_SMALL_INT_VALUE(args[3]);
415 if (max_rep == 0) {
Damien George94f68302014-01-31 23:45:12 +0000416 return args[0];
417 } else if (max_rep < 0) {
xbe480c15a2014-01-30 22:17:30 -0800418 max_rep = 0;
419 }
420 }
Damien George94f68302014-01-31 23:45:12 +0000421
422 // if max_rep is still 0 by this point we will need to do all possible replacements
xbe480c15a2014-01-30 22:17:30 -0800423
424 GET_STR_DATA_LEN(args[0], str, str_len);
425 GET_STR_DATA_LEN(args[1], old, old_len);
426 GET_STR_DATA_LEN(args[2], new, new_len);
Damien George94f68302014-01-31 23:45:12 +0000427
428 // old won't exist in str if it's longer, so nothing to replace
xbe480c15a2014-01-30 22:17:30 -0800429 if (old_len > str_len) {
Damien George94f68302014-01-31 23:45:12 +0000430 return args[0];
xbe480c15a2014-01-30 22:17:30 -0800431 }
432
Damien George94f68302014-01-31 23:45:12 +0000433 // data for the replaced string
434 byte *data = NULL;
435 mp_obj_t replaced_str = MP_OBJ_NULL;
xbe480c15a2014-01-30 22:17:30 -0800436
Damien George94f68302014-01-31 23:45:12 +0000437 // do 2 passes over the string:
438 // first pass computes the required length of the replaced string
439 // second pass does the replacements
440 for (;;) {
441 machine_uint_t replaced_str_index = 0;
442 machine_uint_t num_replacements_done = 0;
443 const byte *old_occurrence;
444 const byte *offset_ptr = str;
445 machine_uint_t offset_num = 0;
446 while ((old_occurrence = find_subbytes(offset_ptr, str_len - offset_num, old, old_len)) != NULL) {
447 // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence
448 if (data != NULL) {
449 memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr);
450 }
451 replaced_str_index += old_occurrence - offset_ptr;
452 // copy the replacement string
453 if (data != NULL) {
454 memcpy(data + replaced_str_index, new, new_len);
455 }
456 replaced_str_index += new_len;
457 offset_ptr = old_occurrence + old_len;
458 offset_num = offset_ptr - str;
xbe480c15a2014-01-30 22:17:30 -0800459
Damien George94f68302014-01-31 23:45:12 +0000460 num_replacements_done++;
461 if (max_rep != 0 && num_replacements_done == max_rep){
462 break;
463 }
464 }
465
466 // copy from just after end of last occurrence of to-be-replaced string to end of old string
467 if (data != NULL) {
468 memcpy(data + replaced_str_index, offset_ptr, str_len - offset_num);
469 }
470 replaced_str_index += str_len - offset_num;
471
472 if (data == NULL) {
473 // first pass
474 if (num_replacements_done == 0) {
475 // no substr found, return original string
476 return args[0];
477 } else {
478 // substr found, allocate new string
479 replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data);
480 }
481 } else {
482 // second pass, we are done
483 break;
484 }
xbe480c15a2014-01-30 22:17:30 -0800485 }
Damien George94f68302014-01-31 23:45:12 +0000486
xbe480c15a2014-01-30 22:17:30 -0800487 return mp_obj_str_builder_end(replaced_str);
488}
489
John R. Lentone8204912014-01-12 21:53:52 +0000490static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
Damiend99b0522013-12-21 18:17:45 +0000491static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200492static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200493static MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
xbe7b0f39f2014-01-08 14:23:45 -0800494static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
Damiend99b0522013-12-21 18:17:45 +0000495static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
xbe480c15a2014-01-30 22:17:30 -0800496static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
Damiend99b0522013-12-21 18:17:45 +0000497
ian-va5a01df2014-01-06 14:14:11 -0800498static const mp_method_t str_type_methods[] = {
John R. Lentone8204912014-01-12 21:53:52 +0000499 { "find", &str_find_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800500 { "join", &str_join_obj },
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200501 { "split", &str_split_obj },
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200502 { "startswith", &str_startswith_obj },
xbe7b0f39f2014-01-08 14:23:45 -0800503 { "strip", &str_strip_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800504 { "format", &str_format_obj },
xbe480c15a2014-01-30 22:17:30 -0800505 { "replace", &str_replace_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800506 { NULL, NULL }, // end-of-list sentinel
507};
Damien George97209d32014-01-07 15:58:30 +0000508
Damiend99b0522013-12-21 18:17:45 +0000509const mp_obj_type_t str_type = {
510 { &mp_const_type },
511 "str",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200512 .print = str_print,
513 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200514 .getiter = mp_obj_new_str_iterator,
ian-v7a16fad2014-01-06 09:52:29 -0800515 .methods = str_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000516};
517
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200518// Reuses most of methods from str
519const mp_obj_type_t bytes_type = {
520 { &mp_const_type },
521 "bytes",
522 .print = str_print,
523 .binary_op = str_binary_op,
524 .getiter = mp_obj_new_bytes_iterator,
525 .methods = str_type_methods,
526};
527
528mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Damien George5fa93b62014-01-22 14:35:10 +0000529 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200530 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000531 o->len = len;
532 *data = o->data;
Damiend99b0522013-12-21 18:17:45 +0000533 return o;
534}
535
Damien George5fa93b62014-01-22 14:35:10 +0000536mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
537 assert(MP_OBJ_IS_STR(o_in));
538 mp_obj_str_t *o = o_in;
539 o->hash = qstr_compute_hash(o->data, o->len);
540 o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
541 return o;
542}
543
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200544static mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
545 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
546 o->base.type = type;
547 o->hash = qstr_compute_hash(data, len);
548 o->len = len;
549 memcpy(o->data, data, len * sizeof(byte));
550 o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
551 return o;
552}
553
Damien George5fa93b62014-01-22 14:35:10 +0000554mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
555 qstr q = qstr_find_strn(data, len);
556 if (q != MP_QSTR_NULL) {
557 // qstr with this data already exists
558 return MP_OBJ_NEW_QSTR(q);
559 } else if (make_qstr_if_not_already) {
560 // no existing qstr, make a new one
561 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
562 } else {
563 // no existing qstr, don't make one
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200564 return str_new(&str_type, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200565 }
Damien George5fa93b62014-01-22 14:35:10 +0000566}
567
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200568mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
569 return str_new(&bytes_type, data, len);
570}
571
Damien George5fa93b62014-01-22 14:35:10 +0000572bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
573 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
574 return s1 == s2;
575 } else {
576 GET_STR_HASH(s1, h1);
577 GET_STR_HASH(s2, h2);
578 if (h1 != h2) {
579 return false;
580 }
581 GET_STR_DATA_LEN(s1, d1, l1);
582 GET_STR_DATA_LEN(s2, d2, l2);
583 if (l1 != l2) {
584 return false;
585 }
Damien George1e708fe2014-01-23 18:27:51 +0000586 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200587 }
Damien George5fa93b62014-01-22 14:35:10 +0000588}
589
Damien Georgeb829b5c2014-01-25 13:51:19 +0000590void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
591void bad_implicit_conversion(mp_obj_t self_in) {
592 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "Can't convert '%s' object to str implicitly", mp_obj_get_type_str(self_in)));
593}
594
Damien George5fa93b62014-01-22 14:35:10 +0000595uint mp_obj_str_get_hash(mp_obj_t self_in) {
596 if (MP_OBJ_IS_STR(self_in)) {
597 GET_STR_HASH(self_in, h);
598 return h;
599 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000600 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000601 }
602}
603
604uint mp_obj_str_get_len(mp_obj_t self_in) {
605 if (MP_OBJ_IS_STR(self_in)) {
606 GET_STR_LEN(self_in, l);
607 return l;
608 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000609 bad_implicit_conversion(self_in);
610 }
611}
612
613// use this if you will anyway convert the string to a qstr
614// will be more efficient for the case where it's already a qstr
615qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
616 if (MP_OBJ_IS_QSTR(self_in)) {
617 return MP_OBJ_QSTR_VALUE(self_in);
618 } else if (MP_OBJ_IS_TYPE(self_in, &str_type)) {
619 mp_obj_str_t *self = self_in;
620 return qstr_from_strn((char*)self->data, self->len);
621 } else {
622 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000623 }
624}
625
626// only use this function if you need the str data to be zero terminated
627// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
628const char *mp_obj_str_get_str(mp_obj_t self_in) {
629 if (MP_OBJ_IS_STR(self_in)) {
630 GET_STR_DATA_LEN(self_in, s, l);
631 (void)l; // len unused
632 return (const char*)s;
633 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000634 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000635 }
636}
637
638const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
639 if (MP_OBJ_IS_STR(self_in)) {
640 GET_STR_DATA_LEN(self_in, s, l);
641 *len = l;
642 return s;
643 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000644 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000645 }
Damiend99b0522013-12-21 18:17:45 +0000646}
xyb8cfc9f02014-01-05 18:47:51 +0800647
648/******************************************************************************/
649/* str iterator */
650
651typedef struct _mp_obj_str_it_t {
652 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000653 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800654 machine_uint_t cur;
655} mp_obj_str_it_t;
656
657mp_obj_t str_it_iternext(mp_obj_t self_in) {
658 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000659 GET_STR_DATA_LEN(self->str, str, len);
660 if (self->cur < len) {
661 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800662 self->cur += 1;
663 return o_out;
664 } else {
665 return mp_const_stop_iteration;
666 }
667}
668
669static const mp_obj_type_t str_it_type = {
670 { &mp_const_type },
671 "str_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200672 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800673};
674
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200675mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
676 mp_obj_str_it_t *self = self_in;
677 GET_STR_DATA_LEN(self->str, str, len);
678 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000679 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200680 self->cur += 1;
681 return o_out;
682 } else {
683 return mp_const_stop_iteration;
684 }
685}
686
687static const mp_obj_type_t bytes_it_type = {
688 { &mp_const_type },
689 "bytes_iterator",
690 .iternext = bytes_it_iternext,
691};
692
693mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800694 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
695 o->base.type = &str_it_type;
696 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200697 o->cur = 0;
698 return o;
699}
700
701mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
702 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
703 o->base.type = &bytes_it_type;
704 o->str = str;
705 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800706 return o;
707}