From 4434cd5764bb4c49c649fb8d956da857ba286eba Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 8 Aug 2024 11:50:23 +0200 Subject: [PATCH] String interning --- src/text_editor/lua_api.cpp | 2 +- src/text_editor/management.cpp | 9 ++++++--- src/text_editor/text_editor.cpp | 7 ++++--- src/text_editor/title_bar.cpp | 4 +--- src/text_editor/todo.txt | 10 ++++------ src/text_editor/window.cpp | 4 +++- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 2827657..05ff2a1 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -221,7 +221,7 @@ String GetStyleString(String name, String default_string) { defer { lua_pop(LuaState, 1); }; if (lua_isstring(LuaState, -1)) { const char *string = lua_tostring(LuaState, -1); - result = Copy(Perm, string); + result = Intern(&GlobalInternTable, string); } } return result; diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 0518ece..e03542a 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -214,10 +214,12 @@ Buffer *BufferOpenFile(String path) { } if (!FileExists(path)) { - path = GetAbsolutePath(sys_allocator, path); + path = GetAbsolutePath(scratch, path); + path = Intern(&GlobalInternTable, path); buffer = CreateBuffer(sys_allocator, path); } else if (IsDir(path)) { - path = GetAbsolutePath(sys_allocator, path); + path = GetAbsolutePath(scratch, path); + path = Intern(&GlobalInternTable, path); buffer = CreateBuffer(sys_allocator, path, 4096 * 2); buffer->is_directory = true; @@ -225,7 +227,8 @@ Buffer *BufferOpenFile(String path) { IKnowWhatImDoing_Appendf(buffer, "%.*s ", FmtString(it.filename)); } } else { - path = GetAbsolutePath(sys_allocator, path); + path = GetAbsolutePath(scratch, path); + path = Intern(&GlobalInternTable, path); String string = ReadFile(scratch, path); buffer = CreateBuffer(sys_allocator, path, string.len * 4); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 4567133..0e9aa05 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -31,6 +31,7 @@ int FullScreenPositionX, FullScreenPositionY; #include "buffer_fuzzy_search.cpp" #include "buffer_test_load.cpp" +#include "intern_table.cpp" #include "management.cpp" #include "window.cpp" #include "process.cpp" @@ -83,7 +84,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { event.kind = EVENT_TEXT_INPUT; SDL_TextInputEvent &b = input_event->text; String string = b.text; - event.text = Copy(Perm, string).data; // @leak: @todo: intern table + event.text = Intern(&GlobalInternTable, string).data; } break; case SDL_EVENT_MOUSE_BUTTON_DOWN: { @@ -136,7 +137,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { event.kind = EVENT_DROP_FILE; SDL_DropEvent &b = input_event->drop; String string = b.data; - event.text = Copy(Perm, string).data; // @leak + event.text = Intern(&GlobalInternTable, string).data; } break; // SDL_SetEventEnabled(SDL_EVENT_DROP_TEXT, true); @@ -144,7 +145,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { // SDL_DropEvent &b = input_event->drop; // event.kind = EVENT_DROP_TEXT; // String string = b.data; - // event.text = Copy(Perm, string).data; // @leak + // event.text = Intern(&GlobalInternTable, string).data; // } break; default: { diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 486c6dd..c549b4d 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -80,9 +80,7 @@ void ApplyTitleBarChangesToWindow(Window *window, View *view, Buffer *buffer) { String filepath = ToString(scratch, buffer_string); if (filepath != last_buffer->name && !BufferNameExists(filepath)) { - // @todo: maybe intern the filenames? - // @leak - last_buffer->name = Copy(Perm, filepath); + last_buffer->name = Intern(&GlobalInternTable, filepath); } Int buffer_pos = XYToPos(*last_buffer, {column, line}); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index f79eeef..59cfb6e 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,10 +1,6 @@ -- I guess it's pretty dangerous passing pointers everywhere? -- text in events aren't we using invalid memory for text? We need an intern table? - -- if exec is prepended with '!' symbol then run a shell command +- Remove pointers and use ViewIDs (enable array debug while doing this) - try using git grep for search for now, combine with fuzzy search buffer -- Test stdin writing code -- Implement shell interaction (last line should have a '$'' symbols, if you press enter it should send that line to stdin of a running shell) +- need to rewrite the path matching in lua - search as a command to execute which is going to be in the title bar - search backwards @@ -44,6 +40,8 @@ BUG: there is a click hang when switching windows sometimes, you click after sel backlog +- Test stdin writing code +- Implement shell interaction (last line should have a '$'' symbols, if you press enter it should send that line to stdin of a running shell) - drop text into window - page up and down should also scroll and leave you in exactly same scroll - I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection) diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 8c9f950..b6a5cc2 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -113,7 +113,9 @@ bool ToggleVisibility(WindowID window_id) { } String BuffCWD(String string) { - String result = Format(Perm, "%.*s/%.*s", FmtString(WorkingDir), FmtString(string)); + Scratch scratch; + String result = Format(scratch, "%.*s/%.*s", FmtString(WorkingDir), FmtString(string)); + result = Intern(&GlobalInternTable, result); return result; }