blob: c30da00d71a552d18eeacf0c4beb2430f2fbddb3 [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
John R. Lentonc1bef212014-01-11 12:39:33 +0000171 case RT_COMPARE_OP_IN:
172 case RT_COMPARE_OP_NOT_IN:
173 /* NOTE `a in b` is `b.__contains__(a)` */
Damien George5fa93b62014-01-22 14:35:10 +0000174 if (MP_OBJ_IS_STR(rhs_in)) {
175 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000176 return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len) == NULL));
John R. Lentonc1bef212014-01-11 12:39:33 +0000177 }
178 break;
Damien George5fa93b62014-01-22 14:35:10 +0000179
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200180 case RT_BINARY_OP_MULTIPLY:
181 {
182 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
183 return NULL;
184 }
185 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000186 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200187 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 +0000188 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
189 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200190 }
Damiend99b0522013-12-21 18:17:45 +0000191 }
192
193 return MP_OBJ_NULL; // op not supported
194}
195
196mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000197 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000198
Damien Georgefe8fb912014-01-02 16:36:09 +0000199 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000200 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000201
202 // process args
Damiend99b0522013-12-21 18:17:45 +0000203 uint seq_len;
204 mp_obj_t *seq_items;
205 if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
206 mp_obj_tuple_get(arg, &seq_len, &seq_items);
207 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
208 mp_obj_list_get(arg, &seq_len, &seq_items);
209 } else {
210 goto bad_arg;
211 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000212
213 // count required length
214 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000215 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000216 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000217 goto bad_arg;
218 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000219 if (i > 0) {
220 required_len += sep_len;
221 }
Damien George5fa93b62014-01-22 14:35:10 +0000222 GET_STR_LEN(seq_items[i], l);
223 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000224 }
225
226 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000227 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200228 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 +0000229 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000230 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000231 memcpy(data, sep_str, sep_len);
232 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000233 }
Damien George5fa93b62014-01-22 14:35:10 +0000234 GET_STR_DATA_LEN(seq_items[i], s, l);
235 memcpy(data, s, l);
236 data += l;
Damiend99b0522013-12-21 18:17:45 +0000237 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000238
239 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000240 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000241
242bad_arg:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000243 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000244}
245
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200246#define is_ws(c) ((c) == ' ' || (c) == '\t')
247
248static mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
249 int splits = -1;
250 mp_obj_t sep = mp_const_none;
251 if (n_args > 1) {
252 sep = args[1];
253 if (n_args > 2) {
254 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
255 }
256 }
257 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000258 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200259 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000260 GET_STR_DATA_LEN(args[0], s, len);
261 const byte *top = s + len;
262 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200263
264 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000265 while (s < top && is_ws(*s)) s++;
266 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200267 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000268 while (s < top && !is_ws(*s)) s++;
269 rt_list_append(res, mp_obj_new_str(start, s - start, false));
270 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200271 break;
272 }
Damien George5fa93b62014-01-22 14:35:10 +0000273 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200274 if (splits > 0) {
275 splits--;
276 }
277 }
278
Damien George5fa93b62014-01-22 14:35:10 +0000279 if (s < top) {
280 rt_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200281 }
282
283 return res;
284}
285
Damien Georgea11ceca2014-01-19 16:02:09 +0000286static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
John R. Lentone8204912014-01-12 21:53:52 +0000287 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000288 assert(MP_OBJ_IS_STR(args[0]));
289 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000290
Damien George5fa93b62014-01-22 14:35:10 +0000291 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
292 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000293
294 size_t start = 0;
295 size_t end = haystack_len;
296 /* TODO use a non-exception-throwing mp_get_index */
297 if (n_args >= 3 && args[2] != mp_const_none) {
298 start = mp_get_index(&str_type, haystack_len, args[2]);
299 }
300 if (n_args >= 4 && args[3] != mp_const_none) {
301 end = mp_get_index(&str_type, haystack_len, args[3]);
302 }
303
Damien George5fa93b62014-01-22 14:35:10 +0000304 const byte *p = find_subbytes(haystack + start, haystack_len - start, needle, needle_len);
Damien George23005372014-01-13 19:39:01 +0000305 if (p == NULL) {
306 // not found
307 return MP_OBJ_NEW_SMALL_INT(-1);
308 } else {
309 // found
310 machine_int_t pos = p - haystack;
John R. Lentone8204912014-01-12 21:53:52 +0000311 if (pos + needle_len > end) {
312 pos = -1;
313 }
Damien George23005372014-01-13 19:39:01 +0000314 return MP_OBJ_NEW_SMALL_INT(pos);
John R. Lentone8204912014-01-12 21:53:52 +0000315 }
John R. Lentone8204912014-01-12 21:53:52 +0000316}
317
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200318// TODO: (Much) more variety in args
319static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
320 GET_STR_DATA_LEN(self_in, str, str_len);
321 GET_STR_DATA_LEN(arg, prefix, prefix_len);
322 if (prefix_len > str_len) {
323 return mp_const_false;
324 }
325 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
326}
327
Damien George5fa93b62014-01-22 14:35:10 +0000328static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
329 for (size_t i = 0; i < str_len; i++) {
330 if (str[i] == c) {
331 return true;
332 }
333 }
334 return false;
335}
336
Damien Georgea11ceca2014-01-19 16:02:09 +0000337mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800338 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000339 assert(MP_OBJ_IS_STR(args[0]));
340
341 const byte *chars_to_del;
342 uint chars_to_del_len;
343 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800344
345 if (n_args == 1) {
346 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000347 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800348 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000349 assert(MP_OBJ_IS_STR(args[1]));
350 GET_STR_DATA_LEN(args[1], s, l);
351 chars_to_del = s;
352 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800353 }
354
Damien George5fa93b62014-01-22 14:35:10 +0000355 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800356
357 size_t first_good_char_pos = 0;
358 bool first_good_char_pos_set = false;
359 size_t last_good_char_pos = 0;
360 for (size_t i = 0; i < orig_str_len; i++) {
361 if (!chr_in_str(chars_to_del, chars_to_del_len, orig_str[i])) {
362 last_good_char_pos = i;
363 if (!first_good_char_pos_set) {
364 first_good_char_pos = i;
365 first_good_char_pos_set = true;
366 }
367 }
368 }
369
370 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000371 // string is all whitespace, return ''
372 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800373 }
374
375 assert(last_good_char_pos >= first_good_char_pos);
376 //+1 to accomodate the last character
377 size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000378 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800379}
380
Damien Georgea11ceca2014-01-19 16:02:09 +0000381mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000382 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000383
Damien George5fa93b62014-01-22 14:35:10 +0000384 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000385 int arg_i = 1;
386 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000387 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000388 if (*str == '{') {
389 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000390 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000391 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200392 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000393 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000394 if (arg_i >= n_args) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000395 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000396 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200397 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000398 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000399 arg_i++;
400 }
401 } else {
402 vstr_add_char(vstr, *str);
403 }
404 }
405
Damien George5fa93b62014-01-22 14:35:10 +0000406 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
407 vstr_free(vstr);
408 return s;
Damiend99b0522013-12-21 18:17:45 +0000409}
410
John R. Lentone8204912014-01-12 21:53:52 +0000411static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
Damiend99b0522013-12-21 18:17:45 +0000412static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200413static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200414static MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
xbe7b0f39f2014-01-08 14:23:45 -0800415static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
Damiend99b0522013-12-21 18:17:45 +0000416static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
417
ian-va5a01df2014-01-06 14:14:11 -0800418static const mp_method_t str_type_methods[] = {
John R. Lentone8204912014-01-12 21:53:52 +0000419 { "find", &str_find_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800420 { "join", &str_join_obj },
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200421 { "split", &str_split_obj },
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200422 { "startswith", &str_startswith_obj },
xbe7b0f39f2014-01-08 14:23:45 -0800423 { "strip", &str_strip_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800424 { "format", &str_format_obj },
425 { NULL, NULL }, // end-of-list sentinel
426};
Damien George97209d32014-01-07 15:58:30 +0000427
Damiend99b0522013-12-21 18:17:45 +0000428const mp_obj_type_t str_type = {
429 { &mp_const_type },
430 "str",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200431 .print = str_print,
432 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200433 .getiter = mp_obj_new_str_iterator,
ian-v7a16fad2014-01-06 09:52:29 -0800434 .methods = str_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000435};
436
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200437// Reuses most of methods from str
438const mp_obj_type_t bytes_type = {
439 { &mp_const_type },
440 "bytes",
441 .print = str_print,
442 .binary_op = str_binary_op,
443 .getiter = mp_obj_new_bytes_iterator,
444 .methods = str_type_methods,
445};
446
447mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Damien George5fa93b62014-01-22 14:35:10 +0000448 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200449 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000450 o->len = len;
451 *data = o->data;
Damiend99b0522013-12-21 18:17:45 +0000452 return o;
453}
454
Damien George5fa93b62014-01-22 14:35:10 +0000455mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
456 assert(MP_OBJ_IS_STR(o_in));
457 mp_obj_str_t *o = o_in;
458 o->hash = qstr_compute_hash(o->data, o->len);
459 o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
460 return o;
461}
462
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200463static mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
464 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
465 o->base.type = type;
466 o->hash = qstr_compute_hash(data, len);
467 o->len = len;
468 memcpy(o->data, data, len * sizeof(byte));
469 o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
470 return o;
471}
472
Damien George5fa93b62014-01-22 14:35:10 +0000473mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
474 qstr q = qstr_find_strn(data, len);
475 if (q != MP_QSTR_NULL) {
476 // qstr with this data already exists
477 return MP_OBJ_NEW_QSTR(q);
478 } else if (make_qstr_if_not_already) {
479 // no existing qstr, make a new one
480 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
481 } else {
482 // no existing qstr, don't make one
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200483 return str_new(&str_type, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200484 }
Damien George5fa93b62014-01-22 14:35:10 +0000485}
486
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200487mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
488 return str_new(&bytes_type, data, len);
489}
490
Damien George5fa93b62014-01-22 14:35:10 +0000491bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
492 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
493 return s1 == s2;
494 } else {
495 GET_STR_HASH(s1, h1);
496 GET_STR_HASH(s2, h2);
497 if (h1 != h2) {
498 return false;
499 }
500 GET_STR_DATA_LEN(s1, d1, l1);
501 GET_STR_DATA_LEN(s2, d2, l2);
502 if (l1 != l2) {
503 return false;
504 }
Damien George1e708fe2014-01-23 18:27:51 +0000505 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200506 }
Damien George5fa93b62014-01-22 14:35:10 +0000507}
508
Damien Georgeb829b5c2014-01-25 13:51:19 +0000509void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
510void bad_implicit_conversion(mp_obj_t self_in) {
511 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)));
512}
513
Damien George5fa93b62014-01-22 14:35:10 +0000514uint mp_obj_str_get_hash(mp_obj_t self_in) {
515 if (MP_OBJ_IS_STR(self_in)) {
516 GET_STR_HASH(self_in, h);
517 return h;
518 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000519 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000520 }
521}
522
523uint mp_obj_str_get_len(mp_obj_t self_in) {
524 if (MP_OBJ_IS_STR(self_in)) {
525 GET_STR_LEN(self_in, l);
526 return l;
527 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000528 bad_implicit_conversion(self_in);
529 }
530}
531
532// use this if you will anyway convert the string to a qstr
533// will be more efficient for the case where it's already a qstr
534qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
535 if (MP_OBJ_IS_QSTR(self_in)) {
536 return MP_OBJ_QSTR_VALUE(self_in);
537 } else if (MP_OBJ_IS_TYPE(self_in, &str_type)) {
538 mp_obj_str_t *self = self_in;
539 return qstr_from_strn((char*)self->data, self->len);
540 } else {
541 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000542 }
543}
544
545// only use this function if you need the str data to be zero terminated
546// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
547const char *mp_obj_str_get_str(mp_obj_t self_in) {
548 if (MP_OBJ_IS_STR(self_in)) {
549 GET_STR_DATA_LEN(self_in, s, l);
550 (void)l; // len unused
551 return (const char*)s;
552 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000553 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000554 }
555}
556
557const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
558 if (MP_OBJ_IS_STR(self_in)) {
559 GET_STR_DATA_LEN(self_in, s, l);
560 *len = l;
561 return s;
562 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000563 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000564 }
Damiend99b0522013-12-21 18:17:45 +0000565}
xyb8cfc9f02014-01-05 18:47:51 +0800566
567/******************************************************************************/
568/* str iterator */
569
570typedef struct _mp_obj_str_it_t {
571 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000572 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800573 machine_uint_t cur;
574} mp_obj_str_it_t;
575
576mp_obj_t str_it_iternext(mp_obj_t self_in) {
577 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000578 GET_STR_DATA_LEN(self->str, str, len);
579 if (self->cur < len) {
580 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800581 self->cur += 1;
582 return o_out;
583 } else {
584 return mp_const_stop_iteration;
585 }
586}
587
588static const mp_obj_type_t str_it_type = {
589 { &mp_const_type },
590 "str_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200591 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800592};
593
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200594mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
595 mp_obj_str_it_t *self = self_in;
596 GET_STR_DATA_LEN(self->str, str, len);
597 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000598 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200599 self->cur += 1;
600 return o_out;
601 } else {
602 return mp_const_stop_iteration;
603 }
604}
605
606static const mp_obj_type_t bytes_it_type = {
607 { &mp_const_type },
608 "bytes_iterator",
609 .iternext = bytes_it_iternext,
610};
611
612mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800613 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
614 o->base.type = &str_it_type;
615 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200616 o->cur = 0;
617 return o;
618}
619
620mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
621 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
622 o->base.type = &bytes_it_type;
623 o->str = str;
624 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800625 return o;
626}