Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use bufio.Reader instead of bufio.Scanner #79

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions internal/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package util

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -43,15 +44,32 @@ func ReadALine(filename string) (string, error) {
return "", fmt.Errorf("error to open the file: %w", err)
}

var line string
fileScanner := bufio.NewScanner(file)
for fileScanner.Scan() {
line = fileScanner.Text()
break
r := bufio.NewReader(file)

data, cont, err := r.ReadLine()
if err != nil && err != io.EOF {
return "", fmt.Errorf("error to read the file: %w", err)
}
if err := fileScanner.Err(); err != nil {
log.Fatalf("error to scan the file: %s", err)

buf := bytes.NewBuffer(data)
if cont {
for {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change it to be simpler.

if cont {
 for {
// ...
 if !cont {
  break
 }
 }
}

for cont {
// ...
}

Copy link
Contributor Author

@0tarof 0tarof Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I fixed it.

data, cont, err = r.ReadLine()
if err == io.EOF {
break
} else if err != nil {
return "", fmt.Errorf("error to read the file: %w", err)
}

if _, err = buf.Write(data); err != nil {
return "", fmt.Errorf("error to append data to buffer: %w", err)
}

if !cont {
break
}
}
}

return line, nil
return buf.String(), nil
}