diff options
author | Christopher Wedgwood <cw@f00f.org> | 2010-04-29 11:01:21 -0700 |
---|---|---|
committer | Christopher Wedgwood <cw@f00f.org> | 2010-04-29 11:01:21 -0700 |
commit | 28acc8311cd49853067b572f2eef89138bc9dd72 (patch) | |
tree | 53f2f43b76762e043636bf3f3dae0a04bc3b6983 | |
parent | 43bb3333f68281fd201f6dfa1ea629d194696014 (diff) | |
download | golang-28acc8311cd49853067b572f2eef89138bc9dd72.tar.gz |
net: parser should handle EOF without newline properly.
Fixes issue 686.
R=rsc
CC=adg, golang-dev
http://codereview.appspot.com/979044
Committer: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/pkg/net/parse.go | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/pkg/net/parse.go b/src/pkg/net/parse.go index ff980f412..2bc0db465 100644 --- a/src/pkg/net/parse.go +++ b/src/pkg/net/parse.go @@ -13,31 +13,32 @@ import ( ) type file struct { - file *os.File - data []byte + file *os.File + data []byte + atEOF bool } func (f *file) close() { f.file.Close() } func (f *file) getLineFromData() (s string, ok bool) { data := f.data - for i := 0; i < len(data); i++ { + i := 0 + for i = 0; i < len(data); i++ { if data[i] == '\n' { s = string(data[0:i]) ok = true // move data i++ n := len(data) - i - for j := 0; j < n; j++ { - data[j] = data[i+j] - } + copy(data[0:], data[i:]) f.data = data[0:n] return } } - if len(f.data) > 0 { + if f.atEOF && len(f.data) > 0 { + // EOF, return all we have s = string(data) - f.data = nil + f.data = f.data[0:0] ok = true } return @@ -49,10 +50,13 @@ func (f *file) readLine() (s string, ok bool) { } if len(f.data) < cap(f.data) { ln := len(f.data) - n, _ := io.ReadFull(f.file, f.data[ln:cap(f.data)]) + n, err := io.ReadFull(f.file, f.data[ln:cap(f.data)]) if n >= 0 { f.data = f.data[0 : ln+n] } + if err == os.EOF { + f.atEOF = true + } } s, ok = f.getLineFromData() return @@ -63,7 +67,7 @@ func open(name string) (*file, os.Error) { if err != nil { return nil, err } - return &file{fd, make([]byte, 1024)[0:0]}, nil + return &file{fd, make([]byte, 1024)[0:0], false}, nil } func byteIndex(s string, c byte) int { |