Skip to content

Commit

Permalink
extract a function getDataOff
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood committed Nov 16, 2020
1 parent b0e1f92 commit a70daa2
Showing 1 changed file with 18 additions and 46 deletions.
64 changes: 18 additions & 46 deletions ringbuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@ func (rb *RingBuf) ReadAt(p []byte, off int64) (n int, err error) {
err = ErrOutOfRange
return
}
var readOff int
if rb.end-rb.begin < int64(len(rb.data)) {
readOff = int(off - rb.begin)
} else {
readOff = rb.index + int(off-rb.begin)
}
if readOff >= len(rb.data) {
readOff -= len(rb.data)
}
readOff := rb.getDataOff(off)
readEnd := readOff + int(rb.end-off)
if readEnd <= len(rb.data) {
n = copy(p, rb.data[readOff:readEnd])
Expand All @@ -80,21 +72,26 @@ func (rb *RingBuf) ReadAt(p []byte, off int64) (n int, err error) {
return
}

func (rb *RingBuf) getDataOff(off int64) int {
var dataOff int
if rb.end-rb.begin < int64(len(rb.data)) {
dataOff = int(off - rb.begin)
} else {
dataOff = rb.index + int(off-rb.begin)
}
if dataOff >= len(rb.data) {
dataOff -= len(rb.data)
}
return dataOff
}

// Slice returns a slice of the supplied range of the ring buffer. It will
// not alloc unless the requested range wraps the ring buffer.
func (rb *RingBuf) Slice(off, length int64) ([]byte, error) {
if off > rb.end || off < rb.begin {
return nil, ErrOutOfRange
}
var readOff int
if rb.end-rb.begin < int64(len(rb.data)) {
readOff = int(off - rb.begin)
} else {
readOff = rb.index + int(off-rb.begin)
}
if readOff >= len(rb.data) {
readOff -= len(rb.data)
}
readOff := rb.getDataOff(off)
readEnd := readOff + int(length)
if readEnd <= len(rb.data) {
return rb.data[readOff:readEnd:readEnd], nil
Expand Down Expand Up @@ -135,15 +132,7 @@ func (rb *RingBuf) WriteAt(p []byte, off int64) (n int, err error) {
err = ErrOutOfRange
return
}
var writeOff int
if rb.end-rb.begin < int64(len(rb.data)) {
writeOff = int(off - rb.begin)
} else {
writeOff = rb.index + int(off-rb.begin)
}
if writeOff > len(rb.data) {
writeOff -= len(rb.data)
}
writeOff := rb.getDataOff(off)
writeEnd := writeOff + int(rb.end-off)
if writeEnd <= len(rb.data) {
n = copy(rb.data[writeOff:writeEnd], p)
Expand All @@ -160,15 +149,7 @@ func (rb *RingBuf) EqualAt(p []byte, off int64) bool {
if off+int64(len(p)) > rb.end || off < rb.begin {
return false
}
var readOff int
if rb.end-rb.begin < int64(len(rb.data)) {
readOff = int(off - rb.begin)
} else {
readOff = rb.index + int(off-rb.begin)
}
if readOff >= len(rb.data) {
readOff -= len(rb.data)
}
readOff := rb.getDataOff(off)
readEnd := readOff + len(p)
if readEnd <= len(rb.data) {
return bytes.Equal(p, rb.data[readOff:readEnd])
Expand All @@ -189,16 +170,7 @@ func (rb *RingBuf) Evacuate(off int64, length int) (newOff int64) {
if off+int64(length) > rb.end || off < rb.begin {
return -1
}
var readOff int
if rb.end-rb.begin < int64(len(rb.data)) {
readOff = int(off - rb.begin)
} else {
readOff = rb.index + int(off-rb.begin)
}
if readOff >= len(rb.data) {
readOff -= len(rb.data)
}

readOff := rb.getDataOff(off)
if readOff == rb.index {
// no copy evacuate
rb.index += length
Expand Down

0 comments on commit a70daa2

Please sign in to comment.