blob: 91c8ddeb133f96930c520b2b5f72be66cb664b08 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
xbeefe34222014-03-16 00:14:26 -070028#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <string.h>
30#include <assert.h>
31
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000033#include "nlr.h"
34#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "obj.h"
37#include "runtime0.h"
38#include "runtime.h"
Dave Hylandsbaf6f142014-03-30 21:06:50 -070039#include "pfenv.h"
Paul Sokolovsky58676fc2014-04-14 01:45:06 +030040#include "objstr.h"
Paul Sokolovsky2a273652014-05-13 08:07:08 +030041#include "objlist.h"
Damiend99b0522013-12-21 18:17:45 +000042
Paul Sokolovsky4db727a2014-03-31 21:18:28 +030043STATIC 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 +020044const mp_obj_t mp_const_empty_bytes;
45
Damien George5fa93b62014-01-22 14:35:10 +000046// use this macro to extract the string hash
47#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; }
48
49// use this macro to extract the string length
50#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; }
51
52// use this macro to extract the string data and length
53#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; }
54
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020055STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
56STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
Paul Sokolovskye9085912014-04-30 05:35:18 +030057STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
Paul Sokolovsky69f3eb22014-05-11 02:44:46 +030058STATIC NORETURN void arg_type_mixup();
59
60STATIC bool is_str_or_bytes(mp_obj_t o) {
61 return MP_OBJ_IS_STR(o) || MP_OBJ_IS_TYPE(o, &mp_type_bytes);
62}
xyb8cfc9f02014-01-05 18:47:51 +080063
64/******************************************************************************/
65/* str */
66
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020067void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
68 // this escapes characters, but it will be very slow to print (calling print many times)
69 bool has_single_quote = false;
70 bool has_double_quote = false;
71 for (const byte *s = str_data, *top = str_data + str_len; (!has_single_quote || !has_double_quote) && s < top; s++) {
72 if (*s == '\'') {
73 has_single_quote = true;
74 } else if (*s == '"') {
75 has_double_quote = true;
76 }
77 }
78 int quote_char = '\'';
79 if (has_single_quote && !has_double_quote) {
80 quote_char = '"';
81 }
82 print(env, "%c", quote_char);
83 for (const byte *s = str_data, *top = str_data + str_len; s < top; s++) {
84 if (*s == quote_char) {
85 print(env, "\\%c", quote_char);
86 } else if (*s == '\\') {
87 print(env, "\\\\");
88 } else if (32 <= *s && *s <= 126) {
89 print(env, "%c", *s);
90 } else if (*s == '\n') {
91 print(env, "\\n");
Andrew Scheller12968fb2014-04-08 02:42:50 +010092 } else if (*s == '\r') {
93 print(env, "\\r");
94 } else if (*s == '\t') {
95 print(env, "\\t");
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +020096 } else {
97 print(env, "\\x%02x", *s);
98 }
99 }
100 print(env, "%c", quote_char);
101}
102
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200103STATIC 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 +0000104 GET_STR_DATA_LEN(self_in, str_data, str_len);
Damien George3e1a5c12014-03-29 13:43:38 +0000105 bool is_bytes = MP_OBJ_IS_TYPE(self_in, &mp_type_bytes);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200106 if (kind == PRINT_STR && !is_bytes) {
Damien George5fa93b62014-01-22 14:35:10 +0000107 print(env, "%.*s", str_len, str_data);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200108 } else {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200109 if (is_bytes) {
110 print(env, "b");
111 }
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +0200112 mp_str_print_quoted(print, env, str_data, str_len);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200113 }
Damiend99b0522013-12-21 18:17:45 +0000114}
115
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200116STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Paul Sokolovskyb473d0a2014-05-06 19:30:30 +0300117#if MICROPY_CPYTHON_COMPAT
118 if (n_kw != 0) {
119 mp_arg_error_unimpl_kw();
120 }
121#endif
122
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200123 switch (n_args) {
124 case 0:
125 return MP_OBJ_NEW_QSTR(MP_QSTR_);
126
127 case 1:
128 {
129 vstr_t *vstr = vstr_new();
130 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[0], PRINT_STR);
Damien George2617eeb2014-05-25 22:27:57 +0100131 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200132 vstr_free(vstr);
133 return s;
134 }
135
136 case 2:
137 case 3:
138 {
139 // TODO: validate 2nd/3rd args
Damien George3e1a5c12014-03-29 13:43:38 +0000140 if (!MP_OBJ_IS_TYPE(args[0], &mp_type_bytes)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100141 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "bytes expected"));
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200142 }
143 GET_STR_DATA_LEN(args[0], str_data, str_len);
144 GET_STR_HASH(args[0], str_hash);
Damien Georgef600a6a2014-05-25 22:34:34 +0100145 mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_str, NULL, str_len);
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200146 o->data = str_data;
147 o->hash = str_hash;
148 return o;
149 }
150
151 default:
Damien Georgeea13f402014-04-05 18:32:08 +0100152 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "str takes at most 3 arguments"));
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200153 }
154}
155
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200156STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
157 if (n_args == 0) {
158 return mp_const_empty_bytes;
159 }
160
Paul Sokolovskyb473d0a2014-05-06 19:30:30 +0300161#if MICROPY_CPYTHON_COMPAT
162 if (n_kw != 0) {
163 mp_arg_error_unimpl_kw();
164 }
165#endif
166
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200167 if (MP_OBJ_IS_STR(args[0])) {
168 if (n_args < 2 || n_args > 3) {
169 goto wrong_args;
170 }
171 GET_STR_DATA_LEN(args[0], str_data, str_len);
172 GET_STR_HASH(args[0], str_hash);
Damien Georgef600a6a2014-05-25 22:34:34 +0100173 mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_bytes, NULL, str_len);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200174 o->data = str_data;
175 o->hash = str_hash;
176 return o;
177 }
178
179 if (n_args > 1) {
180 goto wrong_args;
181 }
182
183 if (MP_OBJ_IS_SMALL_INT(args[0])) {
184 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
185 byte *data;
186
Damien George3e1a5c12014-03-29 13:43:38 +0000187 mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200188 memset(data, 0, len);
189 return mp_obj_str_builder_end(o);
190 }
191
192 int len;
193 byte *data;
194 vstr_t *vstr = NULL;
195 mp_obj_t o = NULL;
196 // Try to create array of exact len if initializer len is known
197 mp_obj_t len_in = mp_obj_len_maybe(args[0]);
198 if (len_in == MP_OBJ_NULL) {
199 len = -1;
200 vstr = vstr_new();
201 } else {
202 len = MP_OBJ_SMALL_INT_VALUE(len_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000203 o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200204 }
205
Damien Georged17926d2014-03-30 13:35:08 +0100206 mp_obj_t iterable = mp_getiter(args[0]);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200207 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100208 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200209 if (len == -1) {
210 vstr_add_char(vstr, MP_OBJ_SMALL_INT_VALUE(item));
211 } else {
212 *data++ = MP_OBJ_SMALL_INT_VALUE(item);
213 }
214 }
215
216 if (len == -1) {
217 vstr_shrink(vstr);
218 // TODO: Optimize, borrow buffer from vstr
219 len = vstr_len(vstr);
Damien George3e1a5c12014-03-29 13:43:38 +0000220 o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200221 memcpy(data, vstr_str(vstr), len);
222 vstr_free(vstr);
223 }
224
225 return mp_obj_str_builder_end(o);
226
227wrong_args:
Damien Georgeea13f402014-04-05 18:32:08 +0100228 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +0200229}
230
Damien George55baff42014-01-21 21:40:13 +0000231// like strstr but with specified length and allows \0 bytes
232// TODO replace with something more efficient/standard
xbe17a5a832014-03-23 23:31:58 -0700233STATIC 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 +0000234 if (hlen >= nlen) {
xbe17a5a832014-03-23 23:31:58 -0700235 machine_uint_t str_index, str_index_end;
236 if (direction > 0) {
237 str_index = 0;
238 str_index_end = hlen - nlen;
239 } else {
240 str_index = hlen - nlen;
241 str_index_end = 0;
242 }
243 for (;;) {
244 if (memcmp(&haystack[str_index], needle, nlen) == 0) {
245 //found
246 return haystack + str_index;
Damien George55baff42014-01-21 21:40:13 +0000247 }
xbe17a5a832014-03-23 23:31:58 -0700248 if (str_index == str_index_end) {
249 //not found
250 break;
Damien George55baff42014-01-21 21:40:13 +0000251 }
xbe17a5a832014-03-23 23:31:58 -0700252 str_index += direction;
Damien George55baff42014-01-21 21:40:13 +0000253 }
254 }
255 return NULL;
256}
257
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200258STATIC 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 +0000259 GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300260 mp_obj_type_t *lhs_type = mp_obj_get_type(lhs_in);
261 mp_obj_type_t *rhs_type = mp_obj_get_type(rhs_in);
Damiend99b0522013-12-21 18:17:45 +0000262 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100263 case MP_BINARY_OP_ADD:
264 case MP_BINARY_OP_INPLACE_ADD:
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300265 if (lhs_type == rhs_type) {
266 // add 2 strings or bytes
Damien George5fa93b62014-01-22 14:35:10 +0000267
268 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
Damien George55baff42014-01-21 21:40:13 +0000269 int alloc_len = lhs_len + rhs_len;
Damien George5fa93b62014-01-22 14:35:10 +0000270
271 /* code for making qstr
Damien George55baff42014-01-21 21:40:13 +0000272 byte *q_ptr;
273 byte *val = qstr_build_start(alloc_len, &q_ptr);
274 memcpy(val, lhs_data, lhs_len);
275 memcpy(val + lhs_len, rhs_data, rhs_len);
Damien George5fa93b62014-01-22 14:35:10 +0000276 return MP_OBJ_NEW_QSTR(qstr_build_end(q_ptr));
277 */
278
279 // code for non-qstr
280 byte *data;
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300281 mp_obj_t s = mp_obj_str_builder_start(lhs_type, alloc_len, &data);
Damien George5fa93b62014-01-22 14:35:10 +0000282 memcpy(data, lhs_data, lhs_len);
283 memcpy(data + lhs_len, rhs_data, rhs_len);
284 return mp_obj_str_builder_end(s);
Damiend99b0522013-12-21 18:17:45 +0000285 }
286 break;
Damien George5fa93b62014-01-22 14:35:10 +0000287
Damien Georged17926d2014-03-30 13:35:08 +0100288 case MP_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +0000289 /* NOTE `a in b` is `b.__contains__(a)` */
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300290 if (lhs_type == rhs_type) {
Damien George5fa93b62014-01-22 14:35:10 +0000291 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
xbe17a5a832014-03-23 23:31:58 -0700292 return MP_BOOL(find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len, 1) != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000293 }
294 break;
Damien George5fa93b62014-01-22 14:35:10 +0000295
Damien Georged0a5bf32014-05-10 13:55:11 +0100296 case MP_BINARY_OP_MULTIPLY: {
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200297 if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George6ac5dce2014-05-21 19:42:43 +0100298 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200299 }
300 int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George5fa93b62014-01-22 14:35:10 +0000301 byte *data;
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300302 mp_obj_t s = mp_obj_str_builder_start(lhs_type, lhs_len * n, &data);
Damien George5fa93b62014-01-22 14:35:10 +0000303 mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
304 return mp_obj_str_builder_end(s);
Paul Sokolovsky545591a2014-01-21 00:27:33 +0200305 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200306
Paul Sokolovsky4db727a2014-03-31 21:18:28 +0300307 case MP_BINARY_OP_MODULO: {
308 mp_obj_t *args;
309 uint n_args;
310 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple)) {
311 // TODO: Support tuple subclasses?
312 mp_obj_tuple_get(rhs_in, &n_args, &args);
313 } else {
314 args = &rhs_in;
315 n_args = 1;
316 }
317 return str_modulo_format(lhs_in, n_args, args);
318 }
319
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300320 //case MP_BINARY_OP_NOT_EQUAL: // This is never passed here
321 case MP_BINARY_OP_EQUAL: // This will be passed only for bytes, str is dealt with in mp_obj_equal()
Damien Georged17926d2014-03-30 13:35:08 +0100322 case MP_BINARY_OP_LESS:
323 case MP_BINARY_OP_LESS_EQUAL:
324 case MP_BINARY_OP_MORE:
325 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300326 if (lhs_type == rhs_type) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200327 GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
328 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, rhs_data, rhs_len));
329 }
Paul Sokolovsky70328e42014-05-15 20:58:40 +0300330 if (lhs_type == &mp_type_bytes) {
331 mp_buffer_info_t bufinfo;
332 if (!mp_get_buffer(rhs_in, &bufinfo, MP_BUFFER_READ)) {
333 goto uncomparable;
334 }
335 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, bufinfo.buf, bufinfo.len));
336 }
337uncomparable:
338 if (op == MP_BINARY_OP_EQUAL) {
339 return mp_const_false;
340 }
Damiend99b0522013-12-21 18:17:45 +0000341 }
342
Damien George6ac5dce2014-05-21 19:42:43 +0100343 return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000344}
345
Damien George729f7b42014-04-17 22:10:53 +0100346STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Paul Sokolovsky5ebd5f02014-05-11 21:22:59 +0300347 mp_obj_type_t *type = mp_obj_get_type(self_in);
Damien George729f7b42014-04-17 22:10:53 +0100348 GET_STR_DATA_LEN(self_in, self_data, self_len);
349 if (value == MP_OBJ_SENTINEL) {
350 // load
Damien Georgeee3fd462014-05-24 23:03:12 +0100351#if MICROPY_PY_SLICE
Damien George729f7b42014-04-17 22:10:53 +0100352 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300353 mp_bound_slice_t slice;
354 if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300355 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
356 "Only slices with step=1 (aka None) are supported"));
Damien George729f7b42014-04-17 22:10:53 +0100357 }
Damien Georgef600a6a2014-05-25 22:34:34 +0100358 return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
Damien George729f7b42014-04-17 22:10:53 +0100359 }
360#endif
Damien George729f7b42014-04-17 22:10:53 +0100361 uint index_val = mp_get_index(type, self_len, index, false);
362 if (type == &mp_type_bytes) {
363 return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);
364 } else {
Damien George2617eeb2014-05-25 22:27:57 +0100365 return mp_obj_new_str((char*)self_data + index_val, 1, true);
Damien George729f7b42014-04-17 22:10:53 +0100366 }
367 } else {
Damien George6ac5dce2014-05-21 19:42:43 +0100368 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100369 }
370}
371
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200372STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky5e5d69b2014-05-11 21:13:01 +0300373 assert(is_str_or_bytes(self_in));
374 const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
Damiend99b0522013-12-21 18:17:45 +0000375
Damien Georgefe8fb912014-01-02 16:36:09 +0000376 // get separation string
Damien George5fa93b62014-01-22 14:35:10 +0000377 GET_STR_DATA_LEN(self_in, sep_str, sep_len);
Damien Georgefe8fb912014-01-02 16:36:09 +0000378
379 // process args
Damiend99b0522013-12-21 18:17:45 +0000380 uint seq_len;
381 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000382 if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000383 mp_obj_tuple_get(arg, &seq_len, &seq_items);
Damiend99b0522013-12-21 18:17:45 +0000384 } else {
Damien Georgea157e4c2014-04-09 19:17:53 +0100385 if (!MP_OBJ_IS_TYPE(arg, &mp_type_list)) {
386 // arg is not a list, try to convert it to one
Paul Sokolovsky881d9af2014-04-10 01:42:40 +0300387 // TODO: Try to optimize?
Damien Georgea157e4c2014-04-09 19:17:53 +0100388 arg = mp_type_list.make_new((mp_obj_t)&mp_type_list, 1, 0, &arg);
389 }
390 mp_obj_list_get(arg, &seq_len, &seq_items);
Damiend99b0522013-12-21 18:17:45 +0000391 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000392
393 // count required length
394 int required_len = 0;
Damiend99b0522013-12-21 18:17:45 +0000395 for (int i = 0; i < seq_len; i++) {
Paul Sokolovsky5e5d69b2014-05-11 21:13:01 +0300396 if (mp_obj_get_type(seq_items[i]) != self_type) {
397 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
398 "join expects a list of str/bytes objects consistent with self object"));
Damiend99b0522013-12-21 18:17:45 +0000399 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000400 if (i > 0) {
401 required_len += sep_len;
402 }
Damien George5fa93b62014-01-22 14:35:10 +0000403 GET_STR_LEN(seq_items[i], l);
404 required_len += l;
Damiend99b0522013-12-21 18:17:45 +0000405 }
406
407 // make joined string
Damien George5fa93b62014-01-22 14:35:10 +0000408 byte *data;
Paul Sokolovsky5e5d69b2014-05-11 21:13:01 +0300409 mp_obj_t joined_str = mp_obj_str_builder_start(self_type, required_len, &data);
Damiend99b0522013-12-21 18:17:45 +0000410 for (int i = 0; i < seq_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000411 if (i > 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000412 memcpy(data, sep_str, sep_len);
413 data += sep_len;
Damiend99b0522013-12-21 18:17:45 +0000414 }
Damien George5fa93b62014-01-22 14:35:10 +0000415 GET_STR_DATA_LEN(seq_items[i], s, l);
416 memcpy(data, s, l);
417 data += l;
Damiend99b0522013-12-21 18:17:45 +0000418 }
Damien Georgefe8fb912014-01-02 16:36:09 +0000419
420 // return joined string
Damien George5fa93b62014-01-22 14:35:10 +0000421 return mp_obj_str_builder_end(joined_str);
Damiend99b0522013-12-21 18:17:45 +0000422}
423
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200424#define is_ws(c) ((c) == ' ' || (c) == '\t')
425
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200426STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
Paul Sokolovskybfb88192014-05-11 21:17:28 +0300427 const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
Damien Georgedeed0872014-04-06 11:11:15 +0100428 machine_int_t splits = -1;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200429 mp_obj_t sep = mp_const_none;
430 if (n_args > 1) {
431 sep = args[1];
432 if (n_args > 2) {
Damien Georgedeed0872014-04-06 11:11:15 +0100433 splits = mp_obj_get_int(args[2]);
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200434 }
435 }
Damien Georgedeed0872014-04-06 11:11:15 +0100436
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200437 mp_obj_t res = mp_obj_new_list(0, NULL);
Damien George5fa93b62014-01-22 14:35:10 +0000438 GET_STR_DATA_LEN(args[0], s, len);
439 const byte *top = s + len;
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200440
Damien Georgedeed0872014-04-06 11:11:15 +0100441 if (sep == mp_const_none) {
442 // sep not given, so separate on whitespace
443
444 // Initial whitespace is not counted as split, so we pre-do it
Damien George5fa93b62014-01-22 14:35:10 +0000445 while (s < top && is_ws(*s)) s++;
Damien Georgedeed0872014-04-06 11:11:15 +0100446 while (s < top && splits != 0) {
447 const byte *start = s;
448 while (s < top && !is_ws(*s)) s++;
Damien Georgef600a6a2014-05-25 22:34:34 +0100449 mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
Damien Georgedeed0872014-04-06 11:11:15 +0100450 if (s >= top) {
451 break;
452 }
453 while (s < top && is_ws(*s)) s++;
454 if (splits > 0) {
455 splits--;
456 }
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200457 }
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200458
Damien Georgedeed0872014-04-06 11:11:15 +0100459 if (s < top) {
Damien Georgef600a6a2014-05-25 22:34:34 +0100460 mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, s, top - s));
Damien Georgedeed0872014-04-06 11:11:15 +0100461 }
462
463 } else {
464 // sep given
465
466 uint sep_len;
467 const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
468
469 if (sep_len == 0) {
470 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
471 }
472
473 for (;;) {
474 const byte *start = s;
475 for (;;) {
476 if (splits == 0 || s + sep_len > top) {
477 s = top;
478 break;
479 } else if (memcmp(s, sep_str, sep_len) == 0) {
480 break;
481 }
482 s++;
483 }
Damien Georgef600a6a2014-05-25 22:34:34 +0100484 mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
Damien Georgedeed0872014-04-06 11:11:15 +0100485 if (s >= top) {
486 break;
487 }
488 s += sep_len;
489 if (splits > 0) {
490 splits--;
491 }
492 }
Paul Sokolovsky4c316552014-01-21 05:00:21 +0200493 }
494
495 return res;
496}
497
Paul Sokolovsky2a273652014-05-13 08:07:08 +0300498STATIC mp_obj_t str_rsplit(uint n_args, const mp_obj_t *args) {
499 if (n_args < 3) {
500 // If we don't have split limit, it doesn't matter from which side
501 // we split.
502 return str_split(n_args, args);
503 }
504 const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
505 mp_obj_t sep = args[1];
506 GET_STR_DATA_LEN(args[0], s, len);
507
508 machine_int_t splits = mp_obj_get_int(args[2]);
509 machine_int_t org_splits = splits;
510 // Preallocate list to the max expected # of elements, as we
511 // will fill it from the end.
512 mp_obj_list_t *res = mp_obj_new_list(splits + 1, NULL);
513 int idx = splits;
514
515 if (sep == mp_const_none) {
516 // TODO
517 assert(0);
518 } else {
519 uint sep_len;
520 const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
521
522 if (sep_len == 0) {
523 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
524 }
525
526 const byte *beg = s;
527 const byte *last = s + len;
528 for (;;) {
529 s = last - sep_len;
530 for (;;) {
531 if (splits == 0 || s < beg) {
532 break;
533 } else if (memcmp(s, sep_str, sep_len) == 0) {
534 break;
535 }
536 s--;
537 }
538 if (s < beg || splits == 0) {
Damien Georgef600a6a2014-05-25 22:34:34 +0100539 res->items[idx] = mp_obj_new_str_of_type(self_type, beg, last - beg);
Paul Sokolovsky2a273652014-05-13 08:07:08 +0300540 break;
541 }
Damien Georgef600a6a2014-05-25 22:34:34 +0100542 res->items[idx--] = mp_obj_new_str_of_type(self_type, s + sep_len, last - s - sep_len);
Paul Sokolovsky2a273652014-05-13 08:07:08 +0300543 last = s;
544 if (splits > 0) {
545 splits--;
546 }
547 }
548 if (idx != 0) {
549 // We split less parts than split limit, now go cleanup surplus
550 int used = org_splits + 1 - idx;
551 memcpy(res->items, &res->items[idx], used * sizeof(mp_obj_t));
552 mp_seq_clear(res->items, used, res->alloc, sizeof(*res->items));
553 res->len = used;
554 }
555 }
556
557 return res;
558}
559
560
xbe3d9a39e2014-04-08 11:42:19 -0700561STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t direction, bool is_index) {
John R. Lentone8204912014-01-12 21:53:52 +0000562 assert(2 <= n_args && n_args <= 4);
Damien George5fa93b62014-01-22 14:35:10 +0000563 assert(MP_OBJ_IS_STR(args[0]));
564 assert(MP_OBJ_IS_STR(args[1]));
John R. Lentone8204912014-01-12 21:53:52 +0000565
Damien George5fa93b62014-01-22 14:35:10 +0000566 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
567 GET_STR_DATA_LEN(args[1], needle, needle_len);
John R. Lentone8204912014-01-12 21:53:52 +0000568
xbec5538882014-03-16 17:58:35 -0700569 machine_uint_t start = 0;
570 machine_uint_t end = haystack_len;
John R. Lentone8204912014-01-12 21:53:52 +0000571 if (n_args >= 3 && args[2] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000572 start = mp_get_index(&mp_type_str, haystack_len, args[2], true);
John R. Lentone8204912014-01-12 21:53:52 +0000573 }
574 if (n_args >= 4 && args[3] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +0000575 end = mp_get_index(&mp_type_str, haystack_len, args[3], true);
John R. Lentone8204912014-01-12 21:53:52 +0000576 }
577
xbe17a5a832014-03-23 23:31:58 -0700578 const byte *p = find_subbytes(haystack + start, end - start, needle, needle_len, direction);
Damien George23005372014-01-13 19:39:01 +0000579 if (p == NULL) {
580 // not found
xbe3d9a39e2014-04-08 11:42:19 -0700581 if (is_index) {
582 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "substring not found"));
583 } else {
584 return MP_OBJ_NEW_SMALL_INT(-1);
585 }
Damien George23005372014-01-13 19:39:01 +0000586 } else {
587 // found
xbe17a5a832014-03-23 23:31:58 -0700588 return MP_OBJ_NEW_SMALL_INT(p - haystack);
John R. Lentone8204912014-01-12 21:53:52 +0000589 }
John R. Lentone8204912014-01-12 21:53:52 +0000590}
591
xbe17a5a832014-03-23 23:31:58 -0700592STATIC mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
xbe3d9a39e2014-04-08 11:42:19 -0700593 return str_finder(n_args, args, 1, false);
xbe17a5a832014-03-23 23:31:58 -0700594}
595
596STATIC mp_obj_t str_rfind(uint n_args, const mp_obj_t *args) {
xbe3d9a39e2014-04-08 11:42:19 -0700597 return str_finder(n_args, args, -1, false);
598}
599
600STATIC mp_obj_t str_index(uint n_args, const mp_obj_t *args) {
601 return str_finder(n_args, args, 1, true);
602}
603
604STATIC mp_obj_t str_rindex(uint n_args, const mp_obj_t *args) {
605 return str_finder(n_args, args, -1, true);
xbe17a5a832014-03-23 23:31:58 -0700606}
607
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200608// TODO: (Much) more variety in args
Paul Sokolovskyc18ef2a2014-05-15 21:18:34 +0300609STATIC mp_obj_t str_startswith(uint n_args, const mp_obj_t *args) {
610 GET_STR_DATA_LEN(args[0], str, str_len);
611 GET_STR_DATA_LEN(args[1], prefix, prefix_len);
612 uint index_val = 0;
613 if (n_args > 2) {
614 index_val = mp_get_index(&mp_type_str, str_len, args[2], true);
615 }
616 if (prefix_len + index_val > str_len) {
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200617 return mp_const_false;
618 }
Paul Sokolovskyc18ef2a2014-05-15 21:18:34 +0300619 return MP_BOOL(memcmp(str + index_val, prefix, prefix_len) == 0);
Paul Sokolovsky1eacefe2014-01-23 01:20:40 +0200620}
621
Paul Sokolovskyd098c6b2014-05-24 22:46:51 +0300622STATIC mp_obj_t str_endswith(uint n_args, const mp_obj_t *args) {
623 GET_STR_DATA_LEN(args[0], str, str_len);
624 GET_STR_DATA_LEN(args[1], suffix, suffix_len);
625 assert(n_args == 2);
626
627 if (suffix_len > str_len) {
628 return mp_const_false;
629 }
630 return MP_BOOL(memcmp(str + (str_len - suffix_len), suffix, suffix_len) == 0);
631}
632
Paul Sokolovsky88107842014-04-26 06:20:08 +0300633enum { LSTRIP, RSTRIP, STRIP };
634
635STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
xbe7b0f39f2014-01-08 14:23:45 -0800636 assert(1 <= n_args && n_args <= 2);
Paul Sokolovskyb2d4fc02014-05-11 13:17:29 +0300637 assert(is_str_or_bytes(args[0]));
638 const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
Damien George5fa93b62014-01-22 14:35:10 +0000639
640 const byte *chars_to_del;
641 uint chars_to_del_len;
642 static const byte whitespace[] = " \t\n\r\v\f";
xbe7b0f39f2014-01-08 14:23:45 -0800643
644 if (n_args == 1) {
645 chars_to_del = whitespace;
Damien George5fa93b62014-01-22 14:35:10 +0000646 chars_to_del_len = sizeof(whitespace);
xbe7b0f39f2014-01-08 14:23:45 -0800647 } else {
Paul Sokolovskyb2d4fc02014-05-11 13:17:29 +0300648 if (mp_obj_get_type(args[1]) != self_type) {
649 arg_type_mixup();
650 }
Damien George5fa93b62014-01-22 14:35:10 +0000651 GET_STR_DATA_LEN(args[1], s, l);
652 chars_to_del = s;
653 chars_to_del_len = l;
xbe7b0f39f2014-01-08 14:23:45 -0800654 }
655
Damien George5fa93b62014-01-22 14:35:10 +0000656 GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
xbe7b0f39f2014-01-08 14:23:45 -0800657
xbec5538882014-03-16 17:58:35 -0700658 machine_uint_t first_good_char_pos = 0;
xbe7b0f39f2014-01-08 14:23:45 -0800659 bool first_good_char_pos_set = false;
xbec5538882014-03-16 17:58:35 -0700660 machine_uint_t last_good_char_pos = 0;
Paul Sokolovskye14d0962014-04-26 06:48:31 +0300661 machine_uint_t i = 0;
662 machine_int_t delta = 1;
663 if (type == RSTRIP) {
664 i = orig_str_len - 1;
665 delta = -1;
666 }
667 for (machine_uint_t len = orig_str_len; len > 0; len--) {
xbe17a5a832014-03-23 23:31:58 -0700668 if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
xbe7b0f39f2014-01-08 14:23:45 -0800669 if (!first_good_char_pos_set) {
Paul Sokolovskybcdffe52014-05-30 03:07:05 +0300670 first_good_char_pos_set = true;
xbe7b0f39f2014-01-08 14:23:45 -0800671 first_good_char_pos = i;
Paul Sokolovsky88107842014-04-26 06:20:08 +0300672 if (type == LSTRIP) {
673 last_good_char_pos = orig_str_len - 1;
674 break;
Paul Sokolovskye14d0962014-04-26 06:48:31 +0300675 } else if (type == RSTRIP) {
676 first_good_char_pos = 0;
677 last_good_char_pos = i;
678 break;
Paul Sokolovsky88107842014-04-26 06:20:08 +0300679 }
xbe7b0f39f2014-01-08 14:23:45 -0800680 }
Paul Sokolovsky88107842014-04-26 06:20:08 +0300681 last_good_char_pos = i;
xbe7b0f39f2014-01-08 14:23:45 -0800682 }
Paul Sokolovskye14d0962014-04-26 06:48:31 +0300683 i += delta;
xbe7b0f39f2014-01-08 14:23:45 -0800684 }
685
Paul Sokolovskybcdffe52014-05-30 03:07:05 +0300686 if (!first_good_char_pos_set) {
Damien George5fa93b62014-01-22 14:35:10 +0000687 // string is all whitespace, return ''
688 return MP_OBJ_NEW_QSTR(MP_QSTR_);
xbe7b0f39f2014-01-08 14:23:45 -0800689 }
690
691 assert(last_good_char_pos >= first_good_char_pos);
692 //+1 to accomodate the last character
xbec5538882014-03-16 17:58:35 -0700693 machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
Paul Sokolovsky88276822014-05-30 03:11:44 +0300694 if (stripped_len == orig_str_len) {
695 // If nothing was stripped, don't bother to dup original string
696 // TODO: watch out for this case when we'll get to bytearray.strip()
697 assert(first_good_char_pos == 0);
698 return args[0];
699 }
Damien Georgef600a6a2014-05-25 22:34:34 +0100700 return mp_obj_new_str_of_type(self_type, orig_str + first_good_char_pos, stripped_len);
xbe7b0f39f2014-01-08 14:23:45 -0800701}
702
Paul Sokolovsky88107842014-04-26 06:20:08 +0300703STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
704 return str_uni_strip(STRIP, n_args, args);
705}
706
707STATIC mp_obj_t str_lstrip(uint n_args, const mp_obj_t *args) {
708 return str_uni_strip(LSTRIP, n_args, args);
709}
710
711STATIC mp_obj_t str_rstrip(uint n_args, const mp_obj_t *args) {
712 return str_uni_strip(RSTRIP, n_args, args);
713}
714
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700715// Takes an int arg, but only parses unsigned numbers, and only changes
716// *num if at least one digit was parsed.
717static int str_to_int(const char *str, int *num) {
718 const char *s = str;
719 if (unichar_isdigit(*s)) {
720 *num = 0;
721 do {
722 *num = *num * 10 + (*s - '0');
723 s++;
724 }
725 while (unichar_isdigit(*s));
726 }
727 return s - str;
728}
729
730static bool isalignment(char ch) {
731 return ch && strchr("<>=^", ch) != NULL;
732}
733
734static bool istype(char ch) {
735 return ch && strchr("bcdeEfFgGnosxX%", ch) != NULL;
736}
737
738static bool arg_looks_integer(mp_obj_t arg) {
739 return MP_OBJ_IS_TYPE(arg, &mp_type_bool) || MP_OBJ_IS_INT(arg);
740}
741
742static bool arg_looks_numeric(mp_obj_t arg) {
743 return arg_looks_integer(arg)
744#if MICROPY_ENABLE_FLOAT
745 || MP_OBJ_IS_TYPE(arg, &mp_type_float)
746#endif
747 ;
748}
749
Dave Hylandsc4029e52014-04-07 11:19:51 -0700750static mp_obj_t arg_as_int(mp_obj_t arg) {
Dave Hylandsf81a49e2014-04-05 08:21:45 -0700751#if MICROPY_ENABLE_FLOAT
752 if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Dave Hylandsc4029e52014-04-07 11:19:51 -0700753
754 // TODO: Needs a way to construct an mpz integer from a float
755
756 mp_small_int_t num = mp_obj_get_float(arg);
757 return MP_OBJ_NEW_SMALL_INT(num);
Dave Hylandsf81a49e2014-04-05 08:21:45 -0700758 }
759#endif
Dave Hylandsc4029e52014-04-07 11:19:51 -0700760 return arg;
Dave Hylandsf81a49e2014-04-05 08:21:45 -0700761}
762
Damien George897fe0c2014-04-15 22:03:55 +0100763mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
Damien George5fa93b62014-01-22 14:35:10 +0000764 assert(MP_OBJ_IS_STR(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000765
Damien George5fa93b62014-01-22 14:35:10 +0000766 GET_STR_DATA_LEN(args[0], str, len);
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700767 int arg_i = 0;
Damiend99b0522013-12-21 18:17:45 +0000768 vstr_t *vstr = vstr_new();
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700769 pfenv_t pfenv_vstr;
770 pfenv_vstr.data = vstr;
771 pfenv_vstr.print_strn = pfenv_vstr_add_strn;
772
Damien George5fa93b62014-01-22 14:35:10 +0000773 for (const byte *top = str + len; str < top; str++) {
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700774 if (*str == '}') {
Damiend99b0522013-12-21 18:17:45 +0000775 str++;
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700776 if (str < top && *str == '}') {
777 vstr_add_char(vstr, '}');
778 continue;
779 }
Damien Georgeea13f402014-04-05 18:32:08 +0100780 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Single '}' encountered in format string"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700781 }
782 if (*str != '{') {
783 vstr_add_char(vstr, *str);
784 continue;
785 }
786
787 str++;
788 if (str < top && *str == '{') {
789 vstr_add_char(vstr, '{');
790 continue;
791 }
792
793 // replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
794
795 vstr_t *field_name = NULL;
796 char conversion = '\0';
797 vstr_t *format_spec = NULL;
798
799 if (str < top && *str != '}' && *str != '!' && *str != ':') {
800 field_name = vstr_new();
801 while (str < top && *str != '}' && *str != '!' && *str != ':') {
802 vstr_add_char(field_name, *str++);
803 }
804 vstr_add_char(field_name, '\0');
805 }
806
807 // conversion ::= "r" | "s"
808
809 if (str < top && *str == '!') {
810 str++;
811 if (str < top && (*str == 'r' || *str == 's')) {
812 conversion = *str++;
Paul Sokolovskyf2b796e2014-01-15 22:45:20 +0200813 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100814 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "end of format while looking for conversion specifier"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700815 }
816 }
817
818 if (str < top && *str == ':') {
819 str++;
820 // {:} is the same as {}, which is the same as {!s}
821 // This makes a difference when passing in a True or False
822 // '{}'.format(True) returns 'True'
823 // '{:d}'.format(True) returns '1'
824 // So we treat {:} as {} and this later gets treated to be {!s}
825 if (*str != '}') {
826 format_spec = vstr_new();
827 while (str < top && *str != '}') {
828 vstr_add_char(format_spec, *str++);
Damiend99b0522013-12-21 18:17:45 +0000829 }
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700830 vstr_add_char(format_spec, '\0');
831 }
832 }
833 if (str >= top) {
Damien Georgeea13f402014-04-05 18:32:08 +0100834 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "unmatched '{' in format"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700835 }
836 if (*str != '}') {
Damien Georgeea13f402014-04-05 18:32:08 +0100837 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "expected ':' after format specifier"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700838 }
839
840 mp_obj_t arg = mp_const_none;
841
842 if (field_name) {
843 if (arg_i > 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100844 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from automatic field numbering to manual field specification"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700845 }
Damien George3bb8bd82014-04-14 21:20:30 +0100846 int index = 0;
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700847 if (str_to_int(vstr_str(field_name), &index) != vstr_len(field_name) - 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100848 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "attributes not supported yet"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700849 }
850 if (index >= n_args - 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100851 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700852 }
853 arg = args[index + 1];
854 arg_i = -1;
855 vstr_free(field_name);
856 field_name = NULL;
857 } else {
858 if (arg_i < 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100859 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from manual field specification to automatic field numbering"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700860 }
861 if (arg_i >= n_args - 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100862 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700863 }
864 arg = args[arg_i + 1];
865 arg_i++;
866 }
867 if (!format_spec && !conversion) {
868 conversion = 's';
869 }
870 if (conversion) {
871 mp_print_kind_t print_kind;
872 if (conversion == 's') {
873 print_kind = PRINT_STR;
874 } else if (conversion == 'r') {
875 print_kind = PRINT_REPR;
876 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100877 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Unknown conversion specifier %c", conversion));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700878 }
879 vstr_t *arg_vstr = vstr_new();
880 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, arg_vstr, arg, print_kind);
Damien George2617eeb2014-05-25 22:27:57 +0100881 arg = mp_obj_new_str(vstr_str(arg_vstr), vstr_len(arg_vstr), false);
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700882 vstr_free(arg_vstr);
883 }
884
885 char sign = '\0';
886 char fill = '\0';
887 char align = '\0';
888 int width = -1;
889 int precision = -1;
890 char type = '\0';
891 int flags = 0;
892
893 if (format_spec) {
894 // The format specifier (from http://docs.python.org/2/library/string.html#formatspec)
895 //
896 // [[fill]align][sign][#][0][width][,][.precision][type]
897 // fill ::= <any character>
898 // align ::= "<" | ">" | "=" | "^"
899 // sign ::= "+" | "-" | " "
900 // width ::= integer
901 // precision ::= integer
902 // type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
903
904 const char *s = vstr_str(format_spec);
905 if (isalignment(*s)) {
906 align = *s++;
907 } else if (*s && isalignment(s[1])) {
908 fill = *s++;
909 align = *s++;
910 }
911 if (*s == '+' || *s == '-' || *s == ' ') {
912 if (*s == '+') {
913 flags |= PF_FLAG_SHOW_SIGN;
914 } else if (*s == ' ') {
915 flags |= PF_FLAG_SPACE_SIGN;
916 }
917 sign = *s++;
918 }
919 if (*s == '#') {
920 flags |= PF_FLAG_SHOW_PREFIX;
921 s++;
922 }
923 if (*s == '0') {
924 if (!align) {
925 align = '=';
926 }
927 if (!fill) {
928 fill = '0';
929 }
930 }
931 s += str_to_int(s, &width);
932 if (*s == ',') {
933 flags |= PF_FLAG_SHOW_COMMA;
934 s++;
935 }
936 if (*s == '.') {
937 s++;
938 s += str_to_int(s, &precision);
939 }
940 if (istype(*s)) {
941 type = *s++;
942 }
943 if (*s) {
Damien Georgeea13f402014-04-05 18:32:08 +0100944 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "Invalid conversion specification"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700945 }
946 vstr_free(format_spec);
947 format_spec = NULL;
948 }
949 if (!align) {
950 if (arg_looks_numeric(arg)) {
951 align = '>';
952 } else {
953 align = '<';
954 }
955 }
956 if (!fill) {
957 fill = ' ';
958 }
959
960 if (sign) {
961 if (type == 's') {
Damien Georgeea13f402014-04-05 18:32:08 +0100962 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Sign not allowed in string format specifier"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700963 }
964 if (type == 'c') {
Damien Georgeea13f402014-04-05 18:32:08 +0100965 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Sign not allowed with integer format specifier 'c'"));
Damiend99b0522013-12-21 18:17:45 +0000966 }
967 } else {
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700968 sign = '-';
969 }
970
971 switch (align) {
972 case '<': flags |= PF_FLAG_LEFT_ADJUST; break;
973 case '=': flags |= PF_FLAG_PAD_AFTER_SIGN; break;
974 case '^': flags |= PF_FLAG_CENTER_ADJUST; break;
975 }
976
977 if (arg_looks_integer(arg)) {
978 switch (type) {
979 case 'b':
Damien Georgea12a0f72014-04-08 01:29:53 +0100980 pfenv_print_mp_int(&pfenv_vstr, arg, 1, 2, 'a', flags, fill, width);
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700981 continue;
982
983 case 'c':
984 {
985 char ch = mp_obj_get_int(arg);
986 pfenv_print_strn(&pfenv_vstr, &ch, 1, flags, fill, width);
987 continue;
988 }
989
990 case '\0': // No explicit format type implies 'd'
991 case 'n': // I don't think we support locales in uPy so use 'd'
992 case 'd':
Damien Georgea12a0f72014-04-08 01:29:53 +0100993 pfenv_print_mp_int(&pfenv_vstr, arg, 1, 10, 'a', flags, fill, width);
Dave Hylandsbaf6f142014-03-30 21:06:50 -0700994 continue;
995
996 case 'o':
Dave Hylandsc4029e52014-04-07 11:19:51 -0700997 if (flags & PF_FLAG_SHOW_PREFIX) {
998 flags |= PF_FLAG_SHOW_OCTAL_LETTER;
999 }
1000
Damien Georgea12a0f72014-04-08 01:29:53 +01001001 pfenv_print_mp_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width);
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001002 continue;
1003
1004 case 'x':
Damien Georgea12a0f72014-04-08 01:29:53 +01001005 pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'a', flags, fill, width);
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001006 continue;
1007
1008 case 'X':
Damien Georgea12a0f72014-04-08 01:29:53 +01001009 pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'A', flags, fill, width);
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001010 continue;
1011
1012 case 'e':
1013 case 'E':
1014 case 'f':
1015 case 'F':
1016 case 'g':
1017 case 'G':
1018 case '%':
1019 // The floating point formatters all work with anything that
1020 // looks like an integer
1021 break;
1022
1023 default:
Damien Georgeea13f402014-04-05 18:32:08 +01001024 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001025 "Unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg)));
1026 }
Damien Georgec322c5f2014-04-02 20:04:15 +01001027 }
Damien George70f33cd2014-04-02 17:06:05 +01001028
Dave Hylands22fe4d72014-04-02 12:07:31 -07001029 // NOTE: no else here. We need the e, f, g etc formats for integer
1030 // arguments (from above if) to take this if.
Damien Georgec322c5f2014-04-02 20:04:15 +01001031 if (arg_looks_numeric(arg)) {
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001032 if (!type) {
1033
1034 // Even though the docs say that an unspecified type is the same
1035 // as 'g', there is one subtle difference, when the exponent
1036 // is one less than the precision.
1037 //
1038 // '{:10.1}'.format(0.0) ==> '0e+00'
1039 // '{:10.1g}'.format(0.0) ==> '0'
1040 //
1041 // TODO: Figure out how to deal with this.
1042 //
1043 // A proper solution would involve adding a special flag
1044 // or something to format_float, and create a format_double
1045 // to deal with doubles. In order to fix this when using
1046 // sprintf, we'd need to use the e format and tweak the
1047 // returned result to strip trailing zeros like the g format
1048 // does.
1049 //
1050 // {:10.3} and {:10.2e} with 1.23e2 both produce 1.23e+02
1051 // but with 1.e2 you get 1e+02 and 1.00e+02
1052 //
1053 // Stripping the trailing 0's (like g) does would make the
1054 // e format give us the right format.
1055 //
1056 // CPython sources say:
1057 // Omitted type specifier. Behaves in the same way as repr(x)
1058 // and str(x) if no precision is given, else like 'g', but with
1059 // at least one digit after the decimal point. */
1060
1061 type = 'g';
1062 }
1063 if (type == 'n') {
1064 type = 'g';
1065 }
1066
1067 flags |= PF_FLAG_PAD_NAN_INF; // '{:06e}'.format(float('-inf')) should give '-00inf'
1068 switch (type) {
Damien Georgec322c5f2014-04-02 20:04:15 +01001069#if MICROPY_ENABLE_FLOAT
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001070 case 'e':
1071 case 'E':
1072 case 'f':
1073 case 'F':
1074 case 'g':
1075 case 'G':
1076 pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), type, flags, fill, width, precision);
1077 break;
1078
1079 case '%':
1080 flags |= PF_FLAG_ADD_PERCENT;
1081 pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg) * 100.0F, 'f', flags, fill, width, precision);
1082 break;
Damien Georgec322c5f2014-04-02 20:04:15 +01001083#endif
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001084
1085 default:
Damien Georgeea13f402014-04-05 18:32:08 +01001086 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001087 "Unknown format code '%c' for object of type 'float'",
1088 type, mp_obj_get_type_str(arg)));
1089 }
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001090 } else {
Damien George70f33cd2014-04-02 17:06:05 +01001091 // arg doesn't look like a number
1092
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001093 if (align == '=') {
Damien Georgeea13f402014-04-05 18:32:08 +01001094 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "'=' alignment not allowed in string format specifier"));
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001095 }
Damien George70f33cd2014-04-02 17:06:05 +01001096
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001097 switch (type) {
1098 case '\0':
1099 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, arg, PRINT_STR);
1100 break;
1101
1102 case 's':
1103 {
1104 uint len;
1105 const char *s = mp_obj_str_get_data(arg, &len);
1106 if (precision < 0) {
1107 precision = len;
1108 }
1109 if (len > precision) {
1110 len = precision;
1111 }
1112 pfenv_print_strn(&pfenv_vstr, s, len, flags, fill, width);
1113 break;
1114 }
1115
1116 default:
Damien Georgeea13f402014-04-05 18:32:08 +01001117 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Dave Hylandsbaf6f142014-03-30 21:06:50 -07001118 "Unknown format code '%c' for object of type 'str'",
1119 type, mp_obj_get_type_str(arg)));
1120 }
Damiend99b0522013-12-21 18:17:45 +00001121 }
1122 }
1123
Damien George2617eeb2014-05-25 22:27:57 +01001124 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +00001125 vstr_free(vstr);
1126 return s;
Damiend99b0522013-12-21 18:17:45 +00001127}
1128
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001129STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args) {
1130 assert(MP_OBJ_IS_STR(pattern));
1131
1132 GET_STR_DATA_LEN(pattern, str, len);
Dave Hylands6756a372014-04-02 11:42:39 -07001133 const byte *start_str = str;
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001134 int arg_i = 0;
1135 vstr_t *vstr = vstr_new();
Dave Hylands6756a372014-04-02 11:42:39 -07001136 pfenv_t pfenv_vstr;
1137 pfenv_vstr.data = vstr;
1138 pfenv_vstr.print_strn = pfenv_vstr_add_strn;
1139
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001140 for (const byte *top = str + len; str < top; str++) {
Dave Hylands6756a372014-04-02 11:42:39 -07001141 if (*str != '%') {
1142 vstr_add_char(vstr, *str);
1143 continue;
1144 }
1145 if (++str >= top) {
1146 break;
1147 }
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001148 if (*str == '%') {
Dave Hylands6756a372014-04-02 11:42:39 -07001149 vstr_add_char(vstr, '%');
1150 continue;
1151 }
1152 if (arg_i >= n_args) {
Damien Georgeea13f402014-04-05 18:32:08 +01001153 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not enough arguments for format string"));
Dave Hylands6756a372014-04-02 11:42:39 -07001154 }
1155 int flags = 0;
1156 char fill = ' ';
1157 bool alt = false;
1158 while (str < top) {
1159 if (*str == '-') flags |= PF_FLAG_LEFT_ADJUST;
1160 else if (*str == '+') flags |= PF_FLAG_SHOW_SIGN;
1161 else if (*str == ' ') flags |= PF_FLAG_SPACE_SIGN;
1162 else if (*str == '#') alt = true;
1163 else if (*str == '0') {
1164 flags |= PF_FLAG_PAD_AFTER_SIGN;
1165 fill = '0';
1166 } else break;
1167 str++;
1168 }
1169 // parse width, if it exists
1170 int width = 0;
1171 if (str < top) {
1172 if (*str == '*') {
1173 width = mp_obj_get_int(args[arg_i++]);
1174 str++;
1175 } else {
1176 for (; str < top && '0' <= *str && *str <= '9'; str++) {
1177 width = width * 10 + *str - '0';
1178 }
1179 }
1180 }
1181 int prec = -1;
1182 if (str < top && *str == '.') {
1183 if (++str < top) {
1184 if (*str == '*') {
1185 prec = mp_obj_get_int(args[arg_i++]);
1186 str++;
1187 } else {
1188 prec = 0;
1189 for (; str < top && '0' <= *str && *str <= '9'; str++) {
1190 prec = prec * 10 + *str - '0';
1191 }
1192 }
1193 }
1194 }
1195
1196 if (str >= top) {
Damien Georgeea13f402014-04-05 18:32:08 +01001197 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incomplete format"));
Dave Hylands6756a372014-04-02 11:42:39 -07001198 }
1199 mp_obj_t arg = args[arg_i];
1200 switch (*str) {
1201 case 'c':
1202 if (MP_OBJ_IS_STR(arg)) {
1203 uint len;
1204 const char *s = mp_obj_str_get_data(arg, &len);
1205 if (len != 1) {
Damien Georgeea13f402014-04-05 18:32:08 +01001206 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "%c requires int or char"));
Dave Hylands6756a372014-04-02 11:42:39 -07001207 break;
1208 }
1209 pfenv_print_strn(&pfenv_vstr, s, 1, flags, ' ', width);
1210 break;
1211 }
1212 if (arg_looks_integer(arg)) {
1213 char ch = mp_obj_get_int(arg);
1214 pfenv_print_strn(&pfenv_vstr, &ch, 1, flags, ' ', width);
1215 break;
1216 }
1217#if MICROPY_ENABLE_FLOAT
1218 // This is what CPython reports, so we report the same.
1219 if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damien Georgeea13f402014-04-05 18:32:08 +01001220 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "integer argument expected, got float"));
Dave Hylands6756a372014-04-02 11:42:39 -07001221
1222 }
1223#endif
Damien Georgeea13f402014-04-05 18:32:08 +01001224 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "an integer is required"));
Dave Hylands6756a372014-04-02 11:42:39 -07001225 break;
1226
1227 case 'd':
1228 case 'i':
1229 case 'u':
Damien Georgea12a0f72014-04-08 01:29:53 +01001230 pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 10, 'a', flags, fill, width);
Dave Hylands6756a372014-04-02 11:42:39 -07001231 break;
1232
1233#if MICROPY_ENABLE_FLOAT
1234 case 'e':
1235 case 'E':
1236 case 'f':
1237 case 'F':
1238 case 'g':
1239 case 'G':
1240 pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), *str, flags, fill, width, prec);
1241 break;
1242#endif
1243
1244 case 'o':
1245 if (alt) {
Dave Hylandsc4029e52014-04-07 11:19:51 -07001246 flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER);
Dave Hylands6756a372014-04-02 11:42:39 -07001247 }
Damien Georgea12a0f72014-04-08 01:29:53 +01001248 pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
Dave Hylands6756a372014-04-02 11:42:39 -07001249 break;
1250
1251 case 'r':
1252 case 's':
1253 {
1254 vstr_t *arg_vstr = vstr_new();
1255 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf,
1256 arg_vstr, arg, *str == 'r' ? PRINT_REPR : PRINT_STR);
1257 uint len = vstr_len(arg_vstr);
1258 if (prec < 0) {
1259 prec = len;
1260 }
1261 if (len > prec) {
1262 len = prec;
1263 }
1264 pfenv_print_strn(&pfenv_vstr, vstr_str(arg_vstr), len, flags, ' ', width);
1265 vstr_free(arg_vstr);
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001266 break;
1267 }
Dave Hylands6756a372014-04-02 11:42:39 -07001268
1269 case 'x':
1270 if (alt) {
1271 flags |= PF_FLAG_SHOW_PREFIX;
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001272 }
Damien Georgea12a0f72014-04-08 01:29:53 +01001273 pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'a', flags, fill, width);
Dave Hylands6756a372014-04-02 11:42:39 -07001274 break;
1275
1276 case 'X':
1277 if (alt) {
1278 flags |= PF_FLAG_SHOW_PREFIX;
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001279 }
Damien Georgea12a0f72014-04-08 01:29:53 +01001280 pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'A', flags, fill, width);
Dave Hylands6756a372014-04-02 11:42:39 -07001281 break;
Damien Georgedeed0872014-04-06 11:11:15 +01001282
Dave Hylands6756a372014-04-02 11:42:39 -07001283 default:
Damien Georgeea13f402014-04-05 18:32:08 +01001284 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Dave Hylands6756a372014-04-02 11:42:39 -07001285 "unsupported format character '%c' (0x%x) at index %d",
1286 *str, *str, str - start_str));
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001287 }
Dave Hylands6756a372014-04-02 11:42:39 -07001288 arg_i++;
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001289 }
1290
1291 if (arg_i != n_args) {
Damien Georgeea13f402014-04-05 18:32:08 +01001292 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not all arguments converted during string formatting"));
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001293 }
1294
Damien George2617eeb2014-05-25 22:27:57 +01001295 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Paul Sokolovsky4db727a2014-03-31 21:18:28 +03001296 vstr_free(vstr);
1297 return s;
1298}
1299
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001300STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
xbe480c15a2014-01-30 22:17:30 -08001301 assert(MP_OBJ_IS_STR(args[0]));
xbe480c15a2014-01-30 22:17:30 -08001302
Damien Georgeff715422014-04-07 00:39:13 +01001303 machine_int_t max_rep = -1;
xbe480c15a2014-01-30 22:17:30 -08001304 if (n_args == 4) {
Damien Georgeff715422014-04-07 00:39:13 +01001305 max_rep = mp_obj_get_int(args[3]);
Paul Sokolovsky4e246082014-02-11 15:29:55 +02001306 if (max_rep == 0) {
1307 return args[0];
1308 } else if (max_rep < 0) {
Damien Georgeff715422014-04-07 00:39:13 +01001309 max_rep = -1;
Paul Sokolovsky4e246082014-02-11 15:29:55 +02001310 }
xbe480c15a2014-01-30 22:17:30 -08001311 }
Damien George94f68302014-01-31 23:45:12 +00001312
xbe729be9b2014-04-07 14:46:39 -07001313 // if max_rep is still -1 by this point we will need to do all possible replacements
xbe480c15a2014-01-30 22:17:30 -08001314
Damien Georgeff715422014-04-07 00:39:13 +01001315 // check argument types
1316
1317 if (!MP_OBJ_IS_STR(args[1])) {
1318 bad_implicit_conversion(args[1]);
1319 }
1320
1321 if (!MP_OBJ_IS_STR(args[2])) {
1322 bad_implicit_conversion(args[2]);
1323 }
1324
1325 // extract string data
1326
xbe480c15a2014-01-30 22:17:30 -08001327 GET_STR_DATA_LEN(args[0], str, str_len);
1328 GET_STR_DATA_LEN(args[1], old, old_len);
1329 GET_STR_DATA_LEN(args[2], new, new_len);
Damien George94f68302014-01-31 23:45:12 +00001330
1331 // old won't exist in str if it's longer, so nothing to replace
xbe480c15a2014-01-30 22:17:30 -08001332 if (old_len > str_len) {
Paul Sokolovsky4e246082014-02-11 15:29:55 +02001333 return args[0];
xbe480c15a2014-01-30 22:17:30 -08001334 }
1335
Damien George94f68302014-01-31 23:45:12 +00001336 // data for the replaced string
1337 byte *data = NULL;
1338 mp_obj_t replaced_str = MP_OBJ_NULL;
xbe480c15a2014-01-30 22:17:30 -08001339
Damien George94f68302014-01-31 23:45:12 +00001340 // do 2 passes over the string:
1341 // first pass computes the required length of the replaced string
1342 // second pass does the replacements
1343 for (;;) {
1344 machine_uint_t replaced_str_index = 0;
1345 machine_uint_t num_replacements_done = 0;
1346 const byte *old_occurrence;
1347 const byte *offset_ptr = str;
Damien Georgeff715422014-04-07 00:39:13 +01001348 machine_uint_t str_len_remain = str_len;
1349 if (old_len == 0) {
1350 // if old_str is empty, copy new_str to start of replaced string
1351 // copy the replacement string
1352 if (data != NULL) {
1353 memcpy(data, new, new_len);
1354 }
1355 replaced_str_index += new_len;
1356 num_replacements_done++;
1357 }
1358 while (num_replacements_done != max_rep && str_len_remain > 0 && (old_occurrence = find_subbytes(offset_ptr, str_len_remain, old, old_len, 1)) != NULL) {
1359 if (old_len == 0) {
1360 old_occurrence += 1;
1361 }
Damien George94f68302014-01-31 23:45:12 +00001362 // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence
1363 if (data != NULL) {
1364 memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr);
1365 }
1366 replaced_str_index += old_occurrence - offset_ptr;
1367 // copy the replacement string
1368 if (data != NULL) {
1369 memcpy(data + replaced_str_index, new, new_len);
1370 }
1371 replaced_str_index += new_len;
1372 offset_ptr = old_occurrence + old_len;
Damien Georgeff715422014-04-07 00:39:13 +01001373 str_len_remain = str + str_len - offset_ptr;
Damien George94f68302014-01-31 23:45:12 +00001374 num_replacements_done++;
Damien George94f68302014-01-31 23:45:12 +00001375 }
1376
1377 // copy from just after end of last occurrence of to-be-replaced string to end of old string
1378 if (data != NULL) {
Damien Georgeff715422014-04-07 00:39:13 +01001379 memcpy(data + replaced_str_index, offset_ptr, str_len_remain);
Damien George94f68302014-01-31 23:45:12 +00001380 }
Damien Georgeff715422014-04-07 00:39:13 +01001381 replaced_str_index += str_len_remain;
Damien George94f68302014-01-31 23:45:12 +00001382
1383 if (data == NULL) {
1384 // first pass
1385 if (num_replacements_done == 0) {
1386 // no substr found, return original string
1387 return args[0];
1388 } else {
1389 // substr found, allocate new string
1390 replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data);
Damien Georgeff715422014-04-07 00:39:13 +01001391 assert(data != NULL);
Damien George94f68302014-01-31 23:45:12 +00001392 }
1393 } else {
1394 // second pass, we are done
1395 break;
1396 }
xbe480c15a2014-01-30 22:17:30 -08001397 }
Damien George94f68302014-01-31 23:45:12 +00001398
xbe480c15a2014-01-30 22:17:30 -08001399 return mp_obj_str_builder_end(replaced_str);
1400}
1401
xbe9e1e8cd2014-03-12 22:57:16 -07001402STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
1403 assert(2 <= n_args && n_args <= 4);
1404 assert(MP_OBJ_IS_STR(args[0]));
1405 assert(MP_OBJ_IS_STR(args[1]));
1406
1407 GET_STR_DATA_LEN(args[0], haystack, haystack_len);
1408 GET_STR_DATA_LEN(args[1], needle, needle_len);
1409
Damien George536dde22014-03-13 22:07:55 +00001410 machine_uint_t start = 0;
1411 machine_uint_t end = haystack_len;
xbe9e1e8cd2014-03-12 22:57:16 -07001412 if (n_args >= 3 && args[2] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +00001413 start = mp_get_index(&mp_type_str, haystack_len, args[2], true);
xbe9e1e8cd2014-03-12 22:57:16 -07001414 }
1415 if (n_args >= 4 && args[3] != mp_const_none) {
Damien George3e1a5c12014-03-29 13:43:38 +00001416 end = mp_get_index(&mp_type_str, haystack_len, args[3], true);
xbe9e1e8cd2014-03-12 22:57:16 -07001417 }
1418
Damien George536dde22014-03-13 22:07:55 +00001419 // if needle_len is zero then we count each gap between characters as an occurrence
1420 if (needle_len == 0) {
1421 return MP_OBJ_NEW_SMALL_INT(end - start + 1);
xbe9e1e8cd2014-03-12 22:57:16 -07001422 }
1423
Damien George536dde22014-03-13 22:07:55 +00001424 // count the occurrences
1425 machine_int_t num_occurrences = 0;
xbec5d70ba2014-03-13 00:29:15 -07001426 for (machine_uint_t haystack_index = start; haystack_index + needle_len <= end; haystack_index++) {
1427 if (memcmp(&haystack[haystack_index], needle, needle_len) == 0) {
1428 num_occurrences++;
1429 haystack_index += needle_len - 1;
1430 }
xbe9e1e8cd2014-03-12 22:57:16 -07001431 }
1432
1433 return MP_OBJ_NEW_SMALL_INT(num_occurrences);
1434}
1435
Damien Georgeb035db32014-03-21 20:39:40 +00001436STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t direction) {
Paul Sokolovsky69f3eb22014-05-11 02:44:46 +03001437 if (!is_str_or_bytes(self_in)) {
1438 assert(0);
1439 }
1440 mp_obj_type_t *self_type = mp_obj_get_type(self_in);
1441 if (self_type != mp_obj_get_type(arg)) {
1442 arg_type_mixup();
xbe613a8e32014-03-18 00:06:29 -07001443 }
Damien Georgeb035db32014-03-21 20:39:40 +00001444
xbe613a8e32014-03-18 00:06:29 -07001445 GET_STR_DATA_LEN(self_in, str, str_len);
1446 GET_STR_DATA_LEN(arg, sep, sep_len);
1447
1448 if (sep_len == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +01001449 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
xbe613a8e32014-03-18 00:06:29 -07001450 }
Damien Georgeb035db32014-03-21 20:39:40 +00001451
1452 mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
1453
1454 if (direction > 0) {
1455 result[0] = self_in;
xbe0a6894c2014-03-21 01:12:26 -07001456 } else {
Damien Georgeb035db32014-03-21 20:39:40 +00001457 result[2] = self_in;
xbe0a6894c2014-03-21 01:12:26 -07001458 }
xbe613a8e32014-03-18 00:06:29 -07001459
xbe17a5a832014-03-23 23:31:58 -07001460 const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
1461 if (position_ptr != NULL) {
1462 machine_uint_t position = position_ptr - str;
Damien Georgef600a6a2014-05-25 22:34:34 +01001463 result[0] = mp_obj_new_str_of_type(self_type, str, position);
xbe17a5a832014-03-23 23:31:58 -07001464 result[1] = arg;
Damien Georgef600a6a2014-05-25 22:34:34 +01001465 result[2] = mp_obj_new_str_of_type(self_type, str + position + sep_len, str_len - position - sep_len);
xbe613a8e32014-03-18 00:06:29 -07001466 }
Damien Georgeb035db32014-03-21 20:39:40 +00001467
xbe0a6894c2014-03-21 01:12:26 -07001468 return mp_obj_new_tuple(3, result);
xbe613a8e32014-03-18 00:06:29 -07001469}
1470
Damien Georgeb035db32014-03-21 20:39:40 +00001471STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
1472 return str_partitioner(self_in, arg, 1);
xbe0a6894c2014-03-21 01:12:26 -07001473}
xbe4504ea82014-03-19 00:46:14 -07001474
Damien Georgeb035db32014-03-21 20:39:40 +00001475STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
1476 return str_partitioner(self_in, arg, -1);
xbe4504ea82014-03-19 00:46:14 -07001477}
1478
Paul Sokolovsky69135212014-05-10 19:47:41 +03001479enum { CASE_UPPER, CASE_LOWER };
1480
1481// Supposedly not too critical operations, so optimize for code size
1482STATIC mp_obj_t str_caseconv(int op, mp_obj_t self_in) {
1483 GET_STR_DATA_LEN(self_in, self_data, self_len);
1484 byte *data;
1485 mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(self_in), self_len, &data);
1486 for (int i = 0; i < self_len; i++) {
1487 if (op == CASE_UPPER) {
1488 *data++ = unichar_toupper(*self_data++);
1489 } else {
1490 *data++ = unichar_tolower(*self_data++);
1491 }
1492 }
1493 *data = 0;
1494 return mp_obj_str_builder_end(s);
1495}
1496
1497STATIC mp_obj_t str_lower(mp_obj_t self_in) {
1498 return str_caseconv(CASE_LOWER, self_in);
1499}
1500
1501STATIC mp_obj_t str_upper(mp_obj_t self_in) {
1502 return str_caseconv(CASE_UPPER, self_in);
1503}
1504
Kim Bautersa3f4b832014-05-31 07:30:03 +01001505enum { IS_SPACE, IS_ALPHA, IS_DIGIT, IS_UPPER, IS_LOWER };
1506
1507STATIC mp_obj_t str_uni_istype(int type, mp_obj_t self_in) {
1508 GET_STR_DATA_LEN(self_in, self_data, self_len);
1509
Paul Sokolovskyf69b9d32014-05-31 10:59:34 +03001510 if (self_len == 0) {
1511 return mp_const_false; // default to False for empty str
1512 }
Kim Bautersa3f4b832014-05-31 07:30:03 +01001513
1514 typedef bool (*check_function)(unichar);
1515 check_function f;
1516
1517 if (type != IS_UPPER && type != IS_LOWER) {
1518 switch (type) {
1519 case IS_SPACE: f = &unichar_isspace; break;
1520 case IS_ALPHA: f = &unichar_isalpha; break;
1521 case IS_DIGIT: f = &unichar_isdigit; break;
1522 default:
1523 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unknown type provided for str_uni_istype"));
1524 }
1525
1526 for (int i = 0; i < self_len; i++) {
Paul Sokolovskyf69b9d32014-05-31 10:59:34 +03001527 if (!f(*self_data++)) {
1528 return mp_const_false;
1529 }
Kim Bautersa3f4b832014-05-31 07:30:03 +01001530 }
1531 } else {
1532 switch (type) {
1533 case IS_UPPER: f = &unichar_isupper; break;
1534 case IS_LOWER: f = &unichar_islower; break;
1535 default:
1536 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unknown type provided for str_uni_istype"));
1537 }
1538
1539 bool contains_alpha = false;
1540
1541 for (int i = 0; i < self_len; i++) { // only check alphanumeric characters
1542 if (unichar_isalpha(*self_data++)) {
1543 contains_alpha = true;
Paul Sokolovskyf69b9d32014-05-31 10:59:34 +03001544 if (!f(*(self_data-1))) {
1545 return mp_const_false; // we already incremented
1546 }
Kim Bautersa3f4b832014-05-31 07:30:03 +01001547 }
1548 }
1549
Paul Sokolovskyf69b9d32014-05-31 10:59:34 +03001550 if (!contains_alpha) {
1551 return mp_const_false;
1552 }
Kim Bautersa3f4b832014-05-31 07:30:03 +01001553 }
1554
1555 return mp_const_true;
1556}
1557
1558STATIC mp_obj_t str_isspace(mp_obj_t self_in) {
1559 return str_uni_istype(IS_SPACE, self_in);
1560}
1561
1562STATIC mp_obj_t str_isalpha(mp_obj_t self_in) {
1563 return str_uni_istype(IS_ALPHA, self_in);
1564}
1565
1566STATIC mp_obj_t str_isdigit(mp_obj_t self_in) {
1567 return str_uni_istype(IS_DIGIT, self_in);
1568}
1569
1570STATIC mp_obj_t str_isupper(mp_obj_t self_in) {
1571 return str_uni_istype(IS_UPPER, self_in);
1572}
1573
1574STATIC mp_obj_t str_islower(mp_obj_t self_in) {
1575 return str_uni_istype(IS_LOWER, self_in);
1576}
1577
Paul Sokolovsky73b70272014-04-13 05:28:46 +03001578#if MICROPY_CPYTHON_COMPAT
1579// These methods are superfluous in the presense of str() and bytes()
1580// constructors.
1581// TODO: should accept kwargs too
1582STATIC mp_obj_t bytes_decode(uint n_args, const mp_obj_t *args) {
1583 mp_obj_t new_args[2];
1584 if (n_args == 1) {
1585 new_args[0] = args[0];
1586 new_args[1] = MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8);
1587 args = new_args;
1588 n_args++;
1589 }
1590 return str_make_new(NULL, n_args, 0, args);
1591}
1592
1593// TODO: should accept kwargs too
1594STATIC mp_obj_t str_encode(uint n_args, const mp_obj_t *args) {
1595 mp_obj_t new_args[2];
1596 if (n_args == 1) {
1597 new_args[0] = args[0];
1598 new_args[1] = MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8);
1599 args = new_args;
1600 n_args++;
1601 }
1602 return bytes_make_new(NULL, n_args, 0, args);
1603}
1604#endif
1605
Damien George57a4b4f2014-04-18 22:29:21 +01001606STATIC machine_int_t str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, int flags) {
1607 if (flags == MP_BUFFER_READ) {
Damien George2da98302014-03-09 19:58:18 +00001608 GET_STR_DATA_LEN(self_in, str_data, str_len);
1609 bufinfo->buf = (void*)str_data;
1610 bufinfo->len = str_len;
Damien George57a4b4f2014-04-18 22:29:21 +01001611 bufinfo->typecode = 'b';
Damien George2da98302014-03-09 19:58:18 +00001612 return 0;
1613 } else {
1614 // can't write to a string
1615 bufinfo->buf = NULL;
1616 bufinfo->len = 0;
Damien George57a4b4f2014-04-18 22:29:21 +01001617 bufinfo->typecode = -1;
Damien George2da98302014-03-09 19:58:18 +00001618 return 1;
1619 }
1620}
1621
Paul Sokolovsky73b70272014-04-13 05:28:46 +03001622#if MICROPY_CPYTHON_COMPAT
1623STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj, 1, 3, bytes_decode);
1624STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj, 1, 3, str_encode);
1625#endif
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001626STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
xbe17a5a832014-03-23 23:31:58 -07001627STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj, 2, 4, str_rfind);
xbe3d9a39e2014-04-08 11:42:19 -07001628STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj, 2, 4, str_index);
1629STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj, 2, 4, str_rindex);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001630STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
1631STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
Paul Sokolovsky2a273652014-05-13 08:07:08 +03001632STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
Paul Sokolovskyc18ef2a2014-05-15 21:18:34 +03001633STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith);
Paul Sokolovskyd098c6b2014-05-24 22:46:51 +03001634STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001635STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
Paul Sokolovsky88107842014-04-26 06:20:08 +03001636STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj, 1, 2, str_lstrip);
1637STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj, 1, 2, str_rstrip);
Damien George897fe0c2014-04-15 22:03:55 +01001638STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, mp_obj_str_format);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001639STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
xbe9e1e8cd2014-03-12 22:57:16 -07001640STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
xbe613a8e32014-03-18 00:06:29 -07001641STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
xbe4504ea82014-03-19 00:46:14 -07001642STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
Paul Sokolovsky69135212014-05-10 19:47:41 +03001643STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_lower_obj, str_lower);
1644STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_upper_obj, str_upper);
Kim Bautersa3f4b832014-05-31 07:30:03 +01001645STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_isspace_obj, str_isspace);
1646STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_isalpha_obj, str_isalpha);
1647STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_isdigit_obj, str_isdigit);
1648STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_isupper_obj, str_isupper);
1649STATIC MP_DEFINE_CONST_FUN_OBJ_1(str_islower_obj, str_islower);
Damiend99b0522013-12-21 18:17:45 +00001650
Damien George9b196cd2014-03-26 21:47:19 +00001651STATIC const mp_map_elem_t str_locals_dict_table[] = {
Paul Sokolovsky73b70272014-04-13 05:28:46 +03001652#if MICROPY_CPYTHON_COMPAT
1653 { MP_OBJ_NEW_QSTR(MP_QSTR_decode), (mp_obj_t)&bytes_decode_obj },
1654 { MP_OBJ_NEW_QSTR(MP_QSTR_encode), (mp_obj_t)&str_encode_obj },
1655#endif
Damien George9b196cd2014-03-26 21:47:19 +00001656 { MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
1657 { MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
xbe3d9a39e2014-04-08 11:42:19 -07001658 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&str_index_obj },
1659 { MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj },
Damien George9b196cd2014-03-26 21:47:19 +00001660 { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
1661 { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
Paul Sokolovsky2a273652014-05-13 08:07:08 +03001662 { MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj },
Damien George9b196cd2014-03-26 21:47:19 +00001663 { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
Paul Sokolovskyd098c6b2014-05-24 22:46:51 +03001664 { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj },
Damien George9b196cd2014-03-26 21:47:19 +00001665 { MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
Paul Sokolovsky88107842014-04-26 06:20:08 +03001666 { MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj },
1667 { MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj },
Damien George9b196cd2014-03-26 21:47:19 +00001668 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
1669 { MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
1670 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
1671 { MP_OBJ_NEW_QSTR(MP_QSTR_partition), (mp_obj_t)&str_partition_obj },
1672 { MP_OBJ_NEW_QSTR(MP_QSTR_rpartition), (mp_obj_t)&str_rpartition_obj },
Paul Sokolovsky69135212014-05-10 19:47:41 +03001673 { MP_OBJ_NEW_QSTR(MP_QSTR_lower), (mp_obj_t)&str_lower_obj },
1674 { MP_OBJ_NEW_QSTR(MP_QSTR_upper), (mp_obj_t)&str_upper_obj },
Kim Bautersa3f4b832014-05-31 07:30:03 +01001675 { MP_OBJ_NEW_QSTR(MP_QSTR_isspace), (mp_obj_t)&str_isspace_obj },
1676 { MP_OBJ_NEW_QSTR(MP_QSTR_isalpha), (mp_obj_t)&str_isalpha_obj },
1677 { MP_OBJ_NEW_QSTR(MP_QSTR_isdigit), (mp_obj_t)&str_isdigit_obj },
1678 { MP_OBJ_NEW_QSTR(MP_QSTR_isupper), (mp_obj_t)&str_isupper_obj },
1679 { MP_OBJ_NEW_QSTR(MP_QSTR_islower), (mp_obj_t)&str_islower_obj },
ian-v7a16fad2014-01-06 09:52:29 -08001680};
Damien George97209d32014-01-07 15:58:30 +00001681
Damien George9b196cd2014-03-26 21:47:19 +00001682STATIC MP_DEFINE_CONST_DICT(str_locals_dict, str_locals_dict_table);
1683
Damien George3e1a5c12014-03-29 13:43:38 +00001684const mp_obj_type_t mp_type_str = {
Damien Georgec5966122014-02-15 16:10:44 +00001685 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +00001686 .name = MP_QSTR_str,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +02001687 .print = str_print,
Paul Sokolovskybe020c22014-03-21 11:39:01 +02001688 .make_new = str_make_new,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +02001689 .binary_op = str_binary_op,
Damien George729f7b42014-04-17 22:10:53 +01001690 .subscr = str_subscr,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001691 .getiter = mp_obj_new_str_iterator,
Damien George2da98302014-03-09 19:58:18 +00001692 .buffer_p = { .get_buffer = str_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +00001693 .locals_dict = (mp_obj_t)&str_locals_dict,
Damiend99b0522013-12-21 18:17:45 +00001694};
1695
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001696// Reuses most of methods from str
Damien George3e1a5c12014-03-29 13:43:38 +00001697const mp_obj_type_t mp_type_bytes = {
Damien Georgec5966122014-02-15 16:10:44 +00001698 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +00001699 .name = MP_QSTR_bytes,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001700 .print = str_print,
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +02001701 .make_new = bytes_make_new,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001702 .binary_op = str_binary_op,
Damien George729f7b42014-04-17 22:10:53 +01001703 .subscr = str_subscr,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001704 .getiter = mp_obj_new_bytes_iterator,
Paul Sokolovsky7a70a3a2014-04-08 17:30:47 +03001705 .buffer_p = { .get_buffer = str_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +00001706 .locals_dict = (mp_obj_t)&str_locals_dict,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001707};
1708
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +02001709// the zero-length bytes
Damien George3e1a5c12014-03-29 13:43:38 +00001710STATIC const mp_obj_str_t empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +02001711const mp_obj_t mp_const_empty_bytes = (mp_obj_t)&empty_bytes_obj;
1712
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001713mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +02001714 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001715 o->base.type = type;
Damien George5fa93b62014-01-22 14:35:10 +00001716 o->len = len;
Paul Sokolovsky504e2332014-04-19 03:09:17 +03001717 o->hash = 0;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +02001718 byte *p = m_new(byte, len + 1);
1719 o->data = p;
1720 *data = p;
Damiend99b0522013-12-21 18:17:45 +00001721 return o;
1722}
1723
Damien George5fa93b62014-01-22 14:35:10 +00001724mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001725 mp_obj_str_t *o = o_in;
1726 o->hash = qstr_compute_hash(o->data, o->len);
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +02001727 byte *p = (byte*)o->data;
1728 p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
Damien George5fa93b62014-01-22 14:35:10 +00001729 return o;
1730}
1731
Damien Georgef600a6a2014-05-25 22:34:34 +01001732mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uint len) {
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +02001733 mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001734 o->base.type = type;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001735 o->len = len;
Paul Sokolovsky5972b4c2014-03-20 16:47:44 +02001736 if (data) {
1737 o->hash = qstr_compute_hash(data, len);
1738 byte *p = m_new(byte, len + 1);
1739 o->data = p;
1740 memcpy(p, data, len * sizeof(byte));
1741 p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
1742 }
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001743 return o;
1744}
1745
Damien George2617eeb2014-05-25 22:27:57 +01001746mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already) {
Damien Georgef600a6a2014-05-25 22:34:34 +01001747 if (make_qstr_if_not_already) {
1748 // use existing, or make a new qstr
Damien George2617eeb2014-05-25 22:27:57 +01001749 return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
Damien George5fa93b62014-01-22 14:35:10 +00001750 } else {
Damien Georgef600a6a2014-05-25 22:34:34 +01001751 qstr q = qstr_find_strn(data, len);
1752 if (q != MP_QSTR_NULL) {
1753 // qstr with this data already exists
1754 return MP_OBJ_NEW_QSTR(q);
1755 } else {
1756 // no existing qstr, don't make one
1757 return mp_obj_new_str_of_type(&mp_type_str, (const byte*)data, len);
1758 }
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +02001759 }
Damien George5fa93b62014-01-22 14:35:10 +00001760}
1761
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001762mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
Damien Georgef600a6a2014-05-25 22:34:34 +01001763 return mp_obj_new_str_of_type(&mp_type_bytes, data, len);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001764}
1765
Damien George5fa93b62014-01-22 14:35:10 +00001766bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
1767 if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
1768 return s1 == s2;
1769 } else {
1770 GET_STR_HASH(s1, h1);
1771 GET_STR_HASH(s2, h2);
Paul Sokolovsky59e269c2014-04-14 01:43:01 +03001772 // If any of hashes is 0, it means it's not valid
1773 if (h1 != 0 && h2 != 0 && h1 != h2) {
Damien George5fa93b62014-01-22 14:35:10 +00001774 return false;
1775 }
1776 GET_STR_DATA_LEN(s1, d1, l1);
1777 GET_STR_DATA_LEN(s2, d2, l2);
1778 if (l1 != l2) {
1779 return false;
1780 }
Damien George1e708fe2014-01-23 18:27:51 +00001781 return memcmp(d1, d2, l1) == 0;
Paul Sokolovsky8965a5e2014-01-20 23:33:19 +02001782 }
Damien George5fa93b62014-01-22 14:35:10 +00001783}
1784
Damien Georgedeed0872014-04-06 11:11:15 +01001785STATIC void bad_implicit_conversion(mp_obj_t self_in) {
Damien Georgeea13f402014-04-05 18:32:08 +01001786 nlr_raise(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 +00001787}
1788
Paul Sokolovsky69f3eb22014-05-11 02:44:46 +03001789STATIC void arg_type_mixup() {
1790 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Can't mix str and bytes arguments"));
1791}
1792
Damien George5fa93b62014-01-22 14:35:10 +00001793uint mp_obj_str_get_hash(mp_obj_t self_in) {
Paul Sokolovskyf130ca12014-04-13 05:41:00 +03001794 // TODO: This has too big overhead for hash accessor
1795 if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) {
Damien George5fa93b62014-01-22 14:35:10 +00001796 GET_STR_HASH(self_in, h);
1797 return h;
1798 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +00001799 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +00001800 }
1801}
1802
1803uint mp_obj_str_get_len(mp_obj_t self_in) {
Damien Georgeee014112014-04-15 23:10:00 +01001804 // TODO This has a double check for the type, one in obj.c and one here
1805 if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) {
Damien George5fa93b62014-01-22 14:35:10 +00001806 GET_STR_LEN(self_in, l);
1807 return l;
1808 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +00001809 bad_implicit_conversion(self_in);
1810 }
1811}
1812
1813// use this if you will anyway convert the string to a qstr
1814// will be more efficient for the case where it's already a qstr
1815qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
1816 if (MP_OBJ_IS_QSTR(self_in)) {
1817 return MP_OBJ_QSTR_VALUE(self_in);
Damien George3e1a5c12014-03-29 13:43:38 +00001818 } else if (MP_OBJ_IS_TYPE(self_in, &mp_type_str)) {
Damien Georgeb829b5c2014-01-25 13:51:19 +00001819 mp_obj_str_t *self = self_in;
1820 return qstr_from_strn((char*)self->data, self->len);
1821 } else {
1822 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +00001823 }
1824}
1825
1826// only use this function if you need the str data to be zero terminated
1827// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
1828const char *mp_obj_str_get_str(mp_obj_t self_in) {
1829 if (MP_OBJ_IS_STR(self_in)) {
1830 GET_STR_DATA_LEN(self_in, s, l);
1831 (void)l; // len unused
1832 return (const char*)s;
1833 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +00001834 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +00001835 }
1836}
1837
Damien George698ec212014-02-08 18:17:23 +00001838const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
Paul Sokolovskyeea01182014-05-11 13:51:24 +03001839 if (is_str_or_bytes(self_in)) {
Damien George5fa93b62014-01-22 14:35:10 +00001840 GET_STR_DATA_LEN(self_in, s, l);
1841 *len = l;
Damien George698ec212014-02-08 18:17:23 +00001842 return (const char*)s;
Damien George5fa93b62014-01-22 14:35:10 +00001843 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +00001844 bad_implicit_conversion(self_in);
Damien George5fa93b62014-01-22 14:35:10 +00001845 }
Damiend99b0522013-12-21 18:17:45 +00001846}
xyb8cfc9f02014-01-05 18:47:51 +08001847
1848/******************************************************************************/
1849/* str iterator */
1850
1851typedef struct _mp_obj_str_it_t {
1852 mp_obj_base_t base;
Damien George5fa93b62014-01-22 14:35:10 +00001853 mp_obj_t str;
xyb8cfc9f02014-01-05 18:47:51 +08001854 machine_uint_t cur;
1855} mp_obj_str_it_t;
1856
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001857STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
xyb8cfc9f02014-01-05 18:47:51 +08001858 mp_obj_str_it_t *self = self_in;
Damien George5fa93b62014-01-22 14:35:10 +00001859 GET_STR_DATA_LEN(self->str, str, len);
1860 if (self->cur < len) {
Damien George2617eeb2014-05-25 22:27:57 +01001861 mp_obj_t o_out = mp_obj_new_str((const char*)str + self->cur, 1, true);
xyb8cfc9f02014-01-05 18:47:51 +08001862 self->cur += 1;
1863 return o_out;
1864 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +01001865 return MP_OBJ_STOP_ITERATION;
xyb8cfc9f02014-01-05 18:47:51 +08001866 }
1867}
1868
Damien George3e1a5c12014-03-29 13:43:38 +00001869STATIC const mp_obj_type_t mp_type_str_it = {
Damien Georgec5966122014-02-15 16:10:44 +00001870 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +00001871 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +03001872 .getiter = mp_identity,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +02001873 .iternext = str_it_iternext,
xyb8cfc9f02014-01-05 18:47:51 +08001874};
1875
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +02001876STATIC mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001877 mp_obj_str_it_t *self = self_in;
1878 GET_STR_DATA_LEN(self->str, str, len);
1879 if (self->cur < len) {
Damien George7c9c6672014-01-25 00:17:36 +00001880 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT((mp_small_int_t)str[self->cur]);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001881 self->cur += 1;
1882 return o_out;
1883 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +01001884 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001885 }
1886}
1887
Damien George3e1a5c12014-03-29 13:43:38 +00001888STATIC const mp_obj_type_t mp_type_bytes_it = {
Damien Georgec5966122014-02-15 16:10:44 +00001889 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +00001890 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +03001891 .getiter = mp_identity,
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001892 .iternext = bytes_it_iternext,
1893};
1894
1895mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
xyb8cfc9f02014-01-05 18:47:51 +08001896 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +00001897 o->base.type = &mp_type_str_it;
xyb8cfc9f02014-01-05 18:47:51 +08001898 o->str = str;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001899 o->cur = 0;
1900 return o;
1901}
1902
1903mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
1904 mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +00001905 o->base.type = &mp_type_bytes_it;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +02001906 o->str = str;
1907 o->cur = 0;
xyb8cfc9f02014-01-05 18:47:51 +08001908 return o;
1909}