From fbb547e41bf14c3312a705a8f1253128c8de869e Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 11 Apr 2025 14:12:53 +0100 Subject: [PATCH] mie: name_map can now generate unique names when given no hints --- mie/include/mie/name.h | 1 + mie/name.c | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/mie/include/mie/name.h b/mie/include/mie/name.h index 7b99230..6faeba9 100644 --- a/mie/include/mie/name.h +++ b/mie/include/mie/name.h @@ -31,6 +31,7 @@ struct mie_name_bucket { struct mie_name_map { b_btree m_entries; + size_t m_next_id; }; extern struct mie_name_map *mie_name_map_create(void); diff --git a/mie/name.c b/mie/name.c index e8179ac..9439808 100644 --- a/mie/name.c +++ b/mie/name.c @@ -128,27 +128,39 @@ struct mie_name *mie_name_map_put( memset(entry, 0x0, sizeof *entry); entry->n_base.e_type = MIE_NAME_MAP_E_NAME; - b_rope base = B_ROPE_CSTR(hint); + b_rope base = {}; + + if (hint) { + b_rope_init_cstr_borrowed(&base, hint); + } char str[256]; - /* first try just the hint on its own */ - b_rope_to_cstr(&base, str, sizeof str); - entry->n_str = str; - entry->n_base.e_hash = base.r_v.v_cstr.hash; - b_status status = put_name(map, entry); + if (hint) { + /* first try just the hint on its own */ + b_rope_to_cstr(&base, str, sizeof str); + entry->n_str = str; + entry->n_base.e_hash = base.r_v.v_cstr.hash; + b_status status = put_name(map, entry); - if (B_OK(status)) { - entry->n_str = b_strdup(str); - return entry; + if (B_OK(status)) { + entry->n_str = b_strdup(str); + return entry; + } } - /* that name already exists, use a suffix to make the name unique. */ + /* that name already exists, use a suffix to make the name unique. + * alternately, no hint was specified, so it's up to us to generate the name */ b_rope suffix = B_ROPE_UINT(0); b_rope unique_name; b_rope_concat(&unique_name, &base, &suffix); - for (unsigned int i = 0;; i++) { + size_t i = 0; + if (!hint || hint[0] == '\0') { + i = map->m_next_id++; + } + + for (;; i++) { suffix.r_v.v_uint = i; b_rope_to_cstr(&unique_name, str, sizeof str);