From b323f5ad780ccfc190176e07aa9329018b81f745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Lu=C3=ADs=20Vaz=20Silva?= Date: Sun, 13 Apr 2025 15:11:34 -0300 Subject: [PATCH] remove raw C string manipulation, add missing chunk --- src/common/rendering/stb_include.cpp | 79 +++++++--------------------- 1 file changed, 19 insertions(+), 60 deletions(-) diff --git a/src/common/rendering/stb_include.cpp b/src/common/rendering/stb_include.cpp index 0179adcfc8..65ff0cd983 100644 --- a/src/common/rendering/stb_include.cpp +++ b/src/common/rendering/stb_include.cpp @@ -41,6 +41,7 @@ #include #include #include +#include static bool stb_include_load_file(FString filename, FString &out) { @@ -52,27 +53,22 @@ static bool stb_include_load_file(FString filename, FString &out) typedef struct { - int offset; - int end; - FString filename; - int next_line_after; + int64_t offset; + int64_t end; + FString filename; + int64_t next_line_after; } include_info; -static void stb_include_append_include(TArray &array, int offset, int end, FString filename, int next_line) -{ - array.Push({offset, end, filename, next_line}); -} - -static int stb_include_isspace(int ch) +static bool stb_include_isspace(int ch) { return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); } // find location of all #include and #inject -static int stb_include_find_includes(const char *text, TArray &plist) +static int64_t stb_include_find_includes(const char *text, TArray &plist) { - int line_count = 1; - int inc_count = 0; + int64_t line_count = 1; + int64_t inc_count = 0; const char *s = text, *start; TArray list; @@ -100,7 +96,7 @@ static int stb_include_find_includes(const char *text, TArray &pli while (*s != '\r' && *s != '\n' && *s != 0) ++s; // s points to the newline, so s-start is everything except the newline - stb_include_append_include(list, start-text, s-text, filename, line_count+1); + list.Push({start-text, s-text, filename, line_count+1}); inc_count++; } } @@ -119,68 +115,31 @@ static int stb_include_find_includes(const char *text, TArray &pli return inc_count; } -// avoid dependency on sprintf() -static void stb_include_itoa(char str[9], int n) -{ - int i; - for (i=0; i < 8; ++i) - str[i] = ' '; - str[i] = 0; - - for (i=1; i < 8; ++i) { - str[7-i] = '0' + (n % 10); - n /= 10; - if (n == 0) - break; - } -} - -static void stb_include_append(FString &str, FString &addstr) -{ - str += addstr; -} - -static void stb_include_append(FString &str, const char * addstr, size_t addlen) -{ - str.AppendCStrPart(addstr, addlen); -} - FString stb_include_string(FString str, FString &error) { error = ""; - char temp[4096]; TArray inc_list; - int i, num = stb_include_find_includes(str.GetChars(), inc_list); - size_t source_len = str.Len(); - FString text; + int64_t num = stb_include_find_includes(str.GetChars(), inc_list); + FString text = ""; size_t last = 0; - for (i=0; i < num; ++i) + for (int64_t i = 0; i < num; ++i) { - stb_include_append(text, str.GetChars() + last, inc_list[i].offset - last); - // write out line directive for the include - strcpy(temp, "#line "); - stb_include_itoa(temp+6, 1); - strcat(temp, " "); - stb_include_itoa(temp+15, i+1); - strcat(temp, "\n"); - stb_include_append(text, temp, strlen(temp)); + text.AppendCStrPart(str.GetChars() + last, inc_list[i].offset - last); + + text.AppendFormat("#line 1 %d\n", i + 1); FString inc = stb_include_file(inc_list[i].filename.GetChars(), error); if (!error.IsEmpty()) { return ""; } - stb_include_append(text, inc); + text += inc; - // write out line directive - strcpy(temp, "\n#line "); - stb_include_itoa(temp+6, inc_list[i].next_line_after); - strcat(temp, " "); - stb_include_itoa(temp+15, 0); - stb_include_append(text, temp, strlen(temp)); + text.AppendFormat("#line %d 0\n", inc_list[i].next_line_after); // no newlines, because we kept the #include newlines, which will get appended next last = inc_list[i].end; } + text.AppendCStrPart(str.GetChars() + last, str.Len() - last); return text; }