-
-
Save distributedlife/7447544 to your computer and use it in GitHub Desktop.
distributedlife.com blog code samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Trim (const char* Bloated, const char* Parameter) | |
| { | |
| long BloatedLength = strlen (Bloated) ; | |
| char* Trimmed = 0 ; | |
| long TrimmedLength = 0 ; | |
| long TrimFromStart = strspn (Bloated, " \t") ; | |
| long TrimFromEnd = 0 ; | |
| long index = 0 ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Count Trailing whitespace | |
| index = BloatedLength - 1 ; | |
| while ( (Bloated[index] == ' ') || (Bloated[index] == '\t')) | |
| { | |
| TrimFromEnd++ ; | |
| if (index == 0) | |
| { | |
| break ; | |
| } | |
| index-- ; | |
| } | |
| TrimmedLength = BloatedLength - TrimFromEnd - TrimFromStart ; | |
| TrimmedLength++ ; //space for null terminating | |
| Trimmed = (char*) malloc (TrimmedLength * sizeof(char)) ; | |
| if (!Trimmed) | |
| { | |
| lr_error_message("Failed to allocate memory for the trimmed string") ; | |
| return ; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //"trim" | |
| memcpy (Trimmed, Bloated + TrimFromStart, TrimmedLength) ; | |
| //set null terminating char | |
| Trimmed[TrimmedLength - 1] = '\0' ; | |
| lr_set (Trimmed, Parameter); | |
| free (Trimmed) ; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment