summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/compile.c7
-rw-r--r--py/misc.h1
-rw-r--r--py/modstruct.c8
-rw-r--r--py/obj.h3
-rw-r--r--py/objint.c7
-rw-r--r--py/objstr.c79
-rw-r--r--py/stream.c19
-rw-r--r--py/vstr.c5
8 files changed, 55 insertions, 74 deletions
diff --git a/py/compile.c b/py/compile.c
index ea0782e3c2..85f5dab835 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -2624,8 +2624,9 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
// concatenate string/bytes
- byte *s_dest;
- mp_obj_t obj = mp_obj_str_builder_start(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, n_bytes, &s_dest);
+ vstr_t vstr;
+ vstr_init_len(&vstr, n_bytes);
+ byte *s_dest = (byte*)vstr.buf;
for (int i = 0; i < n; i++) {
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
mp_uint_t s_len;
@@ -2640,7 +2641,7 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
// load the object
- EMIT_ARG(load_const_obj, mp_obj_str_builder_end(obj));
+ EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
}
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
diff --git a/py/misc.h b/py/misc.h
index df017f44ad..b800efa02b 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -124,6 +124,7 @@ typedef struct _vstr_t {
#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
void vstr_init(vstr_t *vstr, size_t alloc);
+void vstr_init_len(vstr_t *vstr, size_t len);
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
void vstr_clear(vstr_t *vstr);
vstr_t *vstr_new(void);
diff --git a/py/modstruct.c b/py/modstruct.c
index 114135dfd1..681c585779 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -161,8 +161,9 @@ STATIC mp_obj_t struct_pack(mp_uint_t n_args, const mp_obj_t *args) {
const char *fmt = mp_obj_str_get_str(args[0]);
char fmt_type = get_fmt_type(&fmt);
mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
- byte *p;
- mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
+ vstr_t vstr;
+ vstr_init_len(&vstr, size);
+ byte *p = (byte*)vstr.buf;
memset(p, 0, size);
for (mp_uint_t i = 1; i < n_args; i++) {
@@ -190,7 +191,8 @@ STATIC mp_obj_t struct_pack(mp_uint_t n_args, const mp_obj_t *args) {
mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
}
}
- return res;
+
+ return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
diff --git a/py/obj.h b/py/obj.h
index d0ea4c1123..2d506858dd 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -500,9 +500,6 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
void mp_init_emergency_exception_buf(void);
// str
-mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, mp_uint_t len, byte **data);
-mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in);
-mp_obj_t mp_obj_str_builder_end_with_len(mp_obj_t o_in, mp_uint_t len);
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in);
mp_uint_t mp_obj_str_get_len(mp_obj_t self_in);
diff --git a/py/objint.c b/py/objint.c
index 049d0410ea..e8d86ad7ec 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -340,8 +340,9 @@ STATIC mp_obj_t int_to_bytes(mp_uint_t n_args, const mp_obj_t *args) {
mp_int_t val = mp_obj_int_get_checked(args[0]);
mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]);
- byte *data;
- mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
+ vstr_t vstr;
+ vstr_init_len(&vstr, len);
+ byte *data = (byte*)vstr.buf;
memset(data, 0, len);
if (MP_ENDIANNESS_LITTLE) {
@@ -353,7 +354,7 @@ STATIC mp_obj_t int_to_bytes(mp_uint_t n_args, const mp_obj_t *args) {
}
}
- return mp_obj_str_builder_end(o);
+ return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 2, 4, int_to_bytes);
diff --git a/py/objstr.c b/py/objstr.c
index b67913e515..f25ec17378 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -200,11 +200,10 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
if (MP_OBJ_IS_SMALL_INT(args[0])) {
uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
- byte *data;
-
- mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
- memset(data, 0, len);
- return mp_obj_str_builder_end(o);
+ vstr_t vstr;
+ vstr_init_len(&vstr, len);
+ memset(vstr.buf, 0, len);
+ return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
// check if argument has the buffer protocol
@@ -302,10 +301,10 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_const_empty_bytes;
}
}
- byte *data;
- mp_obj_t s = mp_obj_str_builder_start(lhs_type, lhs_len * n, &data);
- mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
- return mp_obj_str_builder_end(s);
+ vstr_t vstr;
+ vstr_init_len(&vstr, lhs_len * n);
+ mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, vstr.buf);
+ return mp_obj_new_str_from_vstr(lhs_type, &vstr);
}
// From now on all operations allow:
@@ -344,12 +343,11 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
switch (op) {
case MP_BINARY_OP_ADD:
case MP_BINARY_OP_INPLACE_ADD: {
- mp_uint_t alloc_len = lhs_len + rhs_len;
- byte *data;
- mp_obj_t s = mp_obj_str_builder_start(lhs_type, alloc_len, &data);
- memcpy(data, lhs_data, lhs_len);
- memcpy(data + lhs_len, rhs_data, rhs_len);
- return mp_obj_str_builder_end(s);
+ vstr_t vstr;
+ vstr_init_len(&vstr, lhs_len + rhs_len);
+ memcpy(vstr.buf, lhs_data, lhs_len);
+ memcpy(vstr.buf + lhs_len, rhs_data, rhs_len);
+ return mp_obj_new_str_from_vstr(lhs_type, &vstr);
}
case MP_BINARY_OP_IN:
@@ -441,8 +439,9 @@ 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(self_type, required_len, &data);
+ vstr_t vstr;
+ vstr_init_len(&vstr, required_len);
+ byte *data = (byte*)vstr.buf;
for (mp_uint_t i = 0; i < seq_len; i++) {
if (i > 0) {
memcpy(data, sep_str, sep_len);
@@ -454,7 +453,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
}
// return joined string
- return mp_obj_str_builder_end(joined_str);
+ return mp_obj_new_str_from_vstr(self_type, &vstr);
}
#define is_ws(c) ((c) == ' ' || (c) == '\t')
@@ -1485,7 +1484,7 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
// data for the replaced string
byte *data = NULL;
- mp_obj_t replaced_str = MP_OBJ_NULL;
+ vstr_t vstr;
// do 2 passes over the string:
// first pass computes the required length of the replaced string
@@ -1537,7 +1536,8 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
return args[0];
} else {
// substr found, allocate new string
- replaced_str = mp_obj_str_builder_start(self_type, replaced_str_index, &data);
+ vstr_init_len(&vstr, replaced_str_index);
+ data = (byte*)vstr.buf;
assert(data != NULL);
}
} else {
@@ -1546,7 +1546,7 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
}
}
- return mp_obj_str_builder_end(replaced_str);
+ return mp_obj_new_str_from_vstr(self_type, &vstr);
}
STATIC mp_obj_t str_count(mp_uint_t n_args, const mp_obj_t *args) {
@@ -1643,13 +1643,13 @@ STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
// Supposedly not too critical operations, so optimize for code size
STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) {
GET_STR_DATA_LEN(self_in, self_data, self_len);
- byte *data;
- mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(self_in), self_len, &data);
+ vstr_t vstr;
+ vstr_init_len(&vstr, self_len);
+ byte *data = (byte*)vstr.buf;
for (mp_uint_t i = 0; i < self_len; i++) {
*data++ = op(*self_data++);
}
- *data = 0;
- return mp_obj_str_builder_end(s);
+ return mp_obj_new_str_from_vstr(mp_obj_get_type(self_in), &vstr);
}
STATIC mp_obj_t str_lower(mp_obj_t self_in) {
@@ -1856,35 +1856,6 @@ const mp_obj_type_t mp_type_bytes = {
// the zero-length bytes
const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
-mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, mp_uint_t len, byte **data) {
- mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
- o->base.type = type;
- o->len = len;
- o->hash = 0;
- byte *p = m_new(byte, len + 1);
- o->data = p;
- *data = p;
- return o;
-}
-
-mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
- mp_obj_str_t *o = o_in;
- o->hash = qstr_compute_hash(o->data, o->len);
- byte *p = (byte*)o->data;
- p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
- return o;
-}
-
-mp_obj_t mp_obj_str_builder_end_with_len(mp_obj_t o_in, mp_uint_t len) {
- mp_obj_str_t *o = o_in;
- o->data = m_renew(byte, (byte*)o->data, o->len + 1, len + 1);
- o->len = len;
- o->hash = qstr_compute_hash(o->data, o->len);
- byte *p = (byte*)o->data;
- p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
- return o;
-}
-
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, mp_uint_t len) {
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
o->base.type = type;
diff --git a/py/stream.c b/py/stream.c
index b7d4a90009..3415b74b93 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -160,11 +160,12 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
}
#endif
- byte *buf;
- mp_obj_t ret_obj = mp_obj_str_builder_start(STREAM_CONTENT_TYPE(o->type->stream_p), sz, &buf);
+ vstr_t vstr;
+ vstr_init_len(&vstr, sz);
int error;
- mp_uint_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
+ mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error);
if (out_sz == MP_STREAM_ERROR) {
+ vstr_clear(&vstr);
if (is_nonblocking_error(error)) {
// https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
// "If the object is in non-blocking mode and no bytes are available,
@@ -175,7 +176,9 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
} else {
- return mp_obj_str_builder_end_with_len(ret_obj, out_sz);
+ vstr.len = out_sz;
+ vstr.buf[vstr.len] = '\0';
+ return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
}
}
@@ -252,7 +255,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
vstr_t vstr;
vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
char *p = vstr.buf;
- mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
+ mp_uint_t current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination
while (true) {
int error;
mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
@@ -276,8 +279,8 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
current_read -= out_sz;
p += out_sz;
} else {
- current_read = DEFAULT_BUFFER_SIZE;
- p = vstr_extend(&vstr, current_read);
+ p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
+ current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination
if (p == NULL) {
// TODO
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
@@ -286,7 +289,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
}
vstr.len = total_size;
- vstr.buf[vstr.len] = '\0'; // XXX is there enough space?
+ vstr.buf[vstr.len] = '\0';
return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
}
diff --git a/py/vstr.c b/py/vstr.c
index 6856cfe397..63358d5957 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -52,6 +52,11 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
vstr->fixed_buf = false;
}
+void vstr_init_len(vstr_t *vstr, size_t len) {
+ vstr_init(vstr, len + 1);
+ vstr_add_len(vstr, len);
+}
+
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
assert(alloc > 0); // need at least room for the null byte
vstr->alloc = alloc;