github.com/vanus-labs/vanus/lib@v0.0.0-20231221070800-1334a7b9605e/bytes/io.go (about)

     1  // Copyright 2023 Linkall Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bytes
    16  
    17  import (
    18  	// standard libraries.
    19  	"errors"
    20  	"io"
    21  )
    22  
    23  var DummyWriter = &nopWriter{}
    24  
    25  type MarkScanner struct {
    26  	Buf []byte
    27  	off int
    28  }
    29  
    30  // Make sure MarkScanner implements io.ByteScanner.
    31  var _ io.ByteScanner = (*MarkScanner)(nil)
    32  
    33  func NewMarkScanner(b []byte) *MarkScanner {
    34  	return &MarkScanner{Buf: b}
    35  }
    36  
    37  func (s *MarkScanner) empty() bool {
    38  	return len(s.Buf) <= s.off
    39  }
    40  
    41  func (s *MarkScanner) ReadByte() (byte, error) {
    42  	if s.empty() {
    43  		return 0, io.EOF
    44  	}
    45  	b := s.Buf[s.off]
    46  	s.off++
    47  	return b, nil
    48  }
    49  
    50  func (s *MarkScanner) UnreadByte() error {
    51  	if s.off <= 0 {
    52  		return errors.New("reader.UnreadByte: at beginning of slice")
    53  	}
    54  	s.off--
    55  	return nil
    56  }
    57  
    58  func (s *MarkScanner) Mark(off int) int {
    59  	return s.off + off
    60  }
    61  
    62  func (s *MarkScanner) Since(mark int, off int) []byte {
    63  	return s.Buf[mark : s.off+off]
    64  }
    65  
    66  func (s *MarkScanner) From(mark int) []byte {
    67  	return s.Buf[mark:]
    68  }
    69  
    70  func (s *MarkScanner) Resume(mark int) error {
    71  	s.off = mark
    72  	return nil
    73  }
    74  
    75  func ScannedBytes(s *MarkScanner, mark int, eof bool) []byte {
    76  	if eof {
    77  		return s.Since(mark, 0)
    78  	}
    79  	return s.Since(mark, -1)
    80  }
    81  
    82  type nopWriter struct{}
    83  
    84  // Make sure nopWriter implements io.Write and io.ByteWriter.
    85  var (
    86  	_ io.Writer     = (*nopWriter)(nil)
    87  	_ io.ByteWriter = (*nopWriter)(nil)
    88  )
    89  
    90  func (w *nopWriter) Write(p []byte) (int, error) {
    91  	return len(p), nil
    92  }
    93  
    94  func (w *nopWriter) WriteByte(_ byte) error {
    95  	return nil
    96  }
    97  
    98  type CopyOnDiffWriter struct {
    99  	Buf []byte
   100  	new []byte
   101  	off int
   102  }
   103  
   104  // Make sure CopyOnDiffWriter implements io.ByteWriter.
   105  var _ io.ByteWriter = (*CopyOnDiffWriter)(nil)
   106  
   107  func (w *CopyOnDiffWriter) WriteByte(c byte) error {
   108  	if w.new != nil {
   109  		w.new = append(w.new, c)
   110  		return nil
   111  	}
   112  	if w.off < len(w.Buf) && w.Buf[w.off] == c {
   113  		w.off++
   114  		return nil
   115  	}
   116  	w.new = make([]byte, w.off+1)
   117  	copy(w.new, w.Buf[:w.off])
   118  	w.new[w.off] = c
   119  	return nil
   120  }
   121  
   122  func (w *CopyOnDiffWriter) Bytes() []byte {
   123  	if w.new != nil {
   124  		return w.new
   125  	}
   126  	return w.Buf[:w.off]
   127  }
   128  
   129  type LastByteWriter interface {
   130  	io.Writer
   131  
   132  	LastByte() (byte, bool)
   133  	TruncateLastByte()
   134  }