mirror of https://github.com/golang/go.git
remove Line in bufio.ReadLine(Bytes|Slice|String)
also drop bool arg from ReadString R=r DELTA=45 (13 added, 1 deleted, 31 changed) OCL=33923 CL=33960
This commit is contained in:
parent
bdcf1f8125
commit
4b409289f6
|
|
@ -214,13 +214,17 @@ func (b *Reader) Buffered() int {
|
|||
return b.w - b.r;
|
||||
}
|
||||
|
||||
// ReadLineSlice reads until the first occurrence of delim in the input,
|
||||
// ReadSlice reads until the first occurrence of delim in the input,
|
||||
// returning a slice pointing at the bytes in the buffer.
|
||||
// The bytes stop being valid at the next read call.
|
||||
// Fails if the line doesn't fit in the buffer.
|
||||
// For internal or advanced use only; most uses should
|
||||
// call ReadLineString or ReadLineBytes instead.
|
||||
func (b *Reader) ReadLineSlice(delim byte) (line []byte, err os.Error) {
|
||||
// If ReadSlice encounters an error before finding a delimiter,
|
||||
// it returns all the data in the buffer and the error itself (often os.EOF).
|
||||
// ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
|
||||
// Because the data returned from ReadSlice will be overwritten
|
||||
// by the next I/O operation, most clients should use
|
||||
// ReadBytes or ReadString instead.
|
||||
// ReadSlice returns err != nil if and only if line does not end in delim.
|
||||
func (b *Reader) ReadSlice(delim byte) (line []byte, err os.Error) {
|
||||
// Look in buffer.
|
||||
if i := findByte(b.buf[b.r:b.w], delim); i >= 0 {
|
||||
line1 := b.buf[b.r:b.r+i+1];
|
||||
|
|
@ -254,13 +258,13 @@ func (b *Reader) ReadLineSlice(delim byte) (line []byte, err os.Error) {
|
|||
panic("not reached");
|
||||
}
|
||||
|
||||
// ReadLineBytes reads until the first occurrence of delim in the input,
|
||||
// returning a new byte array containing the line.
|
||||
// If an error happens, returns the data (without a delimiter)
|
||||
// and the error. (It can't leave the data in the buffer because
|
||||
// it might have read more than the buffer size.)
|
||||
func (b *Reader) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
||||
// Use ReadLineSlice to look for array,
|
||||
// ReadBytes reads until the first occurrence of delim in the input,
|
||||
// returning a string containing the data up to and including the delimiter.
|
||||
// If ReadBytes encounters an error before finding a delimiter,
|
||||
// it returns the data read before the error and the error itself (often os.EOF).
|
||||
// ReadBytes returns err != nil if and only if line does not end in delim.
|
||||
func (b *Reader) ReadBytes(delim byte) (line []byte, err os.Error) {
|
||||
// Use ReadSlice to look for array,
|
||||
// accumulating full buffers.
|
||||
var frag []byte;
|
||||
var full [][]byte;
|
||||
|
|
@ -269,7 +273,7 @@ func (b *Reader) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
|||
|
||||
for {
|
||||
var e os.Error;
|
||||
frag, e = b.ReadLineSlice(delim);
|
||||
frag, e = b.ReadSlice(delim);
|
||||
if e == nil { // got final fragment
|
||||
break
|
||||
}
|
||||
|
|
@ -327,14 +331,13 @@ func (b *Reader) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
|||
return buf, err
|
||||
}
|
||||
|
||||
// ReadLineString reads until the first occurrence of delim in the input,
|
||||
// returning a new string containing the line.
|
||||
// If savedelim, keep delim in the result; otherwise drop it.
|
||||
func (b *Reader) ReadLineString(delim byte, savedelim bool) (line string, err os.Error) {
|
||||
bytes, e := b.ReadLineBytes(delim);
|
||||
if n := len(bytes); !savedelim && n > 0 && bytes[n-1] == delim {
|
||||
bytes = bytes[0:n-1]
|
||||
}
|
||||
// ReadString reads until the first occurrence of delim in the input,
|
||||
// returning a string containing the data up to and including the delimiter.
|
||||
// If ReadString encounters an error before finding a delimiter,
|
||||
// it returns the data read before the error and the error itself (often os.EOF).
|
||||
// ReadString returns err != nil if and only if line does not end in delim.
|
||||
func (b *Reader) ReadString(delim byte) (line string, err os.Error) {
|
||||
bytes, e := b.ReadBytes(delim);
|
||||
return string(bytes), e;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,12 +84,12 @@ var readMakers = []readMaker {
|
|||
readMaker{ "data+err", iotest.DataErrReader },
|
||||
}
|
||||
|
||||
// Call ReadLineString (which ends up calling everything else)
|
||||
// Call ReadString (which ends up calling everything else)
|
||||
// to accumulate the text of a file.
|
||||
func readLines(b *Reader) string {
|
||||
s := "";
|
||||
for {
|
||||
s1, e := b.ReadLineString('\n', true);
|
||||
s1, e := b.ReadString('\n');
|
||||
if e == os.EOF {
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ func (req *Request) write(w io.Writer) os.Error {
|
|||
// The returned bytes are a pointer into storage in
|
||||
// the bufio, so they are only valid until the next bufio read.
|
||||
func readLineBytes(b *bufio.Reader) (p []byte, err os.Error) {
|
||||
if p, err = b.ReadLineSlice('\n'); err != nil {
|
||||
if p, err = b.ReadSlice('\n'); err != nil {
|
||||
// We always know when EOF is coming.
|
||||
// If the caller asked for a line, there should be a line.
|
||||
if err == os.EOF {
|
||||
|
|
|
|||
|
|
@ -59,10 +59,11 @@ func testLog(t *testing.T, flag int, prefix string, pattern string, useLogf bool
|
|||
} else {
|
||||
l.Log("hello", 23, "world");
|
||||
}
|
||||
line, err3 := buf.ReadLineString('\n', false);
|
||||
line, err3 := buf.ReadString('\n');
|
||||
if err3 != nil {
|
||||
t.Fatal("log error", err3);
|
||||
}
|
||||
line = line[0:len(line)-1];
|
||||
pattern = "^"+pattern+"hello 23 world$";
|
||||
matched, err4 := regexp.MatchString(pattern, line);
|
||||
if err4 != nil{
|
||||
|
|
|
|||
|
|
@ -28,7 +28,10 @@ func TestReadLine(t *testing.T) {
|
|||
lineno := 1;
|
||||
byteno := 0;
|
||||
for {
|
||||
bline, berr := br.ReadLineString('\n', false);
|
||||
bline, berr := br.ReadString('\n');
|
||||
if n := len(bline); n > 0 {
|
||||
bline = bline[0:n-1];
|
||||
}
|
||||
line, ok := file.readLine();
|
||||
if (berr != nil) != !ok || bline != line {
|
||||
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
|
||||
|
|
|
|||
|
|
@ -104,13 +104,14 @@ func TestFp(t *testing.T) {
|
|||
|
||||
lineno := 0;
|
||||
for {
|
||||
line, err2 := b.ReadLineString('\n', false);
|
||||
line, err2 := b.ReadString('\n');
|
||||
if err2 == os.EOF {
|
||||
break;
|
||||
}
|
||||
if err2 != nil {
|
||||
panicln("testfp: read testfp.txt:", err2.String());
|
||||
}
|
||||
line = line[0:len(line)-1];
|
||||
lineno++;
|
||||
if len(line) == 0 || line[0] == '#' {
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ func main() {
|
|||
}
|
||||
input := bufio.NewReader(resp.Body);
|
||||
for {
|
||||
line, err := input.ReadLineString('\n', false);
|
||||
line, err := input.ReadString('\n', false);
|
||||
if err != nil {
|
||||
if err == os.EOF {
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ func main() {
|
|||
buf := new(bytes.Buffer);
|
||||
three := strings.Bytes(">THREE ");
|
||||
for {
|
||||
line, err := in.ReadLineSlice('\n');
|
||||
line, err := in.ReadSlice('\n');
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "ReadLine err:", err);
|
||||
os.Exit(2);
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ func main() {
|
|||
buf := make([]byte, 100*1024);
|
||||
top := 0;
|
||||
for {
|
||||
line, err := in.ReadLineSlice('\n');
|
||||
line, err := in.ReadSlice('\n');
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,12 +134,16 @@ func (r *binaryReader) ReadInt64() int64 {
|
|||
return int64(r.ReadUint64());
|
||||
}
|
||||
|
||||
// ReadCString reads a NULL-terminated string.
|
||||
// ReadCString reads a NUL-terminated string.
|
||||
func (r *binaryReader) ReadCString() string {
|
||||
str, err := r.Reader.ReadLineString('\x00', false);
|
||||
str, err := r.Reader.ReadString('\x00');
|
||||
if r.err == nil && err != nil {
|
||||
r.err = err;
|
||||
}
|
||||
n := len(str);
|
||||
if n > 0 {
|
||||
str = str[0:n-1];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue