From 7a938da20e8d243bc254cd821b7cf61b379be4a6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 15 Feb 2023 15:10:00 -0700 Subject: reencode: Rethink the UTF-8 buffer --- reencode.go | 95 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 29 deletions(-) (limited to 'reencode.go') diff --git a/reencode.go b/reencode.go index 0745c43..a33cc8f 100644 --- a/reencode.go +++ b/reencode.go @@ -169,6 +169,46 @@ var ( _ io.Closer = (*ReEncoder)(nil) ) +func (enc *ReEncoder) getRuneFromBytes(str []byte, pos int) (c rune, size int, full bool) { + if pos < enc.bufLen { + var tmp [utf8.UTFMax]byte + n := copy(tmp[:], enc.buf[pos:enc.bufLen]) + n += copy(tmp[n:], str) + c, size := utf8.DecodeRune(tmp[:n]) + if c == utf8.RuneError && size <= 1 { + return c, size, utf8.FullRune(tmp[:n]) + } + return c, size, true + } else { + tmp := str[pos-enc.bufLen:] + c, size := utf8.DecodeRune(tmp) + if c == utf8.RuneError && size <= 1 { + return c, size, utf8.FullRune(tmp) + } + return c, size, true + } +} + +func (enc *ReEncoder) getRuneFromString(str string, pos int) (c rune, size int, full bool) { + if pos < enc.bufLen { + var tmp [utf8.UTFMax]byte + n := copy(tmp[:], enc.buf[pos:enc.bufLen]) + n += copy(tmp[n:], str) + c, size := utf8.DecodeRune(tmp[:n]) + if c == utf8.RuneError && size <= 1 { + return c, size, utf8.FullRune(tmp[:n]) + } + return c, size, true + } else { + tmp := str[pos-enc.bufLen:] + c, size := utf8.DecodeRuneInString(tmp) + if c == utf8.RuneError && size <= 1 { + return c, size, utf8.FullRuneInString(tmp) + } + return c, size, true + } +} + // Write implements io.Writer; it does what you'd expect. // // It is worth noting that Write returns the number of bytes consumed @@ -177,59 +217,56 @@ var ( // but *ReEncoder does because it transforms the data written to it, // and the number of bytes written may be wildly different than the // number of bytes handled. -func (enc *ReEncoder) Write(p []byte) (int, error) { - if len(p) == 0 { +func (enc *ReEncoder) Write(str []byte) (int, error) { + if len(str) == 0 { return 0, nil } var n int - if enc.bufLen > 0 { - copy(enc.buf[enc.bufLen:], p) - c, size := utf8.DecodeRune(enc.buf[:]) - n += size - enc.bufLen - enc.bufLen = 0 - enc.handleRune(c, size) - if enc.err != nil { - return 0, enc.err + for { + c, size, full := enc.getRuneFromBytes(str, n) + if !full { + if n < enc.bufLen { + l := copy(enc.buf[:], enc.buf[n:enc.bufLen]) + l += copy(enc.buf[l:], str) + enc.bufLen = l + } else { + enc.bufLen = copy(enc.buf[:], str[n-enc.bufLen:]) + } + return len(str), nil } - } - for utf8.FullRune(p[n:]) { - c, size := utf8.DecodeRune(p[n:]) enc.handleRune(c, size) if enc.err != nil { return n, enc.err } n += size } - enc.bufLen = copy(enc.buf[:], p[n:]) - return len(p), nil } // WriteString implements io.StringWriter; it does what you'd expect, // but see the notes on the Write method. -func (enc *ReEncoder) WriteString(p string) (int, error) { - if len(p) == 0 { +func (enc *ReEncoder) WriteString(str string) (int, error) { + if len(str) == 0 { return 0, nil } var n int - if enc.bufLen > 0 { - copy(enc.buf[enc.bufLen:], p) - c, size := utf8.DecodeRune(enc.buf[:]) - n += size - enc.bufLen - enc.bufLen = 0 - enc.handleRune(c, size) - if enc.err != nil { - return 0, enc.err + for { + c, size, full := enc.getRuneFromString(str, n) + if !full { + if n < enc.bufLen { + l := copy(enc.buf[:], enc.buf[n:enc.bufLen]) + l += copy(enc.buf[l:], str) + enc.bufLen = l + } else { + enc.bufLen = copy(enc.buf[:], str[n-enc.bufLen:]) + } + return len(str), nil } - } - for utf8.FullRuneInString(p[n:]) { - c, size := utf8.DecodeRuneInString(p[n:]) enc.handleRune(c, size) if enc.err != nil { return n, enc.err } n += size } - return len(p), nil } // WriteByte implements io.ByteWriter; it does what you'd expect. -- cgit v1.2.3-54-g00ecf From 38989a9c4f69abfe04c3eb4ec3382be88802141c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 15 Feb 2023 23:53:59 -0700 Subject: reencode: Fix .stackSize --- reencode.go | 4 ++-- reencode_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'reencode.go') diff --git a/reencode.go b/reencode.go index a33cc8f..fd848f8 100644 --- a/reencode.go +++ b/reencode.go @@ -364,8 +364,8 @@ func (enc *ReEncoder) popWriteBarrier() { func (enc *ReEncoder) stackSize() int { sz := enc.par.StackSize() - for _, barrier := range enc.barriers { - sz += barrier.stackSize + if len(enc.barriers) > 0 { + sz += enc.barriers[len(enc.barriers)-1].stackSize } return sz } diff --git a/reencode_test.go b/reencode_test.go index 82a1861..bc6d246 100644 --- a/reencode_test.go +++ b/reencode_test.go @@ -9,6 +9,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "git.lukeshu.com/go/lowmemjson/internal/fastio" ) func TestReEncode(t *testing.T) { @@ -222,3 +224,17 @@ func TestReEncodeWriteSize(t *testing.T) { assert.Equal(t, input, out.String()) }) } + +func TestReEncoderStackSize(t *testing.T) { + t.Parallel() + + enc := NewReEncoder(fastio.Discard, ReEncoderConfig{}) + assert.Equal(t, 0, enc.stackSize()) + + for i := 0; i < 5; i++ { + assert.NoError(t, enc.WriteByte('[')) + assert.Equal(t, i+1, enc.stackSize()) + enc.pushWriteBarrier() + assert.Equal(t, i+2, enc.stackSize()) + } +} -- cgit v1.2.3-54-g00ecf From dfc67cecbd95344d296c31b537fa3ae8aec8c292 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 14 Feb 2023 22:36:25 -0700 Subject: encode, reencode: Fix handling of invalid UTF-8 --- ReleaseNotes.md | 24 ++++++++ compat/json/compat.go | 5 +- compat/json/compat_test.go | 45 ++++++++++++--- compat/json/testcompat_test.go | 5 +- encode.go | 34 +++++------ encode_escape.go | 37 +++++++++--- errors.go | 3 +- internal/jsonstring/encode_string.go | 65 +++++++++++++++++++-- reencode.go | 107 +++++++++++++++++++++++------------ 9 files changed, 247 insertions(+), 78 deletions(-) (limited to 'reencode.go') diff --git a/ReleaseNotes.md b/ReleaseNotes.md index d9a671a..b1647da 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -19,10 +19,34 @@ - Unicode: + + Feature: Encoder, ReEncoder: Add an `InvalidUTF8` + ReEncoderConfig option and `BackslashEscapeRawByte` + BackslashEscapeMode to allow emitted strings to contain + invalid UTF-8. + + + Change: EscapeDefault, EscapeDefaultNonHTMLSafe: No longer + force long Unicode `\uXXXX` sequences for the U+FFFD Unicode + replacement character. + + + Change: Encoder: Unless overridden by the BackslashEscaper, + now by default uses `\uXXXX` sequences when emitting the + U+FFFD Unicode replacement character in place of invalid + UTF-8. + + Bugfix: Encoder, ReEncoder: Fix an issue with decoding UTF-8 that when a codepoint straddles a write boundary it is interpreted as a sequence of U+FFFD runes. + + Bugfix: compat/json.Valid: Do not consider JSON containing + invalid UTF-8 to be valid (this is different than + `encoding/json` at the time of this writing; but I consider + that to be a bug in `encoding/json`; [go#58517][]). + + + Bugfix: compat/json.Compact, compat/json.Indent: Don't munge + invalid UTF-8 in strings; as `encoding/json` doesn't. + + [go#58517]: https://github.com/golang/go/issues/58517 + # v0.3.6 (2023-02-16) Theme: Architectural improvements diff --git a/compat/json/compat.go b/compat/json/compat.go index 1cdbf0b..d326514 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -160,6 +160,7 @@ func Compact(dst *bytes.Buffer, src []byte) error { start := dst.Len() err := reencode(dst, src, lowmemjson.ReEncoderConfig{ Compact: true, + InvalidUTF8: lowmemjson.InvalidUTF8Preserve, BackslashEscape: lowmemjson.EscapePreserve, }) if err != nil { @@ -173,6 +174,7 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { err := reencode(dst, src, lowmemjson.ReEncoderConfig{ Indent: indent, Prefix: prefix, + InvalidUTF8: lowmemjson.InvalidUTF8Preserve, BackslashEscape: lowmemjson.EscapePreserve, }) if err != nil { @@ -183,7 +185,8 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { func Valid(data []byte) bool { formatter := lowmemjson.NewReEncoder(io.Discard, lowmemjson.ReEncoderConfig{ - Compact: true, + Compact: true, + InvalidUTF8: lowmemjson.InvalidUTF8Error, }) if _, err := formatter.Write(data); err != nil { return false diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index d513c27..d989a4d 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -18,10 +18,11 @@ func TestCompatValid(t *testing.T) { Exp bool } testcases := map[string]testcase{ - "empty": {In: ``, Exp: false}, - "num": {In: `1`, Exp: true}, - "trunc": {In: `{`, Exp: false}, - "object": {In: `{}`, Exp: true}, + "empty": {In: ``, Exp: false}, + "num": {In: `1`, Exp: true}, + "trunc": {In: `{`, Exp: false}, + "object": {In: `{}`, Exp: true}, + "non-utf8": {In: "\"\x85\xcd\"", Exp: false}, // https://github.com/golang/go/issues/58517 } for tcName, tc := range testcases { tc := tc @@ -42,8 +43,9 @@ func TestCompatCompact(t *testing.T) { Err string } testcases := map[string]testcase{ - "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, - "object": {In: `{}`, Out: `{}`}, + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, } for tcName, tc := range testcases { tc := tc @@ -70,8 +72,9 @@ func TestCompatIndent(t *testing.T) { Err string } testcases := map[string]testcase{ - "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, - "object": {In: `{}`, Out: `{}`}, + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, } for tcName, tc := range testcases { tc := tc @@ -89,3 +92,29 @@ func TestCompatIndent(t *testing.T) { }) } } + +func TestCompatMarshal(t *testing.T) { + t.Parallel() + type testcase struct { + In any + Out string + Err string + } + testcases := map[string]testcase{ + "non-utf8": {In: "\x85\xcd", Out: "\"\\ufffd\\ufffd\""}, + "urc": {In: "\ufffd", Out: "\"\ufffd\""}, + } + for tcName, tc := range testcases { + tc := tc + t.Run(tcName, func(t *testing.T) { + t.Parallel() + out, err := Marshal(tc.In) + assert.Equal(t, tc.Out, string(out)) + if tc.Err == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.Err) + } + }) + } +} diff --git a/compat/json/testcompat_test.go b/compat/json/testcompat_test.go index 42cbf5c..e89b4b4 100644 --- a/compat/json/testcompat_test.go +++ b/compat/json/testcompat_test.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "io" + "reflect" _ "unsafe" "git.lukeshu.com/go/lowmemjson" @@ -59,13 +60,13 @@ type encodeState struct { } func (es *encodeState) string(str string, _ bool) { - if err := jsonstring.EncodeStringFromString(&es.Buffer, lowmemjson.EscapeDefault, str); err != nil { + if err := jsonstring.EncodeStringFromString(&es.Buffer, lowmemjson.EscapeDefault, 0, reflect.Value{}, str); err != nil { panic(err) } } func (es *encodeState) stringBytes(str []byte, _ bool) { - if err := jsonstring.EncodeStringFromBytes(&es.Buffer, lowmemjson.EscapeDefault, str); err != nil { + if err := jsonstring.EncodeStringFromBytes(&es.Buffer, lowmemjson.EscapeDefault, 0, reflect.Value{}, str); err != nil { panic(err) } } diff --git a/encode.go b/encode.go index 00d3dad..684cc75 100644 --- a/encode.go +++ b/encode.go @@ -87,7 +87,7 @@ func (enc *Encoder) Encode(obj any) (err error) { if escaper == nil { escaper = EscapeDefault } - if err := encode(enc.w, reflect.ValueOf(obj), escaper, false, 0, map[any]struct{}{}); err != nil { + if err := encode(enc.w, reflect.ValueOf(obj), escaper, enc.w.utf, false, 0, map[any]struct{}{}); err != nil { if rwe, ok := err.(*ReEncodeWriteError); ok { err = &EncodeWriteError{ Err: rwe.Err, @@ -108,7 +108,7 @@ func discardInt(_ int, err error) error { const startDetectingCyclesAfter = 1000 -func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote bool, cycleDepth uint, cycleSeen map[any]struct{}) error { +func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, utf InvalidUTF8Mode, quote bool, cycleDepth uint, cycleSeen map[any]struct{}) error { if !val.IsValid() { return discardInt(w.WriteString("null")) } @@ -197,7 +197,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo Err: err, } } - if err := jsonstring.EncodeStringFromBytes(w, escaper, text); err != nil { + if err := jsonstring.EncodeStringFromBytes(w, escaper, utf, val, text); err != nil { return err } default: @@ -309,14 +309,14 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo } else { if quote { var buf bytes.Buffer - if err := jsonstring.EncodeStringFromString(&buf, escaper, val.String()); err != nil { + if err := jsonstring.EncodeStringFromString(&buf, escaper, utf, val, val.String()); err != nil { return err } - if err := jsonstring.EncodeStringFromBytes(w, escaper, buf.Bytes()); err != nil { + if err := jsonstring.EncodeStringFromBytes(w, escaper, utf, val, buf.Bytes()); err != nil { return err } } else { - if err := jsonstring.EncodeStringFromString(w, escaper, val.String()); err != nil { + if err := jsonstring.EncodeStringFromString(w, escaper, utf, val, val.String()); err != nil { return err } } @@ -327,7 +327,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo return err } } else { - if err := encode(w, val.Elem(), escaper, quote, cycleDepth, cycleSeen); err != nil { + if err := encode(w, val.Elem(), escaper, utf, quote, cycleDepth, cycleSeen); err != nil { return err } } @@ -350,13 +350,13 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo } } empty = false - if err := jsonstring.EncodeStringFromString(w, escaper, field.Name); err != nil { + if err := jsonstring.EncodeStringFromString(w, escaper, utf, val, field.Name); err != nil { return err } if err := w.WriteByte(':'); err != nil { return err } - if err := encode(w, fVal, escaper, field.Quote, cycleDepth, cycleSeen); err != nil { + if err := encode(w, fVal, escaper, utf, field.Quote, cycleDepth, cycleSeen); err != nil { return err } } @@ -394,7 +394,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo for i := 0; iter.Next(); i++ { // TODO: Avoid buffering the map key var k strings.Builder - if err := encode(NewReEncoder(&k, ReEncoderConfig{BackslashEscape: escaper}), iter.Key(), escaper, false, cycleDepth, cycleSeen); err != nil { + if err := encode(NewReEncoder(&k, ReEncoderConfig{BackslashEscape: escaper, InvalidUTF8: utf}), iter.Key(), escaper, utf, false, cycleDepth, cycleSeen); err != nil { return err } kStr := k.String() @@ -403,7 +403,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo } if !strings.HasPrefix(kStr, `"`) { k.Reset() - if err := jsonstring.EncodeStringFromString(&k, escaper, kStr); err != nil { + if err := jsonstring.EncodeStringFromString(&k, escaper, utf, iter.Key(), kStr); err != nil { return err } kStr = k.String() @@ -427,7 +427,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo if err := w.WriteByte(':'); err != nil { return err } - if err := encode(w, kv.V, escaper, false, cycleDepth, cycleSeen); err != nil { + if err := encode(w, kv.V, escaper, utf, false, cycleDepth, cycleSeen); err != nil { return err } } @@ -491,12 +491,12 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo cycleSeen[ptr] = struct{}{} defer delete(cycleSeen, ptr) } - if err := encodeArray(w, val, escaper, cycleDepth, cycleSeen); err != nil { + if err := encodeArray(w, val, escaper, utf, cycleDepth, cycleSeen); err != nil { return err } } case reflect.Array: - if err := encodeArray(w, val, escaper, cycleDepth, cycleSeen); err != nil { + if err := encodeArray(w, val, escaper, utf, cycleDepth, cycleSeen); err != nil { return err } case reflect.Pointer: @@ -516,7 +516,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo cycleSeen[ptr] = struct{}{} defer delete(cycleSeen, ptr) } - if err := encode(w, val.Elem(), escaper, quote, cycleDepth, cycleSeen); err != nil { + if err := encode(w, val.Elem(), escaper, utf, quote, cycleDepth, cycleSeen); err != nil { return err } } @@ -529,7 +529,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo return nil } -func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, cycleDepth uint, cycleSeen map[any]struct{}) error { +func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, utf InvalidUTF8Mode, cycleDepth uint, cycleSeen map[any]struct{}) error { if err := w.WriteByte('['); err != nil { return err } @@ -540,7 +540,7 @@ func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, cycl return err } } - if err := encode(w, val.Index(i), escaper, false, cycleDepth, cycleSeen); err != nil { + if err := encode(w, val.Index(i), escaper, utf, false, cycleDepth, cycleSeen); err != nil { return err } } diff --git a/encode_escape.go b/encode_escape.go index 97da6e9..c9e2bc9 100644 --- a/encode_escape.go +++ b/encode_escape.go @@ -6,12 +6,29 @@ package lowmemjson import ( "fmt" - "unicode/utf8" "git.lukeshu.com/go/lowmemjson/internal/jsonstring" ) -// BackslashEscapeMode identifies one of the three ways that a +// InvalidUTF8Mode identifies one of the 3 ways that an Encoder or +// ReEncoder can behave when encountering invalid UTF-8 in a string +// value: +// +// - Replace the byte with the Unicode replacement character U+FFFD. +// +// - Allow the byte through to the string-encoder, with an +// escape-mode of BackslashEscapeRawByte. +// +// - Emit a syntax error. +type InvalidUTF8Mode = jsonstring.InvalidUTF8Mode + +const ( + InvalidUTF8Replace = jsonstring.InvalidUTF8Replace + InvalidUTF8Preserve = jsonstring.InvalidUTF8Preserve + InvalidUTF8Error = jsonstring.InvalidUTF8Error +) + +// BackslashEscapeMode identifies one of the four ways that a // character may be represented in a JSON string: // // - literally (no backslash escaping) @@ -20,12 +37,18 @@ import ( // single-character) // // - as a long Unicode `\uXXXX` backslash sequence +// +// - as a raw byte; this allows you to emit invalid JSON; JSON must +// be valid UTF-8, but this allows you to emit arbitrary binary +// data. If the character does not satisfy `utf8.RuneSelf <= char +// <= 0xFF`, then the encoder will panic. type BackslashEscapeMode = jsonstring.BackslashEscapeMode const ( BackslashEscapeNone = jsonstring.BackslashEscapeNone BackslashEscapeShort = jsonstring.BackslashEscapeShort BackslashEscapeUnicode = jsonstring.BackslashEscapeUnicode + BackslashEscapeRawByte = jsonstring.BackslashEscapeRawByte ) func hexToInt(c byte) rune { @@ -96,14 +119,13 @@ func EscapeHTMLSafe(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode // behavior of encoding/json. // // It is like EscapeHTMLSafe, but also uses long Unicode `\uXXXX` -// sequences for `\b`, `\f`, and the `\uFFFD` Unicode replacement -// character. +// sequences for `\b` and `\f` // // A ReEncoder uses EscapeDefault if a BackslashEscaper is not // specified. func EscapeDefault(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { switch c { - case '\b', '\f', utf8.RuneError: + case '\b', '\f': return BackslashEscapeUnicode default: return EscapeHTMLSafe(c, wasEscaped) @@ -115,11 +137,10 @@ func EscapeDefault(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { // SetEscapeHTML(false) called on it. // // It is like EscapeJSSafe, but also uses long Unicode `\uXXXX` -// sequences for `\b`, `\f`, and the `\uFFFD` Unicode replacement -// character. +// sequences for `\b` and `\f`. func EscapeDefaultNonHTMLSafe(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { switch c { - case '\b', '\f', utf8.RuneError: + case '\b', '\f': return BackslashEscapeUnicode default: return EscapeJSSafe(c, wasEscaped) diff --git a/errors.go b/errors.go index 516018c..da9de4d 100644 --- a/errors.go +++ b/errors.go @@ -142,7 +142,8 @@ func (e *EncodeWriteError) Unwrap() error { return e.Err } type EncodeTypeError = json.UnsupportedTypeError // An EncodeValueError is returned by Encode when attempting to encode -// an unsupported value (such as a datastructure with a cycle). +// an unsupported value (such as a datastructure with a cycle, or (if +// InvalidUTF8=InvalidUTF8Error) a string with invalid UTF-8). // // type UnsupportedValueError struct { // Value reflect.Value diff --git a/internal/jsonstring/encode_string.go b/internal/jsonstring/encode_string.go index fec2cc0..76bbb38 100644 --- a/internal/jsonstring/encode_string.go +++ b/internal/jsonstring/encode_string.go @@ -5,14 +5,25 @@ package jsonstring import ( + "encoding/json" "fmt" "io" + "reflect" "unicode/utf8" "git.lukeshu.com/go/lowmemjson/internal/fastio" "git.lukeshu.com/go/lowmemjson/internal/fastio/noescape" ) +// InvalidUTF8Mode is describe in the main lowmemjson package docs. +type InvalidUTF8Mode uint8 + +const ( + InvalidUTF8Replace InvalidUTF8Mode = iota + InvalidUTF8Preserve + InvalidUTF8Error +) + // BackslashEscapeMode is describe in the main lowmemjson package // docs. type BackslashEscapeMode uint8 @@ -21,6 +32,7 @@ const ( BackslashEscapeNone BackslashEscapeMode = iota BackslashEscapeShort BackslashEscapeUnicode + BackslashEscapeRawByte ) // BackslashEscaper is describe in the main lowmemjson package docs. @@ -96,19 +108,45 @@ func WriteStringChar(w fastio.AllWriter, c rune, escape BackslashEscapeMode) err default: // obey return writeStringUnicodeEscape(w, c) } + case BackslashEscapeRawByte: + switch { + case c < utf8.RuneSelf: + panic(fmt.Errorf("escaper returned BackslashEscapeRawByte for a character=%q < utf8.RuneSelf", c)) + case c > 0xFF: + panic(fmt.Errorf("escaper returned BackslashEscapeRawByte for a character=%q > 0xFF", c)) + default: + return w.WriteByte(byte(c)) + } default: - panic("escaper returned an invalid escape mode") + panic(fmt.Errorf("escaper returned an invalid escape mode=%d", escape)) } } -func EncodeStringFromString(w fastio.AllWriter, escaper BackslashEscaper, str string) error { +func EncodeStringFromString(w fastio.AllWriter, escaper BackslashEscaper, utf InvalidUTF8Mode, val reflect.Value, str string) error { if err := w.WriteByte('"'); err != nil { return err } - for _, c := range str { - if err := WriteStringChar(w, c, escaper(c, BackslashEscapeNone)); err != nil { + for i := 0; i < len(str); { + escaped := BackslashEscapeNone + c, size := utf8.DecodeRuneInString(str[i:]) + if c == utf8.RuneError && size == 1 { + switch utf { + case InvalidUTF8Replace: + escaped = BackslashEscapeUnicode + case InvalidUTF8Preserve: + escaped = BackslashEscapeRawByte + c = rune(str[i]) + case InvalidUTF8Error: + return &json.UnsupportedValueError{ + Value: val, + Str: fmt.Sprintf("invalid UTF-8 at byte offset %d: %#02x", i, str[i]), + } + } + } + if err := WriteStringChar(w, c, escaper(c, escaped)); err != nil { return err } + i += size } if err := w.WriteByte('"'); err != nil { return err @@ -116,13 +154,28 @@ func EncodeStringFromString(w fastio.AllWriter, escaper BackslashEscaper, str st return nil } -func EncodeStringFromBytes(w fastio.AllWriter, escaper BackslashEscaper, str []byte) error { +func EncodeStringFromBytes(w fastio.AllWriter, escaper BackslashEscaper, utf InvalidUTF8Mode, val reflect.Value, str []byte) error { if err := w.WriteByte('"'); err != nil { return err } for i := 0; i < len(str); { + escaped := BackslashEscapeNone c, size := utf8.DecodeRune(str[i:]) - if err := WriteStringChar(w, c, escaper(c, BackslashEscapeNone)); err != nil { + if c == utf8.RuneError && size == 1 { + switch utf { + case InvalidUTF8Replace: + escaped = BackslashEscapeUnicode + case InvalidUTF8Preserve: + escaped = BackslashEscapeRawByte + c = rune(str[i]) + case InvalidUTF8Error: + return &json.UnsupportedValueError{ + Value: val, + Str: fmt.Sprintf("invalid UTF-8 at byte offset %d: %#02x", i, str[i]), + } + } + } + if err := WriteStringChar(w, c, escaper(c, escaped)); err != nil { return err } i += size diff --git a/reencode.go b/reencode.go index fd848f8..1a9999b 100644 --- a/reencode.go +++ b/reencode.go @@ -54,6 +54,13 @@ type ReEncoderConfig struct { // this is different than the usual behavior. ForceTrailingNewlines bool + // A JSON document is specified to be a sequence of Unicode + // codepoints; InvalidUTF8 controls how the *ReEncoder behaves + // when it encounters invalid UTF-8 bytes in a JSON string + // (i.e. the string is not representable as a sequence of + // Unicode codepoints, and thus the document is invalid JSON). + InvalidUTF8 InvalidUTF8Mode + // Returns whether a given character in a string should be // backslash-escaped. The bool argument is whether it was // \u-escaped in the input. This does not affect characters @@ -119,6 +126,7 @@ func NewReEncoder(out io.Writer, cfg ReEncoderConfig) *ReEncoder { return &ReEncoder{ out: module, esc: escaper, + utf: cfg.InvalidUTF8, allowMultipleValues: cfg.AllowMultipleValues, } } @@ -134,6 +142,7 @@ func NewReEncoder(out io.Writer, cfg ReEncoderConfig) *ReEncoder { type ReEncoder struct { out reEncoderModule esc BackslashEscaper + utf InvalidUTF8Mode allowMultipleValues bool // state: .Write's/.WriteString's/.WriteRune's utf8-decoding buffer @@ -169,43 +178,54 @@ var ( _ io.Closer = (*ReEncoder)(nil) ) -func (enc *ReEncoder) getRuneFromBytes(str []byte, pos int) (c rune, size int, full bool) { +func (enc *ReEncoder) getRuneFromBytes(str []byte, pos int) (c rune, size int, full, isRune bool) { + var tmp []byte if pos < enc.bufLen { - var tmp [utf8.UTFMax]byte - n := copy(tmp[:], enc.buf[pos:enc.bufLen]) - n += copy(tmp[n:], str) - c, size := utf8.DecodeRune(tmp[:n]) - if c == utf8.RuneError && size <= 1 { - return c, size, utf8.FullRune(tmp[:n]) - } - return c, size, true + var buf [utf8.UTFMax]byte + n := copy(buf[:], enc.buf[pos:enc.bufLen]) + n += copy(buf[n:], str) + tmp = buf[:n] } else { - tmp := str[pos-enc.bufLen:] - c, size := utf8.DecodeRune(tmp) - if c == utf8.RuneError && size <= 1 { - return c, size, utf8.FullRune(tmp) - } - return c, size, true + tmp = str[pos-enc.bufLen:] + } + c, size = utf8.DecodeRune(tmp) + switch { + case c == utf8.RuneError && size <= 1 && !utf8.FullRune(tmp): + return c, size, false, true + case c == utf8.RuneError && size == 1 && enc.utf != InvalidUTF8Replace: + return rune(tmp[0]), 1, true, false + default: + return c, size, true, true } } -func (enc *ReEncoder) getRuneFromString(str string, pos int) (c rune, size int, full bool) { +func (enc *ReEncoder) getRuneFromString(str string, pos int) (c rune, size int, full, isRune bool) { if pos < enc.bufLen { - var tmp [utf8.UTFMax]byte - n := copy(tmp[:], enc.buf[pos:enc.bufLen]) - n += copy(tmp[n:], str) - c, size := utf8.DecodeRune(tmp[:n]) - if c == utf8.RuneError && size <= 1 { - return c, size, utf8.FullRune(tmp[:n]) + var buf [utf8.UTFMax]byte + var tmp []byte + n := copy(buf[:], enc.buf[pos:enc.bufLen]) + n += copy(buf[n:], str) + tmp = buf[:n] + c, size = utf8.DecodeRune(tmp) + switch { + case c == utf8.RuneError && size <= 1 && !utf8.FullRune(tmp): + return c, size, false, true + case c == utf8.RuneError && size == 1 && enc.utf != InvalidUTF8Replace: + return rune(tmp[0]), 1, true, false + default: + return c, size, true, true } - return c, size, true } else { tmp := str[pos-enc.bufLen:] c, size := utf8.DecodeRuneInString(tmp) - if c == utf8.RuneError && size <= 1 { - return c, size, utf8.FullRuneInString(tmp) + switch { + case c == utf8.RuneError && size <= 1 && !utf8.FullRuneInString(tmp): + return c, size, false, true + case c == utf8.RuneError && size == 1 && enc.utf != InvalidUTF8Replace: + return rune(tmp[0]), 1, true, false + default: + return c, size, true, true } - return c, size, true } } @@ -223,7 +243,7 @@ func (enc *ReEncoder) Write(str []byte) (int, error) { } var n int for { - c, size, full := enc.getRuneFromBytes(str, n) + c, size, full, isRune := enc.getRuneFromBytes(str, n) if !full { if n < enc.bufLen { l := copy(enc.buf[:], enc.buf[n:enc.bufLen]) @@ -234,7 +254,13 @@ func (enc *ReEncoder) Write(str []byte) (int, error) { } return len(str), nil } - enc.handleRune(c, size) + if enc.utf == InvalidUTF8Error && !isRune { + return n, &ReEncodeSyntaxError{ + Offset: enc.inputPos, + Err: fmt.Errorf("invalid UTF-8: %#02x", c), + } + } + enc.handleRune(c, size, isRune) if enc.err != nil { return n, enc.err } @@ -250,7 +276,7 @@ func (enc *ReEncoder) WriteString(str string) (int, error) { } var n int for { - c, size, full := enc.getRuneFromString(str, n) + c, size, full, isRune := enc.getRuneFromString(str, n) if !full { if n < enc.bufLen { l := copy(enc.buf[:], enc.buf[n:enc.bufLen]) @@ -261,7 +287,13 @@ func (enc *ReEncoder) WriteString(str string) (int, error) { } return len(str), nil } - enc.handleRune(c, size) + if enc.utf == InvalidUTF8Error && !isRune { + return n, &ReEncodeSyntaxError{ + Offset: enc.inputPos, + Err: fmt.Errorf("invalid UTF-8: %#02x", c), + } + } + enc.handleRune(c, size, isRune) if enc.err != nil { return n, enc.err } @@ -298,7 +330,7 @@ func (enc *ReEncoder) Close() error { return enc.err } if len(enc.barriers) == 0 { - if err := enc.handleRuneType(0, jsonparse.RuneTypeEOF, enc.stackSize()); err != nil { + if err := enc.handleRuneType(0, jsonparse.RuneTypeEOF, enc.stackSize(), true); err != nil { enc.err = &ReEncodeWriteError{ Err: err, Offset: enc.inputPos, @@ -312,7 +344,8 @@ func (enc *ReEncoder) Close() error { return nil } -func (enc *ReEncoder) handleRune(c rune, size int) { +// isRune=false indicates that 'c' is a raw byte from invalid UTF-8. +func (enc *ReEncoder) handleRune(c rune, size int, isRune bool) { t, err := enc.par.HandleRune(c) if err != nil { enc.err = &ReEncodeSyntaxError{ @@ -321,7 +354,7 @@ func (enc *ReEncoder) handleRune(c rune, size int) { } return } - if err := enc.handleRuneType(c, t, enc.stackSize()); err != nil { + if err := enc.handleRuneType(c, t, enc.stackSize(), isRune); err != nil { enc.err = &ReEncodeWriteError{ Err: err, Offset: enc.inputPos, @@ -370,7 +403,7 @@ func (enc *ReEncoder) stackSize() int { return sz } -func (enc *ReEncoder) handleRuneType(c rune, t jsonparse.RuneType, stackSize int) error { +func (enc *ReEncoder) handleRuneType(c rune, t jsonparse.RuneType, stackSize int, isRune bool) error { switch t { case jsonparse.RuneTypeStringEsc, jsonparse.RuneTypeStringEscU: return nil @@ -410,6 +443,10 @@ func (enc *ReEncoder) handleRuneType(c rune, t jsonparse.RuneType, stackSize int if t > jsonparse.RuneTypeEOF { panic(fmt.Errorf("should not happen: handleRune called with %#v", t)) } - return enc.out.HandleRune(c, t, BackslashEscapeNone, stackSize) + esc := BackslashEscapeNone + if !isRune { + esc = BackslashEscapeRawByte + } + return enc.out.HandleRune(c, t, esc, stackSize) } } -- cgit v1.2.3-54-g00ecf From 1a5b0561f53441d8a259a5096281699b5af16a6c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 16 Feb 2023 16:53:53 -0700 Subject: reencode: Add CompactFloats --- ReleaseNotes.md | 9 ++++++++- compat/json/compat_test.go | 3 +++ reencode.go | 10 ++++++++-- reencode_test.go | 6 ++++-- 4 files changed, 23 insertions(+), 5 deletions(-) (limited to 'reencode.go') diff --git a/ReleaseNotes.md b/ReleaseNotes.md index b1647da..ae147b1 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -4,11 +4,15 @@ User-facing changes: - - General bugfixes: + - General Changes: + Encoder, ReEncoder: Now correctly trims unnecessary the trailing '0's from the fraction-part when compacting numbers. + + ReEncoder: No longer compact floating-point numbers by + default, add a `CompactFloats` ReEncoderConfig option to + control this. + - Compatibility bugfixes: + compat/json.Valid: No longer considers truncated JSON @@ -17,6 +21,9 @@ + compat/json.Compact, compat/json.Indent: Don't write to the destination buffer if there is a syntax error. + + compat/json.Compact, compat/json.Indent: No longer compact + floating-point numbers; as `encoding/json` doesn't. + - Unicode: + Feature: Encoder, ReEncoder: Add an `InvalidUTF8` diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index d989a4d..128bd1b 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -46,6 +46,7 @@ func TestCompatCompact(t *testing.T) { "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, } for tcName, tc := range testcases { tc := tc @@ -75,6 +76,7 @@ func TestCompatIndent(t *testing.T) { "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, } for tcName, tc := range testcases { tc := tc @@ -103,6 +105,7 @@ func TestCompatMarshal(t *testing.T) { testcases := map[string]testcase{ "non-utf8": {In: "\x85\xcd", Out: "\"\\ufffd\\ufffd\""}, "urc": {In: "\ufffd", Out: "\"\ufffd\""}, + "float": {In: 1.2e3, Out: `1200`}, } for tcName, tc := range testcases { tc := tc diff --git a/reencode.go b/reencode.go index 1a9999b..1943b9c 100644 --- a/reencode.go +++ b/reencode.go @@ -54,6 +54,10 @@ type ReEncoderConfig struct { // this is different than the usual behavior. ForceTrailingNewlines bool + // CompactFloats causes the *ReEncoder to trim unnecessary '0' + // digits from floating-point number values. + CompactFloats bool + // A JSON document is specified to be a sequence of Unicode // codepoints; InvalidUTF8 controls how the *ReEncoder behaves // when it encounters invalid UTF-8 bytes in a JSON string @@ -109,8 +113,10 @@ func NewReEncoder(out io.Writer, cfg ReEncoderConfig) *ReEncoder { } // Numbers - module = &reEncodeCompactNum{ - out: module, + if cfg.CompactFloats { + module = &reEncodeCompactNum{ + out: module, + } } // Strings diff --git a/reencode_test.go b/reencode_test.go index bc6d246..715e976 100644 --- a/reencode_test.go +++ b/reencode_test.go @@ -135,7 +135,8 @@ func TestReEncode(t *testing.T) { }, "numbers": { enc: ReEncoderConfig{ - Compact: true, + Compact: true, + CompactFloats: true, }, in: []any{ Number("1.200e003"), @@ -144,7 +145,8 @@ func TestReEncode(t *testing.T) { }, "numbers-zero": { enc: ReEncoderConfig{ - Compact: true, + Compact: true, + CompactFloats: true, }, in: []any{ Number("1.000e000"), -- cgit v1.2.3-54-g00ecf From 00187950437a10952b82353405e5ba4b4515fb29 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 16 Feb 2023 19:06:46 -0700 Subject: reencode: Don't normalize the capitalization of \uXXXX hex escapes --- ReleaseNotes.md | 5 +++ compat/json/compat.go | 2 +- compat/json/compat_test.go | 54 +++++++++++++++++++------------- encode_escape.go | 39 +++++++++++++++++++++-- internal/jsonstring/encode_string.go | 60 ++++++++++++++++++++++++++---------- reencode.go | 3 +- 6 files changed, 121 insertions(+), 42 deletions(-) (limited to 'reencode.go') diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 73df694..a8496e0 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -39,6 +39,11 @@ BackslashEscapeMode to allow emitted strings to contain invalid UTF-8. + + Feature: ReEncoder: No longer unconditionally normalizes + `\uXXXX` hex characters to lower-case; now this is controlled + by the `BackslashEscaper` (and the default is now to leave the + capitalization alone). + + Change: EscapeDefault, EscapeDefaultNonHTMLSafe: No longer force long Unicode `\uXXXX` sequences for the U+FFFD Unicode replacement character. diff --git a/compat/json/compat.go b/compat/json/compat.go index d33f278..3a9bd6c 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -157,7 +157,7 @@ func HTMLEscape(dst *bytes.Buffer, src []byte) { case lowmemjson.BackslashEscapeNone: dst.WriteRune(c) case lowmemjson.BackslashEscapeUnicode: - _ = jsonstring.WriteStringUnicodeEscape(dst, c) + _ = jsonstring.WriteStringUnicodeEscape(dst, c, mode) default: panic(fmt.Errorf("lowmemjson.EscapeHTMLSafe returned an unexpected escape mode=%d", mode)) } diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index c83ca7e..29a8b37 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -18,7 +18,10 @@ func TestCompatHTMLEscape(t *testing.T) { Out string } testcases := map[string]testcase{ - "invalid": {In: `x`, Out: `x`}, + "invalid": {In: `x`, Out: `x`}, + "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`}, + "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`}, + "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`}, } for tcName, tc := range testcases { tc := tc @@ -39,11 +42,14 @@ func TestCompatValid(t *testing.T) { Exp bool } testcases := map[string]testcase{ - "empty": {In: ``, Exp: false}, - "num": {In: `1`, Exp: true}, - "trunc": {In: `{`, Exp: false}, - "object": {In: `{}`, Exp: true}, - "non-utf8": {In: "\"\x85\xcd\"", Exp: false}, // https://github.com/golang/go/issues/58517 + "empty": {In: ``, Exp: false}, + "num": {In: `1`, Exp: true}, + "trunc": {In: `{`, Exp: false}, + "object": {In: `{}`, Exp: true}, + "non-utf8": {In: "\"\x85\xcd\"", Exp: false}, // https://github.com/golang/go/issues/58517 + "hex-lower": {In: `"\uabcd"`, Exp: true}, + "hex-upper": {In: `"\uABCD"`, Exp: true}, + "hex-mixed": {In: `"\uAbCd"`, Exp: true}, } for tcName, tc := range testcases { tc := tc @@ -64,10 +70,13 @@ func TestCompatCompact(t *testing.T) { Err string } testcases := map[string]testcase{ - "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, - "object": {In: `{}`, Out: `{}`}, - "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, - "float": {In: `1.200e003`, Out: `1.200e003`}, + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, + "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`}, + "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`}, + "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`}, } for tcName, tc := range testcases { tc := tc @@ -94,17 +103,20 @@ func TestCompatIndent(t *testing.T) { Err string } testcases := map[string]testcase{ - "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, - "object": {In: `{}`, Out: `{}`}, - "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, - "float": {In: `1.200e003`, Out: `1.200e003`}, - "tailws0": {In: `0`, Out: `0`}, - "tailws1": {In: `0 `, Out: `0 `}, - "tailws2": {In: `0 `, Out: `0 `}, - "tailws3": {In: "0\n", Out: "0\n"}, - "headws1": {In: ` 0`, Out: `0`}, - "objws1": {In: `{"a" : 1}`, Out: "{\n>.\"a\": 1\n>}"}, - "objws2": {In: "{\"a\"\n:\n1}", Out: "{\n>.\"a\": 1\n>}"}, + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, + "tailws0": {In: `0`, Out: `0`}, + "tailws1": {In: `0 `, Out: `0 `}, + "tailws2": {In: `0 `, Out: `0 `}, + "tailws3": {In: "0\n", Out: "0\n"}, + "headws1": {In: ` 0`, Out: `0`}, + "objws1": {In: `{"a" : 1}`, Out: "{\n>.\"a\": 1\n>}"}, + "objws2": {In: "{\"a\"\n:\n1}", Out: "{\n>.\"a\": 1\n>}"}, + "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`}, + "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`}, + "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`}, } for tcName, tc := range testcases { tc := tc diff --git a/encode_escape.go b/encode_escape.go index c9e2bc9..664c762 100644 --- a/encode_escape.go +++ b/encode_escape.go @@ -36,7 +36,8 @@ const ( // - as a short "well-known" `\X` backslash sequence (where `X` is a // single-character) // -// - as a long Unicode `\uXXXX` backslash sequence +// - as a long Unicode `\uXXXX` backslash sequence (with 16 +// permutations of capitalization) // // - as a raw byte; this allows you to emit invalid JSON; JSON must // be valid UTF-8, but this allows you to emit arbitrary binary @@ -47,8 +48,29 @@ type BackslashEscapeMode = jsonstring.BackslashEscapeMode const ( BackslashEscapeNone = jsonstring.BackslashEscapeNone BackslashEscapeShort = jsonstring.BackslashEscapeShort - BackslashEscapeUnicode = jsonstring.BackslashEscapeUnicode BackslashEscapeRawByte = jsonstring.BackslashEscapeRawByte + + BackslashEscapeUnicodeXXXX = jsonstring.BackslashEscapeUnicodeXXXX + BackslashEscapeUnicodeXXXx = jsonstring.BackslashEscapeUnicodeXXXx + BackslashEscapeUnicodeXXxX = jsonstring.BackslashEscapeUnicodeXXxX + BackslashEscapeUnicodeXXxx = jsonstring.BackslashEscapeUnicodeXXxx + BackslashEscapeUnicodeXxXX = jsonstring.BackslashEscapeUnicodeXxXX + BackslashEscapeUnicodeXxXx = jsonstring.BackslashEscapeUnicodeXxXx + BackslashEscapeUnicodeXxxX = jsonstring.BackslashEscapeUnicodeXxxX + BackslashEscapeUnicodeXxxx = jsonstring.BackslashEscapeUnicodeXxxx + BackslashEscapeUnicodexXXX = jsonstring.BackslashEscapeUnicodexXXX + BackslashEscapeUnicodexXXx = jsonstring.BackslashEscapeUnicodexXXx + BackslashEscapeUnicodexXxX = jsonstring.BackslashEscapeUnicodexXxX + BackslashEscapeUnicodexXxx = jsonstring.BackslashEscapeUnicodexXxx + BackslashEscapeUnicodexxXX = jsonstring.BackslashEscapeUnicodexxXX + BackslashEscapeUnicodexxXx = jsonstring.BackslashEscapeUnicodexxXx + BackslashEscapeUnicodexxxX = jsonstring.BackslashEscapeUnicodexxxX + BackslashEscapeUnicodexxxx = jsonstring.BackslashEscapeUnicodexxxx + + BackslashEscapeUnicodeMin = jsonstring.BackslashEscapeUnicodeMin + BackslashEscapeUnicodeMax = jsonstring.BackslashEscapeUnicodeMax + + BackslashEscapeUnicode = jsonstring.BackslashEscapeUnicode // back-compat ) func hexToInt(c byte) rune { @@ -72,13 +94,24 @@ func hexToRune(a, b, c, d byte) rune { hexToInt(d)<<0 } +func hexToMode(a, b, c, d byte) BackslashEscapeMode { + // The 0b0010_0000 bit is the ASCII "lowercase bit". + return BackslashEscapeUnicodeMin + BackslashEscapeMode(0| + ((a&0b0010_0000)>>2)| + ((b&0b0010_0000)>>3)| + ((c&0b0010_0000)>>4)| + ((d&0b0010_0000)>>5)) +} + // A BackslashEscaper controls how a ReEncoder emits a character in a // JSON string. The `rune` argument is the character being // considered, and the `BackslashEscapeMode` argument is how it was // originally encoded in the input. // // The ReEncoder will panic if a BackslashEscaper returns an unknown -// BackslashEscapeMode. +// BackslashEscapeMode. However, a BackslashEscaper should be +// permissive of BackslashEscapeModes it doesn't recognize; it is safe +// to just return them unmodified. type BackslashEscaper = func(rune, BackslashEscapeMode) BackslashEscapeMode // EscapePreserve is a BackslashEscaper that preserves the original diff --git a/internal/jsonstring/encode_string.go b/internal/jsonstring/encode_string.go index 2488cb2..1416b3e 100644 --- a/internal/jsonstring/encode_string.go +++ b/internal/jsonstring/encode_string.go @@ -31,22 +31,49 @@ type BackslashEscapeMode uint8 const ( BackslashEscapeNone BackslashEscapeMode = iota BackslashEscapeShort - BackslashEscapeUnicode BackslashEscapeRawByte + + // It is significant to the implementation that if X=binary-0 + // and x=binary-1, then these "BackslashEscapeUnicode" + // constants are counting in-order from 0 to 15. + + BackslashEscapeUnicodeXXXX + BackslashEscapeUnicodeXXXx + BackslashEscapeUnicodeXXxX + BackslashEscapeUnicodeXXxx + BackslashEscapeUnicodeXxXX + BackslashEscapeUnicodeXxXx + BackslashEscapeUnicodeXxxX + BackslashEscapeUnicodeXxxx + BackslashEscapeUnicodexXXX + BackslashEscapeUnicodexXXx + BackslashEscapeUnicodexXxX + BackslashEscapeUnicodexXxx + BackslashEscapeUnicodexxXX + BackslashEscapeUnicodexxXx + BackslashEscapeUnicodexxxX + BackslashEscapeUnicodexxxx + + BackslashEscapeUnicodeMin = BackslashEscapeUnicodeXXXX + BackslashEscapeUnicodeMax = BackslashEscapeUnicodexxxx + + BackslashEscapeUnicode = BackslashEscapeUnicodexxxx // back-compat ) // BackslashEscaper is describe in the main lowmemjson package docs. type BackslashEscaper = func(rune, BackslashEscapeMode) BackslashEscapeMode -func WriteStringUnicodeEscape(w io.Writer, c rune) error { - const alphabet = "0123456789abcdef" +func WriteStringUnicodeEscape(w io.Writer, c rune, mode BackslashEscapeMode) error { + const alphabet = "0123456789ABCDEF" + _mode := byte(mode - BackslashEscapeUnicodeMin) buf := [6]byte{ '\\', 'u', - alphabet[(c>>12)&0xf], - alphabet[(c>>8)&0xf], - alphabet[(c>>4)&0xf], - alphabet[(c>>0)&0xf], + // The 0b0010_0000 bit is the ASCII "lowercase bit". + alphabet[(c>>12)&0xf] | ((_mode << 2) & 0b0010_0000), + alphabet[(c>>8)&0xf] | ((_mode << 3) & 0b0010_0000), + alphabet[(c>>4)&0xf] | ((_mode << 4) & 0b0010_0000), + alphabet[(c>>0)&0xf] | ((_mode << 5) & 0b0010_0000), } _, err := noescape.Write(w, buf[:]) return err @@ -84,7 +111,7 @@ func WriteStringChar(w fastio.AllWriter, c rune, escape BackslashEscapeMode) err case '\b', '\f', '\n', '\r', '\t': // short-escape if possible return writeStringShortEscape(w, c) default: - return WriteStringUnicodeEscape(w, c) + return WriteStringUnicodeEscape(w, c, BackslashEscapeUnicode) } case c == '"' || c == '\\': // override, gotta escape these return writeStringShortEscape(w, c) @@ -100,14 +127,6 @@ func WriteStringChar(w fastio.AllWriter, c rune, escape BackslashEscapeMode) err _, err := w.WriteRune(c) return err } - case BackslashEscapeUnicode: - switch { - case c > 0xFFFF: // override, can't escape these (TODO: unless we use UTF-16 surrogates?) - _, err := w.WriteRune(c) - return err - default: // obey - return WriteStringUnicodeEscape(w, c) - } case BackslashEscapeRawByte: switch { case c < utf8.RuneSelf: @@ -118,6 +137,15 @@ func WriteStringChar(w fastio.AllWriter, c rune, escape BackslashEscapeMode) err return w.WriteByte(byte(c)) } default: + if BackslashEscapeUnicodeMin <= escape && escape <= BackslashEscapeUnicodeMax { + switch { + case c > 0xFFFF: // override, can't escape these (TODO: unless we use UTF-16 surrogates?) + _, err := w.WriteRune(c) + return err + default: // obey + return WriteStringUnicodeEscape(w, c, escape) + } + } panic(fmt.Errorf("escaper returned an invalid escape mode=%d", escape)) } } diff --git a/reencode.go b/reencode.go index 1943b9c..7439bf0 100644 --- a/reencode.go +++ b/reencode.go @@ -441,8 +441,9 @@ func (enc *ReEncoder) handleRuneType(c rune, t jsonparse.RuneType, stackSize int enc.uhex[2] = byte(c) return nil case jsonparse.RuneTypeStringEscUD: + mode := hexToMode(enc.uhex[0], enc.uhex[1], enc.uhex[2], byte(c)) c = hexToRune(enc.uhex[0], enc.uhex[1], enc.uhex[2], byte(c)) - return enc.out.HandleRune(c, jsonparse.RuneTypeStringChar, BackslashEscapeUnicode, stackSize) + return enc.out.HandleRune(c, jsonparse.RuneTypeStringChar, mode, stackSize) case jsonparse.RuneTypeError: panic(fmt.Errorf("should not happen: handleRune called with %#v", t)) default: -- cgit v1.2.3-54-g00ecf