intern.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package strings
  2. import "core:runtime"
  3. // Custom string entry struct
  4. Intern_Entry :: struct {
  5. len: int,
  6. str: [1]byte, // string is allocated inline with the entry to keep allocations simple
  7. }
  8. /*
  9. Intern is a more memory efficient string map
  10. Uses Specified Allocator for `Intern_Entry` strings
  11. Fields:
  12. - allocator: The allocator used for the Intern_Entry strings
  13. - entries: A map of strings to interned string entries
  14. */
  15. Intern :: struct {
  16. allocator: runtime.Allocator,
  17. entries: map[string]^Intern_Entry,
  18. }
  19. /*
  20. Initializes the entries map and sets the allocator for the string entries
  21. *Allocates Using Provided Allocators*
  22. Inputs:
  23. - m: A pointer to the Intern struct to be initialized
  24. - allocator: The allocator for the Intern_Entry strings (Default: context.allocator)
  25. - map_allocator: The allocator for the map of entries (Default: context.allocator)
  26. */
  27. intern_init :: proc(m: ^Intern, allocator := context.allocator, map_allocator := context.allocator) {
  28. m.allocator = allocator
  29. m.entries = make(map[string]^Intern_Entry, 16, map_allocator)
  30. }
  31. /*
  32. Frees the map and all its content allocated using the `.allocator`.
  33. Inputs:
  34. - m: A pointer to the Intern struct to be destroyed
  35. */
  36. intern_destroy :: proc(m: ^Intern) {
  37. for _, value in m.entries {
  38. free(value, m.allocator)
  39. }
  40. delete(m.entries)
  41. }
  42. /*
  43. Returns an interned copy of the given text, adding it to the map if not already present.
  44. *Allocate using the Intern's Allocator (First time string is seen only)*
  45. Inputs:
  46. - m: A pointer to the Intern struct
  47. - text: The string to be interned
  48. NOTE: The returned string lives as long as the map entry lives.
  49. Returns:
  50. The interned string and an allocator error if any
  51. */
  52. intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {
  53. entry := _intern_get_entry(m, text) or_return
  54. #no_bounds_check return string(entry.str[:entry.len]), nil
  55. }
  56. /*
  57. Returns an interned copy of the given text as a cstring, adding it to the map if not already present.
  58. *Allocate using the Intern's Allocator (First time string is seen only)*
  59. Inputs:
  60. - m: A pointer to the Intern struct
  61. - text: The string to be interned
  62. NOTE: The returned cstring lives as long as the map entry lives
  63. Returns:
  64. The interned cstring and an allocator error if any
  65. */
  66. intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) {
  67. entry := _intern_get_entry(m, text) or_return
  68. return cstring(&entry.str[0]), nil
  69. }
  70. /*
  71. Internal function to lookup whether the text string exists in the map, returns the entry
  72. Sets and allocates the entry if it wasn't set yet
  73. *Allocate using the Intern's Allocator (First time string is seen only)*
  74. Inputs:
  75. - m: A pointer to the Intern struct
  76. - text: The string to be looked up or interned
  77. Returns:
  78. The new or existing interned entry and an allocator error if any
  79. */
  80. _intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
  81. if prev, ok := m.entries[text]; ok {
  82. return prev, nil
  83. }
  84. if m.allocator.procedure == nil {
  85. m.allocator = context.allocator
  86. }
  87. entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
  88. bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return
  89. new_entry = (^Intern_Entry)(raw_data(bytes))
  90. new_entry.len = len(text)
  91. copy(new_entry.str[:new_entry.len], text)
  92. new_entry.str[new_entry.len] = 0
  93. key := string(new_entry.str[:new_entry.len])
  94. m.entries[key] = new_entry
  95. return new_entry, nil
  96. }