go-hep.org/x/hep@v0.38.1/hbook/io.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package hbook
     6  
     7  import "io"
     8  
     9  type rbuffer struct {
    10  	p []byte // buffer of data to read from
    11  	c int    // current position in buffer of data
    12  }
    13  
    14  func newRBuffer(p []byte) *rbuffer {
    15  	return &rbuffer{p: p}
    16  }
    17  
    18  func (r *rbuffer) Read(p []byte) (int, error) {
    19  	if r.c >= len(r.p) {
    20  		return 0, io.EOF
    21  	}
    22  	n := copy(p, r.p[r.c:])
    23  	r.c += n
    24  	return n, nil
    25  }
    26  
    27  func (r *rbuffer) Bytes() []byte { return r.p[r.c:] }
    28  
    29  func (r *rbuffer) next(n int) []byte {
    30  	m := len(r.p[r.c:])
    31  	if n > m {
    32  		n = m
    33  	}
    34  	p := r.p[r.c : r.c+n]
    35  	r.c += n
    36  	return p
    37  }