From 1f66f0d9a2d9bde9d8aac465739afe33ff174707 Mon Sep 17 00:00:00 2001 From: brankoku <48296877+brankoku@users.noreply.github.com> Date: Sun, 24 Nov 2019 08:01:35 -0500 Subject: [PATCH] [text] `TextFormat()` caching (#1015) --- src/text.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/text.c b/src/text.c index 3f8e22b10..2fff08d8c 100644 --- a/src/text.c +++ b/src/text.c @@ -1102,9 +1102,16 @@ unsigned int TextLength(const char *text) } // Formatting of text with variables to 'embed' +// WARNING: the string returned will expire after this function is called MAX_TEXT_BUFFERS times const char *TextFormat(const char *text, ...) { - static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + #define MAX_TEXT_BUFFERS 8 + // We create an array of buffers so strings don't expire until MAX_TEXT_BUFFERS invocations + static char cache[MAX_TEXT_BUFFERS][MAX_TEXT_BUFFER_LENGTH] = { 0 }; + static int index = 0; + char *buffer = cache[index]; + index += 1; + index %= MAX_TEXT_BUFFERS; va_list args; va_start(args, text); @@ -1112,6 +1119,7 @@ const char *TextFormat(const char *text, ...) va_end(args); return buffer; + #undef MAX_TEXT_BUFFERS } // Get a piece of a text string