github.com/nikandfor/hacked@v0.0.0-20231207014854-3b383967fdf4/low/buf.go (about)

     1  package low
     2  
     3  import "io"
     4  
     5  const Spaces = "                                                                                                                                "
     6  
     7  type (
     8  	Buf []byte
     9  
    10  	BufReader struct {
    11  		Buf
    12  		R int
    13  	}
    14  )
    15  
    16  func (w *Buf) Write(p []byte) (int, error) {
    17  	*w = append(*w, p...)
    18  
    19  	return len(p), nil
    20  }
    21  
    22  func (w *Buf) WriteAt(p []byte, off int64) (int, error) {
    23  	if int(off)+len(p) <= len(*w) {
    24  		return copy((*w)[off:], p), nil
    25  	}
    26  
    27  	for cap(*w) < int(off) {
    28  		*w = append((*w)[:cap(*w)], 0, 0, 0, 0, 0, 0, 0, 0)
    29  	}
    30  
    31  	*w = (*w)[:off]
    32  	*w = append(*w, p...)
    33  
    34  	return len(p), nil
    35  }
    36  
    37  func (w *Buf) NewLine() {
    38  	l := len(*w)
    39  	if l == 0 || (*w)[l-1] != '\n' {
    40  		*w = append(*w, '\n')
    41  	}
    42  }
    43  
    44  func (w Buf) ReadAt(p []byte, off int64) (int, error) {
    45  	if off >= int64(len(w)) {
    46  		return 0, io.EOF
    47  	}
    48  
    49  	n := copy(p, w[off:])
    50  	if n < len(p) {
    51  		return n, io.EOF
    52  	}
    53  
    54  	return n, nil
    55  }
    56  
    57  func (w *Buf) Reset()        { *w = (*w)[:0] }
    58  func (w *Buf) Len() int      { return len(*w) }
    59  func (w *Buf) LenF() float64 { return float64(w.Len()) }
    60  func (w *Buf) Bytes() []byte { return *w }
    61  
    62  func (r *BufReader) Read(p []byte) (n int, err error) {
    63  	n = copy(p, r.Buf[r.R:])
    64  	r.R += n
    65  
    66  	if r.R == len(r.Buf) {
    67  		err = io.EOF
    68  	}
    69  
    70  	return
    71  }
    72  
    73  func (r *BufReader) Reset()        { r.R = 0 }
    74  func (r *BufReader) Len() int      { return r.Buf.Len() - r.R }
    75  func (r *BufReader) LenF() float64 { return float64(r.Len()) }
    76  func (r *BufReader) Bytes() []byte { return r.Buf[r.R:] }