string printing: simplify

This commit is contained in:
2021-07-23 17:24:36 +02:00
parent 67e8524b40
commit 03a2d9c70e
2 changed files with 3 additions and 26 deletions

26
cfg.h
View File

@@ -102,29 +102,6 @@ bool string_compare(string* a, string* b) {
return strncmp(a->str, b->str, a->len) == 0; return strncmp(a->str, b->str, a->len) == 0;
} }
ssize_t string_count_escaped_chars(string* s) {
size_t i;
ssize_t res = 0;
for (i = 0; i < s->len; i++) if (s->str[i] == '"' || s->str[i] == '\\') res++;
return res;
}
string string_quote(string* replace) {
size_t i = 0;
ssize_t quotes = string_count_escaped_chars(replace);
string res = new_string_sized(replace->len+quotes);
res.len = res.cap;
quotes = 0;
for (i = 0; i < replace->len; i++) {
if (replace->str[i] == '"') res.str[i+(quotes++)] = '\\';
else if (replace->str[i] == '\\') res.str[i+(quotes++)] = '\\';
res.str[i+quotes] = replace->str[i];
}
return res;
}
ssize_t string_find_whitespace(string* s) { ssize_t string_find_whitespace(string* s) {
ssize_t i; ssize_t i;
char c; char c;
@@ -413,8 +390,7 @@ string config_value_str(config_value* c, size_t indent) {
return res; return res;
case config_value_string: case config_value_string:
string_cappend(&res, "\""); string_cappend(&res, "\"");
sub = string_quote(&c->s); string_append(&res, &c->s);
string_append(&res, &sub);
string_cappend(&res, "\""); string_cappend(&res, "\"");
break; break;
case config_value_number: case config_value_number:

View File

@@ -4,7 +4,8 @@ hi
name 1 name 1
val "hi" val "hi"
list list
- "value" - "value
\"multi"
- -1 - -1
- -
- "this" - "this"