string-util: introduce STRING_ALLOW_NEWLINES switch for string_is_safe()

This also make StringSafeFlags defined in basic-forward.h so other
headers can use the enum.
This commit is contained in:
Yu Watanabe
2026-05-18 02:55:34 +09:00
parent 8e8d3a22b2
commit b74f016e59
4 changed files with 35 additions and 6 deletions
+5 -1
View File
@@ -1113,7 +1113,11 @@ bool string_is_safe(const char *p, StringSafeFlags flags) {
return false;
for (const char *t = p; *t; t++) {
if ((*t > 0 && *t < ' ') || *t == 0x7f) /* never allow control characters */
/* never allow control characters, except for new line */
if ((*t > 0 && *t < ' ' && *t != '\n') || *t == 0x7f)
return false;
if (!FLAGS_SET(flags, STRING_ALLOW_NEWLINES) && *t == '\n')
return false;
if (!FLAGS_SET(flags, STRING_ALLOW_BACKSLASHES) && *t == '\\')