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:
Johan Wikman
2017-09-26 13:34:11 +03:00
parent bb95074e88
commit 4220e3ca6a
3 changed files with 121 additions and 33 deletions

View File

@ -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
*