mirror of
https://github.com/amnezia-vpn/amneziawg-tools.git
synced 2025-08-01 17:12:50 +02:00
feat: improve whitespace removal
This commit is contained in:
parent
09e36b2416
commit
50b56f7839
1 changed files with 33 additions and 3 deletions
36
src/config.c
36
src/config.c
|
@ -653,6 +653,9 @@ bool config_read_line(struct config_ctx *ctx, const char *input)
|
|||
size_t len, cleaned_len = 0;
|
||||
char *line, *comment;
|
||||
bool ret = true;
|
||||
bool found_equals = false;
|
||||
bool found_value_start = false;
|
||||
size_t value_end = 0;
|
||||
|
||||
/* This is what strchrnul is for, but that isn't portable. */
|
||||
comment = strchr(input, COMMENT_CHAR);
|
||||
|
@ -668,10 +671,37 @@ bool config_read_line(struct config_ctx *ctx, const char *input)
|
|||
goto out;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (!char_is_space(input[i]))
|
||||
line[cleaned_len++] = input[i];
|
||||
/* Remove preceding and trailing whitespaces before value
|
||||
First pass: find the actual end of the value (trim trailing spaces) */
|
||||
for (size_t i = len; i > 0; --i) {
|
||||
if (!char_is_space(input[i - 1])) {
|
||||
value_end = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Second pass: clean according to KEY = VALUE rules */
|
||||
for (size_t i = 0; i < value_end; ++i) {
|
||||
if (!found_equals) {
|
||||
/* Before '=': remove all whitespace */
|
||||
if (input[i] == '=') {
|
||||
line[cleaned_len++] = input[i];
|
||||
found_equals = true;
|
||||
} else if (!char_is_space(input[i])) {
|
||||
line[cleaned_len++] = input[i];
|
||||
}
|
||||
} else if (!found_value_start) {
|
||||
/* After '=' but before value: skip whitespace until first non-space */
|
||||
if (!char_is_space(input[i])) {
|
||||
line[cleaned_len++] = input[i];
|
||||
found_value_start = true;
|
||||
}
|
||||
} else {
|
||||
/* Within value: preserve all characters including spaces */
|
||||
line[cleaned_len++] = input[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (!cleaned_len)
|
||||
goto out;
|
||||
ret = process_line(ctx, line);
|
||||
|
|
Loading…
Add table
Reference in a new issue