diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2025-01-08 22:20:43 +0100 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2025-01-08 22:20:43 +0100 |
| commit | 16e3abfe82912c1ef47008261020715d425b0dfb (patch) | |
| tree | a0126f62807186e0200d5cf1620f6463ca03a64d /core/strings | |
| parent | 2620721128b2835826faa5d224bf9373a92b1998 (diff) | |
strings: use map_entry
Diffstat (limited to 'core/strings')
| -rw-r--r-- | core/strings/intern.odin | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/core/strings/intern.odin b/core/strings/intern.odin index 4c270980c..0b8ed173e 100644 --- a/core/strings/intern.odin +++ b/core/strings/intern.odin @@ -89,6 +89,7 @@ intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runt entry := _intern_get_entry(m, text) or_return return cstring(&entry.str[0]), nil } + /* Internal function to lookup whether the text string exists in the map, returns the entry Sets and allocates the entry if it wasn't set yet @@ -104,13 +105,15 @@ Returns: - err: An allocator error if one occured, `nil` otherwise */ _intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check { - if prev, ok := m.entries[text]; ok { - return prev, nil - } if m.allocator.procedure == nil { m.allocator = context.allocator } + key_ptr, val_ptr, inserted := map_entry(&m.entries, text) or_return + if !inserted { + return val_ptr^, nil + } + entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1 bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return new_entry = (^Intern_Entry)(raw_data(bytes)) @@ -120,6 +123,9 @@ _intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry new_entry.str[new_entry.len] = 0 key := string(new_entry.str[:new_entry.len]) - m.entries[key] = new_entry - return new_entry, nil + + key_ptr^ = key + val_ptr^ = new_entry + + return } |