From 190281785ccc39c7ce21eca474fac2fe512492d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 26 Feb 2025 00:05:52 +0900 Subject: [PATCH] fix(pbuf): involve or parse judgement on slice --- item.go | 5 +++++ pbuf/bytes.go | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/item.go b/item.go index e191f46..27c5c76 100644 --- a/item.go +++ b/item.go @@ -58,6 +58,11 @@ func (b *Item[T]) Trans() (tb *Item[T]) { return tb } +// HasInvolved whether this item is buffered. +func (b *Item[T]) HasInvolved() bool { + return b.stat.isbuffered() +} + // IsTrans whether this item has been marked as trans. func (b *Item[T]) IsTrans() bool { return b.stat.isintrans() diff --git a/pbuf/bytes.go b/pbuf/bytes.go index 17235ab..3f07b07 100644 --- a/pbuf/bytes.go +++ b/pbuf/bytes.go @@ -87,7 +87,10 @@ func (b Bytes) Copy() (cb Bytes) { // SliceFrom dat[from:] with Ref. func (b Bytes) SliceFrom(from int) Bytes { if b.buf.IsTrans() { - return InvolveBytes(b.dat[from:]...) + if b.buf.HasInvolved() { + return InvolveBytes(b.dat[from:]...) + } + return ParseBytes(b.dat[from:]...) } nb := b.Ref() skip(nb.buf.Pointer(), from) @@ -98,7 +101,10 @@ func (b Bytes) SliceFrom(from int) Bytes { // SliceTo dat[:to] with Ref. func (b Bytes) SliceTo(to int) Bytes { if b.buf.IsTrans() { - return InvolveBytes(b.dat[:to]...) + if b.buf.HasInvolved() { + return InvolveBytes(b.dat[:to]...) + } + return ParseBytes(b.dat[:to]...) } nb := b.Ref() nb.buf.Pointer().Truncate(to) @@ -109,7 +115,10 @@ func (b Bytes) SliceTo(to int) Bytes { // Slice dat[from:to] with Ref. func (b Bytes) Slice(from, to int) Bytes { if b.buf.IsTrans() { - return InvolveBytes(b.dat[from:to]...) + if b.buf.HasInvolved() { + return InvolveBytes(b.dat[from:to]...) + } + return ParseBytes(b.dat[from:to]...) } nb := b.Ref() buf := nb.buf.Pointer()