blob: 936542b0e991193aa003242bb3d07996b30a695f [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"
10#include "runtime0.h"
11#include "runtime.h"
12
13typedef struct _mp_obj_str_t {
14 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +000015 machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
16 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 +020017 const byte *data;
Damiend99b0522013-12-21 18:17:45 +000018} mp_obj_str_t;
19
Paul Sokolovsky4db727a2014-03-31 21:18:28 +030020STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args);
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);
Damien George3e1a5c12014-03-29 13:43:38 +000074 bool is_bytes = MP_OBJ_IS_TYPE(self_in, &mp_type_bytes);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020075 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
Damien George3e1a5c12014-03-29 13:43:38 +0000103 if (!MP_OBJ_IS_TYPE(args[0], &mp_type_bytes)) {
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200104 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);
Damien George3e1a5c12014-03-29 13:43:38 +0000108 mp_obj_str_t *o = str_new(&mp_type_str, NULL, str_len);
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200109 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);
Damien George3e1a5c12014-03-29 13:43:38 +0000130 mp_obj_str_t *o = str_new(&mp_type_bytes, NULL, str_len);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200131 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
Damien George3e1a5c12014-03-29 13:43:38 +0000144 mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200145 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);
Damien George3e1a5c12014-03-29 13:43:38 +0000160 o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200161 }
162
Damien Georged17926d2014-03-30 13:35:08 +0100163 mp_obj_t iterable = mp_getiter(args[0]);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200164 mp_obj_t item;
Damien Georged17926d2014-03-30 13:35:08 +0100165 while ((item = mp_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);
Damien George3e1a5c12014-03-29 13:43:38 +0000177 o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200178 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) {
Damien Georged17926d2014-03-30 13:35:08 +0100218 case MP_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);
Damien George3e1a5c12014-03-29 13:43:38 +0000223 if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytes)) {
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
Damien George3e1a5c12014-03-29 13:43:38 +0000229 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_slice)) {
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
Damien Georged17926d2014-03-30 13:35:08 +0100242 case MP_BINARY_OP_ADD:
243 case MP_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 Georged17926d2014-03-30 13:35:08 +0100267 case MP_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
Damien Georged17926d2014-03-30 13:35:08 +0100275 case MP_BINARY_OP_MULTIPLY:
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200276 {
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
Paul Sokolovsky4db727a2014-03-31 21:18:28 +0300287 case MP_BINARY_OP_MODULO: {
288 mp_obj_t *args;
289 uint n_args;
290 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple)) {
291 // TODO: Support tuple subclasses?
292 mp_obj_tuple_get(rhs_in, &n_args, &args);
293 } else {
294 args = &rhs_in;
295 n_args = 1;
296 }
297 return str_modulo_format(lhs_in, n_args, args);
298 }
299
Damien Georged17926d2014-03-30 13:35:08 +0100300 // These 2 are never passed here, dealt with as a special case in mp_binary_op().
301 //case MP_BINARY_OP_EQUAL:
302 //case MP_BINARY_OP_NOT_EQUAL:
303 case MP_BINARY_OP_LESS:
304 case MP_BINARY_OP_LESS_EQUAL:
305 case MP_BINARY_OP_MORE:
306 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200307 if (MP_OBJ_IS_STR(rhs_in)) {
308 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
309 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, rhs_data, rhs_len));
310 }
Damiend99b0522013-12-21 18:17:45 +0000311 }
312
313 return MP_OBJ_NULL; // op not supported
314}
315
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200316STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Damien George5fa93b62014-01-22 14:35:10 +0000317 assert(MP_OBJ_IS_STR(self_in));
Damiend99b0522013-12-21 18:17:45 +0000318
Damien Georgefe8fb912014-01-02 16:36:09 +0000319 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000320 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000321
322 // process args
Damiend99b0522013-12-21 18:17:45 +0000323 uint seq_len;
324 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000325 if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000326 mp_obj_tuple_get(arg, &seq_len, &seq_items);
Damien George3e1a5c12014-03-29 13:43:38 +0000327 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000328 mp_obj_list_get(arg, &seq_len, &seq_items);
329 } else {
330 goto bad_arg;
331 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000332
333 // count required length
334 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000335 for (int i = 0; i < seq_len; i++) {
Damien George5fa93b62014-01-22 14:35:10 +0000336 if (!MP_OBJ_IS_STR(seq_items[i])) {
Damiend99b0522013-12-21 18:17:45 +0000337 goto bad_arg;
338 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000339 if (i > 0) {
340 required_len += sep_len;
341 }
Damien George5fa93b62014-01-22 14:35:10 +0000342 GET_STR_LEN(seq_items[i], l);
343 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000344 }
345
346 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000347 byte *data;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200348 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 +0000349 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000350 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000351 memcpy(data, sep_str, sep_len);
352 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000353 }
Damien George5fa93b62014-01-22 14:35:10 +0000354 GET_STR_DATA_LEN(seq_items[i], s, l);
355 memcpy(data, s, l);
356 data += l;
Damiend99b0522013-12-21 18:17:45 +0000357 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000358
359 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000360 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000361
362bad_arg:
Damien Georgec5966122014-02-15 16:10:44 +0000363 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "?str.join expecting a list of str's"));
Damiend99b0522013-12-21 18:17:45 +0000364}
365
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200366#define is_ws(c) ((c) == ' ' || (c) == '\t')
367
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200368STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200369 int splits = -1;
370 mp_obj_t sep = mp_const_none;
371 if (n_args > 1) {
372 sep = args[1];
373 if (n_args > 2) {
374 splits = MP_OBJ_SMALL_INT_VALUE(args[2]);
375 }
376 }
377 assert(sep == mp_const_none);
Damien George12eacca2014-01-21 21:54:15 +0000378 (void)sep; // unused; to hush compiler warning
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200379 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000380 GET_STR_DATA_LEN(args[0], s, len);
381 const byte *top = s + len;
382 const byte *start;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200383
384 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000385 while (s < top && is_ws(*s)) s++;
386 while (s < top && splits != 0) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200387 start = s;
Damien George5fa93b62014-01-22 14:35:10 +0000388 while (s < top && !is_ws(*s)) s++;
Damien George15d18062014-03-31 16:28:13 +0100389 mp_obj_list_append(res, mp_obj_new_str(start, s - start, false));
Damien George5fa93b62014-01-22 14:35:10 +0000390 if (s >= top) {
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200391 break;
392 }
Damien George5fa93b62014-01-22 14:35:10 +0000393 while (s < top && is_ws(*s)) s++;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200394 if (splits > 0) {
395 splits--;
396 }
397 }
398
Damien George5fa93b62014-01-22 14:35:10 +0000399 if (s < top) {
Damien George15d18062014-03-31 16:28:13 +0100400 mp_obj_list_append(res, mp_obj_new_str(s, top - s, false));
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200401 }
402
403 return res;
404}
405
xbe17a5a832014-03-23 23:31:58 -0700406STATIC 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 +0000407 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000408 assert(MP_OBJ_IS_STR(args[0]));
409 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000410
Damien George5fa93b62014-01-22 14:35:10 +0000411 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
412 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000413
xbec5538882014-03-16 17:58:35 -0700414 machine_uint_t start = 0;
415 machine_uint_t end = haystack_len;
John R. Lentone8204912014-01-12 21:53:52 +0000416 if (n_args >= 3 && args[2] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000417 start = mp_get_index(&mp_type_str, haystack_len, args[2], true);
John R. Lentone8204912014-01-12 21:53:52 +0000418 }
419 if (n_args >= 4 && args[3] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000420 end = mp_get_index(&mp_type_str, haystack_len, args[3], true);
John R. Lentone8204912014-01-12 21:53:52 +0000421 }
422
xbe17a5a832014-03-23 23:31:58 -0700423 const byte *p = find_subbytes(haystack + start, end - start, needle, needle_len, direction);
Damien George23005372014-01-13 19:39:01 +0000424 if (p == NULL) {
425 // not found
426 return MP_OBJ_NEW_SMALL_INT(-1);
427 } else {
428 // found
xbe17a5a832014-03-23 23:31:58 -0700429 return MP_OBJ_NEW_SMALL_INT(p - haystack);
John R. Lentone8204912014-01-12 21:53:52 +0000430 }
John R. Lentone8204912014-01-12 21:53:52 +0000431}
432
xbe17a5a832014-03-23 23:31:58 -0700433STATIC mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
434 return str_finder(n_args, args, 1);
435}
436
437STATIC mp_obj_t str_rfind(uint n_args, const mp_obj_t *args) {
438 return str_finder(n_args, args, -1);
439}
440
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200441// TODO: (Much) more variety in args
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200442STATIC mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200443 GET_STR_DATA_LEN(self_in, str, str_len);
444 GET_STR_DATA_LEN(arg, prefix, prefix_len);
445 if (prefix_len > str_len) {
446 return mp_const_false;
447 }
448 return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
449}
450
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200451STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800452 assert(1 <= n_args && n_args <= 2);
Damien George5fa93b62014-01-22 14:35:10 +0000453 assert(MP_OBJ_IS_STR(args[0]));
454
455 const byte *chars_to_del;
456 uint chars_to_del_len;
457 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800458
459 if (n_args == 1) {
460 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000461 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800462 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000463 assert(MP_OBJ_IS_STR(args[1]));
464 GET_STR_DATA_LEN(args[1], s, l);
465 chars_to_del = s;
466 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800467 }
468
Damien George5fa93b62014-01-22 14:35:10 +0000469 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800470
xbec5538882014-03-16 17:58:35 -0700471 machine_uint_t first_good_char_pos = 0;
xbe7b0f39f2014-01-08 14:23:45 -0800472 bool first_good_char_pos_set = false;
xbec5538882014-03-16 17:58:35 -0700473 machine_uint_t last_good_char_pos = 0;
474 for (machine_uint_t i = 0; i < orig_str_len; i++) {
xbe17a5a832014-03-23 23:31:58 -0700475 if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
xbe7b0f39f2014-01-08 14:23:45 -0800476 last_good_char_pos = i;
477 if (!first_good_char_pos_set) {
478 first_good_char_pos = i;
479 first_good_char_pos_set = true;
480 }
481 }
482 }
483
484 if (first_good_char_pos == 0 && last_good_char_pos == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000485 // string is all whitespace, return ''
486 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800487 }
488
489 assert(last_good_char_pos >= first_good_char_pos);
490 //+1 to accomodate the last character
xbec5538882014-03-16 17:58:35 -0700491 machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Damien George5fa93b62014-01-22 14:35:10 +0000492 return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
xbe7b0f39f2014-01-08 14:23:45 -0800493}
494
Damien Georgea11ceca2014-01-19 16:02:09 +0000495mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000496 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000497
Damien George5fa93b62014-01-22 14:35:10 +0000498 GET_STR_DATA_LEN(args[0], str, len);
Damiend99b0522013-12-21 18:17:45 +0000499 int arg_i = 1;
500 vstr_t *vstr = vstr_new();
Damien George5fa93b62014-01-22 14:35:10 +0000501 for (const byte *top = str + len; str < top; str++) {
Damiend99b0522013-12-21 18:17:45 +0000502 if (*str == '{') {
503 str++;
Damien George5fa93b62014-01-22 14:35:10 +0000504 if (str < top && *str == '{') {
Damiend99b0522013-12-21 18:17:45 +0000505 vstr_add_char(vstr, '{');
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200506 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000507 while (str < top && *str != '}') str++;
Damiend99b0522013-12-21 18:17:45 +0000508 if (arg_i >= n_args) {
Damien Georgec5966122014-02-15 16:10:44 +0000509 nlr_jump(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
Damiend99b0522013-12-21 18:17:45 +0000510 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200511 // TODO: may be PRINT_REPR depending on formatting code
Damien George4899ff92014-01-15 22:39:03 +0000512 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
Damiend99b0522013-12-21 18:17:45 +0000513 arg_i++;
514 }
515 } else {
516 vstr_add_char(vstr, *str);
517 }
518 }
519
Damien George5fa93b62014-01-22 14:35:10 +0000520 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
521 vstr_free(vstr);
522 return s;
Damiend99b0522013-12-21 18:17:45 +0000523}
524
Paul Sokolovsky4db727a2014-03-31 21:18:28 +0300525STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args) {
526 assert(MP_OBJ_IS_STR(pattern));
527
528 GET_STR_DATA_LEN(pattern, str, len);
529 int arg_i = 0;
530 vstr_t *vstr = vstr_new();
531 for (const byte *top = str + len; str < top; str++) {
532 if (*str == '%') {
533 if (++str >= top) {
534 break;
535 }
536 if (*str == '%') {
537 vstr_add_char(vstr, '%');
538 } else {
539 if (arg_i >= n_args) {
540 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "not enough arguments for format string"));
541 }
542 switch (*str) {
543 case 's':
544 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_STR);
545 break;
546 case 'r':
547 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[arg_i], PRINT_REPR);
548 break;
549 }
550 arg_i++;
551 }
552 } else {
553 vstr_add_char(vstr, *str);
554 }
555 }
556
557 if (arg_i != n_args) {
558 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "not all arguments converted during string formatting"));
559 }
560
561 mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
562 vstr_free(vstr);
563 return s;
564}
565
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200566STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
xbe480c15a2014-01-30 22:17:30 -0800567 assert(MP_OBJ_IS_STR(args[0]));
568 assert(MP_OBJ_IS_STR(args[1]));
569 assert(MP_OBJ_IS_STR(args[2]));
570
Damien George94f68302014-01-31 23:45:12 +0000571 machine_int_t max_rep = 0;
xbe480c15a2014-01-30 22:17:30 -0800572 if (n_args == 4) {
Paul Sokolovsky4e246082014-02-11 15:29:55 +0200573 assert(MP_OBJ_IS_SMALL_INT(args[3]));
574 max_rep = MP_OBJ_SMALL_INT_VALUE(args[3]);
575 if (max_rep == 0) {
576 return args[0];
577 } else if (max_rep < 0) {
578 max_rep = 0;
579 }
xbe480c15a2014-01-30 22:17:30 -0800580 }
Damien George94f68302014-01-31 23:45:12 +0000581
582 // if max_rep is still 0 by this point we will need to do all possible replacements
xbe480c15a2014-01-30 22:17:30 -0800583
584 GET_STR_DATA_LEN(args[0], str, str_len);
585 GET_STR_DATA_LEN(args[1], old, old_len);
586 GET_STR_DATA_LEN(args[2], new, new_len);
Damien George94f68302014-01-31 23:45:12 +0000587
588 // old won't exist in str if it's longer, so nothing to replace
xbe480c15a2014-01-30 22:17:30 -0800589 if (old_len > str_len) {
Paul Sokolovsky4e246082014-02-11 15:29:55 +0200590 return args[0];
xbe480c15a2014-01-30 22:17:30 -0800591 }
592
Damien George94f68302014-01-31 23:45:12 +0000593 // data for the replaced string
594 byte *data = NULL;
595 mp_obj_t replaced_str = MP_OBJ_NULL;
xbe480c15a2014-01-30 22:17:30 -0800596
Damien George94f68302014-01-31 23:45:12 +0000597 // do 2 passes over the string:
598 // first pass computes the required length of the replaced string
599 // second pass does the replacements
600 for (;;) {
601 machine_uint_t replaced_str_index = 0;
602 machine_uint_t num_replacements_done = 0;
603 const byte *old_occurrence;
604 const byte *offset_ptr = str;
605 machine_uint_t offset_num = 0;
xbe17a5a832014-03-23 23:31:58 -0700606 while ((old_occurrence = find_subbytes(offset_ptr, str_len - offset_num, old, old_len, 1)) != NULL) {
Damien George94f68302014-01-31 23:45:12 +0000607 // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence
608 if (data != NULL) {
609 memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr);
610 }
611 replaced_str_index += old_occurrence - offset_ptr;
612 // copy the replacement string
613 if (data != NULL) {
614 memcpy(data + replaced_str_index, new, new_len);
615 }
616 replaced_str_index += new_len;
617 offset_ptr = old_occurrence + old_len;
618 offset_num = offset_ptr - str;
xbe480c15a2014-01-30 22:17:30 -0800619
Damien George94f68302014-01-31 23:45:12 +0000620 num_replacements_done++;
621 if (max_rep != 0 && num_replacements_done == max_rep){
622 break;
623 }
624 }
625
626 // copy from just after end of last occurrence of to-be-replaced string to end of old string
627 if (data != NULL) {
628 memcpy(data + replaced_str_index, offset_ptr, str_len - offset_num);
629 }
630 replaced_str_index += str_len - offset_num;
631
632 if (data == NULL) {
633 // first pass
634 if (num_replacements_done == 0) {
635 // no substr found, return original string
636 return args[0];
637 } else {
638 // substr found, allocate new string
639 replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data);
640 }
641 } else {
642 // second pass, we are done
643 break;
644 }
xbe480c15a2014-01-30 22:17:30 -0800645 }
Damien George94f68302014-01-31 23:45:12 +0000646
xbe480c15a2014-01-30 22:17:30 -0800647 return mp_obj_str_builder_end(replaced_str);
648}
649
xbe9e1e8cd2014-03-12 22:57:16 -0700650STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
651 assert(2 <= n_args && n_args <= 4);
652 assert(MP_OBJ_IS_STR(args[0]));
653 assert(MP_OBJ_IS_STR(args[1]));
654
655 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
656 GET_STR_DATA_LEN(args[1], needle, needle_len);
657
Damien George536dde22014-03-13 22:07:55 +0000658 machine_uint_t start = 0;
659 machine_uint_t end = haystack_len;
xbe9e1e8cd2014-03-12 22:57:16 -0700660 if (n_args >= 3 && args[2] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000661 start = mp_get_index(&mp_type_str, haystack_len, args[2], true);
xbe9e1e8cd2014-03-12 22:57:16 -0700662 }
663 if (n_args >= 4 && args[3] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000664 end = mp_get_index(&mp_type_str, haystack_len, args[3], true);
xbe9e1e8cd2014-03-12 22:57:16 -0700665 }
666
Damien George536dde22014-03-13 22:07:55 +0000667 // if needle_len is zero then we count each gap between characters as an occurrence
668 if (needle_len == 0) {
669 return MP_OBJ_NEW_SMALL_INT(end - start + 1);
xbe9e1e8cd2014-03-12 22:57:16 -0700670 }
671
Damien George536dde22014-03-13 22:07:55 +0000672 // count the occurrences
673 machine_int_t num_occurrences = 0;
xbec5d70ba2014-03-13 00:29:15 -0700674 for (machine_uint_t haystack_index = start; haystack_index + needle_len <= end; haystack_index++) {
675 if (memcmp(&haystack[haystack_index], needle, needle_len) == 0) {
676 num_occurrences++;
677 haystack_index += needle_len - 1;
678 }
xbe9e1e8cd2014-03-12 22:57:16 -0700679 }
680
681 return MP_OBJ_NEW_SMALL_INT(num_occurrences);
682}
683
Damien Georgeb035db32014-03-21 20:39:40 +0000684STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t direction) {
xbe613a8e32014-03-18 00:06:29 -0700685 assert(MP_OBJ_IS_STR(self_in));
686 if (!MP_OBJ_IS_STR(arg)) {
687 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
688 "Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
689 }
Damien Georgeb035db32014-03-21 20:39:40 +0000690
xbe613a8e32014-03-18 00:06:29 -0700691 GET_STR_DATA_LEN(self_in, str, str_len);
692 GET_STR_DATA_LEN(arg, sep, sep_len);
693
694 if (sep_len == 0) {
695 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
696 }
Damien Georgeb035db32014-03-21 20:39:40 +0000697
698 mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
699
700 if (direction > 0) {
701 result[0] = self_in;
xbe0a6894c2014-03-21 01:12:26 -0700702 } else {
Damien Georgeb035db32014-03-21 20:39:40 +0000703 result[2] = self_in;
xbe0a6894c2014-03-21 01:12:26 -0700704 }
xbe613a8e32014-03-18 00:06:29 -0700705
xbe17a5a832014-03-23 23:31:58 -0700706 const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
707 if (position_ptr != NULL) {
708 machine_uint_t position = position_ptr - str;
709 result[0] = mp_obj_new_str(str, position, false);
710 result[1] = arg;
711 result[2] = mp_obj_new_str(str + position + sep_len, str_len - position - sep_len, false);
xbe613a8e32014-03-18 00:06:29 -0700712 }
Damien Georgeb035db32014-03-21 20:39:40 +0000713
xbe0a6894c2014-03-21 01:12:26 -0700714 return mp_obj_new_tuple(3, result);
xbe613a8e32014-03-18 00:06:29 -0700715}
716
Damien Georgeb035db32014-03-21 20:39:40 +0000717STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
718 return str_partitioner(self_in, arg, 1);
xbe0a6894c2014-03-21 01:12:26 -0700719}
xbe4504ea82014-03-19 00:46:14 -0700720
Damien Georgeb035db32014-03-21 20:39:40 +0000721STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
722 return str_partitioner(self_in, arg, -1);
xbe4504ea82014-03-19 00:46:14 -0700723}
724
Damien George2da98302014-03-09 19:58:18 +0000725STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
726 if (flags == BUFFER_READ) {
727 GET_STR_DATA_LEN(self_in, str_data, str_len);
728 bufinfo->buf = (void*)str_data;
729 bufinfo->len = str_len;
730 return 0;
731 } else {
732 // can't write to a string
733 bufinfo->buf = NULL;
734 bufinfo->len = 0;
735 return 1;
736 }
737}
738
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200739STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
xbe17a5a832014-03-23 23:31:58 -0700740STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj, 2, 4, str_rfind);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200741STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
742STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
743STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
744STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
745STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
746STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
xbe9e1e8cd2014-03-12 22:57:16 -0700747STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
xbe613a8e32014-03-18 00:06:29 -0700748STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
xbe4504ea82014-03-19 00:46:14 -0700749STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
Damiend99b0522013-12-21 18:17:45 +0000750
Damien George9b196cd2014-03-26 21:47:19 +0000751STATIC const mp_map_elem_t str_locals_dict_table[] = {
752 { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
753 { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
754 { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
755 { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
756 { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
757 { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
758 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
759 { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
760 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
761 { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj },
762 { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800763};
Damien George97209d32014-01-07 15:58:30 +0000764
Damien George9b196cd2014-03-26 21:47:19 +0000765STATIC MP_DEFINE_CONST_DICT(str_locals_dict, str_locals_dict_table);
766
Damien George3e1a5c12014-03-29 13:43:38 +0000767const mp_obj_type_t mp_type_str = {
Damien Georgec5966122014-02-15 16:10:44 +0000768 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000769 .name = MP_QSTR_str,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200770 .print = str_print,
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200771 .make_new = str_make_new,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200772 .binary_op = str_binary_op,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200773 .getiter = mp_obj_new_str_iterator,
Damien George2da98302014-03-09 19:58:18 +0000774 .buffer_p = { .get_buffer = str_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000775 .locals_dict = (mp_obj_t)&str_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000776};
777
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200778// Reuses most of methods from str
Damien George3e1a5c12014-03-29 13:43:38 +0000779const mp_obj_type_t mp_type_bytes = {
Damien Georgec5966122014-02-15 16:10:44 +0000780 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000781 .name = MP_QSTR_bytes,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200782 .print = str_print,
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200783 .make_new = bytes_make_new,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200784 .binary_op = str_binary_op,
785 .getiter = mp_obj_new_bytes_iterator,
Damien George9b196cd2014-03-26 21:47:19 +0000786 .locals_dict = (mp_obj_t)&str_locals_dict,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200787};
788
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200789// the zero-length bytes
Damien George3e1a5c12014-03-29 13:43:38 +0000790STATIC const mp_obj_str_t empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200791const mp_obj_t mp_const_empty_bytes = (mp_obj_t)&empty_bytes_obj;
792
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200793mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200794 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200795 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +0000796 o->len = len;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200797 byte *p = m_new(byte, len + 1);
798 o->data = p;
799 *data = p;
Damiend99b0522013-12-21 18:17:45 +0000800 return o;
801}
802
Damien George5fa93b62014-01-22 14:35:10 +0000803mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000804 mp_obj_str_t *o = o_in;
805 o->hash = qstr_compute_hash(o->data, o->len);
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200806 byte *p = (byte*)o->data;
807 p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
Damien George5fa93b62014-01-22 14:35:10 +0000808 return o;
809}
810
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200811STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200812 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200813 o->base.type = type;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200814 o->len = len;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +0200815 if (data) {
816 o->hash = qstr_compute_hash(data, len);
817 byte *p = m_new(byte, len + 1);
818 o->data = p;
819 memcpy(p, data, len * sizeof(byte));
820 p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
821 }
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200822 return o;
823}
824
Damien George5fa93b62014-01-22 14:35:10 +0000825mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
826 qstr q = qstr_find_strn(data, len);
827 if (q != MP_QSTR_NULL) {
828 // qstr with this data already exists
829 return MP_OBJ_NEW_QSTR(q);
830 } else if (make_qstr_if_not_already) {
831 // no existing qstr, make a new one
832 return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
833 } else {
834 // no existing qstr, don't make one
Damien George3e1a5c12014-03-29 13:43:38 +0000835 return str_new(&mp_type_str, data, len);
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200836 }
Damien George5fa93b62014-01-22 14:35:10 +0000837}
838
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200839mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
Damien George3e1a5c12014-03-29 13:43:38 +0000840 return str_new(&mp_type_bytes, data, len);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200841}
842
Damien George5fa93b62014-01-22 14:35:10 +0000843bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
844 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
845 return s1 == s2;
846 } else {
847 GET_STR_HASH(s1, h1);
848 GET_STR_HASH(s2, h2);
849 if (h1 != h2) {
850 return false;
851 }
852 GET_STR_DATA_LEN(s1, d1, l1);
853 GET_STR_DATA_LEN(s2, d2, l2);
854 if (l1 != l2) {
855 return false;
856 }
Damien George1e708fe2014-01-23 18:27:51 +0000857 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +0200858 }
Damien George5fa93b62014-01-22 14:35:10 +0000859}
860
Damien Georgeb829b5c2014-01-25 13:51:19 +0000861void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
862void bad_implicit_conversion(mp_obj_t self_in) {
Damien Georgec5966122014-02-15 16:10:44 +0000863 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 +0000864}
865
Damien George5fa93b62014-01-22 14:35:10 +0000866uint mp_obj_str_get_hash(mp_obj_t self_in) {
867 if (MP_OBJ_IS_STR(self_in)) {
868 GET_STR_HASH(self_in, h);
869 return h;
870 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000871 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000872 }
873}
874
875uint mp_obj_str_get_len(mp_obj_t self_in) {
876 if (MP_OBJ_IS_STR(self_in)) {
877 GET_STR_LEN(self_in, l);
878 return l;
879 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000880 bad_implicit_conversion(self_in);
881 }
882}
883
884// use this if you will anyway convert the string to a qstr
885// will be more efficient for the case where it's already a qstr
886qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
887 if (MP_OBJ_IS_QSTR(self_in)) {
888 return MP_OBJ_QSTR_VALUE(self_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000889 } else if (MP_OBJ_IS_TYPE(self_in, &mp_type_str)) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000890 mp_obj_str_t *self = self_in;
891 return qstr_from_strn((char*)self->data, self->len);
892 } else {
893 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000894 }
895}
896
897// only use this function if you need the str data to be zero terminated
898// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
899const char *mp_obj_str_get_str(mp_obj_t self_in) {
900 if (MP_OBJ_IS_STR(self_in)) {
901 GET_STR_DATA_LEN(self_in, s, l);
902 (void)l; // len unused
903 return (const char*)s;
904 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000905 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000906 }
907}
908
Damien George698ec212014-02-08 18:17:23 +0000909const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
Damien George5fa93b62014-01-22 14:35:10 +0000910 if (MP_OBJ_IS_STR(self_in)) {
911 GET_STR_DATA_LEN(self_in, s, l);
912 *len = l;
Damien George698ec212014-02-08 18:17:23 +0000913 return (const char*)s;
Damien George5fa93b62014-01-22 14:35:10 +0000914 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000915 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +0000916 }
Damiend99b0522013-12-21 18:17:45 +0000917}
xyb8cfc9f02014-01-05 18:47:51 +0800918
919/******************************************************************************/
920/* str iterator */
921
922typedef struct _mp_obj_str_it_t {
923 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +0000924 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +0800925 machine_uint_t cur;
926} mp_obj_str_it_t;
927
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200928STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
xyb8cfc9f02014-01-05 18:47:51 +0800929 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +0000930 GET_STR_DATA_LEN(self->str, str, len);
931 if (self->cur < len) {
932 mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +0800933 self->cur += 1;
934 return o_out;
935 } else {
Damien George66eaf842014-03-26 19:27:58 +0000936 return MP_OBJ_NULL;
xyb8cfc9f02014-01-05 18:47:51 +0800937 }
938}
939
Damien George3e1a5c12014-03-29 13:43:38 +0000940STATIC const mp_obj_type_t mp_type_str_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000941 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000942 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300943 .getiter = mp_identity,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200944 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +0800945};
946
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200947STATIC mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200948 mp_obj_str_it_t *self = self_in;
949 GET_STR_DATA_LEN(self->str, str, len);
950 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +0000951 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200952 self->cur += 1;
953 return o_out;
954 } else {
Damien George66eaf842014-03-26 19:27:58 +0000955 return MP_OBJ_NULL;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200956 }
957}
958
Damien George3e1a5c12014-03-29 13:43:38 +0000959STATIC const mp_obj_type_t mp_type_bytes_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000960 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000961 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300962 .getiter = mp_identity,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200963 .iternext = bytes_it_iternext,
964};
965
966mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +0800967 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000968 o->base.type = &mp_type_str_it;
xyb8cfc9f02014-01-05 18:47:51 +0800969 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200970 o->cur = 0;
971 return o;
972}
973
974mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
975 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000976 o->base.type = &mp_type_bytes_it;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200977 o->str = str;
978 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +0800979 return o;
980}