From 6c467f5fbc333d45519184d6ea99842032044c76 Mon Sep 17 00:00:00 2001 From: suzaku Date: Sat, 18 Sep 2021 14:07:07 +0800 Subject: [PATCH] Reuse function --- set/subtract.go | 8 +++----- set/union.go | 17 ----------------- set/utils.go | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/set/subtract.go b/set/subtract.go index 0ae3ad2..e501489 100644 --- a/set/subtract.go +++ b/set/subtract.go @@ -17,23 +17,21 @@ limitations under the License. package set import ( - "bufio" "io" ) func Subtract(f1, f2 io.Reader) <-chan string { ch := make(chan string, 16) + chLines1 := readNonEmptyLines(f1) go func() { defer close(ch) - scanner1 := bufio.NewScanner(f1) searcher := &rowSearcher{ chRowsInBulk: readLinesInBulk(f2, 64), } var lastLine string var f2Exhausted bool - for scanner1.Scan() { - line := scanner1.Text() - if len(line) == 0 || line == lastLine { + for line := range chLines1 { + if line == lastLine { continue } lastLine = line diff --git a/set/union.go b/set/union.go index 2be8d85..51b9d40 100644 --- a/set/union.go +++ b/set/union.go @@ -17,7 +17,6 @@ limitations under the License. package set import ( - "bufio" "io" ) @@ -88,19 +87,3 @@ func Union(f1, f2 io.Reader) <-chan string { }() return ch } - -func readNonEmptyLines(r io.Reader) <-chan string { - scanner := bufio.NewScanner(r) - chLine := make(chan string, 10) - go func() { - for scanner.Scan() { - line := scanner.Text() - if len(line) == 0 { - continue - } - chLine <- line - } - close(chLine) - }() - return chLine -} diff --git a/set/utils.go b/set/utils.go index 6d6459d..c526b8d 100644 --- a/set/utils.go +++ b/set/utils.go @@ -69,3 +69,19 @@ func (rs *rowSearcher) Search(row string) (found bool, inRange bool, exhausted b } return } + +func readNonEmptyLines(r io.Reader) <-chan string { + scanner := bufio.NewScanner(r) + chLine := make(chan string, 10) + go func() { + for scanner.Scan() { + line := scanner.Text() + if len(line) == 0 { + continue + } + chLine <- line + } + close(chLine) + }() + return chLine +}