Skip to content

Commit

Permalink
[desafio-07] b -> readBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMercer committed Aug 12, 2023
1 parent 135f5eb commit 51edce4
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions desafio-07/MatMercer/go/tac.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func tac(fileName string) error {
// buffers stdout by 128kb
stdout = bufio.NewWriterSize(os.Stdout, 128<<(10))

fs := fi.Size()
bufSize := min(fs, maxBufSize)
b := make([]byte, bufSize)
fileSize := fi.Size()
bufSize := min(fileSize, maxBufSize)
readBuf := make([]byte, bufSize)
maxRead := bufSize
start := fs
start := fileSize
lineAcc := bytes.NewBuffer([]byte{})
r := NewReverseReader(f, fs)
r := NewReverseReader(f, fileSize)
for start != 0 {
start -= bufSize
if start < 0 {
Expand All @@ -74,17 +74,17 @@ func tac(fileName string) error {
start = 0
}

_, err = r.Read(b[:maxRead])
_, err = r.Read(readBuf[:maxRead])
if err != nil {
return err
}

// search until backwards \n and prints it
lastEnd := maxRead
for i := maxRead - 1; i >= 0; i-- {
if b[i] == '\n' {
// write everything but '\n', since b[i] == '\n'
_, err = stdout.Write(b[i+1 : lastEnd])
if readBuf[i] == '\n' {
// write everything but '\n', since readBuf[i] == '\n'
_, err = stdout.Write(readBuf[i+1 : lastEnd])
if err != nil {
return err
}
Expand All @@ -102,7 +102,7 @@ func tac(fileName string) error {
// on last iteration, create a new accumulator with [max-read][current-accumulator]
if i == 0 {
newAcc := bytes.NewBuffer(nil)
_, err = newAcc.Write(b[i:lastEnd])
_, err = newAcc.Write(readBuf[i:lastEnd])
if err != nil {
return err
}
Expand Down

0 comments on commit 51edce4

Please sign in to comment.