From 5876cb0e63f9cfd606fad348d6735d117cb3794d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 6 Aug 2009 12:05:16 -0700 Subject: [PATCH] - bufio.ReadRune fix for empty reads w/o errors - added testcases R=rsc DELTA=61 (56 added, 2 deleted, 3 changed) OCL=32831 CL=32834 --- src/pkg/bufio/bufio.go | 8 ++---- src/pkg/bufio/bufio_test.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/pkg/bufio/bufio.go b/src/pkg/bufio/bufio.go index c64e01b446..295647d5a0 100644 --- a/src/pkg/bufio/bufio.go +++ b/src/pkg/bufio/bufio.go @@ -195,13 +195,11 @@ func (b *Reader) UnreadByte() os.Error { // rune and its size in bytes. func (b *Reader) ReadRune() (rune int, size int, err os.Error) { for b.r + utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) { - n := b.w - b.r; b.fill(); if b.err != nil { - return 0, 0, b.err - } - if b.w - b.r == n { - // no bytes read + if b.r == b.w { + return 0, 0, b.err; + } break; } } diff --git a/src/pkg/bufio/bufio_test.go b/src/pkg/bufio/bufio_test.go index ec7f949725..c08f68ebe4 100644 --- a/src/pkg/bufio/bufio_test.go +++ b/src/pkg/bufio/bufio_test.go @@ -168,6 +168,62 @@ func TestReader(t *testing.T) { } } +// A StringReader delivers its data one string segment at a time via Read. +type StringReader struct { + data []string; + step int; +} + +func (r *StringReader) Read (p []byte) (n int, err os.Error) { + if r.step < len(r.data) { + s := r.data[r.step]; + for i := 0; i < len(s); i++ { + p[i] = s[i]; + } + n = len(s); + r.step++; + } else { + err = os.EOF; + } + return; +} + +func readRuneSegments(t *testing.T, segments []string) { + got := ""; + want := strings.Join(segments, ""); + r := bufio.NewReader(&StringReader{data: segments}); + for { + rune, size, err := r.ReadRune(); + if err != nil { + if err != os.EOF { + return; + } + break; + } + got += string(rune); + } + if got != want { + t.Errorf("segments=%v got=%s want=%s", segments, got, want); + } +} + +var segmentList = [][]string { + []string{}, + []string{""}, + []string{"日", "本語"}, + []string{"\u65e5", "\u672c", "\u8a9e"}, + []string{"\U000065e5, "", \U0000672c", "\U00008a9e"}, + []string{"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"}, + []string{"Hello", ", ", "World", "!"}, + []string{"Hello", ", ", "", "World", "!"}, +} + +func TestReadRune(t *testing.T) { + for _, s := range segmentList { + readRuneSegments(t, s); + } +} + func TestWriter(t *testing.T) { var data [8192]byte;