modstruct: Basic implementation of native struct alignment and types.

This commit is contained in:
Paul Sokolovsky
2014-04-11 03:47:21 +03:00
parent ef9124f5ff
commit 0c43cf9154
4 changed files with 46 additions and 15 deletions

View File

@@ -37,7 +37,7 @@ STATIC uint calcsize_items(const char *fmt) {
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
const char *fmt = mp_obj_str_get_str(fmt_in);
char fmt_type = get_fmt_type(&fmt);
assert(fmt_type == '<' || fmt_type == '>'); (void)fmt_type;
(void)fmt_type;
machine_uint_t size;
for (size = 0; *fmt; fmt++) {
int sz = mp_binary_get_size(*fmt);
@@ -53,7 +53,6 @@ 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);
assert(fmt_type == '<' || fmt_type == '>'); (void)fmt_type;
uint size = calcsize_items(fmt);
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
buffer_info_t bufinfo;
@@ -61,7 +60,7 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
byte *p = bufinfo.buf;
for (uint i = 0; i < size; i++) {
mp_obj_t item = mp_binary_get_val_unaligned(*fmt++, &p);
mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
res->items[i] = item;
}
return res;