STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) { // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))." const char *fmt = mp_obj_str_get_str(fmt_in); char fmt_type = get_fmt_type(&fmt); uint size = calcsize_items(fmt); mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL); mp_buffer_info_t bufinfo; mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); byte *p = bufinfo.buf; for (uint i = 0; i < size; i++) { machine_uint_t sz = 1; if (unichar_isdigit(*fmt)) { sz = get_fmt_num(&fmt); } if (sz > 1) { // TODO: size spec support only for string len assert(*fmt == 's'); } mp_obj_t item; if (*fmt == 's') { item = mp_obj_new_bytes(p, sz); p += sz; fmt++; } else { item = mp_binary_get_val(fmt_type, *fmt++, &p); } res->items[i] = item; } return res; }
mp_obj_t array_it_iternext(mp_obj_t self_in) { mp_obj_array_it_t *self = self_in; if (self->cur < self->array->len) { return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++); } else { return mp_const_stop_iteration; } }
STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_array_t *o = lhs; switch (op) { case RT_BINARY_OP_SUBSCR: { uint index = mp_get_index(o->base.type, o->len, rhs); return mp_binary_get_val(o->typecode, o->items, index); } default: // op not supported return MP_OBJ_NULL; } }
STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_array_t *o = o_in; if (o->typecode == BYTEARRAY_TYPECODE) { print(env, "bytearray(b", o->typecode); mp_str_print_quoted(print, env, o->items, o->len); } else { print(env, "array('%c'", o->typecode); if (o->len > 0) { print(env, ", [", o->typecode); for (int i = 0; i < o->len; i++) { if (i > 0) { print(env, ", "); } mp_obj_print_helper(print, env, mp_binary_get_val(o->typecode, o->items, i), PRINT_REPR); } print(env, "]"); } } print(env, ")"); }
STATIC mp_obj_t ffivar_get(mp_obj_t self_in) { mp_obj_ffivar_t *self = self_in; return mp_binary_get_val(self->type, self->var, 0); }