strip_whitespace(): Handle quotes correctly

This commit is contained in:
Michael Schupikov 2017-01-09 19:16:56 +01:00
parent e38d4b3a06
commit e1917e4fd1

View file

@ -10,24 +10,35 @@
const char whitespace[] = " \f\n\r\t\v"; const char whitespace[] = " \f\n\r\t\v";
/**************************************************************************//** void strip_whitespace(char *str)
* {
* \brief Strips the whitespace in front and rear of the string. If whitespace
* is found in the middle of the string, all but one space is removed
* between words.
*
* \param str Pointer to the char string that should be stripped. The string
* is modified. If NULL is passed, no operation is performed.
*
******************************************************************************/
void strip_whitespace(char * restrict const str) {
if (str) { if (str) {
size_t count = 0; size_t count = 0;
char quote = ' ';
for (size_t index = 0; str[index]; ++index) { for (size_t index = 0; str[index]; ++index) {
if (' ' != str[index] && '\t' != str[index]) { if (quote != '\'' && quote != '\"') {
if (' ' != str[index] && '\t' != str[index]) {
str[count++] = str[index];
if (str[index] == '\'') {
quote = '\'';
} else if (str[index] == '\"') {
quote = '\"';
}
} else if(0 < count && ' ' != str[count - 1] && '\t' != str[count - 1]) {
str[count++] = ' ';
}
} else if (quote == '\'') {
str[count++] = str[index]; str[count++] = str[index];
} else if(0 < count && ' ' != str[count - 1] && '\t' != str[count - 1]) { if (str[index] == '\'') {
str[count++] = ' '; quote = ' ';
}
} else if (quote == '\"') {
str[count++] = str[index];
if (str[index] == '\"') {
quote = ' ';
}
} else {
/* Unexpected value for quote. */
} }
} }
str[count] = '\0'; str[count] = '\0';
@ -37,6 +48,7 @@ void strip_whitespace(char * restrict const str) {
} }
} }
void strip_quotes(char *str) { void strip_quotes(char *str) {
bool in_str = false; bool in_str = false;
bool in_chr = false; bool in_chr = false;