# HG changeset patch # User Michael Pavone # Date 1661646928 25200 # Node ID de3d20e585556ededbe2b667bc5611b429db347d # Parent 4c265d2f6c880d35e10387f9ca18e0501c8e53e3 Mostly fix label sorting post-refactor diff -r 4c265d2f6c88 -r de3d20e58555 disasm.c --- a/disasm.c Sat Aug 27 17:07:57 2022 -0700 +++ b/disasm.c Sat Aug 27 17:35:28 2022 -0700 @@ -3,14 +3,14 @@ label_def *find_label(disasm_context *context, uint32_t address) { char key[MAX_INT_KEY_SIZE]; - tern_int_key(address & context->address_mask, key); + tern_sortable_int_key(address & context->address_mask, key); return tern_find_ptr(context->labels, key); } label_def *add_find_label(disasm_context *context, uint32_t address) { char key[MAX_INT_KEY_SIZE]; - tern_int_key(address & context->address_mask, key); + tern_sortable_int_key(address & context->address_mask, key); label_def *def = tern_find_ptr(context->labels, key); if (!def) { diff -r 4c265d2f6c88 -r de3d20e58555 tern.c --- a/tern.c Sat Aug 27 17:07:57 2022 -0700 +++ b/tern.c Sat Aug 27 17:35:28 2022 -0700 @@ -293,13 +293,33 @@ char * tern_int_key(uint32_t key, char * buf) { - char * cur = buf; + + int len = 0; + uint32_t tmp = key; + while (tmp) + { + tmp >>= 7; + ++len; + } + buf[len] = 0; + char * cur = buf + len - 1; while (key) { - *(cur++) = (key & 0x7F) + 1; + *(cur--) = (key & 0x7F) + 1; key >>= 7; } - *cur = 0; + return buf; +} + +char * tern_sortable_int_key(uint32_t key, char * buf) +{ + buf[MAX_INT_KEY_SIZE - 1] = 0; + char * cur = buf + MAX_INT_KEY_SIZE - 2; + while (cur >= buf) + { + *(cur--) = (key & 0x7F) + 1; + key >>= 7; + } return buf; } diff -r 4c265d2f6c88 -r de3d20e58555 tern.h --- a/tern.h Sat Aug 27 17:07:57 2022 -0700 +++ b/tern.h Sat Aug 27 17:35:28 2022 -0700 @@ -53,6 +53,7 @@ uint32_t tern_count(tern_node *head); void tern_foreach(tern_node *head, iter_fun fun, void *data); char * tern_int_key(uint32_t key, char * buf); +char * tern_sortable_int_key(uint32_t key, char * buf); void tern_free(tern_node *head); #endif //TERN_H_