Skip to content

Commit

Permalink
chore: add read file in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Jun 17, 2024
1 parent 7c52936 commit f237afe
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package files

import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -251,3 +252,35 @@ func (f *Files) ReadFileLines(filePath string, opts ...Delimiter) ([]string, err

return lines, nil
}

func (f *Files) ReadFileLinesBytes(filePath string, opts ...Delimiter) ([][]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

if len(opts) == 0 {
opts = append(opts, SLASH_N)
}

var lines [][]byte
reader := bufio.NewReader(file)
delimiter := opts[0].Value()

for {
line, err := reader.ReadBytes(delimiter)
if err != nil {
if err == io.EOF {
if len(line) > 0 {
lines = append(lines, bytes.TrimSuffix(line, []byte{delimiter}))
}
break
}
return nil, err
}
lines = append(lines, bytes.TrimSuffix(line, []byte{delimiter}))
}

return lines, nil
}

0 comments on commit f237afe

Please sign in to comment.