MXS-1450 Add more string trimming functions
- trim_leading - trim_trailing Implemented trim in terms of trim_leading and trim_trailing
This commit is contained in:
@ -433,13 +433,24 @@ bool mxs_mkdir_all(const char *path, int mask)
|
||||
return mkdir_all_internal(local_path, (mode_t)mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim leading and trailing whitespace from a string
|
||||
*
|
||||
* @param str String to trim
|
||||
* @return Trimmed string
|
||||
*/
|
||||
char* trim(char *str)
|
||||
char* trim_leading(char* str)
|
||||
{
|
||||
char* ptr = str;
|
||||
|
||||
while (isspace(*ptr))
|
||||
{
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if (ptr != str)
|
||||
{
|
||||
memmove(str, ptr, strlen(ptr) + 1);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char* trim_trailing(char* str)
|
||||
{
|
||||
char* ptr = strchr(str, '\0') - 1;
|
||||
|
||||
@ -453,21 +464,14 @@ char* trim(char *str)
|
||||
*(ptr + 1) = '\0';
|
||||
}
|
||||
|
||||
ptr = str;
|
||||
|
||||
while (isspace(*ptr))
|
||||
{
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if (ptr != str)
|
||||
{
|
||||
memmove(str, ptr, strlen(ptr) + 1);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char* trim(char *str)
|
||||
{
|
||||
return trim_leading(trim_trailing(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all whitespace with spaces and squeeze repeating whitespace characters
|
||||
*
|
||||
|
Reference in New Issue
Block a user