Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
3aee82b6
Commit
3aee82b6
authored
Jan 26, 2011
by
Brad Fitzpatrick
Committed by
Adam Langley
Jan 26, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/line: make it an io.Reader too
R=agl1, bradfitzwork, rsc CC=golang-dev
https://golang.org/cl/4066043
parent
434f1e32
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
5 deletions
+42
-5
src/pkg/encoding/line/line.go
src/pkg/encoding/line/line.go
+21
-5
src/pkg/encoding/line/line_test.go
src/pkg/encoding/line/line_test.go
+21
-0
No files found.
src/pkg/encoding/line/line.go
View file @
3aee82b6
...
@@ -28,6 +28,26 @@ func NewReader(input io.Reader, maxLineLength int) *Reader {
...
@@ -28,6 +28,26 @@ func NewReader(input io.Reader, maxLineLength int) *Reader {
}
}
}
}
// 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.
// 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
// 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
// set and the beginning of the line is returned. The rest of the line will be
...
@@ -36,11 +56,7 @@ func NewReader(input io.Reader, maxLineLength int) *Reader {
...
@@ -36,11 +56,7 @@ func NewReader(input io.Reader, maxLineLength int) *Reader {
// the Reader and is only valid until the next call to ReadLine. ReadLine
// 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.
// either returns a non-nil line or it returns an error, never both.
func
(
l
*
Reader
)
ReadLine
()
(
line
[]
byte
,
isPrefix
bool
,
err
os
.
Error
)
{
func
(
l
*
Reader
)
ReadLine
()
(
line
[]
byte
,
isPrefix
bool
,
err
os
.
Error
)
{
if
l
.
consumed
>
0
{
l
.
removeConsumedFromBuffer
()
n
:=
copy
(
l
.
buf
,
l
.
buf
[
l
.
consumed
:
])
l
.
buf
=
l
.
buf
[
:
n
]
l
.
consumed
=
0
}
if
len
(
l
.
buf
)
==
0
&&
l
.
err
!=
nil
{
if
len
(
l
.
buf
)
==
0
&&
l
.
err
!=
nil
{
err
=
l
.
err
err
=
l
.
err
...
...
src/pkg/encoding/line/line_test.go
View file @
3aee82b6
...
@@ -6,6 +6,7 @@ package line
...
@@ -6,6 +6,7 @@ package line
import
(
import
(
"bytes"
"bytes"
"io"
"os"
"os"
"testing"
"testing"
)
)
...
@@ -87,3 +88,23 @@ func TestLineTooLong(t *testing.T) {
...
@@ -87,3 +88,23 @@ func TestLineTooLong(t *testing.T) {
t
.
Errorf
(
"bad result for third line: %x"
,
line
)
t
.
Errorf
(
"bad result for third line: %x"
,
line
)
}
}
}
}
func
TestReadAfterLines
(
t
*
testing
.
T
)
{
line1
:=
"line1"
restData
:=
"line2
\n
line 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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment