summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--encode.go27
-rw-r--r--misc.go9
-rw-r--r--test_export.go4
3 files changed, 21 insertions, 19 deletions
diff --git a/encode.go b/encode.go
index af7c57e..f6f8f0e 100644
--- a/encode.go
+++ b/encode.go
@@ -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})
}
diff --git a/misc.go b/misc.go
index ad69d91..dce9dac 100644
--- a/misc.go
+++ b/misc.go
@@ -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