Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
2495c14d
Commit
2495c14d
authored
Aug 04, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WriteString
R=rsc DELTA=41 (41 added, 0 deleted, 0 changed) OCL=32692 CL=32697
parent
f17c6bca
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
+41
-0
bufio.go
src/pkg/bufio/bufio.go
+19
-0
bufio_test.go
src/pkg/bufio/bufio_test.go
+22
-0
No files found.
src/pkg/bufio/bufio.go
View file @
2495c14d
...
...
@@ -487,6 +487,25 @@ func (b *Writer) WriteByte(c byte) os.Error {
return
nil
}
// WriteString writes a string.
func
(
b
*
Writer
)
WriteString
(
s
string
)
os
.
Error
{
if
b
.
err
!=
nil
{
return
b
.
err
}
// Common case, worth making fast.
if
b
.
Available
()
>=
len
(
s
)
||
len
(
b
.
buf
)
>=
len
(
s
)
&&
b
.
Flush
()
==
nil
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
// loop over bytes, not runes.
b
.
buf
[
b
.
n
]
=
s
[
i
];
b
.
n
++
;
}
return
nil
;
}
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
// loop over bytes, not runes.
b
.
WriteByte
(
s
[
i
]);
}
return
b
.
err
}
// buffered input and output
// ReadWriter stores pointers to a Reader and a Writer.
...
...
src/pkg/bufio/bufio_test.go
View file @
2495c14d
...
...
@@ -298,3 +298,25 @@ func TestNewWriterSizeIdempotent(t *testing.T) {
t
.
Error
(
"NewWriterSize did not enlarge buffer"
);
}
}
func
TestWriteString
(
t
*
testing
.
T
)
{
const
BufSize
=
8
;
buf
:=
new
(
bytes
.
Buffer
);
b
,
err
:=
NewWriterSize
(
buf
,
BufSize
);
if
err
!=
nil
{
t
.
Error
(
"NewWriterSize create fail"
,
err
);
}
b
.
WriteString
(
"0"
);
// easy
b
.
WriteString
(
"123456"
);
// still easy
b
.
WriteString
(
"7890"
);
// easy after flush
b
.
WriteString
(
"abcdefghijklmnopqrstuvwxy"
);
// hard
b
.
WriteString
(
"z"
);
b
.
Flush
();
if
b
.
err
!=
nil
{
t
.
Error
(
"WriteString"
,
b
.
err
);
}
s
:=
"01234567890abcdefghijklmnopqrstuvwxyz"
;
if
string
(
buf
.
Data
())
!=
s
{
t
.
Errorf
(
"WriteString wants %q gets %q"
,
s
,
string
(
buf
.
Data
()))
}
}
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