From 0203a47bf983984589a8d95c4f75d41bee90d596 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 4 Sep 2025 23:25:42 +0200 Subject: [PATCH] REDESIGNED: `LoadTextLines()`/`UnloadTextLines()` --- src/raylib.h | 2 +- src/rtext.c | 40 +++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 41bc1926e..5635546e0 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1507,7 +1507,7 @@ RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size); // WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use // WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree() RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n') -RLAPI void UnloadTextLines(char **text); // Unload text lines +RLAPI void UnloadTextLines(char **text, int lineCount); // Unload text lines RLAPI int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending diff --git a/src/rtext.c b/src/rtext.c index c6078d7cb..f34606bc1 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1448,36 +1448,38 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint) // Text strings management functions //---------------------------------------------------------------------------------- // Load text as separate lines ('\n') -// WARNING: There is a limit set for number of lines and line-size +// NOTE: Returned lines end with null terminator '\0' char **LoadTextLines(const char *text, int *count) { - #define MAX_TEXTLINES_COUNT 512 - #define MAX_TEXTLINES_LINE_LEN 512 + int lineCount = 1; + int textSize = strlen(text); - char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *)); - for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1); - int textSize = (int)strlen(text); - int k = 0; - - for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++) + // Text pass to get required line count + for (int i = 0; i < textSize; i++) { - if ((text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1))) - { - strncpy(lines[k], &text[i - len], len); - len = 0; - k++; - } - else len++; + if (text[i] == '\n') lineCount++; } - *count += k; + char **lines = (char **)RL_CALLOC(lineCount, sizeof(char *)); + for (int i = 0, l = 0, lineLen = 0; i < lineCount; i++, lineLen++) + { + if (text[i] == '\n') + { + lines[l] = (char *)RL_CALLOC(lineLen + 1, 1); + strncpy(lines[l], &text[i - lineLen], lineLen); + lineLen = 0; + l++; + } + } + + *count = lineCount; return lines; } // Unload text lines -void UnloadTextLines(char **lines) +void UnloadTextLines(char **lines, int lineCount) { - for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) RL_FREE(lines[i]); + for (int i = 0; i < lineCount; i++) RL_FREE(lines[i]); RL_FREE(lines); }