diff --git a/src/raylib.h b/src/raylib.h index 7709b10f2..c9886e467 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -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) diff --git a/src/text.c b/src/text.c index b1f786b71..7179b2a7f 100644 --- a/src/text.c +++ b/src/text.c @@ -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)