blob: cf68e95fe8a4308dba63f6cc06d3491d274a9c14 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <string.h>
3#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
Damien George9b196cd2014-03-26 21:47:19 +000010#include "map.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "runtime0.h"
12#include "runtime.h"
13
14typedef struct _mp_obj_str_t {
15 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +000016 machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
17 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
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +020018 const byte *data;
Damiend99b0522013-12-21 18:17:45 +000019} mp_obj_str_t;
20
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +020021const mp_obj_t mp_const_empty_bytes;
22
Damien George5fa93b62014-01-22 14:35:10 +000023// use this macro to extract the string hash
24#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; }
25
26// use this macro to extract the string length
27#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; }
28
29// use this macro to extract the string data and length
30#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; }
31
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020032STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
33STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
Paul Sokolovskybe020c22014-03-21 11:39:01 +020034STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
xyb8cfc9f02014-01-05 18:47:51 +080035
36/******************************************************************************/
37/* str */
38
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020039void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
40 // this escapes characters, but it will be very slow to print (calling print many times)
41 bool has_single_quote = false;
42 bool has_double_quote = false;
43 for (const byte *s = str_data, *top = str_data + str_len; (!has_single_quote || !has_double_quote) && s < top; s++) {
44 if (*s == '\'') {
45 has_single_quote = true;
46 } else if (*s == '"') {
47 has_double_quote = true;
48 }
49 }
50 int quote_char = '\'';
51 if (has_single_quote && !has_double_quote) {
52 quote_char = '"';
53 }
54 print(env, "%c", quote_char);
55 for (const byte *s = str_data, *top = str_data + str_len; s < top; s++) {
56 if (*s == quote_char) {
57 print(env, "\\%c", quote_char);
58 } else if (*s == '\\') {
59 print(env, "\\\\");
60 } else if (32 <= *s && *s <= 126) {
61 print(env, "%c", *s);
62 } else if (*s == '\n') {
63 print(env, "\\n");
64 // TODO add more escape codes here if we want to match CPython
65 } else {
66 print(env, "\\x%02x", *s);
67 }
68 }
69 print(env, "%c", quote_char);
70}
71
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020072STATIC 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 +000073 GET_STR_DATA_LEN(self_in, str_data, str_len);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020074 bool is_bytes = MP_OBJ_IS_TYPE(self_in, &bytes_type);
75 if (kind == PRINT_STR && !is_bytes) {
Damien George5fa93b62014-01-22 14:35:10 +000076 print(env, "%.*s", str_len, str_data);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020077 } else {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020078 if (is_bytes) {
79 print(env, "b");
80 }
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020081 mp_str_print_quoted(print, env, str_data, str_len);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020082 }
Damiend99b0522013-12-21 18:17:45 +000083}
84
Paul Sokolovskybe020c22014-03-21 11:39:01 +020085STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
86 switch (n_args) {
87 case 0:
88 return MP_OBJ_NEW_QSTR(MP_QSTR_);
89
90 case 1:
91 {
92 vstr_t *vstr = vstr_new();
93 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[0], PRINT_STR);
94 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
95 vstr_free(vstr);
96 return s;
97 }
98
99 case 2:
100 case 3:
101 {
102 // TODO: validate 2nd/3rd args
103 if (!MP_OBJ_IS_TYPE(args[0], &bytes_type)) {
104 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "bytes expected"));
105 }
106 GET_STR_DATA_LEN(args[0], str_data, str_len);
107 GET_STR_HASH(args[0], str_hash);
108 mp_obj_str_t *o = str_new(&str_type, NULL, str_len);
109 o->data = str_data;
110 o->hash = str_hash;
111 return o;
112 }
113
114 default:
115 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "str takes at most 3 arguments"));
116 }
117}
118
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200119STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
120 if (n_args == 0) {
121 return mp_const_empty_bytes;
122 }
123
124 if (MP_OBJ_IS_STR(args[0])) {
125 if (n_args < 2 || n_args > 3) {
126 goto wrong_args;
127 }
128 GET_STR_DATA_LEN(args[0], str_data, str_len);
129 GET_STR_HASH(args[0], str_hash);
130 mp_obj_str_t *o = str_new(&bytes_type, NULL, str_len);
131 o->data = str_data;
132 o->hash = str_hash;
133 return o;
134 }
135
136 if (n_args > 1) {
137 goto wrong_args;
138 }
139
140 if (MP_OBJ_IS_SMALL_INT(args[0])) {
141 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
142 byte *data;
143
144 mp_obj_t o = mp_obj_str_builder_start(&bytes_type, len, &data);
145 memset(data, 0, len);
146 return mp_obj_str_builder_end(o);
147 }
148
149 int len;
150 byte *data;
151 vstr_t *vstr = NULL;
152 mp_obj_t o = NULL;
153 // Try to create array of exact len if initializer len is known
154 mp_obj_t len_in = mp_obj_len_maybe(args[0]);
155 if (len_in == MP_OBJ_NULL) {
156 len = -1;
157 vstr = vstr_new();
158 } else {
159 len = MP_OBJ_SMALL_INT_VALUE(len_in);
160 o = mp_obj_str_builder_start(&bytes_type, len, &data);
161 }
162
163 mp_obj_t iterable = rt_getiter(args[0]);
164 mp_obj_t item;
Damien George66eaf842014-03-26 19:27:58 +0000165 while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200166 if (len == -1) {
167 vstr_add_char(vstr, MP_OBJ_SMALL_INT_VALUE(item));
168 } else {
169 *data++ = MP_OBJ_SMALL_INT_VALUE(item);
170 }
171 }
172
173 if (len == -1) {
174 vstr_shrink(vstr);
175 // TODO: Optimize, borrow buffer from vstr
176 len = vstr_len(vstr);
177 o = mp_obj_str_builder_start(&bytes_type, len, &data);
178 memcpy(data, vstr_str(vstr), len);
179 vstr_free(vstr);
180 }
181
182 return mp_obj_str_builder_end(o);
183
184wrong_args:
185 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
186}
187
Damien George55baff42014-01-21 21:40:13 +0000188// like strstr but with specified length and allows \0 bytes
189// TODO replace with something more efficient/standard
xbe17a5a832014-03-23 23:31:58 -0700190STATIC const byte *find_subbytes(const byte *haystack, machine_uint_t hlen, const byte *needle, machine_uint_t nlen, machine_int_t direction) {
Damien George55baff42014-01-21 21:40:13 +0000191 if (hlen >= nlen) {
xbe17a5a832014-03-23 23:31:58 -0700192 machine_uint_t str_index, str_index_end;
193 if (direction > 0) {
194 str_index = 0;
195 str_index_end = hlen - nlen;
196 } else {
197 str_index = hlen - nlen;
198 str_index_end = 0;
199 }
200 for (;;) {
201 if (memcmp(&haystack[str_index], needle, nlen) == 0) {
202 //found
203 return haystack + str_index;
Damien George55baff42014-01-21 21:40:13 +0000204 }
xbe17a5a832014-03-23 23:31:58 -0700205 if (str_index == str_index_end) {
206 //not found
207 break;
Damien George55baff42014-01-21 21:40:13 +0000208 }
xbe17a5a832014-03-23 23:31:58 -0700209 str_index += direction;
Damien George55baff42014-01-21 21:40:13 +0000210 }
211 }
212 return NULL;
213}
214
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200215STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000216 GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
Damiend99b0522013-12-21 18:17:45 +0000217 switch (op) {
218 case RT_BINARY_OP_SUBSCR:
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200219 // TODO: need predicate to check for int-like type (bools are such for example)
220 // ["no", "yes"][1 == 2] is common idiom
221 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
xbe9e1e8cd2014-03-12 22:57:16 -0700222 uint index = mp_get_index(mp_obj_get_type(lhs_in), lhs_len, rhs_in, false);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200223 if (MP_OBJ_IS_TYPE(lhs_in, &bytes_type)) {
Damien George7c9c6672014-01-25 00:17:36 +0000224 return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200225 } else {
226 return mp_obj_new_str(lhs_data + index, 1, true);
227 }
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200228#if MICROPY_ENABLE_SLICE
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200229 } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200230 machine_uint_t start, stop;
Paul Sokolovskyea2509d2014-02-02 08:57:05 +0200231 if (!m_seq_get_fast_slice_indexes(lhs_len, rhs_in, &start, &stop)) {
232 assert(0);
233 }
Damien George5fa93b62014-01-22 14:35:10 +0000234 return mp_obj_new_str(lhs_data + start, stop - start, false);
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200235#endif
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200236 } else {
Paul Sokolovskyf8b9d3c2014-01-04 01:38:26 +0200237 // Message doesn't match CPython, but we don't have so much bytes as they
238 // to spend them on verbose wording
Damien Georgec5966122014-02-15 16:10:44 +0000239 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "index must be int"));
Paul Sokolovsky31ba60f2014-01-03 02:51:16 +0200240 }
Damiend99b0522013-12-21 18:17:45 +0000241
242 case RT_BINARY_OP_ADD:
243 case RT_BINARY_OP_INPLACE_ADD:
Damien George5fa93b62014-01-22 14:35:10 +0000244 if (MP_OBJ_IS_STR(rhs_in)) {
Damiend99b0522013-12-21 18:17:45 +0000245 // add 2 strings
Damien George5fa93b62014-01-22 14:35:10 +0000246
247 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000248 int alloc_len = lhs_len + rhs_len;
Damien George5fa93b62014-01-22 14:35:10 +0000249
250 /* code for making qstr
Damien George55baff42014-01-21 21:40:13 +0000251 byte *q_ptr;
252 byte *val = qstr_build_start(alloc_len, &q_ptr);
253 memcpy(val, lhs_data, lhs_len);
254 memcpy(val + lhs_len, rhs_data, rhs_len);
Damien George5fa93b62014-01-22 14:35:10 +0000255 return MP_OBJ_NEW_QSTR(qstr_build_end(q_ptr));
256 */
257
258 // code for non-qstr
259 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200260 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 +0000261 memcpy(data, lhs_data, lhs_len);
262 memcpy(data + lhs_len, rhs_data, rhs_len);
263 return mp_obj_str_builder_end(s);
Damiend99b0522013-12-21 18:17:45 +0000264 }
265 break;
Damien George5fa93b62014-01-22 14:35:10 +0000266
Damien George9aa2a522014-02-01 23:04:09 +0000267 case RT_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +0000268 /* NOTE `a in b` is `b.__contains__(a)` */
Damien George5fa93b62014-01-22 14:35:10 +0000269 if (MP_OBJ_IS_STR(rhs_in)) {
270 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
xbe17a5a832014-03-23 23:31:58 -0700271 return MP_BOOL(find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len, 1) != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000272 }
273 break;
Damien George5fa93b62014-01-22 14:35:10 +0000274
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200275 case RT_BINARY_OP_MULTIPLY:
276 {
277 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
278 return NULL;
279 }
280 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000281 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200282 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 +0000283 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
284 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200285 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200286
287 // These 2 are never passed here, dealt with as a special case in rt_binary_op().
288 //case RT_BINARY_OP_EQUAL:
289 //case RT_BINARY_OP_NOT_EQUAL:
290 case RT_BINARY_OP_LESS:
291 case RT_BINARY_OP_LESS_EQUAL:
292 case RT_BINARY_OP_MORE:
293 case RT_BINARY_OP_MORE_EQUAL:
294 if (MP_OBJ_IS_STR(rhs_in)) {
295 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
296 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, rhs_data, rhs_len));
297 }
Damiend99b0522013-12-21 18:17:45 +0000298 }
299
300 return MP_OBJ_NULL; // op not supported
301}
302
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200303STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000304 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000305
Damien Georgefe8fb912014-01-02 16:36:09 +0000306 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000307 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000308
309 // process args
Damiend99b0522013-12-21 18:17:45 +0000310 uint seq_len;
311 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000312 if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000313 mp_obj_tuple_get(arg, &seq_len, &seq_items);
314 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
315 mp_obj_list_get(arg, &seq_len, &seq_items);
316 } else {
317 goto bad_arg;
318 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000319
320 // count required length
321 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000322 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000323 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000324 goto bad_arg;
325 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000326 if (i > 0) {
327 required_len += sep_len;
328 }
Damien George5fa93b62014-01-22 14:35:10 +0000329 GET_STR_LEN(seq_items[i], l);
330 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000331 }
332
333 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000334 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200335 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 +0000336 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000337 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000338 memcpy(data, sep_str, sep_len);
339 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000340 }
Damien George5fa93b62014-01-22 14:35:10 +0000341 GET_STR_DATA_LEN(seq_items[i], s, l);
342 memcpy(data, s, l);
343 data += l;
Damiend99b0522013-12-21 18:17:45 +0000344 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000345
346 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000347 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000348
349bad_arg:
Damien Georgec5966122014-02-15 16:10:44 +0000350 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000351}
352
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200353#define is_ws(c) ((c) == ' ' || (c) == '\t')
354
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200355STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200356 int splits = -1;
357 mp_obj_t sep = mp_const_none;
358 if (n_args > 1) {
359 sep = args[1];
360 if (n_args > 2) {
361 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
362 }
363 }
364 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000365 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200366 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000367 GET_STR_DATA_LEN(args[0], s, len);
368 const byte *top = s + len;
369 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200370
371 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000372 while (s < top && is_ws(*s)) s++;
373 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200374 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000375 while (s < top && !is_ws(*s)) s++;
376 rt_list_append(res, mp_obj_new_str(start, s - start, false));
377 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200378 break;
379 }
Damien George5fa93b62014-01-22 14:35:10 +0000380 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200381 if (splits > 0) {
382 splits--;
383 }
384 }
385
Damien George5fa93b62014-01-22 14:35:10 +0000386 if (s < top) {
387 rt_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200388 }
389
390 return res;
391}
392
xbe17a5a832014-03-23 23:31:58 -0700393STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t direction) {
John R. Lentone8204912014-01-12 21:53:52 +0000394 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000395 assert(MP_OBJ_IS_STR(args[0]));
396 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000397
Damien George5fa93b62014-01-22 14:35:10 +0000398 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
399 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000400
xbec5538882014-03-16 17:58:35 -0700401 machine_uint_t start = 0;
402 machine_uint_t end = haystack_len;
John R. Lentone8204912014-01-12 21:53:52 +0000403 if (n_args >= 3 && args[2] != mp_const_none) {
xbe9e1e8cd2014-03-12 22:57:16 -0700404 start = mp_get_index(&str_type, haystack_len, args[2], true);
John R. Lentone8204912014-01-12 21:53:52 +0000405 }
406 if (n_args >= 4 && args[3] != mp_const_none) {
xbe9e1e8cd2014-03-12 22:57:16 -0700407 end = mp_get_index(&str_type, haystack_len, args[3], true);
John R. Lentone8204912014-01-12 21:53:52 +0000408 }
409
xbe17a5a832014-03-23 23:31:58 -0700410 const byte *p = find_subbytes(haystack + start, end - start, needle, needle_len, direction);
Damien George23005372014-01-13 19:39:01 +0000411 if (p == NULL) {
412 // not found
413 return MP_OBJ_NEW_SMALL_INT(-1);
414 } else {
415 // found
xbe17a5a832014-03-23 23:31:58 -0700416 return MP_OBJ_NEW_SMALL_INT(p - haystack);
John R. Lentone8204912014-01-12 21:53:52 +0000417 }
John R. Lentone8204912014-01-12 21:53:52 +0000418}
419
xbe17a5a832014-03-23 23:31:58 -0700420STATIC mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
421 return str_finder(n_args, args, 1);
422}
423
424STATIC mp_obj_t str_rfind(uint n_args, const mp_obj_t *args) {
425 return str_finder(n_args, args, -1);
426}
427
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200428// TODO: (Much) more variety in args
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200429STATIC mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200430 GET_STR_DATA_LEN(self_in, str, str_len);
431 GET_STR_DATA_LEN(arg, prefix, prefix_len);
432 if (prefix_len > str_len) {
433 return mp_const_false;
434 }
435 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
436}
437
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200438STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800439 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000440 assert(MP_OBJ_IS_STR(args[0]));
441
442 const byte *chars_to_del;
443 uint chars_to_del_len;
444 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800445
446 if (n_args == 1) {
447 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000448 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800449 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000450 assert(MP_OBJ_IS_STR(args[1]));
451 GET_STR_DATA_LEN(args[1], s, l);
452 chars_to_del = s;
453 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800454 }
455
Damien George5fa93b62014-01-22 14:35:10 +0000456 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800457
xbec5538882014-03-16 17:58:35 -0700458 machine_uint_t first_good_char_pos = 0;
xbe7b0f39f2014-01-08 14:23:45 -0800459 bool first_good_char_pos_set = false;
xbec5538882014-03-16 17:58:35 -0700460 machine_uint_t last_good_char_pos = 0;
461 for (machine_uint_t i = 0; i < orig_str_len; i++) {
xbe17a5a832014-03-23 23:31:58 -0700462 if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
xbe7b0f39f2014-01-08 14:23:45 -0800463 last_good_char_pos = i;
464 if (!first_good_char_pos_set) {
465 first_good_char_pos = i;
466 first_good_char_pos_set = true;
467 }
468 }
469 }
470
471 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000472 // string is all whitespace, return ''
473 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800474 }
475
476 assert(last_good_char_pos >= first_good_char_pos);
477 //+1 to accomodate the last character
xbec5538882014-03-16 17:58:35 -0700478 machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000479 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800480}
481
Damien Georgea11ceca2014-01-19 16:02:09 +0000482mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000483 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000484
Damien George5fa93b62014-01-22 14:35:10 +0000485 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000486 int arg_i = 1;
487 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000488 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000489 if (*str == '{') {
490 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000491 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000492 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200493 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000494 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000495 if (arg_i >= n_args) {
Damien Georgec5966122014-02-15 16:10:44 +0000496 nlr_jump(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000497 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200498 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000499 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000500 arg_i++;
501 }
502 } else {
503 vstr_add_char(vstr, *str);
504 }
505 }
506
Damien George5fa93b62014-01-22 14:35:10 +0000507 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
508 vstr_free(vstr);
509 return s;
Damiend99b0522013-12-21 18:17:45 +0000510}
511
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200512STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
xbe480c15a2014-01-30 22:17:30 -0800513 assert(MP_OBJ_IS_STR(args[0]));
514 assert(MP_OBJ_IS_STR(args[1]));
515 assert(MP_OBJ_IS_STR(args[2]));
516
Damien George94f68302014-01-31 23:45:12 +0000517 machine_int_t max_rep = 0;
xbe480c15a2014-01-30 22:17:30 -0800518 if (n_args == 4) {
Paul Sokolovsky4e246082014-02-11 15:29:55 +0200519 assert(MP_OBJ_IS_SMALL_INT(args[3]));
520 max_rep = MP_OBJ_SMALL_INT_VALUE(args[3]);
521 if (max_rep == 0) {
522 return args[0];
523 } else if (max_rep < 0) {
524 max_rep = 0;
525 }
xbe480c15a2014-01-30 22:17:30 -0800526 }
Damien George94f68302014-01-31 23:45:12 +0000527
528 // if max_rep is still 0 by this point we will need to do all possible replacements
xbe480c15a2014-01-30 22:17:30 -0800529
530 GET_STR_DATA_LEN(args[0], str, str_len);
531 GET_STR_DATA_LEN(args[1], old, old_len);
532 GET_STR_DATA_LEN(args[2], new, new_len);
Damien George94f68302014-01-31 23:45:12 +0000533
534 // old won't exist in str if it's longer, so nothing to replace
xbe480c15a2014-01-30 22:17:30 -0800535 if (old_len > str_len) {
Paul Sokolovsky4e246082014-02-11 15:29:55 +0200536 return args[0];
xbe480c15a2014-01-30 22:17:30 -0800537 }
538
Damien George94f68302014-01-31 23:45:12 +0000539 // data for the replaced string
540 byte *data = NULL;
541 mp_obj_t replaced_str = MP_OBJ_NULL;
xbe480c15a2014-01-30 22:17:30 -0800542
Damien George94f68302014-01-31 23:45:12 +0000543 // do 2 passes over the string:
544 // first pass computes the required length of the replaced string
545 // second pass does the replacements
546 for (;;) {
547 machine_uint_t replaced_str_index = 0;
548 machine_uint_t num_replacements_done = 0;
549 const byte *old_occurrence;
550 const byte *offset_ptr = str;
551 machine_uint_t offset_num = 0;
xbe17a5a832014-03-23 23:31:58 -0700552 while ((old_occurrence = find_subbytes(offset_ptr, str_len - offset_num, old, old_len, 1)) != NULL) {
Damien George94f68302014-01-31 23:45:12 +0000553 // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence
554 if (data != NULL) {
555 memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr);
556 }
557 replaced_str_index += old_occurrence - offset_ptr;
558 // copy the replacement string
559 if (data != NULL) {
560 memcpy(data + replaced_str_index, new, new_len);
561 }
562 replaced_str_index += new_len;
563 offset_ptr = old_occurrence + old_len;
564 offset_num = offset_ptr - str;
xbe480c15a2014-01-30 22:17:30 -0800565
Damien George94f68302014-01-31 23:45:12 +0000566 num_replacements_done++;
567 if (max_rep != 0 && num_replacements_done == max_rep){
568 break;
569 }
570 }
571
572 // copy from just after end of last occurrence of to-be-replaced string to end of old string
573 if (data != NULL) {
574 memcpy(data + replaced_str_index, offset_ptr, str_len - offset_num);
575 }
576 replaced_str_index += str_len - offset_num;
577
578 if (data == NULL) {
579 // first pass
580 if (num_replacements_done == 0) {
581 // no substr found, return original string
582 return args[0];
583 } else {
584 // substr found, allocate new string
585 replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data);
586 }
587 } else {
588 // second pass, we are done
589 break;
590 }
xbe480c15a2014-01-30 22:17:30 -0800591 }
Damien George94f68302014-01-31 23:45:12 +0000592
xbe480c15a2014-01-30 22:17:30 -0800593 return mp_obj_str_builder_end(replaced_str);
594}
595
xbe9e1e8cd2014-03-12 22:57:16 -0700596STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
597 assert(2 <= n_args && n_args <= 4);
598 assert(MP_OBJ_IS_STR(args[0]));
599 assert(MP_OBJ_IS_STR(args[1]));
600
601 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
602 GET_STR_DATA_LEN(args[1], needle, needle_len);
603
Damien George536dde22014-03-13 22:07:55 +0000604 machine_uint_t start = 0;
605 machine_uint_t end = haystack_len;
xbe9e1e8cd2014-03-12 22:57:16 -0700606 if (n_args >= 3 && args[2] != mp_const_none) {
607 start = mp_get_index(&str_type, haystack_len, args[2], true);
608 }
609 if (n_args >= 4 && args[3] != mp_const_none) {
610 end = mp_get_index(&str_type, haystack_len, args[3], true);
611 }
612
Damien George536dde22014-03-13 22:07:55 +0000613 // if needle_len is zero then we count each gap between characters as an occurrence
614 if (needle_len == 0) {
615 return MP_OBJ_NEW_SMALL_INT(end - start + 1);
xbe9e1e8cd2014-03-12 22:57:16 -0700616 }
617
Damien George536dde22014-03-13 22:07:55 +0000618 // count the occurrences
619 machine_int_t num_occurrences = 0;
xbec5d70ba2014-03-13 00:29:15 -0700620 for (machine_uint_t haystack_index = start; haystack_index + needle_len <= end; haystack_index++) {
621 if (memcmp(&haystack[haystack_index], needle, needle_len) == 0) {
622 num_occurrences++;
623 haystack_index += needle_len - 1;
624 }
xbe9e1e8cd2014-03-12 22:57:16 -0700625 }
626
627 return MP_OBJ_NEW_SMALL_INT(num_occurrences);
628}
629
Damien Georgeb035db32014-03-21 20:39:40 +0000630STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t direction) {
xbe613a8e32014-03-18 00:06:29 -0700631 assert(MP_OBJ_IS_STR(self_in));
632 if (!MP_OBJ_IS_STR(arg)) {
633 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
634 "Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
635 }
Damien Georgeb035db32014-03-21 20:39:40 +0000636
xbe613a8e32014-03-18 00:06:29 -0700637 GET_STR_DATA_LEN(self_in, str, str_len);
638 GET_STR_DATA_LEN(arg, sep, sep_len);
639
640 if (sep_len == 0) {
641 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
642 }
Damien Georgeb035db32014-03-21 20:39:40 +0000643
644 mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
645
646 if (direction > 0) {
647 result[0] = self_in;
xbe0a6894c2014-03-21 01:12:26 -0700648 } else {
Damien Georgeb035db32014-03-21 20:39:40 +0000649 result[2] = self_in;
xbe0a6894c2014-03-21 01:12:26 -0700650 }
xbe613a8e32014-03-18 00:06:29 -0700651
xbe17a5a832014-03-23 23:31:58 -0700652 const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
653 if (position_ptr != NULL) {
654 machine_uint_t position = position_ptr - str;
655 result[0] = mp_obj_new_str(str, position, false);
656 result[1] = arg;
657 result[2] = mp_obj_new_str(str + position + sep_len, str_len - position - sep_len, false);
xbe613a8e32014-03-18 00:06:29 -0700658 }
Damien Georgeb035db32014-03-21 20:39:40 +0000659
xbe0a6894c2014-03-21 01:12:26 -0700660 return mp_obj_new_tuple(3, result);
xbe613a8e32014-03-18 00:06:29 -0700661}
662
Damien Georgeb035db32014-03-21 20:39:40 +0000663STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
664 return str_partitioner(self_in, arg, 1);
xbe0a6894c2014-03-21 01:12:26 -0700665}
xbe4504ea82014-03-19 00:46:14 -0700666
Damien Georgeb035db32014-03-21 20:39:40 +0000667STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
668 return str_partitioner(self_in, arg, -1);
xbe4504ea82014-03-19 00:46:14 -0700669}
670
Damien George2da98302014-03-09 19:58:18 +0000671STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
672 if (flags == BUFFER_READ) {
673 GET_STR_DATA_LEN(self_in, str_data, str_len);
674 bufinfo->buf = (void*)str_data;
675 bufinfo->len = str_len;
676 return 0;
677 } else {
678 // can't write to a string
679 bufinfo->buf = NULL;
680 bufinfo->len = 0;
681 return 1;
682 }
683}
684
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200685STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
xbe17a5a832014-03-23 23:31:58 -0700686STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj, 2, 4, str_rfind);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200687STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
688STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
689STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
690STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
691STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
692STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
xbe9e1e8cd2014-03-12 22:57:16 -0700693STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
xbe613a8e32014-03-18 00:06:29 -0700694STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
xbe4504ea82014-03-19 00:46:14 -0700695STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
Damiend99b0522013-12-21 18:17:45 +0000696
Damien George9b196cd2014-03-26 21:47:19 +0000697STATIC const mp_map_elem_t str_locals_dict_table[] = {
698 { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
699 { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
700 { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
701 { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
702 { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
703 { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
704 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
705 { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
706 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
707 { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj },
708 { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800709};
Damien George97209d32014-01-07 15:58:30 +0000710
Damien George9b196cd2014-03-26 21:47:19 +0000711STATIC MP_DEFINE_CONST_DICT(str_locals_dict, str_locals_dict_table);
712
Damiend99b0522013-12-21 18:17:45 +0000713const mp_obj_type_t str_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000714 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000715 .name = MP_QSTR_str,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200716 .print = str_print,
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200717 .make_new = str_make_new,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200718 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200719 .getiter = mp_obj_new_str_iterator,
Damien George2da98302014-03-09 19:58:18 +0000720 .buffer_p = { .get_buffer = str_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000721 .locals_dict = (mp_obj_t)&str_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000722};
723
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200724// Reuses most of methods from str
725const mp_obj_type_t bytes_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000726 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000727 .name = MP_QSTR_bytes,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200728 .print = str_print,
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200729 .make_new = bytes_make_new,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200730 .binary_op = str_binary_op,
731 .getiter = mp_obj_new_bytes_iterator,
Damien George9b196cd2014-03-26 21:47:19 +0000732 .locals_dict = (mp_obj_t)&str_locals_dict,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200733};
734
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200735// the zero-length bytes
736STATIC const mp_obj_str_t empty_bytes_obj = {{&bytes_type}, 0, 0, NULL};
737const mp_obj_t mp_const_empty_bytes = (mp_obj_t)&empty_bytes_obj;
738
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200739mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200740 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200741 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000742 o->len = len;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200743 byte *p = m_new(byte, len + 1);
744 o->data = p;
745 *data = p;
Damiend99b0522013-12-21 18:17:45 +0000746 return o;
747}
748
Damien George5fa93b62014-01-22 14:35:10 +0000749mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000750 mp_obj_str_t *o = o_in;
751 o->hash = qstr_compute_hash(o->data, o->len);
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200752 byte *p = (byte*)o->data;
753 p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
Damien George5fa93b62014-01-22 14:35:10 +0000754 return o;
755}
756
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200757STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200758 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200759 o->base.type = type;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200760 o->len = len;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200761 if (data) {
762 o->hash = qstr_compute_hash(data, len);
763 byte *p = m_new(byte, len + 1);
764 o->data = p;
765 memcpy(p, data, len * sizeof(byte));
766 p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
767 }
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200768 return o;
769}
770
Damien George5fa93b62014-01-22 14:35:10 +0000771mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
772 qstr q = qstr_find_strn(data, len);
773 if (q != MP_QSTR_NULL) {
774 // qstr with this data already exists
775 return MP_OBJ_NEW_QSTR(q);
776 } else if (make_qstr_if_not_already) {
777 // no existing qstr, make a new one
778 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
779 } else {
780 // no existing qstr, don't make one
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200781 return str_new(&str_type, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200782 }
Damien George5fa93b62014-01-22 14:35:10 +0000783}
784
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200785mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
786 return str_new(&bytes_type, data, len);
787}
788
Damien George5fa93b62014-01-22 14:35:10 +0000789bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
790 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
791 return s1 == s2;
792 } else {
793 GET_STR_HASH(s1, h1);
794 GET_STR_HASH(s2, h2);
795 if (h1 != h2) {
796 return false;
797 }
798 GET_STR_DATA_LEN(s1, d1, l1);
799 GET_STR_DATA_LEN(s2, d2, l2);
800 if (l1 != l2) {
801 return false;
802 }
Damien George1e708fe2014-01-23 18:27:51 +0000803 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200804 }
Damien George5fa93b62014-01-22 14:35:10 +0000805}
806
Damien Georgeb829b5c2014-01-25 13:51:19 +0000807void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
808void bad_implicit_conversion(mp_obj_t self_in) {
Damien Georgec5966122014-02-15 16:10:44 +0000809 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Can't convert '%s' object to str implicitly", mp_obj_get_type_str(self_in)));
Damien Georgeb829b5c2014-01-25 13:51:19 +0000810}
811
Damien George5fa93b62014-01-22 14:35:10 +0000812uint mp_obj_str_get_hash(mp_obj_t self_in) {
813 if (MP_OBJ_IS_STR(self_in)) {
814 GET_STR_HASH(self_in, h);
815 return h;
816 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000817 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000818 }
819}
820
821uint mp_obj_str_get_len(mp_obj_t self_in) {
822 if (MP_OBJ_IS_STR(self_in)) {
823 GET_STR_LEN(self_in, l);
824 return l;
825 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000826 bad_implicit_conversion(self_in);
827 }
828}
829
830// use this if you will anyway convert the string to a qstr
831// will be more efficient for the case where it's already a qstr
832qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
833 if (MP_OBJ_IS_QSTR(self_in)) {
834 return MP_OBJ_QSTR_VALUE(self_in);
835 } else if (MP_OBJ_IS_TYPE(self_in, &str_type)) {
836 mp_obj_str_t *self = self_in;
837 return qstr_from_strn((char*)self->data, self->len);
838 } else {
839 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000840 }
841}
842
843// only use this function if you need the str data to be zero terminated
844// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
845const char *mp_obj_str_get_str(mp_obj_t self_in) {
846 if (MP_OBJ_IS_STR(self_in)) {
847 GET_STR_DATA_LEN(self_in, s, l);
848 (void)l; // len unused
849 return (const char*)s;
850 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000851 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000852 }
853}
854
Damien George698ec212014-02-08 18:17:23 +0000855const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
Damien George5fa93b62014-01-22 14:35:10 +0000856 if (MP_OBJ_IS_STR(self_in)) {
857 GET_STR_DATA_LEN(self_in, s, l);
858 *len = l;
Damien George698ec212014-02-08 18:17:23 +0000859 return (const char*)s;
Damien George5fa93b62014-01-22 14:35:10 +0000860 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000861 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000862 }
Damiend99b0522013-12-21 18:17:45 +0000863}
xyb8cfc9f02014-01-05 18:47:51 +0800864
865/******************************************************************************/
866/* str iterator */
867
868typedef struct _mp_obj_str_it_t {
869 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000870 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800871 machine_uint_t cur;
872} mp_obj_str_it_t;
873
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200874STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
xyb8cfc9f02014-01-05 18:47:51 +0800875 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000876 GET_STR_DATA_LEN(self->str, str, len);
877 if (self->cur < len) {
878 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800879 self->cur += 1;
880 return o_out;
881 } else {
Damien George66eaf842014-03-26 19:27:58 +0000882 return MP_OBJ_NULL;
xyb8cfc9f02014-01-05 18:47:51 +0800883 }
884}
885
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200886STATIC const mp_obj_type_t str_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000887 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000888 .name = MP_QSTR_iterator,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200889 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800890};
891
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200892STATIC mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200893 mp_obj_str_it_t *self = self_in;
894 GET_STR_DATA_LEN(self->str, str, len);
895 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000896 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200897 self->cur += 1;
898 return o_out;
899 } else {
Damien George66eaf842014-03-26 19:27:58 +0000900 return MP_OBJ_NULL;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200901 }
902}
903
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200904STATIC const mp_obj_type_t bytes_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000905 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000906 .name = MP_QSTR_iterator,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200907 .iternext = bytes_it_iternext,
908};
909
910mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800911 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
912 o->base.type = &str_it_type;
913 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200914 o->cur = 0;
915 return o;
916}
917
918mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
919 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
920 o->base.type = &bytes_it_type;
921 o->str = str;
922 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800923 return o;
924}