From 09a956aa74245ecb1e547a47c012cfe7c263ef41 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Thu, 22 Dec 2022 14:21:01 +0900 Subject: [PATCH] Execute BuffEndNum() outside the loop Execute BuffEndNum() outside the loop for better performance. --- oviewer/document.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/oviewer/document.go b/oviewer/document.go index bba45c19..304e86b3 100644 --- a/oviewer/document.go +++ b/oviewer/document.go @@ -279,11 +279,11 @@ func (m *Document) firstLine() int { return m.SkipLines + m.Header } -// SearchLine searches the document and returns the matching line. +// SearchLine searches the document and returns the matching line number. func (m *Document) SearchLine(ctx context.Context, searcher Searcher, lN int) (int, error) { lN = max(lN, 0) - - for n := lN; n < m.BufEndNum(); n++ { + end := m.BufEndNum() + for n := lN; n < end; n++ { if searcher.Match(m.GetLine(n)) { return n, nil } @@ -297,7 +297,7 @@ func (m *Document) SearchLine(ctx context.Context, searcher Searcher, lN int) (i return 0, ErrNotFound } -// BackSearchLine does a backward search on the document and returns a matching line. +// BackSearchLine does a backward search on the document and returns a matching line number. func (m *Document) BackSearchLine(ctx context.Context, searcher Searcher, lN int) (int, error) { lN = min(lN, m.BufEndNum()-1)