From 63cc7c671d35df9b7c2dcd8a0d2567453b718208 Mon Sep 17 00:00:00 2001 From: DissolveDZ Date: Sat, 30 Sep 2023 18:29:45 +0200 Subject: [PATCH] improved TextSubtext function to use memcpy --- src/rtext.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 5531f06da..aba276aa2 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1430,27 +1430,18 @@ bool TextIsEqual(const char *text1, const char *text2) // Get a piece of a text string const char *TextSubtext(const char *text, int position, int length) { - static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; - memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); - + if (!length) + return ""; int textLength = TextLength(text); - if (position >= textLength) { position = textLength - 1; length = 0; } - - if (length >= textLength) length = textLength; - - for (int c = 0 ; c < length ; c++) - { - *(buffer + c) = *(text + position); - text++; - } - - *(buffer + length) = '\0'; - + if (length > textLength) length = textLength; + char *buffer = malloc(length + 1); + memcpy(buffer, text + position, length); + buffer[length] = '\0'; return buffer; }