diff options
author | Nico Weber <thakis@chromium.org> | 2023-01-22 14:03:01 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-22 21:27:32 +0000 |
commit | 868b358b1acb41ecabba515dd8be3de420d90807 (patch) | |
tree | 086df342997a6cc2f41cfa1f4cba3987e90d221a /Userland/Libraries | |
parent | ba97f471ca030fe1ef1475231796dee0a74446f8 (diff) | |
download | serenity-868b358b1acb41ecabba515dd8be3de420d90807.zip |
LibJS: Add spec comments to quote_json_string
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/JSONObject.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index 7978a44ba7..a044f25158 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -350,10 +350,15 @@ ThrowCompletionOr<DeprecatedString> JSONObject::serialize_json_array(VM& vm, Str // 25.5.2.2 QuoteJSONString ( value ), https://tc39.es/ecma262/#sec-quotejsonstring DeprecatedString JSONObject::quote_json_string(DeprecatedString string) { + // 1. Let product be the String value consisting solely of the code unit 0x0022 (QUOTATION MARK). StringBuilder builder; builder.append('"'); + + // 2. For each code point C of StringToCodePoints(value), do auto utf_view = Utf8View(string); for (auto code_point : utf_view) { + // a. If C is listed in the “Code Point” column of Table 70, then + // i. Set product to the string-concatenation of product and the escape sequence for C as specified in the “Escape Sequence” column of the corresponding row. switch (code_point) { case '\b': builder.append("\\b"sv); @@ -377,14 +382,23 @@ DeprecatedString JSONObject::quote_json_string(DeprecatedString string) builder.append("\\\\"sv); break; default: + // b. Else if C has a numeric value less than 0x0020 (SPACE), or if C has the same numeric value as a leading surrogate or trailing surrogate, then if (code_point < 0x20 || Utf16View::is_high_surrogate(code_point) || Utf16View::is_low_surrogate(code_point)) { + // i. Let unit be the code unit whose numeric value is that of C. + // ii. Set product to the string-concatenation of product and UnicodeEscape(unit). builder.appendff("\\u{:04x}", code_point); - } else { + } + // c. Else, + else { + // i. Set product to the string-concatenation of product and UTF16EncodeCodePoint(C). builder.append_code_point(code_point); } } } + // 3. Set product to the string-concatenation of product and the code unit 0x0022 (QUOTATION MARK). builder.append('"'); + + // 4. Return product. return builder.to_deprecated_string(); } |