summaryrefslogtreecommitdiffstatshomepage
path: root/py/objstr.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 33bfcc3756..3a4b0b97f3 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -332,6 +333,7 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
+ mp_obj_type_t *type = mp_obj_get_type(self_in);
GET_STR_DATA_LEN(self_in, self_data, self_len);
if (value == MP_OBJ_SENTINEL) {
// load
@@ -341,10 +343,9 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (!mp_seq_get_fast_slice_indexes(self_len, index, &start, &stop)) {
assert(0);
}
- return mp_obj_new_str(self_data + start, stop - start, false);
+ return str_new(type, self_data + start, stop - start);
}
#endif
- mp_obj_type_t *type = mp_obj_get_type(self_in);
uint index_val = mp_get_index(type, self_len, index, false);
if (type == &mp_type_bytes) {
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);
@@ -357,7 +358,8 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
- assert(MP_OBJ_IS_STR(self_in));
+ assert(is_str_or_bytes(self_in));
+ const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
// get separation string
GET_STR_DATA_LEN(self_in, sep_str, sep_len);
@@ -379,8 +381,9 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
// count required length
int required_len = 0;
for (int i = 0; i < seq_len; i++) {
- if (!MP_OBJ_IS_STR(seq_items[i])) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "join expected a list of str's"));
+ if (mp_obj_get_type(seq_items[i]) != self_type) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "join expects a list of str/bytes objects consistent with self object"));
}
if (i > 0) {
required_len += sep_len;
@@ -391,7 +394,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
// make joined string
byte *data;
- mp_obj_t joined_str = mp_obj_str_builder_start(mp_obj_get_type(self_in), required_len, &data);
+ mp_obj_t joined_str = mp_obj_str_builder_start(self_type, required_len, &data);
for (int i = 0; i < seq_len; i++) {
if (i > 0) {
memcpy(data, sep_str, sep_len);
@@ -409,6 +412,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
#define is_ws(c) ((c) == ' ' || (c) == '\t')
STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
+ const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
machine_int_t splits = -1;
mp_obj_t sep = mp_const_none;
if (n_args > 1) {
@@ -430,7 +434,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
while (s < top && splits != 0) {
const byte *start = s;
while (s < top && !is_ws(*s)) s++;
- mp_obj_list_append(res, mp_obj_new_str(start, s - start, false));
+ mp_obj_list_append(res, str_new(self_type, start, s - start));
if (s >= top) {
break;
}
@@ -441,7 +445,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
}
if (s < top) {
- mp_obj_list_append(res, mp_obj_new_str(s, top - s, false));
+ mp_obj_list_append(res, str_new(self_type, s, top - s));
}
} else {
@@ -465,7 +469,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
}
s++;
}
- mp_obj_list_append(res, mp_obj_new_str(start, s - start, false));
+ mp_obj_list_append(res, str_new(self_type, start, s - start));
if (s >= top) {
break;
}