]> Cypherpunks repositories - gostls13.git/commitdiff
remove Line in bufio.ReadLine(Bytes|Slice|String)
authorRuss Cox <rsc@golang.org>
Thu, 27 Aug 2009 18:20:15 +0000 (11:20 -0700)
committerRuss Cox <rsc@golang.org>
Thu, 27 Aug 2009 18:20:15 +0000 (11:20 -0700)
also drop bool arg from ReadString

R=r
DELTA=45  (13 added, 1 deleted, 31 changed)
OCL=33923
CL=33960

src/pkg/bufio/bufio.go
src/pkg/bufio/bufio_test.go
src/pkg/http/request.go
src/pkg/log/log_test.go
src/pkg/net/parse_test.go
src/pkg/strconv/fp_test.go
src/pkg/unicode/maketables.go
test/bench/k-nucleotide.go
test/bench/reverse-complement.go
usr/austin/sym/binary.go

index 4f97871035b300f52e256e27c6cc51a204e2562b..895dbf6e7f69f384b018f6180d880e0f455d8233 100644 (file)
@@ -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;
 }
 
index 389b4097de285e4aa81f99eeaf4b2bcd83398593..7100bcfa5deb9047b59017af61ade92127010a23 100644 (file)
@@ -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
                }
index dabd39d208ca1d80830d073f5147e057b066b508..e276deeffc4cefd77cdb148358b32ae8609cac50 100644 (file)
@@ -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 {
index 12f732632586bc30625af35a7fa1ee97ae40f09e..52be6803d5a4c3b7ddbc13a55d506cb0696b6fda 100644 (file)
@@ -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{
index 227ae55f1dc17ceb236ae18c8f40f78b4292ce06..c95138896b9924a172a6663e6744f848bf2e226b 100644 (file)
@@ -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",
index 0890b0fad2cc67380c499a6ca6dcb69572665660..c38762dfb705f1c2dc6ec1a4254ee8af9ff7e3d8 100644 (file)
@@ -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
index 36e67be1bcb7dc864af9f824efe84049ad86d134..8e91276bf7338afb0b35ac1c2f7ff6707062da16 100644 (file)
@@ -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;
index 1c9ce35bf21b7a37e052a8dd05ef84434299b967..3206774296f793f1b507f626f099ed9b5d7dbe6f 100644 (file)
@@ -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);
index 3a0964db9b99c109834d762dfcd7379f3de0d9e8..28feed09419baf0bda8b79acf77ddc5f1c55ae65 100644 (file)
@@ -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
                }
index d06179cec08a19253cdb65b718d32ed3e20ed03b..015175d743265d8643f68580246551beaac09808 100644 (file)
@@ -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;
 }