From edd3beedb404412428a72ca077d53d1f8c550607 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 6 Apr 2025 10:14:57 +0800 Subject: bare: Zero-copy []byte and string conversions --- forged/internal/bare/reader.go | 4 +++- forged/internal/bare/writer.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/forged/internal/bare/reader.go b/forged/internal/bare/reader.go index fd1398c..58325e3 100644 --- a/forged/internal/bare/reader.go +++ b/forged/internal/bare/reader.go @@ -9,6 +9,8 @@ import ( "io" "math" "unicode/utf8" + + "go.lindenii.runxiyu.org/forge/forged/internal/misc" ) type byteReader interface { @@ -150,7 +152,7 @@ func (r *Reader) ReadString() (string, error) { if !utf8.Valid(buf) { return "", ErrInvalidStr } - return string(buf), nil + return misc.BytesToString(buf), nil } // Reads a fixed amount of arbitrary data, defined by the length of the slice. diff --git a/forged/internal/bare/writer.go b/forged/internal/bare/writer.go index 03cbf96..bada045 100644 --- a/forged/internal/bare/writer.go +++ b/forged/internal/bare/writer.go @@ -8,6 +8,8 @@ import ( "fmt" "io" "math" + + "go.lindenii.runxiyu.org/forge/forged/internal/misc" ) // A Writer for BARE primitive types. @@ -85,7 +87,7 @@ func (w *Writer) WriteBool(b bool) error { } func (w *Writer) WriteString(str string) error { - return w.WriteData([]byte(str)) + return w.WriteData(misc.StringToBytes(str)) } // Writes a fixed amount of arbitrary data, defined by the length of the slice. -- cgit v1.2.3