diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 23:00:11 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 23:00:11 -0700 |
commit | 5d5ac549dc1a25963418fda4b20ed181b7afd8d2 (patch) | |
tree | 8c5c2e9f5bd1c3a2208efe4d11965ecaa95472ec /encode_string.go | |
parent | 005dfe26e308b965c2f42c81d34a4b48757414a3 (diff) | |
parent | ccf8dc4b21bb1a547f118affab22bca3a02df270 (diff) |
Merge branch 'lukeshu/tune'
Diffstat (limited to 'encode_string.go')
-rw-r--r-- | encode_string.go | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/encode_string.go b/encode_string.go index c5cb442..12f934e 100644 --- a/encode_string.go +++ b/encode_string.go @@ -45,7 +45,7 @@ func writeStringShortEscape(w io.Writer, c rune) (int, error) { return w.Write(buf[:]) } -func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escaper BackslashEscaper) (int, error) { +func writeStringChar(w internal.AllWriter, c rune, wasEscaped BackslashEscapeMode, escaper BackslashEscaper) (int, error) { if escaper == nil { escaper = EscapeDefault } @@ -62,19 +62,19 @@ func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escape case c == '"' || c == '\\': // override, gotta escape these return writeStringShortEscape(w, c) default: // obey - return writeRune(w, c) + return w.WriteRune(c) } case BackslashEscapeShort: switch c { case '"', '\\', '/', '\b', '\f', '\n', '\r', '\t': // obey return writeStringShortEscape(w, c) default: // override, can't short-escape these - return writeRune(w, c) + return w.WriteRune(c) } case BackslashEscapeUnicode: switch { case c > 0xFFFF: // override, can't escape these (TODO: unless we use UTF-16 surrogates?) - return writeRune(w, c) + return w.WriteRune(c) default: // obey return writeStringUnicodeEscape(w, c) } @@ -83,29 +83,47 @@ func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escape } } -func encodeStringFromString(w io.Writer, escaper BackslashEscaper, str string) { - encodeWriteByte(w, '"') +func encodeStringFromString(w internal.AllWriter, escaper BackslashEscaper, str string) error { + if err := w.WriteByte('"'); err != nil { + return err + } for _, c := range str { if _, err := writeStringChar(w, c, BackslashEscapeNone, escaper); err != nil { - panic(encodeError{err}) + return err } } - encodeWriteByte(w, '"') + if err := w.WriteByte('"'); err != nil { + return err + } + return nil } -func encodeStringFromBytes(w io.Writer, escaper BackslashEscaper, str []byte) { - encodeWriteByte(w, '"') +func encodeStringFromBytes(w internal.AllWriter, escaper BackslashEscaper, str []byte) error { + if err := w.WriteByte('"'); err != nil { + return err + } for i := 0; i < len(str); { c, size := utf8.DecodeRune(str[i:]) if _, err := writeStringChar(w, c, BackslashEscapeNone, escaper); err != nil { - panic(encodeError{err}) + return err } i += size } - encodeWriteByte(w, '"') + if err := w.WriteByte('"'); err != nil { + return err + } + return nil } func init() { - internal.EncodeStringFromString = func(w io.Writer, s string) { encodeStringFromString(w, nil, s) } - internal.EncodeStringFromBytes = func(w io.Writer, s []byte) { encodeStringFromBytes(w, nil, s) } + internal.EncodeStringFromString = func(w io.Writer, s string) { + if err := encodeStringFromString(internal.NewAllWriter(w), nil, s); err != nil { + panic(err) + } + } + internal.EncodeStringFromBytes = func(w io.Writer, s []byte) { + if err := encodeStringFromBytes(internal.NewAllWriter(w), nil, s); err != nil { + panic(err) + } + } } |