Skip to content

Commit

Permalink
Merge pull request #40 from kishoresenji/master
Browse files Browse the repository at this point in the history
optimize SourceLine to not create a new instance of Error from String method
  • Loading branch information
ConradIrwin committed Jan 20, 2022
2 parents b6240f0 + d88c094 commit ee157bf
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions stackframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (frame *StackFrame) Func() *runtime.Func {
func (frame *StackFrame) String() string {
str := fmt.Sprintf("%s:%d (0x%x)\n", frame.File, frame.LineNumber, frame.ProgramCounter)

source, err := frame.SourceLine()
source, err := frame.sourceLine()
if err != nil {
return str
}
Expand All @@ -63,13 +63,21 @@ func (frame *StackFrame) String() string {

// SourceLine gets the line of code (from File and Line) of the original source if possible.
func (frame *StackFrame) SourceLine() (string, error) {
source, err := frame.sourceLine()
if err != nil {
return source, New(err)
}
return source, err
}

func (frame *StackFrame) sourceLine() (string, error) {
if frame.LineNumber <= 0 {
return "???", nil
}

file, err := os.Open(frame.File)
if err != nil {
return "", New(err)
return "", err
}
defer file.Close()

Expand All @@ -82,7 +90,7 @@ func (frame *StackFrame) SourceLine() (string, error) {
currentLine++
}
if err := scanner.Err(); err != nil {
return "", New(err)
return "", err
}

return "???", nil
Expand Down

0 comments on commit ee157bf

Please sign in to comment.