From 7e1712bac3bb7c2d2d406c8b680c74b564384437 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 28 Aug 2022 21:22:51 +0700 Subject: lib/json: remove solidus (slash) being escaped/unescaped The standard json package does not escape the solidus, even though the RFC said so. Someone also report this as an error in RFC [1] by removing solidus from list of escaped characters but the author itself reject it. [1] https://www.rfc-editor.org/errata/eid3159 --- lib/json/json.go | 9 ++++----- lib/json/json_test.go | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/json/json.go b/lib/json/json.go index bd8bd204..bbd2be16 100644 --- a/lib/json/json.go +++ b/lib/json/json.go @@ -21,7 +21,6 @@ const ( const ( bDoubleQuote = '"' bRevSolidus = '\\' - bSolidus = '/' bBackspace = '\b' bFormFeed = '\f' bLineFeed = '\n' @@ -30,7 +29,7 @@ const ( ) // Escape the following character: `"` (quotation mark), -// `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), +// `\` (reverse solidus), `\b` (backspace), `\f` (formfeed), // `\n` (newline), `\r` (carriage return`), `\t` (horizontal tab), and control // character from 0 - 31. // @@ -41,7 +40,7 @@ func Escape(in []byte) []byte { var buf bytes.Buffer for x := 0; x < len(in); x++ { - if in[x] == bDoubleQuote || in[x] == bRevSolidus || in[x] == bSolidus { + if in[x] == bDoubleQuote || in[x] == bRevSolidus { buf.WriteByte(bRevSolidus) buf.WriteByte(in[x]) continue @@ -83,7 +82,7 @@ func Escape(in []byte) []byte { } // EscapeString escape the following character: `"` (quotation mark), -// `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), +// `\` (reverse solidus), `\b` (backspace), `\f` (formfeed), // `\n` (newline), `\r` (carriage return`), `\t` (horizontal tab), and control // character from 0 - 31. // @@ -220,7 +219,7 @@ func Unescape(in []byte, strict bool) ([]byte, error) { esc = false continue } - if in[x] == bDoubleQuote || in[x] == bRevSolidus || in[x] == bSolidus { + if in[x] == bDoubleQuote || in[x] == bRevSolidus { buf.WriteByte(in[x]) esc = false continue diff --git a/lib/json/json_test.go b/lib/json/json_test.go index faab6899..bd246782 100644 --- a/lib/json/json_test.go +++ b/lib/json/json_test.go @@ -12,7 +12,7 @@ import ( func TestEscape(t *testing.T) { in := []byte("\"\\/\b\f\n\r\t") - exp := []byte(`\"\\\/\b\f\n\r\t`) + exp := []byte(`\"\\/\b\f\n\r\t`) got := Escape(in) test.Assert(t, "Escape", exp, got) } @@ -27,7 +27,7 @@ func TestEscapeString(t *testing.T) { }, { in: ` this\ is //\"☺"`, - exp: `\tthis\\ is\n\t\t\/\/\\\"☺\"`, + exp: `\tthis\\ is\n\t\t//\\\"☺\"`, }, { in: `  `, exp: `\u0002\b\f\u000E\u000F\u0010\u0014\u001E\u001F `, }} -- cgit v1.3