Skip to content

Commit 77089be

Browse files
committed
py: Add comments for vstr_init and mp_obj_new_str.
1 parent 05005f6 commit 77089be

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,8 @@ const mp_obj_type_t mp_type_bytes = {
18561856
// the zero-length bytes
18571857
const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
18581858

1859+
// Create a str/bytes object using the given data. New memory is allocated and
1860+
// the data is copied across.
18591861
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, mp_uint_t len) {
18601862
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
18611863
o->base.type = type;
@@ -1870,6 +1872,9 @@ mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, mp_
18701872
return o;
18711873
}
18721874

1875+
// Create a str/bytes object from the given vstr. The vstr buffer is resized to
1876+
// the exact length required and then reused for the str/bytes object. The vstr
1877+
// is cleared and can safely be passed to vstr_free if it was heap allocated.
18731878
mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) {
18741879
// if not a bytes object, look if a qstr with this data already exists
18751880
if (type == &mp_type_str) {

py/vstr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
// returned value is always at least 1 greater than argument
3636
#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
3737

38+
// Init the vstr so it allocs exactly given number of bytes.
39+
// Length is set to zero, and null byte written in first position.
3840
void vstr_init(vstr_t *vstr, size_t alloc) {
3941
if (alloc < 2) {
4042
// need at least 1 byte for the null byte at the end
@@ -52,6 +54,8 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
5254
vstr->fixed_buf = false;
5355
}
5456

57+
// Init the vstr so it allocs exactly enough ram to hold given length (plus the
58+
// null terminating byte), set the length, and write the null byte at the end.
5559
void vstr_init_len(vstr_t *vstr, size_t len) {
5660
vstr_init(vstr, len + 1);
5761
vstr_add_len(vstr, len);

0 commit comments

Comments
 (0)