Skip to content

Commit 0203a47

Browse files
committed
REDESIGNED: LoadTextLines()/UnloadTextLines()
1 parent 8116ebd commit 0203a47

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/raylib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size);
15071507
// WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use
15081508
// WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree()
15091509
RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n')
1510-
RLAPI void UnloadTextLines(char **text); // Unload text lines
1510+
RLAPI void UnloadTextLines(char **text, int lineCount); // Unload text lines
15111511
RLAPI int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied
15121512
RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
15131513
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending

src/rtext.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,36 +1448,38 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
14481448
// Text strings management functions
14491449
//----------------------------------------------------------------------------------
14501450
// Load text as separate lines ('\n')
1451-
// WARNING: There is a limit set for number of lines and line-size
1451+
// NOTE: Returned lines end with null terminator '\0'
14521452
char **LoadTextLines(const char *text, int *count)
14531453
{
1454-
#define MAX_TEXTLINES_COUNT 512
1455-
#define MAX_TEXTLINES_LINE_LEN 512
1454+
int lineCount = 1;
1455+
int textSize = strlen(text);
14561456

1457-
char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *));
1458-
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1);
1459-
int textSize = (int)strlen(text);
1460-
int k = 0;
1457+
// Text pass to get required line count
1458+
for (int i = 0; i < textSize; i++)
1459+
{
1460+
if (text[i] == '\n') lineCount++;
1461+
}
14611462

1462-
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++)
1463+
char **lines = (char **)RL_CALLOC(lineCount, sizeof(char *));
1464+
for (int i = 0, l = 0, lineLen = 0; i < lineCount; i++, lineLen++)
14631465
{
1464-
if ((text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1)))
1466+
if (text[i] == '\n')
14651467
{
1466-
strncpy(lines[k], &text[i - len], len);
1467-
len = 0;
1468-
k++;
1468+
lines[l] = (char *)RL_CALLOC(lineLen + 1, 1);
1469+
strncpy(lines[l], &text[i - lineLen], lineLen);
1470+
lineLen = 0;
1471+
l++;
14691472
}
1470-
else len++;
14711473
}
14721474

1473-
*count += k;
1475+
*count = lineCount;
14741476
return lines;
14751477
}
14761478

14771479
// Unload text lines
1478-
void UnloadTextLines(char **lines)
1480+
void UnloadTextLines(char **lines, int lineCount)
14791481
{
1480-
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) RL_FREE(lines[i]);
1482+
for (int i = 0; i < lineCount; i++) RL_FREE(lines[i]);
14811483
RL_FREE(lines);
14821484
}
14831485

0 commit comments

Comments
 (0)