mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net: parser should handle EOF without newline properly.
Fixes #686. R=rsc CC=adg, golang-dev https://golang.org/cl/979044
This commit is contained in:
parent
39f009cb8e
commit
be9f6344a7
1 changed files with 14 additions and 10 deletions
|
|
@ -13,31 +13,32 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type file struct {
|
type file struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
data []byte
|
data []byte
|
||||||
|
atEOF bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *file) close() { f.file.Close() }
|
func (f *file) close() { f.file.Close() }
|
||||||
|
|
||||||
func (f *file) getLineFromData() (s string, ok bool) {
|
func (f *file) getLineFromData() (s string, ok bool) {
|
||||||
data := f.data
|
data := f.data
|
||||||
for i := 0; i < len(data); i++ {
|
i := 0
|
||||||
|
for i = 0; i < len(data); i++ {
|
||||||
if data[i] == '\n' {
|
if data[i] == '\n' {
|
||||||
s = string(data[0:i])
|
s = string(data[0:i])
|
||||||
ok = true
|
ok = true
|
||||||
// move data
|
// move data
|
||||||
i++
|
i++
|
||||||
n := len(data) - i
|
n := len(data) - i
|
||||||
for j := 0; j < n; j++ {
|
copy(data[0:], data[i:])
|
||||||
data[j] = data[i+j]
|
|
||||||
}
|
|
||||||
f.data = data[0:n]
|
f.data = data[0:n]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(f.data) > 0 {
|
if f.atEOF && len(f.data) > 0 {
|
||||||
|
// EOF, return all we have
|
||||||
s = string(data)
|
s = string(data)
|
||||||
f.data = nil
|
f.data = f.data[0:0]
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -49,10 +50,13 @@ func (f *file) readLine() (s string, ok bool) {
|
||||||
}
|
}
|
||||||
if len(f.data) < cap(f.data) {
|
if len(f.data) < cap(f.data) {
|
||||||
ln := len(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 {
|
if n >= 0 {
|
||||||
f.data = f.data[0 : ln+n]
|
f.data = f.data[0 : ln+n]
|
||||||
}
|
}
|
||||||
|
if err == os.EOF {
|
||||||
|
f.atEOF = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s, ok = f.getLineFromData()
|
s, ok = f.getLineFromData()
|
||||||
return
|
return
|
||||||
|
|
@ -63,7 +67,7 @@ func open(name string) (*file, os.Error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func byteIndex(s string, c byte) int {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue