mirror of
https://github.com/swaywm/sway.git
synced 2026-04-29 06:46:22 -04:00
strip_whitespace(): Improve and simplify (#1034)
Now strip_whitespace() also strips the unnecessary whitespace between words in the string. That fixes issues reading the configuration file if too much space is included between tokens.
This commit is contained in:
parent
84358788aa
commit
f213180533
3 changed files with 27 additions and 17 deletions
|
|
@ -10,22 +10,32 @@
|
|||
|
||||
const char whitespace[] = " \f\n\r\t\v";
|
||||
|
||||
char *strip_whitespace(char *_str) {
|
||||
if (*_str == '\0')
|
||||
return _str;
|
||||
char *strold = _str;
|
||||
while (*_str == ' ' || *_str == '\t') {
|
||||
_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 (NULL != str) {
|
||||
unsigned int count = 0u;
|
||||
for (unsigned int index = 0u; str[index]; ++index) {
|
||||
if (' ' != str[index] && '\t' != str[index]) {
|
||||
str[count++] = str[index];
|
||||
} else if(0u < count && ' ' != str[count-1u] && '\t' != str[count-1u]) {
|
||||
str[count++] = ' ';
|
||||
}
|
||||
}
|
||||
str[count] = '\0';
|
||||
if (0u < count && (' ' == str[count-1u] || '\t' == str[count-1u])) {
|
||||
str[count-1u] = '\0';
|
||||
}
|
||||
}
|
||||
char *str = strdup(_str);
|
||||
free(strold);
|
||||
int i;
|
||||
for (i = 0; str[i] != '\0'; ++i);
|
||||
do {
|
||||
i--;
|
||||
} while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
|
||||
str[i + 1] = '\0';
|
||||
return str;
|
||||
}
|
||||
|
||||
void strip_quotes(char *str) {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ extern int setenv(const char *, const char *, int);
|
|||
// array of whitespace characters to use for delims
|
||||
extern const char whitespace[];
|
||||
|
||||
char *strip_whitespace(char *str);
|
||||
void strip_whitespace(char *str);
|
||||
char *strip_comments(char *str);
|
||||
void strip_quotes(char *str);
|
||||
|
||||
|
|
|
|||
|
|
@ -632,7 +632,7 @@ bool read_config(FILE *file, struct sway_config *config) {
|
|||
continue;
|
||||
}
|
||||
line_number++;
|
||||
line = strip_whitespace(line);
|
||||
strip_whitespace(line);
|
||||
if (line[0] == '#') {
|
||||
free(line);
|
||||
continue;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue