From 3861e8289ae40bac168275ce6f10b231e11baa55 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Mon, 28 Nov 2022 22:19:12 +0000 Subject: Refactor string vector code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, string vectors were built by reading input line-by line, and multiple copies of string vectors were made when searching. Now, input is read into one big buffer, and string vectors only contain references to the strings in this buffer. This both speeds up reading of input, and avoids unnecessary copying of strings in various places. The main downside currently is that input read from stdin is no longer UTF-8 normalised. This means, for example, that a search for `e` won't necessarily match `é`. Normalisation is very slow relative to the rest of tofi, however, and not needed for most use-cases. This could either be solved by accepting the slowdown, or making this an option, such as --unicode or --unicode-normalize. --- src/main_compgen.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/main_compgen.c') diff --git a/src/main_compgen.c b/src/main_compgen.c index 743d2cc..6375c54 100644 --- a/src/main_compgen.c +++ b/src/main_compgen.c @@ -1,10 +1,16 @@ #include +#include #include "compgen.h" #include "string_vec.h" int main() { - struct string_vec commands = compgen_cached(); - string_vec_save(&commands, stdout); - string_vec_destroy(&commands); + char *buf = compgen_cached(); + struct string_ref_vec commands = string_ref_vec_from_buffer(buf); + for (size_t i = 0; i < commands.count; i++) { + fputs(commands.buf[i].string, stdout); + fputc('\n', stdout); + } + string_ref_vec_destroy(&commands); + free(buf); } -- cgit v1.2.3