mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add a try variant of StringBuilder::append_escaped_for_json
This will allow us to make a fallible version of the JSON serializers.
This commit is contained in:
parent
7440b632fe
commit
9da8c78133
Notes:
sideshowbarker
2024-07-17 18:10:37 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/9da8c78133 Pull-request: https://github.com/SerenityOS/serenity/pull/12754 Reviewed-by: https://github.com/bgianfo ✅ Reviewed-by: https://github.com/elcuco
2 changed files with 14 additions and 7 deletions
|
@ -171,31 +171,37 @@ void StringBuilder::append_as_lowercase(char ch)
|
|||
}
|
||||
|
||||
void StringBuilder::append_escaped_for_json(StringView string)
|
||||
{
|
||||
MUST(try_append_escaped_for_json(string));
|
||||
}
|
||||
|
||||
ErrorOr<void> StringBuilder::try_append_escaped_for_json(StringView string)
|
||||
{
|
||||
for (auto ch : string) {
|
||||
switch (ch) {
|
||||
case '\b':
|
||||
append("\\b");
|
||||
TRY(try_append("\\b"));
|
||||
break;
|
||||
case '\n':
|
||||
append("\\n");
|
||||
TRY(try_append("\\n"));
|
||||
break;
|
||||
case '\t':
|
||||
append("\\t");
|
||||
TRY(try_append("\\t"));
|
||||
break;
|
||||
case '\"':
|
||||
append("\\\"");
|
||||
TRY(try_append("\\\""));
|
||||
break;
|
||||
case '\\':
|
||||
append("\\\\");
|
||||
TRY(try_append("\\\\"));
|
||||
break;
|
||||
default:
|
||||
if (ch >= 0 && ch <= 0x1f)
|
||||
appendff("\\u{:04x}", ch);
|
||||
TRY(try_appendff("\\u{:04x}", ch));
|
||||
else
|
||||
append(ch);
|
||||
TRY(try_append(ch));
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ public:
|
|||
return vformat(*this, fmtstr.view(), variadic_format_params);
|
||||
}
|
||||
ErrorOr<void> try_append(char const*, size_t);
|
||||
ErrorOr<void> try_append_escaped_for_json(StringView);
|
||||
|
||||
void append(StringView);
|
||||
#ifndef KERNEL
|
||||
|
|
Loading…
Reference in a new issue