From 3dde3a723a5ff9ca18795a53b711b6a6759e64b2 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 11 Jul 2022 21:50:43 -0600 Subject: [PATCH] ezpz case actually looks ok --- main.go | 31 ++++++++++++++++++++++++++----- main_test.go | 6 ++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index d003709..2c62f1a 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bufio" "bytes" - "errors" "flag" "io" "os" @@ -57,14 +56,36 @@ func _readPage(d byte, r *bufio.Reader) ([]byte, error) { } func putPage(b []byte) error { - return _putPage(stdout, b) + return _putPage(stdout, b, []byte(columnDelimiter)[0], minimumInstances) } -func _putPage(w io.Writer, b []byte) error { +func _putPage(w io.Writer, b []byte, d byte, instances int) error { + nonFirstColumn := []byte{} for _, line := range lines(b) { - _ = line + //line = bytes.TrimSpace(line) + i := 0 + for i < len(line) && line[i] == d { + i += 1 + } + for i < len(line) { + j := 0 + for j+i < len(line) && line[i+j] == d { + j += 1 + } + if j >= instances { + nonFirstColumn = append(nonFirstColumn, line[i+j:]...) + line = line[:i+j] + break + } + i += 1 + } + w.Write(line) + w.Write([]byte{'\n'}) } - return errors.New("not impl") + if len(nonFirstColumn) > 0 { + return _putPage(w, nonFirstColumn, d, instances) + } + return nil } func lines(b []byte) [][]byte { diff --git a/main_test.go b/main_test.go index 2eadc87..ad9db95 100644 --- a/main_test.go +++ b/main_test.go @@ -10,7 +10,8 @@ import ( ) func Test_Main(t *testing.T) { - stdin = strings.NewReader("\ta a a\t\tb b b\na a\t\tb b\fc c\td d\nc c\td.") + input := "\ta a a\t\tb b b\na a\t\tb b\fc c\td d\nc c\td." + stdin = strings.NewReader(input) stdout = bytes.NewBuffer(nil) columnDelimiter = "\t" pageDelimiter = "\f" @@ -18,7 +19,8 @@ func Test_Main(t *testing.T) { if err := _main(); err != nil { t.Error(err) } - t.Errorf("%s", stdout.(*bytes.Buffer).Bytes()) + t.Logf("input:\n%s", input) + t.Errorf("result:\n%s", stdout.(*bytes.Buffer).Bytes()) } func TestReadPage(t *testing.T) {