summaryrefslogtreecommitdiff
path: root/src/pkg/encoding/line
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-06-30 15:34:22 +0200
committerOndřej Surý <ondrej@sury.org>2011-06-30 15:34:22 +0200
commitd39f5aa373a4422f7a5f3ee764fb0f6b0b719d61 (patch)
tree1833f8b72a4b3a8f00d0d143b079a8fcad01c6ae /src/pkg/encoding/line
parent8652e6c371b8905498d3d314491d36c58d5f68d5 (diff)
downloadgolang-upstream/58.tar.gz
Imported Upstream version 58upstream/58
Diffstat (limited to 'src/pkg/encoding/line')
-rw-r--r--src/pkg/encoding/line/Makefile11
-rw-r--r--src/pkg/encoding/line/line.go115
-rw-r--r--src/pkg/encoding/line/line_test.go133
3 files changed, 0 insertions, 259 deletions
diff --git a/src/pkg/encoding/line/Makefile b/src/pkg/encoding/line/Makefile
deleted file mode 100644
index 1af355c27..000000000
--- a/src/pkg/encoding/line/Makefile
+++ /dev/null
@@ -1,11 +0,0 @@
-# Copyright 2010 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-include ../../../Make.inc
-
-TARG=encoding/line
-GOFILES=\
- line.go\
-
-include ../../../Make.pkg
diff --git a/src/pkg/encoding/line/line.go b/src/pkg/encoding/line/line.go
deleted file mode 100644
index 123962b1f..000000000
--- a/src/pkg/encoding/line/line.go
+++ /dev/null
@@ -1,115 +0,0 @@
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Package line implements a Reader that reads lines delimited by '\n' or
-// ' \r\n'.
-package line
-
-import (
- "io"
- "os"
-)
-
-// Reader reads lines, delimited by '\n' or \r\n', from an io.Reader.
-type Reader struct {
- buf []byte
- consumed int
- in io.Reader
- err os.Error
-}
-
-// NewReader returns a new Reader that will read successive
-// lines from the input Reader.
-func NewReader(input io.Reader, maxLineLength int) *Reader {
- return &Reader{
- buf: make([]byte, 0, maxLineLength),
- consumed: 0,
- in: input,
- }
-}
-
-// Read reads from any buffered data past the last line read, or from the underlying
-// io.Reader if the buffer is empty.
-func (l *Reader) Read(p []byte) (n int, err os.Error) {
- l.removeConsumedFromBuffer()
- if len(l.buf) > 0 {
- n = copy(p, l.buf)
- l.consumed += n
- return
- }
- return l.in.Read(p)
-}
-
-func (l *Reader) removeConsumedFromBuffer() {
- if l.consumed > 0 {
- n := copy(l.buf, l.buf[l.consumed:])
- l.buf = l.buf[:n]
- l.consumed = 0
- }
-}
-
-// ReadLine tries to return a single line, not including the end-of-line bytes.
-// If the line was found to be longer than the maximum length then isPrefix is
-// set and the beginning of the line is returned. The rest of the line will be
-// returned from future calls. isPrefix will be false when returning the last
-// fragment of the line. The returned buffer points into the internal state of
-// the Reader and is only valid until the next call to ReadLine. ReadLine
-// either returns a non-nil line or it returns an error, never both.
-func (l *Reader) ReadLine() (line []byte, isPrefix bool, err os.Error) {
- l.removeConsumedFromBuffer()
-
- if len(l.buf) == 0 && l.err != nil {
- err = l.err
- return
- }
-
- scannedTo := 0
-
- for {
- i := scannedTo
- for ; i < len(l.buf); i++ {
- if l.buf[i] == '\r' && len(l.buf) > i+1 && l.buf[i+1] == '\n' {
- line = l.buf[:i]
- l.consumed = i + 2
- return
- } else if l.buf[i] == '\n' {
- line = l.buf[:i]
- l.consumed = i + 1
- return
- }
- }
-
- if i == cap(l.buf) {
- line = l.buf[:i]
- l.consumed = i
- isPrefix = true
- return
- }
-
- if l.err != nil {
- line = l.buf
- l.consumed = i
- return
- }
-
- // We don't want to rescan the input that we just scanned.
- // However, we need to back up one byte because the last byte
- // could have been a '\r' and we do need to rescan that.
- scannedTo = i
- if scannedTo > 0 {
- scannedTo--
- }
- oldLen := len(l.buf)
- l.buf = l.buf[:cap(l.buf)]
- n, readErr := l.in.Read(l.buf[oldLen:])
- l.buf = l.buf[:oldLen+n]
- if readErr != nil {
- l.err = readErr
- if len(l.buf) == 0 {
- return nil, false, readErr
- }
- }
- }
- panic("unreachable")
-}
diff --git a/src/pkg/encoding/line/line_test.go b/src/pkg/encoding/line/line_test.go
deleted file mode 100644
index ff3d51669..000000000
--- a/src/pkg/encoding/line/line_test.go
+++ /dev/null
@@ -1,133 +0,0 @@
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package line
-
-import (
- "bytes"
- "io"
- "io/ioutil"
- "os"
- "testing"
-)
-
-var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy")
-var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy")
-var testInputrn = []byte("012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\nuvw\r\nxy\r\n\n\r\n")
-
-// TestReader wraps a []byte and returns reads of a specific length.
-type testReader struct {
- data []byte
- stride int
-}
-
-func (t *testReader) Read(buf []byte) (n int, err os.Error) {
- n = t.stride
- if n > len(t.data) {
- n = len(t.data)
- }
- if n > len(buf) {
- n = len(buf)
- }
- copy(buf, t.data)
- t.data = t.data[n:]
- if len(t.data) == 0 {
- err = os.EOF
- }
- return
-}
-
-func testLineReader(t *testing.T, input []byte) {
- for stride := 1; stride < len(input); stride++ {
- done := 0
- reader := testReader{input, stride}
- l := NewReader(&reader, len(input)+1)
- for {
- line, isPrefix, err := l.ReadLine()
- if len(line) > 0 && err != nil {
- t.Errorf("ReadLine returned both data and error: %s", err)
- }
- if isPrefix {
- t.Errorf("ReadLine returned prefix")
- }
- if err != nil {
- if err != os.EOF {
- t.Fatalf("Got unknown error: %s", err)
- }
- break
- }
- if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) {
- t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line)
- }
- done += len(line)
- }
- if done != len(testOutput) {
- t.Error("ReadLine didn't return everything")
- }
- }
-}
-
-func TestReader(t *testing.T) {
- testLineReader(t, testInput)
- testLineReader(t, testInputrn)
-}
-
-func TestLineTooLong(t *testing.T) {
- buf := bytes.NewBuffer([]byte("aaabbbcc\n"))
- l := NewReader(buf, 3)
- line, isPrefix, err := l.ReadLine()
- if !isPrefix || !bytes.Equal(line, []byte("aaa")) || err != nil {
- t.Errorf("bad result for first line: %x %s", line, err)
- }
- line, isPrefix, err = l.ReadLine()
- if !isPrefix || !bytes.Equal(line, []byte("bbb")) || err != nil {
- t.Errorf("bad result for second line: %x", line)
- }
- line, isPrefix, err = l.ReadLine()
- if isPrefix || !bytes.Equal(line, []byte("cc")) || err != nil {
- t.Errorf("bad result for third line: %x", line)
- }
-}
-
-func TestReadAfterLines(t *testing.T) {
- line1 := "line1"
- restData := "line2\nline 3\n"
- inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData))
- outbuf := new(bytes.Buffer)
- maxLineLength := len(line1) + len(restData)/2
- l := NewReader(inbuf, maxLineLength)
- line, isPrefix, err := l.ReadLine()
- if isPrefix || err != nil || string(line) != line1 {
- t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line))
- }
- n, err := io.Copy(outbuf, l)
- if int(n) != len(restData) || err != nil {
- t.Errorf("bad result for Read: n=%d err=%v", n, err)
- }
- if outbuf.String() != restData {
- t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData)
- }
-}
-
-func TestReadEmptyBuffer(t *testing.T) {
- l := NewReader(bytes.NewBuffer(nil), 10)
- line, isPrefix, err := l.ReadLine()
- if err != os.EOF {
- t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
- }
-}
-
-func TestLinesAfterRead(t *testing.T) {
- l := NewReader(bytes.NewBuffer([]byte("foo")), 10)
- _, err := ioutil.ReadAll(l)
- if err != nil {
- t.Error(err)
- return
- }
-
- line, isPrefix, err := l.ReadLine()
- if err != os.EOF {
- t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
- }
-}