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()