Browse Source

Redesigned TextSplit() function

pull/755/head
Ray 5 years ago
parent
commit
6dbec47488
1 changed files with 24 additions and 54 deletions
  1. +24
    -54
      src/text.c

+ 24
- 54
src/text.c View File

@ -1180,70 +1180,40 @@ const char *TextJoin(const char **textList, int count, const char *delimiter)
}
// Split string into multiple strings
// REQUIRES: strlen(), strcpy(), strtok()
// WARNING: Allocated memory should be manually freed
char **TextSplit(const char *text, char delimiter, int *count)
const char **TextSplit(const char *text, char delimiter, int *count)
{
#define MAX_SUBSTRING_LENGTH 128
// NOTE: Current implementation returns a copy of the provided string with '\0' (string end delimiter)
// inserted between strings defined by "delimiter" parameter. No memory is dynamically allocated,
// all used memory is static... it has some limitations:
// 1. Maximum number of possible split strings is set by MAX_SUBSTRINGS_COUNT
// 2. Maximum size of text to split is MAX_TEXT_BUFFER_LENGTH
// TODO: Allocate memory properly for every substring size
char **result = NULL;
#define MAX_SUBSTRINGS_COUNT 64
int len = strlen(text);
char *textcopy = (char *)malloc(len + 1);
strcpy(textcopy, text);
int counter = 1;
// Count how many substrings we have on text and init memory for each of them
for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++;
// Memory allocation for substrings
result = (char **)malloc(sizeof(char *)*counter);
for (int i = 0; i < counter; i++) result[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH);
char *substrPtr = NULL;
char delimiters[1] = { delimiter }; // Only caring for one delimiter
substrPtr = strtok(textcopy, delimiters);
for (int i = 0; (i < counter) && (substrPtr != NULL); i++)
{
strcpy(result[i], substrPtr);
substrPtr = strtok(NULL, delimiters);
}
*count = counter;
free(textcopy);
return result;
}
// Get pointers to substrings separated by delimiter
void TextSplitEx(const char *text, char delimiter, int *count, const char **ptrs, int *lengths)
{
int elementsCount = 0;
int charsCount = 0;
static const char *result[MAX_SUBSTRINGS_COUNT] = { NULL };
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
ptrs[0] = text;
result[0] = buffer;
int counter = 1;
for (int i = 0; text[i] != '\0'; i++)
// Count how many substrings we have on text and point to every one
for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
{
charsCount++;
if (text[i] == delimiter)
buffer[i] = text[i];
if (buffer[i] == '\0') break;
else if (buffer[i] == delimiter)
{
lengths[elementsCount] = charsCount - 1;
charsCount = 0;
elementsCount++;
n">ptrs[elementsCount] = &text[i + 1];
buffer[i] = '\0'; // Set an end of string at this point
result[counter] = buffer + i + 1;
counter++;
if (counter == MAX_SUBSTRINGS_COUNT) break;
}
}
lengths[elementsCount] = charsCount;
elementsCount++;
*count = elementsCount;
*count = counter;
return result;
}
// Append text at specific position and move cursor!

Loading…
Cancel
Save