From edd3beedb404412428a72ca077d53d1f8c550607 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 06 Apr 2025 10:14:57 +0800 Subject: [PATCH] bare: Zero-copy []byte and string conversions --- forged/internal/bare/reader.go | 4 +++- forged/internal/bare/writer.go | 4 +++- diff --git a/forged/internal/bare/reader.go b/forged/internal/bare/reader.go index fd1398c10c62c29c9252fe42d033062aa6a119ad..58325e30cc9b989581d46c2fa659ee141527f251 100644 --- a/forged/internal/bare/reader.go +++ b/forged/internal/bare/reader.go @@ -9,6 +9,8 @@ "fmt" "io" "math" "unicode/utf8" + + "go.lindenii.runxiyu.org/forge/forged/internal/misc" ) type byteReader interface { @@ -150,7 +152,7 @@ } 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 03cbf96ed2644f173815923aa87d57dab55c64ed..bada0456e788a832eba53ed9d9b59bd1419b2dee 100644 --- a/forged/internal/bare/writer.go +++ b/forged/internal/bare/writer.go @@ -8,6 +8,8 @@ "encoding/binary" "fmt" "io" "math" + + "go.lindenii.runxiyu.org/forge/forged/internal/misc" ) // A Writer for BARE primitive types. @@ -85,7 +87,7 @@ return binary.Write(w.base, binary.LittleEndian, b) } 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. -- 2.48.1