Skip to content

Instantly share code, notes, and snippets.

@distributedlife
Created November 13, 2013 11:26
Show Gist options
  • Select an option

  • Save distributedlife/7447544 to your computer and use it in GitHub Desktop.

Select an option

Save distributedlife/7447544 to your computer and use it in GitHub Desktop.
distributedlife.com blog code samples
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 ;
//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 ;
}
//"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