mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-14 18:45:50 +00:00
Implement a new StringInterner
This commit is contained in:
@@ -366,6 +366,7 @@ gb_global bool global_module_path_set = false;
|
||||
#include "string_set.cpp"
|
||||
#include "priority_queue.cpp"
|
||||
#include "thread_pool.cpp"
|
||||
#include "string_interner.cpp"
|
||||
|
||||
|
||||
gb_internal String obfuscate_string(String const &s, char const *prefix) {
|
||||
@@ -388,48 +389,6 @@ gb_internal i32 obfuscate_i32(i32 i) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct StringIntern {
|
||||
StringIntern *next;
|
||||
isize len;
|
||||
char str[1];
|
||||
};
|
||||
|
||||
PtrMap<uintptr, StringIntern *> string_intern_map = {}; // Key: u64
|
||||
gb_global Arena string_intern_arena = {};
|
||||
|
||||
gb_internal char const *string_intern(char const *text, isize len) {
|
||||
u64 hash = gb_fnv64a(text, len);
|
||||
uintptr key = cast(uintptr)(hash ? hash : 1);
|
||||
StringIntern **found = map_get(&string_intern_map, key);
|
||||
if (found) {
|
||||
for (StringIntern *it = *found; it != nullptr; it = it->next) {
|
||||
if (it->len == len && gb_strncmp(it->str, (char *)text, len) == 0) {
|
||||
return it->str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringIntern *new_intern = cast(StringIntern *)arena_alloc(&string_intern_arena, gb_offset_of(StringIntern, str) + len + 1, gb_align_of(StringIntern));
|
||||
new_intern->len = len;
|
||||
new_intern->next = found ? *found : nullptr;
|
||||
gb_memmove(new_intern->str, text, len);
|
||||
new_intern->str[len] = 0;
|
||||
map_set(&string_intern_map, key, new_intern);
|
||||
return new_intern->str;
|
||||
}
|
||||
|
||||
gb_internal char const *string_intern(String const &string) {
|
||||
return string_intern(cast(char const *)string.text, string.len);
|
||||
}
|
||||
|
||||
gb_internal void init_string_interner(void) {
|
||||
map_init(&string_intern_map);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
gb_internal i32 next_pow2(i32 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user