Add str_clean_whitespaces_simple and str_skip_to_whitespace_const

This commit is contained in:
Jordy Ruiz 2019-02-17 19:29:33 +01:00
parent 8e8e05c4f3
commit 3d787ceada
2 changed files with 57 additions and 0 deletions

View file

@ -1913,6 +1913,38 @@ void str_clean_whitespaces(char *str_in)
} }
} }
/* removes leading and trailing spaces */
void str_clean_whitespaces_simple(char *str_in)
{
char *read = str_in;
char *write = str_in;
/* skip initial whitespace */
while(*read == ' ')
read++;
/* end of read string is detected in the loop */
while(1)
{
/* skip whitespace */
int found_whitespace = 0;
for(; *read == ' ' && !found_whitespace; read++)
found_whitespace = 1;
/* if not at the end of the string, put a found whitespace here */
if(*read)
{
if(found_whitespace)
*write++ = ' ';
*write++ = *read++;
}
else
{
*write = 0;
break;
}
}
}
char *str_skip_to_whitespace(char *str) char *str_skip_to_whitespace(char *str)
{ {
while(*str && (*str != ' ' && *str != '\t' && *str != '\n')) while(*str && (*str != ' ' && *str != '\t' && *str != '\n'))
@ -1920,6 +1952,13 @@ char *str_skip_to_whitespace(char *str)
return str; return str;
} }
const char *str_skip_to_whitespace_const(const char *str)
{
while(*str && (*str != ' ' && *str != '\t' && *str != '\n'))
str++;
return str;
}
char *str_skip_whitespaces(char *str) char *str_skip_whitespaces(char *str)
{ {
while(*str && (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r')) while(*str && (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r'))

View file

@ -912,6 +912,18 @@ int str_check_pathname(const char* str);
*/ */
void str_clean_whitespaces(char *str); void str_clean_whitespaces(char *str);
/*
Function: str_clean_whitespaces_simple
Removes leading and trailing spaces
Parameters:
str - String to clean up
Remarks:
- The strings are treated as zero-terminated strings.
*/
void str_clean_whitespaces_simple(char *str);
/* /*
Function: str_skip_to_whitespace Function: str_skip_to_whitespace
Skips leading non-whitespace characters(all but ' ', '\t', '\n', '\r'). Skips leading non-whitespace characters(all but ' ', '\t', '\n', '\r').
@ -928,6 +940,12 @@ void str_clean_whitespaces(char *str);
*/ */
char *str_skip_to_whitespace(char *str); char *str_skip_to_whitespace(char *str);
/*
Function: str_skip_to_whitespace_const
See str_skip_to_whitespace.
*/
const char *str_skip_to_whitespace_const(const char *str);
/* /*
Function: str_skip_whitespaces Function: str_skip_whitespaces
Skips leading whitespace characters(' ', '\t', '\n', '\r'). Skips leading whitespace characters(' ', '\t', '\n', '\r').