diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-16 20:14:10 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-16 20:15:38 -0600 |
commit | 48d7aa22ba239e4413cb7bd86e1c7ce3f64832c3 (patch) | |
tree | b7ce019adc1f82446feae6629b2dc361ddcbe058 | |
parent | 9f75bb80b99f94c5c87582acb99e57c232caf01d (diff) |
encode: Don't use generics for encodeString
-rw-r--r-- | encode.go | 27 | ||||
-rw-r--r-- | misc.go | 9 | ||||
-rw-r--r-- | test_export.go | 4 |
3 files changed, 21 insertions, 19 deletions
@@ -15,6 +15,7 @@ import ( "sort" "strconv" "strings" + "unicode/utf8" "unsafe" ) @@ -145,7 +146,7 @@ func encode(w io.Writer, val reflect.Value, quote bool, cycleDepth uint, cycleSe SourceFunc: "MarshalText", }}) } - encodeString(w, text) + encodeStringFromBytes(w, text) default: switch val.Kind() { @@ -201,10 +202,10 @@ func encode(w io.Writer, val reflect.Value, quote bool, cycleDepth uint, cycleSe } else { if quote { var buf bytes.Buffer - encodeString(&buf, val.String()) - encodeString(w, buf.Bytes()) + encodeStringFromString(&buf, val.String()) + encodeStringFromBytes(w, buf.Bytes()) } else { - encodeString(w, val.String()) + encodeStringFromString(w, val.String()) } } case reflect.Interface: @@ -228,7 +229,7 @@ func encode(w io.Writer, val reflect.Value, quote bool, cycleDepth uint, cycleSe encodeWriteByte(w, ',') } empty = false - encodeString(w, field.Name) + encodeStringFromString(w, field.Name) encodeWriteByte(w, ':') encode(w, fVal, field.Quote, cycleDepth, cycleSeen) } @@ -270,7 +271,7 @@ func encode(w io.Writer, val reflect.Value, quote bool, cycleDepth uint, cycleSe } if !strings.HasPrefix(kStr, `"`) { k.Reset() - encodeString(&k, kStr) + encodeStringFromString(&k, kStr) kStr = k.String() } kvs[i].K = kStr @@ -369,10 +370,20 @@ func encode(w io.Writer, val reflect.Value, quote bool, cycleDepth uint, cycleSe } } -func encodeString[T interface{ []byte | string }](w io.Writer, str T) { +func encodeStringFromString(w io.Writer, str string) { + encodeWriteByte(w, '"') + for _, c := range str { + if _, err := writeStringChar(w, c, BackslashEscapeNone, nil); err != nil { + panic(encodeError{err}) + } + } + encodeWriteByte(w, '"') +} + +func encodeStringFromBytes(w io.Writer, str []byte) { encodeWriteByte(w, '"') for i := 0; i < len(str); { - c, size := decodeRune(str[i:]) + c, size := utf8.DecodeRune(str[i:]) if _, err := writeStringChar(w, c, BackslashEscapeNone, nil); err != nil { panic(encodeError{err}) } @@ -36,15 +36,6 @@ var ( // generic I/O ///////////////////////////////////////////////////////////////// -func decodeRune[T interface{ []byte | string }](s T) (r rune, size int) { - iface := any(s) - if str, ok := iface.(string); ok { - return utf8.DecodeRuneInString(str) - } else { - return utf8.DecodeRune(iface.([]byte)) - } -} - func writeByte(w io.Writer, c byte) error { if br, ok := w.(interface{ WriteByte(byte) error }); ok { return br.WriteByte(c) diff --git a/test_export.go b/test_export.go index 1f97a7a..ccac2b2 100644 --- a/test_export.go +++ b/test_export.go @@ -9,8 +9,8 @@ import ( ) func init() { - internal.EncodeStringFromString = encodeString[string] - internal.EncodeStringFromBytes = encodeString[[]byte] + internal.EncodeStringFromString = encodeStringFromString + internal.EncodeStringFromBytes = encodeStringFromBytes } var parseTag = internal.ParseTag |