blob: 84ac74bab97a3bf987bc63d1eaa6977ba9c3302a [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
Damien George5fa93b62014-01-22 14:35:10 +000037void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
38 GET_STR_DATA_LEN(self_in, str_data, str_len);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020039 bool is_bytes = MP_OBJ_IS_TYPE(self_in, &bytes_type);
40 if (kind == PRINT_STR && !is_bytes) {
Damien George5fa93b62014-01-22 14:35:10 +000041 print(env, "%.*s", str_len, str_data);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020042 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +000043 // this escapes characters, but it will be very slow to print (calling print many times)
44 bool has_single_quote = false;
45 bool has_double_quote = false;
46 for (const byte *s = str_data, *top = str_data + str_len; (!has_single_quote || !has_double_quote) && s < top; s++) {
47 if (*s == '\'') {
48 has_single_quote = true;
49 } else if (*s == '"') {
50 has_double_quote = true;
51 }
52 }
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020053 if (is_bytes) {
54 print(env, "b");
55 }
Damien Georgeb829b5c2014-01-25 13:51:19 +000056 int quote_char = '\'';
57 if (has_single_quote && !has_double_quote) {
58 quote_char = '"';
59 }
60 print(env, "%c", quote_char);
61 for (const byte *s = str_data, *top = str_data + str_len; s < top; s++) {
62 if (*s == quote_char) {
63 print(env, "\\%c", quote_char);
64 } else if (*s == '\\') {
65 print(env, "\\\\");
66 } else if (32 <= *s && *s <= 126) {
67 print(env, "%c", *s);
68 } else if (*s == '\n') {
69 print(env, "\\n");
70 // TODO add more escape codes here if we want to match CPython
71 } else {
72 print(env, "\\x%02x", *s);
73 }
74 }
75 print(env, "%c", quote_char);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020076 }
Damiend99b0522013-12-21 18:17:45 +000077}
78
Damien George55baff42014-01-21 21:40:13 +000079// like strstr but with specified length and allows \0 bytes
80// TODO replace with something more efficient/standard
81static const byte *find_subbytes(const byte *haystack, uint hlen, const byte *needle, uint nlen) {
82 if (hlen >= nlen) {
83 for (uint i = 0; i <= hlen - nlen; i++) {
84 bool found = true;
85 for (uint j = 0; j < nlen; j++) {
86 if (haystack[i + j] != needle[j]) {
87 found = false;
88 break;
89 }
90 }
91 if (found) {
92 return haystack + i;
93 }
94 }
95 }
96 return NULL;
97}
98
Damiend99b0522013-12-21 18:17:45 +000099mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000100 GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
Damiend99b0522013-12-21 18:17:45 +0000101 switch (op) {
102 case RT_BINARY_OP_SUBSCR:
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200103 // TODO: need predicate to check for int-like type (bools are such for example)
104 // ["no", "yes"][1 == 2] is common idiom
105 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George5fa93b62014-01-22 14:35:10 +0000106 uint index = mp_get_index(mp_obj_get_type(lhs_in), lhs_len, rhs_in);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200107 if (MP_OBJ_IS_TYPE(lhs_in, &bytes_type)) {
Damien George7c9c6672014-01-25 00:17:36 +0000108 return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200109 } else {
110 return mp_obj_new_str(lhs_data + index, 1, true);
111 }
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200112#if MICROPY_ENABLE_SLICE
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200113 } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
Damien Georgec8d13842014-01-04 01:06:10 +0000114 machine_int_t start, stop, step;
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200115 mp_obj_slice_get(rhs_in, &start, &stop, &step);
116 assert(step == 1);
Paul Sokolovskydecad082014-01-03 23:36:56 +0200117 if (start < 0) {
Damien George55baff42014-01-21 21:40:13 +0000118 start = lhs_len + start;
Paul Sokolovsky6ee1e382014-01-04 03:47:34 +0200119 if (start < 0) {
120 start = 0;
121 }
Damien George55baff42014-01-21 21:40:13 +0000122 } else if (start > lhs_len) {
123 start = lhs_len;
Paul Sokolovskydecad082014-01-03 23:36:56 +0200124 }
125 if (stop <= 0) {
Damien George55baff42014-01-21 21:40:13 +0000126 stop = lhs_len + stop;
Paul Sokolovsky6ee1e382014-01-04 03:47:34 +0200127 // CPython returns empty string in such case
128 if (stop < 0) {
129 stop = start;
130 }
Damien George55baff42014-01-21 21:40:13 +0000131 } else if (stop > lhs_len) {
132 stop = lhs_len;
Paul Sokolovskydecad082014-01-03 23:36:56 +0200133 }
Damien George5fa93b62014-01-22 14:35:10 +0000134 return mp_obj_new_str(lhs_data + start, stop - start, false);
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200135#endif
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200136 } else {
Paul Sokolovskyf8b9d3c2014-01-04 01:38:26 +0200137 // Message doesn't match CPython, but we don't have so much bytes as they
138 // to spend them on verbose wording
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000139 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "index must be int"));
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200140 }
Damiend99b0522013-12-21 18:17:45 +0000141
142 case RT_BINARY_OP_ADD:
143 case RT_BINARY_OP_INPLACE_ADD:
Damien George5fa93b62014-01-22 14:35:10 +0000144 if (MP_OBJ_IS_STR(rhs_in)) {
Damiend99b0522013-12-21 18:17:45 +0000145 // add 2 strings
Damien George5fa93b62014-01-22 14:35:10 +0000146
147 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000148 int alloc_len = lhs_len + rhs_len;
Damien George5fa93b62014-01-22 14:35:10 +0000149
150 /* code for making qstr
Damien George55baff42014-01-21 21:40:13 +0000151 byte *q_ptr;
152 byte *val = qstr_build_start(alloc_len, &q_ptr);
153 memcpy(val, lhs_data, lhs_len);
154 memcpy(val + lhs_len, rhs_data, rhs_len);
Damien George5fa93b62014-01-22 14:35:10 +0000155 return MP_OBJ_NEW_QSTR(qstr_build_end(q_ptr));
156 */
157
158 // code for non-qstr
159 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200160 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 +0000161 memcpy(data, lhs_data, lhs_len);
162 memcpy(data + lhs_len, rhs_data, rhs_len);
163 return mp_obj_str_builder_end(s);
Damiend99b0522013-12-21 18:17:45 +0000164 }
165 break;
Damien George5fa93b62014-01-22 14:35:10 +0000166
John R. Lentonc1bef212014-01-11 12:39:33 +0000167 case RT_COMPARE_OP_IN:
168 case RT_COMPARE_OP_NOT_IN:
169 /* NOTE `a in b` is `b.__contains__(a)` */
Damien George5fa93b62014-01-22 14:35:10 +0000170 if (MP_OBJ_IS_STR(rhs_in)) {
171 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000172 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 +0000173 }
174 break;
Damien George5fa93b62014-01-22 14:35:10 +0000175
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200176 case RT_BINARY_OP_MULTIPLY:
177 {
178 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
179 return NULL;
180 }
181 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000182 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200183 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 +0000184 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
185 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200186 }
Damiend99b0522013-12-21 18:17:45 +0000187 }
188
189 return MP_OBJ_NULL; // op not supported
190}
191
192mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000193 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000194
Damien Georgefe8fb912014-01-02 16:36:09 +0000195 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000196 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000197
198 // process args
Damiend99b0522013-12-21 18:17:45 +0000199 uint seq_len;
200 mp_obj_t *seq_items;
201 if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
202 mp_obj_tuple_get(arg, &seq_len, &seq_items);
203 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
204 mp_obj_list_get(arg, &seq_len, &seq_items);
205 } else {
206 goto bad_arg;
207 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000208
209 // count required length
210 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000211 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000212 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000213 goto bad_arg;
214 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000215 if (i > 0) {
216 required_len += sep_len;
217 }
Damien George5fa93b62014-01-22 14:35:10 +0000218 GET_STR_LEN(seq_items[i], l);
219 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000220 }
221
222 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000223 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200224 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 +0000225 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000226 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000227 memcpy(data, sep_str, sep_len);
228 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000229 }
Damien George5fa93b62014-01-22 14:35:10 +0000230 GET_STR_DATA_LEN(seq_items[i], s, l);
231 memcpy(data, s, l);
232 data += l;
Damiend99b0522013-12-21 18:17:45 +0000233 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000234
235 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000236 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000237
238bad_arg:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000239 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000240}
241
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200242#define is_ws(c) ((c) == ' ' || (c) == '\t')
243
244static mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
245 int splits = -1;
246 mp_obj_t sep = mp_const_none;
247 if (n_args > 1) {
248 sep = args[1];
249 if (n_args > 2) {
250 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
251 }
252 }
253 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000254 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200255 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000256 GET_STR_DATA_LEN(args[0], s, len);
257 const byte *top = s + len;
258 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200259
260 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000261 while (s < top && is_ws(*s)) s++;
262 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200263 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000264 while (s < top && !is_ws(*s)) s++;
265 rt_list_append(res, mp_obj_new_str(start, s - start, false));
266 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200267 break;
268 }
Damien George5fa93b62014-01-22 14:35:10 +0000269 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200270 if (splits > 0) {
271 splits--;
272 }
273 }
274
Damien George5fa93b62014-01-22 14:35:10 +0000275 if (s < top) {
276 rt_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200277 }
278
279 return res;
280}
281
Damien Georgea11ceca2014-01-19 16:02:09 +0000282static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
John R. Lentone8204912014-01-12 21:53:52 +0000283 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000284 assert(MP_OBJ_IS_STR(args[0]));
285 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000286
Damien George5fa93b62014-01-22 14:35:10 +0000287 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
288 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000289
290 size_t start = 0;
291 size_t end = haystack_len;
292 /* TODO use a non-exception-throwing mp_get_index */
293 if (n_args >= 3 && args[2] != mp_const_none) {
294 start = mp_get_index(&str_type, haystack_len, args[2]);
295 }
296 if (n_args >= 4 && args[3] != mp_const_none) {
297 end = mp_get_index(&str_type, haystack_len, args[3]);
298 }
299
Damien George5fa93b62014-01-22 14:35:10 +0000300 const byte *p = find_subbytes(haystack + start, haystack_len - start, needle, needle_len);
Damien George23005372014-01-13 19:39:01 +0000301 if (p == NULL) {
302 // not found
303 return MP_OBJ_NEW_SMALL_INT(-1);
304 } else {
305 // found
306 machine_int_t pos = p - haystack;
John R. Lentone8204912014-01-12 21:53:52 +0000307 if (pos + needle_len > end) {
308 pos = -1;
309 }
Damien George23005372014-01-13 19:39:01 +0000310 return MP_OBJ_NEW_SMALL_INT(pos);
John R. Lentone8204912014-01-12 21:53:52 +0000311 }
John R. Lentone8204912014-01-12 21:53:52 +0000312}
313
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200314// TODO: (Much) more variety in args
315static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
316 GET_STR_DATA_LEN(self_in, str, str_len);
317 GET_STR_DATA_LEN(arg, prefix, prefix_len);
318 if (prefix_len > str_len) {
319 return mp_const_false;
320 }
321 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
322}
323
Damien George5fa93b62014-01-22 14:35:10 +0000324static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
325 for (size_t i = 0; i < str_len; i++) {
326 if (str[i] == c) {
327 return true;
328 }
329 }
330 return false;
331}
332
Damien Georgea11ceca2014-01-19 16:02:09 +0000333mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800334 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000335 assert(MP_OBJ_IS_STR(args[0]));
336
337 const byte *chars_to_del;
338 uint chars_to_del_len;
339 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800340
341 if (n_args == 1) {
342 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000343 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800344 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000345 assert(MP_OBJ_IS_STR(args[1]));
346 GET_STR_DATA_LEN(args[1], s, l);
347 chars_to_del = s;
348 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800349 }
350
Damien George5fa93b62014-01-22 14:35:10 +0000351 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800352
353 size_t first_good_char_pos = 0;
354 bool first_good_char_pos_set = false;
355 size_t last_good_char_pos = 0;
356 for (size_t i = 0; i < orig_str_len; i++) {
357 if (!chr_in_str(chars_to_del, chars_to_del_len, orig_str[i])) {
358 last_good_char_pos = i;
359 if (!first_good_char_pos_set) {
360 first_good_char_pos = i;
361 first_good_char_pos_set = true;
362 }
363 }
364 }
365
366 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000367 // string is all whitespace, return ''
368 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800369 }
370
371 assert(last_good_char_pos >= first_good_char_pos);
372 //+1 to accomodate the last character
373 size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000374 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800375}
376
Damien Georgea11ceca2014-01-19 16:02:09 +0000377mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000378 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000379
Damien George5fa93b62014-01-22 14:35:10 +0000380 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000381 int arg_i = 1;
382 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000383 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000384 if (*str == '{') {
385 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000386 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000387 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200388 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000389 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000390 if (arg_i >= n_args) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000391 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000392 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200393 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000394 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000395 arg_i++;
396 }
397 } else {
398 vstr_add_char(vstr, *str);
399 }
400 }
401
Damien George5fa93b62014-01-22 14:35:10 +0000402 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
403 vstr_free(vstr);
404 return s;
Damiend99b0522013-12-21 18:17:45 +0000405}
406
John R. Lentone8204912014-01-12 21:53:52 +0000407static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
Damiend99b0522013-12-21 18:17:45 +0000408static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200409static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200410static MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
xbe7b0f39f2014-01-08 14:23:45 -0800411static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
Damiend99b0522013-12-21 18:17:45 +0000412static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
413
ian-va5a01df2014-01-06 14:14:11 -0800414static const mp_method_t str_type_methods[] = {
John R. Lentone8204912014-01-12 21:53:52 +0000415 { "find", &str_find_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800416 { "join", &str_join_obj },
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200417 { "split", &str_split_obj },
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200418 { "startswith", &str_startswith_obj },
xbe7b0f39f2014-01-08 14:23:45 -0800419 { "strip", &str_strip_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800420 { "format", &str_format_obj },
421 { NULL, NULL }, // end-of-list sentinel
422};
Damien George97209d32014-01-07 15:58:30 +0000423
Damiend99b0522013-12-21 18:17:45 +0000424const mp_obj_type_t str_type = {
425 { &mp_const_type },
426 "str",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200427 .print = str_print,
428 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200429 .getiter = mp_obj_new_str_iterator,
ian-v7a16fad2014-01-06 09:52:29 -0800430 .methods = str_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000431};
432
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200433// Reuses most of methods from str
434const mp_obj_type_t bytes_type = {
435 { &mp_const_type },
436 "bytes",
437 .print = str_print,
438 .binary_op = str_binary_op,
439 .getiter = mp_obj_new_bytes_iterator,
440 .methods = str_type_methods,
441};
442
443mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Damien George5fa93b62014-01-22 14:35:10 +0000444 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200445 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000446 o->len = len;
447 *data = o->data;
Damiend99b0522013-12-21 18:17:45 +0000448 return o;
449}
450
Damien George5fa93b62014-01-22 14:35:10 +0000451mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
452 assert(MP_OBJ_IS_STR(o_in));
453 mp_obj_str_t *o = o_in;
454 o->hash = qstr_compute_hash(o->data, o->len);
455 o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
456 return o;
457}
458
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200459static mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
460 mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
461 o->base.type = type;
462 o->hash = qstr_compute_hash(data, len);
463 o->len = len;
464 memcpy(o->data, data, len * sizeof(byte));
465 o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
466 return o;
467}
468
Damien George5fa93b62014-01-22 14:35:10 +0000469mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
470 qstr q = qstr_find_strn(data, len);
471 if (q != MP_QSTR_NULL) {
472 // qstr with this data already exists
473 return MP_OBJ_NEW_QSTR(q);
474 } else if (make_qstr_if_not_already) {
475 // no existing qstr, make a new one
476 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
477 } else {
478 // no existing qstr, don't make one
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200479 return str_new(&str_type, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200480 }
Damien George5fa93b62014-01-22 14:35:10 +0000481}
482
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200483mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
484 return str_new(&bytes_type, data, len);
485}
486
Damien George5fa93b62014-01-22 14:35:10 +0000487bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
488 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
489 return s1 == s2;
490 } else {
491 GET_STR_HASH(s1, h1);
492 GET_STR_HASH(s2, h2);
493 if (h1 != h2) {
494 return false;
495 }
496 GET_STR_DATA_LEN(s1, d1, l1);
497 GET_STR_DATA_LEN(s2, d2, l2);
498 if (l1 != l2) {
499 return false;
500 }
Damien George1e708fe2014-01-23 18:27:51 +0000501 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200502 }
Damien George5fa93b62014-01-22 14:35:10 +0000503}
504
Damien Georgeb829b5c2014-01-25 13:51:19 +0000505void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
506void bad_implicit_conversion(mp_obj_t self_in) {
507 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)));
508}
509
Damien George5fa93b62014-01-22 14:35:10 +0000510uint mp_obj_str_get_hash(mp_obj_t self_in) {
511 if (MP_OBJ_IS_STR(self_in)) {
512 GET_STR_HASH(self_in, h);
513 return h;
514 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000515 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000516 }
517}
518
519uint mp_obj_str_get_len(mp_obj_t self_in) {
520 if (MP_OBJ_IS_STR(self_in)) {
521 GET_STR_LEN(self_in, l);
522 return l;
523 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000524 bad_implicit_conversion(self_in);
525 }
526}
527
528// use this if you will anyway convert the string to a qstr
529// will be more efficient for the case where it's already a qstr
530qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
531 if (MP_OBJ_IS_QSTR(self_in)) {
532 return MP_OBJ_QSTR_VALUE(self_in);
533 } else if (MP_OBJ_IS_TYPE(self_in, &str_type)) {
534 mp_obj_str_t *self = self_in;
535 return qstr_from_strn((char*)self->data, self->len);
536 } else {
537 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000538 }
539}
540
541// only use this function if you need the str data to be zero terminated
542// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
543const char *mp_obj_str_get_str(mp_obj_t self_in) {
544 if (MP_OBJ_IS_STR(self_in)) {
545 GET_STR_DATA_LEN(self_in, s, l);
546 (void)l; // len unused
547 return (const char*)s;
548 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000549 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000550 }
551}
552
553const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
554 if (MP_OBJ_IS_STR(self_in)) {
555 GET_STR_DATA_LEN(self_in, s, l);
556 *len = l;
557 return s;
558 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000559 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000560 }
Damiend99b0522013-12-21 18:17:45 +0000561}
xyb8cfc9f02014-01-05 18:47:51 +0800562
563/******************************************************************************/
564/* str iterator */
565
566typedef struct _mp_obj_str_it_t {
567 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000568 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800569 machine_uint_t cur;
570} mp_obj_str_it_t;
571
572mp_obj_t str_it_iternext(mp_obj_t self_in) {
573 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000574 GET_STR_DATA_LEN(self->str, str, len);
575 if (self->cur < len) {
576 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800577 self->cur += 1;
578 return o_out;
579 } else {
580 return mp_const_stop_iteration;
581 }
582}
583
584static const mp_obj_type_t str_it_type = {
585 { &mp_const_type },
586 "str_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200587 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800588};
589
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200590mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
591 mp_obj_str_it_t *self = self_in;
592 GET_STR_DATA_LEN(self->str, str, len);
593 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000594 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200595 self->cur += 1;
596 return o_out;
597 } else {
598 return mp_const_stop_iteration;
599 }
600}
601
602static const mp_obj_type_t bytes_it_type = {
603 { &mp_const_type },
604 "bytes_iterator",
605 .iternext = bytes_it_iternext,
606};
607
608mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800609 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
610 o->base.type = &str_it_type;
611 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200612 o->cur = 0;
613 return o;
614}
615
616mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
617 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
618 o->base.type = &bytes_it_type;
619 o->str = str;
620 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800621 return o;
622}