blob: 3f6aa483e299bf2746bb66d300cbbcf88d272c50 [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)) {
Damien Georgec8d13842014-01-04 01:06:10 +0000118 machine_int_t start, stop, step;
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200119 mp_obj_slice_get(rhs_in, &start, &stop, &step);
120 assert(step == 1);
Paul Sokolovskydecad082014-01-03 23:36:56 +0200121 if (start < 0) {
Damien George55baff42014-01-21 21:40:13 +0000122 start = lhs_len + start;
Paul Sokolovsky6ee1e382014-01-04 03:47:34 +0200123 if (start < 0) {
124 start = 0;
125 }
Damien George55baff42014-01-21 21:40:13 +0000126 } else if (start > lhs_len) {
127 start = lhs_len;
Paul Sokolovskydecad082014-01-03 23:36:56 +0200128 }
129 if (stop <= 0) {
Damien George55baff42014-01-21 21:40:13 +0000130 stop = lhs_len + stop;
Paul Sokolovsky6ee1e382014-01-04 03:47:34 +0200131 // CPython returns empty string in such case
132 if (stop < 0) {
133 stop = start;
134 }
Damien George55baff42014-01-21 21:40:13 +0000135 } else if (stop > lhs_len) {
136 stop = lhs_len;
Paul Sokolovskydecad082014-01-03 23:36:56 +0200137 }
Damien George5fa93b62014-01-22 14:35:10 +0000138 return mp_obj_new_str(lhs_data + start, stop - start, false);
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200139#endif
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200140 } else {
Paul Sokolovskyf8b9d3c2014-01-04 01:38:26 +0200141 // Message doesn't match CPython, but we don't have so much bytes as they
142 // to spend them on verbose wording
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000143 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "index must be int"));
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200144 }
Damiend99b0522013-12-21 18:17:45 +0000145
146 case RT_BINARY_OP_ADD:
147 case RT_BINARY_OP_INPLACE_ADD:
Damien George5fa93b62014-01-22 14:35:10 +0000148 if (MP_OBJ_IS_STR(rhs_in)) {
Damiend99b0522013-12-21 18:17:45 +0000149 // add 2 strings
Damien George5fa93b62014-01-22 14:35:10 +0000150
151 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000152 int alloc_len = lhs_len + rhs_len;
Damien George5fa93b62014-01-22 14:35:10 +0000153
154 /* code for making qstr
Damien George55baff42014-01-21 21:40:13 +0000155 byte *q_ptr;
156 byte *val = qstr_build_start(alloc_len, &q_ptr);
157 memcpy(val, lhs_data, lhs_len);
158 memcpy(val + lhs_len, rhs_data, rhs_len);
Damien George5fa93b62014-01-22 14:35:10 +0000159 return MP_OBJ_NEW_QSTR(qstr_build_end(q_ptr));
160 */
161
162 // code for non-qstr
163 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200164 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 +0000165 memcpy(data, lhs_data, lhs_len);
166 memcpy(data + lhs_len, rhs_data, rhs_len);
167 return mp_obj_str_builder_end(s);
Damiend99b0522013-12-21 18:17:45 +0000168 }
169 break;
Damien George5fa93b62014-01-22 14:35:10 +0000170
Damien George9aa2a522014-02-01 23:04:09 +0000171 case RT_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +0000172 /* NOTE `a in b` is `b.__contains__(a)` */
Damien George5fa93b62014-01-22 14:35:10 +0000173 if (MP_OBJ_IS_STR(rhs_in)) {
174 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George9aa2a522014-02-01 23:04:09 +0000175 return MP_BOOL(find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len) != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000176 }
177 break;
Damien George5fa93b62014-01-22 14:35:10 +0000178
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200179 case RT_BINARY_OP_MULTIPLY:
180 {
181 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
182 return NULL;
183 }
184 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000185 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200186 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 +0000187 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
188 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200189 }
Damiend99b0522013-12-21 18:17:45 +0000190 }
191
192 return MP_OBJ_NULL; // op not supported
193}
194
195mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000196 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000197
Damien Georgefe8fb912014-01-02 16:36:09 +0000198 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000199 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000200
201 // process args
Damiend99b0522013-12-21 18:17:45 +0000202 uint seq_len;
203 mp_obj_t *seq_items;
204 if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
205 mp_obj_tuple_get(arg, &seq_len, &seq_items);
206 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
207 mp_obj_list_get(arg, &seq_len, &seq_items);
208 } else {
209 goto bad_arg;
210 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000211
212 // count required length
213 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000214 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000215 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000216 goto bad_arg;
217 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000218 if (i > 0) {
219 required_len += sep_len;
220 }
Damien George5fa93b62014-01-22 14:35:10 +0000221 GET_STR_LEN(seq_items[i], l);
222 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000223 }
224
225 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000226 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200227 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 +0000228 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000229 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000230 memcpy(data, sep_str, sep_len);
231 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000232 }
Damien George5fa93b62014-01-22 14:35:10 +0000233 GET_STR_DATA_LEN(seq_items[i], s, l);
234 memcpy(data, s, l);
235 data += l;
Damiend99b0522013-12-21 18:17:45 +0000236 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000237
238 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000239 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000240
241bad_arg:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000242 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000243}
244
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200245#define is_ws(c) ((c) == ' ' || (c) == '\t')
246
247static mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
248 int splits = -1;
249 mp_obj_t sep = mp_const_none;
250 if (n_args > 1) {
251 sep = args[1];
252 if (n_args > 2) {
253 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
254 }
255 }
256 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000257 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200258 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000259 GET_STR_DATA_LEN(args[0], s, len);
260 const byte *top = s + len;
261 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200262
263 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000264 while (s < top && is_ws(*s)) s++;
265 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200266 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000267 while (s < top && !is_ws(*s)) s++;
268 rt_list_append(res, mp_obj_new_str(start, s - start, false));
269 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200270 break;
271 }
Damien George5fa93b62014-01-22 14:35:10 +0000272 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200273 if (splits > 0) {
274 splits--;
275 }
276 }
277
Damien George5fa93b62014-01-22 14:35:10 +0000278 if (s < top) {
279 rt_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200280 }
281
282 return res;
283}
284
Damien Georgea11ceca2014-01-19 16:02:09 +0000285static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
John R. Lentone8204912014-01-12 21:53:52 +0000286 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000287 assert(MP_OBJ_IS_STR(args[0]));
288 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000289
Damien George5fa93b62014-01-22 14:35:10 +0000290 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
291 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000292
293 size_t start = 0;
294 size_t end = haystack_len;
295 /* TODO use a non-exception-throwing mp_get_index */
296 if (n_args >= 3 && args[2] != mp_const_none) {
297 start = mp_get_index(&str_type, haystack_len, args[2]);
298 }
299 if (n_args >= 4 && args[3] != mp_const_none) {
300 end = mp_get_index(&str_type, haystack_len, args[3]);
301 }
302
Damien George5fa93b62014-01-22 14:35:10 +0000303 const byte *p = find_subbytes(haystack + start, haystack_len - start, needle, needle_len);
Damien George23005372014-01-13 19:39:01 +0000304 if (p == NULL) {
305 // not found
306 return MP_OBJ_NEW_SMALL_INT(-1);
307 } else {
308 // found
309 machine_int_t pos = p - haystack;
John R. Lentone8204912014-01-12 21:53:52 +0000310 if (pos + needle_len > end) {
311 pos = -1;
312 }
Damien George23005372014-01-13 19:39:01 +0000313 return MP_OBJ_NEW_SMALL_INT(pos);
John R. Lentone8204912014-01-12 21:53:52 +0000314 }
John R. Lentone8204912014-01-12 21:53:52 +0000315}
316
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200317// TODO: (Much) more variety in args
318static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
319 GET_STR_DATA_LEN(self_in, str, str_len);
320 GET_STR_DATA_LEN(arg, prefix, prefix_len);
321 if (prefix_len > str_len) {
322 return mp_const_false;
323 }
324 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
325}
326
Damien George5fa93b62014-01-22 14:35:10 +0000327static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
328 for (size_t i = 0; i < str_len; i++) {
329 if (str[i] == c) {
330 return true;
331 }
332 }
333 return false;
334}
335
Damien Georgea11ceca2014-01-19 16:02:09 +0000336mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800337 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000338 assert(MP_OBJ_IS_STR(args[0]));
339
340 const byte *chars_to_del;
341 uint chars_to_del_len;
342 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800343
344 if (n_args == 1) {
345 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000346 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800347 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000348 assert(MP_OBJ_IS_STR(args[1]));
349 GET_STR_DATA_LEN(args[1], s, l);
350 chars_to_del = s;
351 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800352 }
353
Damien George5fa93b62014-01-22 14:35:10 +0000354 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800355
356 size_t first_good_char_pos = 0;
357 bool first_good_char_pos_set = false;
358 size_t last_good_char_pos = 0;
359 for (size_t i = 0; i < orig_str_len; i++) {
360 if (!chr_in_str(chars_to_del, chars_to_del_len, orig_str[i])) {
361 last_good_char_pos = i;
362 if (!first_good_char_pos_set) {
363 first_good_char_pos = i;
364 first_good_char_pos_set = true;
365 }
366 }
367 }
368
369 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000370 // string is all whitespace, return ''
371 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800372 }
373
374 assert(last_good_char_pos >= first_good_char_pos);
375 //+1 to accomodate the last character
376 size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000377 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800378}
379
Damien Georgea11ceca2014-01-19 16:02:09 +0000380mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000381 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000382
Damien George5fa93b62014-01-22 14:35:10 +0000383 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000384 int arg_i = 1;
385 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000386 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000387 if (*str == '{') {
388 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000389 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000390 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200391 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000392 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000393 if (arg_i >= n_args) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000394 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000395 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200396 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000397 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000398 arg_i++;
399 }
400 } else {
401 vstr_add_char(vstr, *str);
402 }
403 }
404
Damien George5fa93b62014-01-22 14:35:10 +0000405 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
406 vstr_free(vstr);
407 return s;
Damiend99b0522013-12-21 18:17:45 +0000408}
409
xbe480c15a2014-01-30 22:17:30 -0800410mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
411 assert(MP_OBJ_IS_STR(args[0]));
412 assert(MP_OBJ_IS_STR(args[1]));
413 assert(MP_OBJ_IS_STR(args[2]));
414
Damien George94f68302014-01-31 23:45:12 +0000415 machine_int_t max_rep = 0;
xbe480c15a2014-01-30 22:17:30 -0800416 if (n_args == 4) {
417 assert(MP_OBJ_IS_SMALL_INT(args[3]));
418 max_rep = MP_OBJ_SMALL_INT_VALUE(args[3]);
419 if (max_rep == 0) {
Damien George94f68302014-01-31 23:45:12 +0000420 return args[0];
421 } else if (max_rep < 0) {
xbe480c15a2014-01-30 22:17:30 -0800422 max_rep = 0;
423 }
424 }
Damien George94f68302014-01-31 23:45:12 +0000425
426 // if max_rep is still 0 by this point we will need to do all possible replacements
xbe480c15a2014-01-30 22:17:30 -0800427
428 GET_STR_DATA_LEN(args[0], str, str_len);
429 GET_STR_DATA_LEN(args[1], old, old_len);
430 GET_STR_DATA_LEN(args[2], new, new_len);
Damien George94f68302014-01-31 23:45:12 +0000431
432 // old won't exist in str if it's longer, so nothing to replace
xbe480c15a2014-01-30 22:17:30 -0800433 if (old_len > str_len) {
Damien George94f68302014-01-31 23:45:12 +0000434 return args[0];
xbe480c15a2014-01-30 22:17:30 -0800435 }
436
Damien George94f68302014-01-31 23:45:12 +0000437 // data for the replaced string
438 byte *data = NULL;
439 mp_obj_t replaced_str = MP_OBJ_NULL;
xbe480c15a2014-01-30 22:17:30 -0800440
Damien George94f68302014-01-31 23:45:12 +0000441 // do 2 passes over the string:
442 // first pass computes the required length of the replaced string
443 // second pass does the replacements
444 for (;;) {
445 machine_uint_t replaced_str_index = 0;
446 machine_uint_t num_replacements_done = 0;
447 const byte *old_occurrence;
448 const byte *offset_ptr = str;
449 machine_uint_t offset_num = 0;
450 while ((old_occurrence = find_subbytes(offset_ptr, str_len - offset_num, old, old_len)) != NULL) {
451 // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence
452 if (data != NULL) {
453 memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr);
454 }
455 replaced_str_index += old_occurrence - offset_ptr;
456 // copy the replacement string
457 if (data != NULL) {
458 memcpy(data + replaced_str_index, new, new_len);
459 }
460 replaced_str_index += new_len;
461 offset_ptr = old_occurrence + old_len;
462 offset_num = offset_ptr - str;
xbe480c15a2014-01-30 22:17:30 -0800463
Damien George94f68302014-01-31 23:45:12 +0000464 num_replacements_done++;
465 if (max_rep != 0 && num_replacements_done == max_rep){
466 break;
467 }
468 }
469
470 // copy from just after end of last occurrence of to-be-replaced string to end of old string
471 if (data != NULL) {
472 memcpy(data + replaced_str_index, offset_ptr, str_len - offset_num);
473 }
474 replaced_str_index += str_len - offset_num;
475
476 if (data == NULL) {
477 // first pass
478 if (num_replacements_done == 0) {
479 // no substr found, return original string
480 return args[0];
481 } else {
482 // substr found, allocate new string
483 replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data);
484 }
485 } else {
486 // second pass, we are done
487 break;
488 }
xbe480c15a2014-01-30 22:17:30 -0800489 }
Damien George94f68302014-01-31 23:45:12 +0000490
xbe480c15a2014-01-30 22:17:30 -0800491 return mp_obj_str_builder_end(replaced_str);
492}
493
John R. Lentone8204912014-01-12 21:53:52 +0000494static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
Damiend99b0522013-12-21 18:17:45 +0000495static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200496static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200497static MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
xbe7b0f39f2014-01-08 14:23:45 -0800498static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
Damiend99b0522013-12-21 18:17:45 +0000499static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
xbe480c15a2014-01-30 22:17:30 -0800500static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
Damiend99b0522013-12-21 18:17:45 +0000501
ian-va5a01df2014-01-06 14:14:11 -0800502static const mp_method_t str_type_methods[] = {
John R. Lentone8204912014-01-12 21:53:52 +0000503 { "find", &str_find_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800504 { "join", &str_join_obj },
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200505 { "split", &str_split_obj },
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200506 { "startswith", &str_startswith_obj },
xbe7b0f39f2014-01-08 14:23:45 -0800507 { "strip", &str_strip_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800508 { "format", &str_format_obj },
xbe480c15a2014-01-30 22:17:30 -0800509 { "replace", &str_replace_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800510 { NULL, NULL }, // end-of-list sentinel
511};
Damien George97209d32014-01-07 15:58:30 +0000512
Damiend99b0522013-12-21 18:17:45 +0000513const mp_obj_type_t str_type = {
514 { &mp_const_type },
515 "str",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200516 .print = str_print,
517 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200518 .getiter = mp_obj_new_str_iterator,
ian-v7a16fad2014-01-06 09:52:29 -0800519 .methods = str_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000520};
521
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200522// Reuses most of methods from str
523const mp_obj_type_t bytes_type = {
524 { &mp_const_type },
525 "bytes",
526 .print = str_print,
527 .binary_op = str_binary_op,
528 .getiter = mp_obj_new_bytes_iterator,
529 .methods = str_type_methods,
530};
531
532mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Damien George5fa93b62014-01-22 14:35:10 +0000533 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200534 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000535 o->len = len;
536 *data = o->data;
Damiend99b0522013-12-21 18:17:45 +0000537 return o;
538}
539
Damien George5fa93b62014-01-22 14:35:10 +0000540mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
541 assert(MP_OBJ_IS_STR(o_in));
542 mp_obj_str_t *o = o_in;
543 o->hash = qstr_compute_hash(o->data, o->len);
544 o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
545 return o;
546}
547
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200548static mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
549 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
550 o->base.type = type;
551 o->hash = qstr_compute_hash(data, len);
552 o->len = len;
553 memcpy(o->data, data, len * sizeof(byte));
554 o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
555 return o;
556}
557
Damien George5fa93b62014-01-22 14:35:10 +0000558mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
559 qstr q = qstr_find_strn(data, len);
560 if (q != MP_QSTR_NULL) {
561 // qstr with this data already exists
562 return MP_OBJ_NEW_QSTR(q);
563 } else if (make_qstr_if_not_already) {
564 // no existing qstr, make a new one
565 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
566 } else {
567 // no existing qstr, don't make one
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200568 return str_new(&str_type, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200569 }
Damien George5fa93b62014-01-22 14:35:10 +0000570}
571
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200572mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
573 return str_new(&bytes_type, data, len);
574}
575
Damien George5fa93b62014-01-22 14:35:10 +0000576bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
577 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
578 return s1 == s2;
579 } else {
580 GET_STR_HASH(s1, h1);
581 GET_STR_HASH(s2, h2);
582 if (h1 != h2) {
583 return false;
584 }
585 GET_STR_DATA_LEN(s1, d1, l1);
586 GET_STR_DATA_LEN(s2, d2, l2);
587 if (l1 != l2) {
588 return false;
589 }
Damien George1e708fe2014-01-23 18:27:51 +0000590 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200591 }
Damien George5fa93b62014-01-22 14:35:10 +0000592}
593
Damien Georgeb829b5c2014-01-25 13:51:19 +0000594void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
595void bad_implicit_conversion(mp_obj_t self_in) {
596 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)));
597}
598
Damien George5fa93b62014-01-22 14:35:10 +0000599uint mp_obj_str_get_hash(mp_obj_t self_in) {
600 if (MP_OBJ_IS_STR(self_in)) {
601 GET_STR_HASH(self_in, h);
602 return h;
603 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000604 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000605 }
606}
607
608uint mp_obj_str_get_len(mp_obj_t self_in) {
609 if (MP_OBJ_IS_STR(self_in)) {
610 GET_STR_LEN(self_in, l);
611 return l;
612 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000613 bad_implicit_conversion(self_in);
614 }
615}
616
617// use this if you will anyway convert the string to a qstr
618// will be more efficient for the case where it's already a qstr
619qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
620 if (MP_OBJ_IS_QSTR(self_in)) {
621 return MP_OBJ_QSTR_VALUE(self_in);
622 } else if (MP_OBJ_IS_TYPE(self_in, &str_type)) {
623 mp_obj_str_t *self = self_in;
624 return qstr_from_strn((char*)self->data, self->len);
625 } else {
626 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000627 }
628}
629
630// only use this function if you need the str data to be zero terminated
631// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
632const char *mp_obj_str_get_str(mp_obj_t self_in) {
633 if (MP_OBJ_IS_STR(self_in)) {
634 GET_STR_DATA_LEN(self_in, s, l);
635 (void)l; // len unused
636 return (const char*)s;
637 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000638 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000639 }
640}
641
642const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
643 if (MP_OBJ_IS_STR(self_in)) {
644 GET_STR_DATA_LEN(self_in, s, l);
645 *len = l;
646 return s;
647 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000648 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000649 }
Damiend99b0522013-12-21 18:17:45 +0000650}
xyb8cfc9f02014-01-05 18:47:51 +0800651
652/******************************************************************************/
653/* str iterator */
654
655typedef struct _mp_obj_str_it_t {
656 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000657 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800658 machine_uint_t cur;
659} mp_obj_str_it_t;
660
661mp_obj_t str_it_iternext(mp_obj_t self_in) {
662 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000663 GET_STR_DATA_LEN(self->str, str, len);
664 if (self->cur < len) {
665 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800666 self->cur += 1;
667 return o_out;
668 } else {
669 return mp_const_stop_iteration;
670 }
671}
672
673static const mp_obj_type_t str_it_type = {
674 { &mp_const_type },
675 "str_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200676 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800677};
678
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200679mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
680 mp_obj_str_it_t *self = self_in;
681 GET_STR_DATA_LEN(self->str, str, len);
682 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000683 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200684 self->cur += 1;
685 return o_out;
686 } else {
687 return mp_const_stop_iteration;
688 }
689}
690
691static const mp_obj_type_t bytes_it_type = {
692 { &mp_const_type },
693 "bytes_iterator",
694 .iternext = bytes_it_iternext,
695};
696
697mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800698 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
699 o->base.type = &str_it_type;
700 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200701 o->cur = 0;
702 return o;
703}
704
705mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
706 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
707 o->base.type = &bytes_it_type;
708 o->str = str;
709 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800710 return o;
711}