Added TextTrim function

Added TextRightTrim
Added TextLeftTrim
This commit is contained in:
Skabunkel 2019-02-09 23:23:23 +01:00
parent 366313bfd0
commit 3e7b65ee2e
2 changed files with 75 additions and 0 deletions

View File

@ -1184,6 +1184,9 @@ RLAPI const char *TextToUpper(const char *text); // Get upp
RLAPI const char *TextToLower(const char *text); // Get lower case version of provided string
RLAPI const char *TextToPascal(const char *text); // Get Pascal case notation version of provided string
RLAPI int TextToInteger(const char *text); // Get integer value from text (negative values not supported)
RLAPI const char *TextTrim(const char *text, char trimChar); // Removes all chars equal to trimChar from the begining and end of the given text (memory should be freed!)
RLAPI const char *TextRightTrim(const char *text, char trimChar); // Removes all chars equal to trimChar from the end of the given text (memory should be freed!)
RLAPI const char *TextLeftTrim(const char *text, char trimChar); // Removes all chars equal to trimChar from the begining of the given text (memory should be freed!)
//------------------------------------------------------------------------------------
// Basic 3d Shapes Drawing Functions (Module: models)

View File

@ -1089,6 +1089,78 @@ const char *TextSubtext(const char *text, int position, int length)
return buffer;
}
// removes a specific char from the begining of a string
// REQUIRES: strlen()
const char *TextLeftTrim(const char *text, char trimChar)
{
int textLength = strlen(text);
int leftOffset = 0;
for(leftOffset; leftOffset < textLength; leftOffset++)
{
if(*(text + leftOffset) != trimChar)
break;
}
const char *subText = TextSubtext(text, leftOffset, textLength-leftOffset);
int subTextLength = strlen(subText);
char *result = malloc(subTextLength+1);
if (!result) return NULL; // Memory could not be allocated
strcpy(result, subText);
*(result+subTextLength) = '\0';
return result;
}
// removes all char from the end of a string
// REQUIRES: strlen()
const char *TextRightTrim(const char *text, char trimChar)
{
int textLength = strlen(text);
int rightOffset = textLength-1;
for(rightOffset; -1 < rightOffset; rightOffset--)
{
if(*(text + rightOffset) != trimChar)
break;
}
const char *subText = TextSubtext(text, 0, rightOffset+1);
int subTextLength = strlen(subText);
char *result = malloc(subTextLength+1);
if (!result) return NULL; // Memory could not be allocated
strcpy(result, subText);
*(result+subTextLength) = '\0';
return result;
}
// removes a specific char from the begining of a string
// REQUIRES: strlen()
const char *TextTrim(const char *text, char trimChar)
{
int textLength = strlen(text);
int leftOffset = 0;
for(leftOffset; leftOffset < textLength; leftOffset++)
{
if(*(text + leftOffset) != trimChar)
break;
}
int rightOffset = textLength-1;
for(rightOffset; leftOffset < rightOffset; rightOffset--)
{
if(*(text + rightOffset) != trimChar)
break;
}
const char *subText = TextSubtext(text, leftOffset, rightOffset+1-leftOffset);
int subTextLength = strlen(subText);
char *result = malloc(subTextLength+1);
if (!result) return NULL; // Memory could not be allocated
strcpy(result, subText);
*(result+subTextLength) = '\0';
return result;
}
// Replace text string
// REQUIRES: strlen(), strstr(), strncpy(), strcpy()
// WARNING: Internally allocated memory must be freed by the user (if return != NULL)