From c37440a7cf42ac63b919c752ca73a85067e05992 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 27 Feb 2017 14:03:11 -0800 Subject: [PATCH] Breaking changes to make govet pass * Peek -> PeekRunes * ReadRune(int) rune, error -> ReadRune(int) rune, int, error --- buffruneio.go | 25 ++++++++++++++++--------- buffruneio_test.go | 33 +++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/buffruneio.go b/buffruneio.go index 41cab87..4e6d6ea 100644 --- a/buffruneio.go +++ b/buffruneio.go @@ -31,8 +31,13 @@ func NewReader(rd io.Reader) *Reader { } } +type runeWithSize struct { + r rune + size int +} + func (rd *Reader) feedBuffer() error { - r, _, err := rd.input.ReadRune() + r, size, err := rd.input.ReadRune() if err != nil { if err != io.EOF { @@ -41,7 +46,9 @@ func (rd *Reader) feedBuffer() error { r = EOF } - rd.buffer.PushBack(r) + newRuneWithSize := runeWithSize{r, size} + + rd.buffer.PushBack(newRuneWithSize) if rd.current == nil { rd.current = rd.buffer.Back() } @@ -49,17 +56,17 @@ func (rd *Reader) feedBuffer() error { } // ReadRune reads the next rune from buffer, or from the underlying reader if needed. -func (rd *Reader) ReadRune() (rune, error) { +func (rd *Reader) ReadRune() (rune, int, error) { if rd.current == rd.buffer.Back() || rd.current == nil { err := rd.feedBuffer() if err != nil { - return EOF, err + return EOF, 0, err } } - r := rd.current.Value + runeWithSize := rd.current.Value.(runeWithSize) rd.current = rd.current.Next() - return r.(rune), nil + return runeWithSize.r, runeWithSize.size, nil } // UnreadRune pushes back the previously read rune in the buffer, extending it if needed. @@ -84,9 +91,9 @@ func (rd *Reader) Forget() { } } -// Peek returns at most the next n runes, reading from the uderlying source if +// PeekRune returns at most the next n runes, reading from the uderlying source if // needed. Does not move the current index. It includes EOF if reached. -func (rd *Reader) Peek(n int) []rune { +func (rd *Reader) PeekRunes(n int) []rune { res := make([]rune, 0, n) cursor := rd.current for i := 0; i < n; i++ { @@ -98,7 +105,7 @@ func (rd *Reader) Peek(n int) []rune { cursor = rd.buffer.Back() } if cursor != nil { - r := cursor.Value.(rune) + r := cursor.Value.(runeWithSize).r res = append(res, r) if r == EOF { return res diff --git a/buffruneio_test.go b/buffruneio_test.go index f912149..67b0cba 100644 --- a/buffruneio_test.go +++ b/buffruneio_test.go @@ -1,10 +1,19 @@ package buffruneio import ( + "runtime/debug" "strings" "testing" ) +func assertNoError(t *testing.T, err error) { + if err != nil { + t.Log("unexpected error", err) + debug.PrintStack() + t.FailNow() + } +} + func assumeRunesArray(t *testing.T, expected []rune, got []rune) { if len(expected) != len(got) { t.Fatal("expected", len(expected), "runes, but got", len(got)) @@ -17,15 +26,15 @@ func assumeRunesArray(t *testing.T, expected []rune, got []rune) { } func assumeRune(t *testing.T, rd *Reader, r rune) { - gotRune, err := rd.ReadRune() - if err != nil { - t.Fatal("unexpected error", err) - } + gotRune, size, err := rd.ReadRune() + assertNoError(t, err) if gotRune != r { t.Fatal("got", string(gotRune), "(", []byte(string(gotRune)), ")", "expected", string(r), "(", []byte(string(r)), ")") + t.Fatal("got size", size, + "expected", len([]byte(string(r)))) } } @@ -55,7 +64,7 @@ func TestUnread(t *testing.T) { assumeRune(t, rd, 'a') assumeRune(t, rd, 'b') - rd.UnreadRune() + assertNoError(t, rd.UnreadRune()) assumeRune(t, rd, 'b') assumeRune(t, rd, EOF) } @@ -64,10 +73,10 @@ func TestUnreadEOF(t *testing.T) { s := "" rd := NewReader(strings.NewReader(s)) - rd.UnreadRune() + _ = rd.UnreadRune() assumeRune(t, rd, EOF) assumeRune(t, rd, EOF) - rd.UnreadRune() + assertNoError(t, rd.UnreadRune()) assumeRune(t, rd, EOF) } @@ -98,7 +107,7 @@ func TestPeekEmpty(t *testing.T) { s := "" rd := NewReader(strings.NewReader(s)) - runes := rd.Peek(1) + runes := rd.PeekRunes(1) if len(runes) != 1 { t.Fatal("incorrect number of runes", len(runes)) } @@ -111,14 +120,14 @@ func TestPeek(t *testing.T) { s := "a" rd := NewReader(strings.NewReader(s)) - runes := rd.Peek(1) + runes := rd.PeekRunes(1) assumeRunesArray(t, []rune{'a'}, runes) - runes = rd.Peek(1) + runes = rd.PeekRunes(1) assumeRunesArray(t, []rune{'a'}, runes) assumeRune(t, rd, 'a') - runes = rd.Peek(1) + runes = rd.PeekRunes(1) assumeRunesArray(t, []rune{EOF}, runes) assumeRune(t, rd, EOF) @@ -128,7 +137,7 @@ func TestPeekLarge(t *testing.T) { s := "abcdefg" rd := NewReader(strings.NewReader(s)) - runes := rd.Peek(100) + runes := rd.PeekRunes(100) if len(runes) != len(s)+1 { t.Fatal("incorrect number of runes", len(runes)) }