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
33f3ed77
Commit
33f3ed77
authored
Jan 16, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify fixup for bufio
R=rsc DELTA=88 (0 added, 12 deleted, 76 changed) OCL=22884 CL=22890
parent
3decb42e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
84 deletions
+72
-84
bufio.go
src/lib/bufio.go
+13
-17
bufio_test.go
src/lib/bufio_test.go
+59
-67
No files found.
src/lib/bufio.go
View file @
33f3ed77
...
@@ -18,7 +18,7 @@ import (
...
@@ -18,7 +18,7 @@ import (
// - buffered output
// - buffered output
const
(
const
(
D
efaultBufSize
=
4096
d
efaultBufSize
=
4096
)
)
export
var
(
export
var
(
...
@@ -30,7 +30,7 @@ export var (
...
@@ -30,7 +30,7 @@ export var (
ShortWrite
=
os
.
NewError
(
"short write"
);
ShortWrite
=
os
.
NewError
(
"short write"
);
)
)
func
C
opySlice
(
dst
[]
byte
,
src
[]
byte
)
{
func
c
opySlice
(
dst
[]
byte
,
src
[]
byte
)
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
dst
[
i
]
=
src
[
i
]
dst
[
i
]
=
src
[
i
]
}
}
...
@@ -57,9 +57,7 @@ export func NewBufReadSize(rd io.Read, size int) (b *BufRead, err *os.Error) {
...
@@ -57,9 +57,7 @@ export func NewBufReadSize(rd io.Read, size int) (b *BufRead, err *os.Error) {
}
}
export
func
NewBufRead
(
rd
io
.
Read
)
(
b
*
BufRead
,
err
*
os
.
Error
)
{
export
func
NewBufRead
(
rd
io
.
Read
)
(
b
*
BufRead
,
err
*
os
.
Error
)
{
// 6g BUG return NewBufReadSize(rd, DefaultBufSize)
return
NewBufReadSize
(
rd
,
defaultBufSize
);
r
,
e
:=
NewBufReadSize
(
rd
,
DefaultBufSize
);
return
r
,
e
}
}
// Read a new chunk into the buffer.
// Read a new chunk into the buffer.
...
@@ -70,7 +68,7 @@ func (b *BufRead) Fill() *os.Error {
...
@@ -70,7 +68,7 @@ func (b *BufRead) Fill() *os.Error {
// Slide existing data to beginning.
// Slide existing data to beginning.
if
b
.
w
>
b
.
r
{
if
b
.
w
>
b
.
r
{
C
opySlice
(
b
.
buf
[
0
:
b
.
w
-
b
.
r
],
b
.
buf
[
b
.
r
:
b
.
w
]);
c
opySlice
(
b
.
buf
[
0
:
b
.
w
-
b
.
r
],
b
.
buf
[
b
.
r
:
b
.
w
]);
b
.
w
-=
b
.
r
;
b
.
w
-=
b
.
r
;
}
else
{
}
else
{
b
.
w
=
0
b
.
w
=
0
...
@@ -107,7 +105,7 @@ func (b *BufRead) Read(p []byte) (nn int, err *os.Error) {
...
@@ -107,7 +105,7 @@ func (b *BufRead) Read(p []byte) (nn int, err *os.Error) {
if
n
>
b
.
w
-
b
.
r
{
if
n
>
b
.
w
-
b
.
r
{
n
=
b
.
w
-
b
.
r
n
=
b
.
w
-
b
.
r
}
}
C
opySlice
(
p
[
0
:
n
],
b
.
buf
[
b
.
r
:
b
.
r
+
n
]);
c
opySlice
(
p
[
0
:
n
],
b
.
buf
[
b
.
r
:
b
.
r
+
n
]);
p
=
p
[
n
:
len
(
p
)];
p
=
p
[
n
:
len
(
p
)];
b
.
r
+=
n
;
b
.
r
+=
n
;
nn
+=
n
nn
+=
n
...
@@ -170,7 +168,7 @@ func (b *BufRead) ReadRune() (rune int, size int, err *os.Error) {
...
@@ -170,7 +168,7 @@ func (b *BufRead) ReadRune() (rune int, size int, err *os.Error) {
// Helper function: look for byte c in array p,
// Helper function: look for byte c in array p,
// returning its index or -1.
// returning its index or -1.
func
F
indByte
(
p
[]
byte
,
c
byte
)
int
{
func
f
indByte
(
p
[]
byte
,
c
byte
)
int
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
if
p
[
i
]
==
c
{
if
p
[
i
]
==
c
{
return
i
return
i
...
@@ -197,7 +195,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
...
@@ -197,7 +195,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
}
}
// Look in buffer.
// Look in buffer.
if
i
:=
F
indByte
(
b
.
buf
[
b
.
r
:
b
.
w
],
delim
);
i
>=
0
{
if
i
:=
f
indByte
(
b
.
buf
[
b
.
r
:
b
.
w
],
delim
);
i
>=
0
{
line1
:=
b
.
buf
[
b
.
r
:
b
.
r
+
i
+
1
];
line1
:=
b
.
buf
[
b
.
r
:
b
.
r
+
i
+
1
];
b
.
r
+=
i
+
1
;
b
.
r
+=
i
+
1
;
return
line1
,
nil
return
line1
,
nil
...
@@ -217,7 +215,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
...
@@ -217,7 +215,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
}
}
// Search new part of buffer
// Search new part of buffer
if
i
:=
F
indByte
(
b
.
buf
[
n
:
b
.
w
],
delim
);
i
>=
0
{
if
i
:=
f
indByte
(
b
.
buf
[
n
:
b
.
w
],
delim
);
i
>=
0
{
line
:=
b
.
buf
[
0
:
n
+
i
+
1
];
line
:=
b
.
buf
[
0
:
n
+
i
+
1
];
b
.
r
=
n
+
i
+
1
;
b
.
r
=
n
+
i
+
1
;
return
line
,
nil
return
line
,
nil
...
@@ -304,10 +302,10 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err *os.Error) {
...
@@ -304,10 +302,10 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err *os.Error) {
buf
:=
make
([]
byte
,
n
);
buf
:=
make
([]
byte
,
n
);
n
=
0
;
n
=
0
;
for
i
:=
0
;
i
<
nfull
;
i
++
{
for
i
:=
0
;
i
<
nfull
;
i
++
{
C
opySlice
(
buf
[
n
:
n
+
len
(
full
[
i
])],
full
[
i
]);
c
opySlice
(
buf
[
n
:
n
+
len
(
full
[
i
])],
full
[
i
]);
n
+=
len
(
full
[
i
])
n
+=
len
(
full
[
i
])
}
}
C
opySlice
(
buf
[
n
:
n
+
len
(
frag
)],
frag
);
c
opySlice
(
buf
[
n
:
n
+
len
(
frag
)],
frag
);
return
buf
,
err
return
buf
,
err
}
}
...
@@ -346,9 +344,7 @@ export func NewBufWriteSize(wr io.Write, size int) (b *BufWrite, err *os.Error)
...
@@ -346,9 +344,7 @@ export func NewBufWriteSize(wr io.Write, size int) (b *BufWrite, err *os.Error)
}
}
export
func
NewBufWrite
(
wr
io
.
Write
)
(
b
*
BufWrite
,
err
*
os
.
Error
)
{
export
func
NewBufWrite
(
wr
io
.
Write
)
(
b
*
BufWrite
,
err
*
os
.
Error
)
{
// 6g BUG return NewBufWriteSize(wr, DefaultBufSize)
return
NewBufWriteSize
(
wr
,
defaultBufSize
);
r
,
e
:=
NewBufWriteSize
(
wr
,
DefaultBufSize
);
return
r
,
e
}
}
// Flush the output buffer.
// Flush the output buffer.
...
@@ -365,7 +361,7 @@ func (b *BufWrite) Flush() *os.Error {
...
@@ -365,7 +361,7 @@ func (b *BufWrite) Flush() *os.Error {
}
}
if
e
!=
nil
{
if
e
!=
nil
{
if
n
<
b
.
n
{
if
n
<
b
.
n
{
C
opySlice
(
b
.
buf
[
0
:
b
.
n
-
n
],
b
.
buf
[
n
:
b
.
n
])
c
opySlice
(
b
.
buf
[
0
:
b
.
n
-
n
],
b
.
buf
[
n
:
b
.
n
])
}
}
b
.
n
-=
n
;
b
.
n
-=
n
;
b
.
err
=
e
;
b
.
err
=
e
;
...
@@ -400,7 +396,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err *os.Error) {
...
@@ -400,7 +396,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err *os.Error) {
if
n
>
len
(
p
)
{
if
n
>
len
(
p
)
{
n
=
len
(
p
)
n
=
len
(
p
)
}
}
C
opySlice
(
b
.
buf
[
b
.
n
:
b
.
n
+
n
],
p
[
0
:
n
]);
c
opySlice
(
b
.
buf
[
b
.
n
:
b
.
n
+
n
],
p
[
0
:
n
]);
b
.
n
+=
n
;
b
.
n
+=
n
;
nn
+=
n
;
nn
+=
n
;
p
=
p
[
n
:
len
(
p
)]
p
=
p
[
n
:
len
(
p
)]
...
...
src/lib/bufio_test.go
View file @
33f3ed77
...
@@ -13,55 +13,47 @@ import (
...
@@ -13,55 +13,47 @@ import (
"testing"
;
"testing"
;
)
)
func
StringToBytes
(
s
string
)
[]
byte
{
b
:=
make
([]
byte
,
len
(
s
));
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
b
[
i
]
=
s
[
i
]
}
return
b
}
// Should be in language!
// Should be in language!
func
C
opy
(
p
[]
byte
,
q
[]
byte
)
{
func
c
opy
(
p
[]
byte
,
q
[]
byte
)
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
p
[
i
]
=
q
[
i
]
p
[
i
]
=
q
[
i
]
}
}
}
}
// Reads from p.
// Reads from p.
type
B
yteReader
struct
{
type
b
yteReader
struct
{
p
[]
byte
p
[]
byte
}
}
func
N
ewByteReader
(
p
[]
byte
)
io
.
Read
{
func
n
ewByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
B
yteReader
);
b
:=
new
(
b
yteReader
);
b
.
p
=
p
;
b
.
p
=
p
;
return
b
return
b
}
}
func
(
b
*
B
yteReader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
func
(
b
*
b
yteReader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
n
:=
len
(
p
);
n
:=
len
(
p
);
if
n
>
len
(
b
.
p
)
{
if
n
>
len
(
b
.
p
)
{
n
=
len
(
b
.
p
)
n
=
len
(
b
.
p
)
}
}
C
opy
(
p
[
0
:
n
],
b
.
p
[
0
:
n
]);
c
opy
(
p
[
0
:
n
],
b
.
p
[
0
:
n
]);
b
.
p
=
b
.
p
[
n
:
len
(
b
.
p
)];
b
.
p
=
b
.
p
[
n
:
len
(
b
.
p
)];
return
n
,
nil
return
n
,
nil
}
}
// Reads from p but only returns half of what you asked for.
// Reads from p but only returns half of what you asked for.
type
H
alfByteReader
struct
{
type
h
alfByteReader
struct
{
p
[]
byte
p
[]
byte
}
}
func
N
ewHalfByteReader
(
p
[]
byte
)
io
.
Read
{
func
n
ewHalfByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
H
alfByteReader
);
b
:=
new
(
h
alfByteReader
);
b
.
p
=
p
;
b
.
p
=
p
;
return
b
return
b
}
}
func
(
b
*
H
alfByteReader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
func
(
b
*
h
alfByteReader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
n
:=
len
(
p
)
/
2
;
n
:=
len
(
p
)
/
2
;
if
n
==
0
&&
len
(
p
)
>
0
{
if
n
==
0
&&
len
(
p
)
>
0
{
n
=
1
n
=
1
...
@@ -69,23 +61,23 @@ func (b *HalfByteReader) Read(p []byte) (int, *os.Error) {
...
@@ -69,23 +61,23 @@ func (b *HalfByteReader) Read(p []byte) (int, *os.Error) {
if
n
>
len
(
b
.
p
)
{
if
n
>
len
(
b
.
p
)
{
n
=
len
(
b
.
p
)
n
=
len
(
b
.
p
)
}
}
C
opy
(
p
[
0
:
n
],
b
.
p
[
0
:
n
]);
c
opy
(
p
[
0
:
n
],
b
.
p
[
0
:
n
]);
b
.
p
=
b
.
p
[
n
:
len
(
b
.
p
)];
b
.
p
=
b
.
p
[
n
:
len
(
b
.
p
)];
return
n
,
nil
return
n
,
nil
}
}
// Reads from a reader and rot13s the result.
// Reads from a reader and rot13s the result.
type
R
ot13Reader
struct
{
type
r
ot13Reader
struct
{
r
io
.
Read
r
io
.
Read
}
}
func
NewRot13Reader
(
r
io
.
Read
)
*
R
ot13Reader
{
func
newRot13Reader
(
r
io
.
Read
)
*
r
ot13Reader
{
r13
:=
new
(
R
ot13Reader
);
r13
:=
new
(
r
ot13Reader
);
r13
.
r
=
r
;
r13
.
r
=
r
;
return
r13
return
r13
}
}
func
(
r13
*
R
ot13Reader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
func
(
r13
*
r
ot13Reader
)
Read
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
n
,
e
:=
r13
.
r
.
Read
(
p
);
n
,
e
:=
r13
.
r
.
Read
(
p
);
if
e
!=
nil
{
if
e
!=
nil
{
return
n
,
e
return
n
,
e
...
@@ -102,18 +94,18 @@ func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) {
...
@@ -102,18 +94,18 @@ func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) {
return
n
,
nil
return
n
,
nil
}
}
type
Readm
aker
struct
{
type
readM
aker
struct
{
name
string
;
name
string
;
fn
*
([]
byte
)
io
.
Read
;
fn
*
([]
byte
)
io
.
Read
;
}
}
var
read
makers
=
[]
Readm
aker
{
var
read
Makers
=
[]
readM
aker
{
Readmaker
{
"full"
,
func
(
p
[]
byte
)
io
.
Read
{
return
N
ewByteReader
(
p
)
}
},
readMaker
{
"full"
,
func
(
p
[]
byte
)
io
.
Read
{
return
n
ewByteReader
(
p
)
}
},
Readmaker
{
"half"
,
func
(
p
[]
byte
)
io
.
Read
{
return
N
ewHalfByteReader
(
p
)
}
},
readMaker
{
"half"
,
func
(
p
[]
byte
)
io
.
Read
{
return
n
ewHalfByteReader
(
p
)
}
},
}
}
// Call ReadLineString (which ends up calling everything else)
// Call ReadLineString (which ends up calling everything else)
// to accumulate the text of a file.
// to accumulate the text of a file.
func
R
eadLines
(
b
*
BufRead
)
string
{
func
r
eadLines
(
b
*
BufRead
)
string
{
s
:=
""
;
s
:=
""
;
for
{
for
{
s1
,
e
:=
b
.
ReadLineString
(
'\n'
,
true
);
s1
,
e
:=
b
.
ReadLineString
(
'\n'
,
true
);
...
@@ -129,7 +121,7 @@ func ReadLines(b *BufRead) string {
...
@@ -129,7 +121,7 @@ func ReadLines(b *BufRead) string {
}
}
// Call ReadByte to accumulate the text of a file
// Call ReadByte to accumulate the text of a file
func
R
eadBytes
(
buf
*
BufRead
)
string
{
func
r
eadBytes
(
buf
*
BufRead
)
string
{
var
b
[
1000
]
byte
;
var
b
[
1000
]
byte
;
nb
:=
0
;
nb
:=
0
;
for
{
for
{
...
@@ -148,7 +140,7 @@ func ReadBytes(buf *BufRead) string {
...
@@ -148,7 +140,7 @@ func ReadBytes(buf *BufRead) string {
}
}
// Call Read to accumulate the text of a file
// Call Read to accumulate the text of a file
func
R
eads
(
buf
*
BufRead
,
m
int
)
string
{
func
r
eads
(
buf
*
BufRead
,
m
int
)
string
{
var
b
[
1000
]
byte
;
var
b
[
1000
]
byte
;
nb
:=
0
;
nb
:=
0
;
for
{
for
{
...
@@ -161,19 +153,19 @@ func Reads(buf *BufRead, m int) string {
...
@@ -161,19 +153,19 @@ func Reads(buf *BufRead, m int) string {
return
string
(
b
[
0
:
nb
])
return
string
(
b
[
0
:
nb
])
}
}
type
Bufr
eader
struct
{
type
bufR
eader
struct
{
name
string
;
name
string
;
fn
*
(
*
BufRead
)
string
;
fn
*
(
*
BufRead
)
string
;
}
}
var
bufreaders
=
[]
Bufr
eader
{
var
bufreaders
=
[]
bufR
eader
{
Bufreader
{
"1"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
1
)
}
},
bufReader
{
"1"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
1
)
}
},
Bufreader
{
"2"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
2
)
}
},
bufReader
{
"2"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
2
)
}
},
Bufreader
{
"3"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
3
)
}
},
bufReader
{
"3"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
3
)
}
},
Bufreader
{
"4"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
4
)
}
},
bufReader
{
"4"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
4
)
}
},
Bufreader
{
"5"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
5
)
}
},
bufReader
{
"5"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
5
)
}
},
Bufreader
{
"7"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
7
)
}
},
bufReader
{
"7"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
7
)
}
},
Bufreader
{
"bytes"
,
&
R
eadBytes
},
bufReader
{
"bytes"
,
&
r
eadBytes
},
Bufreader
{
"lines"
,
&
R
eadLines
},
bufReader
{
"lines"
,
&
r
eadLines
},
}
}
var
bufsizes
=
[]
int
{
var
bufsizes
=
[]
int
{
...
@@ -182,13 +174,13 @@ var bufsizes = []int {
...
@@ -182,13 +174,13 @@ var bufsizes = []int {
}
}
export
func
TestBufReadSimple
(
t
*
testing
.
T
)
{
export
func
TestBufReadSimple
(
t
*
testing
.
T
)
{
b
,
e
:=
NewBufRead
(
NewByteReader
(
StringTo
Bytes
(
"hello world"
)));
b
,
e
:=
NewBufRead
(
newByteReader
(
io
.
String
Bytes
(
"hello world"
)));
if
s
:=
R
eadBytes
(
b
);
s
!=
"hello world"
{
if
s
:=
r
eadBytes
(
b
);
s
!=
"hello world"
{
t
.
Errorf
(
"simple hello world test failed: got %q"
,
s
);
t
.
Errorf
(
"simple hello world test failed: got %q"
,
s
);
}
}
b
,
e
=
NewBufRead
(
NewRot13Reader
(
NewByteReader
(
StringTo
Bytes
(
"hello world"
))));
b
,
e
=
NewBufRead
(
newRot13Reader
(
newByteReader
(
io
.
String
Bytes
(
"hello world"
))));
if
s
:=
R
eadBytes
(
b
);
s
!=
"uryyb jbeyq"
{
if
s
:=
r
eadBytes
(
b
);
s
!=
"uryyb jbeyq"
{
t
.
Error
(
"rot13 hello world test failed: got %q"
,
s
);
t
.
Error
(
"rot13 hello world test failed: got %q"
,
s
);
}
}
}
}
...
@@ -206,11 +198,11 @@ export func TestBufRead(t *testing.T) {
...
@@ -206,11 +198,11 @@ export func TestBufRead(t *testing.T) {
for
h
:=
0
;
h
<
len
(
texts
);
h
++
{
for
h
:=
0
;
h
<
len
(
texts
);
h
++
{
text
:=
texts
[
h
];
text
:=
texts
[
h
];
textbytes
:=
StringTo
Bytes
(
text
);
textbytes
:=
io
.
String
Bytes
(
text
);
for
i
:=
0
;
i
<
len
(
read
m
akers
);
i
++
{
for
i
:=
0
;
i
<
len
(
read
M
akers
);
i
++
{
for
j
:=
0
;
j
<
len
(
bufreaders
);
j
++
{
for
j
:=
0
;
j
<
len
(
bufreaders
);
j
++
{
for
k
:=
0
;
k
<
len
(
bufsizes
);
k
++
{
for
k
:=
0
;
k
<
len
(
bufsizes
);
k
++
{
readmaker
:=
read
m
akers
[
i
];
readmaker
:=
read
M
akers
[
i
];
bufreader
:=
bufreaders
[
j
];
bufreader
:=
bufreaders
[
j
];
bufsize
:=
bufsizes
[
k
];
bufsize
:=
bufsizes
[
k
];
read
:=
readmaker
.
fn
(
textbytes
);
read
:=
readmaker
.
fn
(
textbytes
);
...
@@ -226,72 +218,72 @@ export func TestBufRead(t *testing.T) {
...
@@ -226,72 +218,72 @@ export func TestBufRead(t *testing.T) {
}
}
}
}
type
W
riteBuffer
interface
{
type
w
riteBuffer
interface
{
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
);
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
);
GetBytes
()
[]
byte
GetBytes
()
[]
byte
}
}
// Accumulates bytes into a byte array.
// Accumulates bytes into a byte array.
type
B
yteWriter
struct
{
type
b
yteWriter
struct
{
p
[]
byte
;
p
[]
byte
;
n
int
n
int
}
}
func
NewByteWriter
()
W
riteBuffer
{
func
newByteWriter
()
w
riteBuffer
{
return
new
(
B
yteWriter
)
return
new
(
b
yteWriter
)
}
}
func
(
w
*
B
yteWriter
)
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
func
(
w
*
b
yteWriter
)
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
if
w
.
p
==
nil
{
if
w
.
p
==
nil
{
w
.
p
=
make
([]
byte
,
len
(
p
)
+
100
)
w
.
p
=
make
([]
byte
,
len
(
p
)
+
100
)
}
else
if
w
.
n
+
len
(
p
)
>=
len
(
w
.
p
)
{
}
else
if
w
.
n
+
len
(
p
)
>=
len
(
w
.
p
)
{
newp
:=
make
([]
byte
,
len
(
w
.
p
)
*
2
+
len
(
p
));
newp
:=
make
([]
byte
,
len
(
w
.
p
)
*
2
+
len
(
p
));
C
opy
(
newp
[
0
:
w
.
n
],
w
.
p
[
0
:
w
.
n
]);
c
opy
(
newp
[
0
:
w
.
n
],
w
.
p
[
0
:
w
.
n
]);
w
.
p
=
newp
w
.
p
=
newp
}
}
C
opy
(
w
.
p
[
w
.
n
:
w
.
n
+
len
(
p
)],
p
);
c
opy
(
w
.
p
[
w
.
n
:
w
.
n
+
len
(
p
)],
p
);
w
.
n
+=
len
(
p
);
w
.
n
+=
len
(
p
);
return
len
(
p
),
nil
return
len
(
p
),
nil
}
}
func
(
w
*
B
yteWriter
)
GetBytes
()
[]
byte
{
func
(
w
*
b
yteWriter
)
GetBytes
()
[]
byte
{
return
w
.
p
[
0
:
w
.
n
]
return
w
.
p
[
0
:
w
.
n
]
}
}
// Accumulates bytes written into a byte array
// Accumulates bytes written into a byte array
// but Write only takes half of what you give it.
// but Write only takes half of what you give it.
// TODO: Could toss this -- Write() is not supposed to do that.
// TODO: Could toss this -- Write() is not supposed to do that.
type
H
alfByteWriter
struct
{
type
h
alfByteWriter
struct
{
bw
W
riteBuffer
bw
w
riteBuffer
}
}
func
NewHalfByteWriter
()
W
riteBuffer
{
func
newHalfByteWriter
()
w
riteBuffer
{
w
:=
new
(
H
alfByteWriter
);
w
:=
new
(
h
alfByteWriter
);
w
.
bw
=
N
ewByteWriter
();
w
.
bw
=
n
ewByteWriter
();
return
w
return
w
}
}
func
(
w
*
H
alfByteWriter
)
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
func
(
w
*
h
alfByteWriter
)
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
)
{
n
:=
(
len
(
p
)
+
1
)
/
2
;
n
:=
(
len
(
p
)
+
1
)
/
2
;
// BUG return w.bw.Write(p[0:n])
// BUG return w.bw.Write(p[0:n])
r
,
e
:=
w
.
bw
.
Write
(
p
[
0
:
n
]);
r
,
e
:=
w
.
bw
.
Write
(
p
[
0
:
n
]);
return
r
,
e
return
r
,
e
}
}
func
(
w
*
H
alfByteWriter
)
GetBytes
()
[]
byte
{
func
(
w
*
h
alfByteWriter
)
GetBytes
()
[]
byte
{
return
w
.
bw
.
GetBytes
()
return
w
.
bw
.
GetBytes
()
}
}
type
Writem
aker
struct
{
type
writeM
aker
struct
{
name
string
;
name
string
;
fn
*
()
W
riteBuffer
;
fn
*
()
w
riteBuffer
;
}
}
export
func
TestBufWrite
(
t
*
testing
.
T
)
{
export
func
TestBufWrite
(
t
*
testing
.
T
)
{
var
data
[
8192
]
byte
;
var
data
[
8192
]
byte
;
var
writers
=
[]
Writem
aker
{
var
writers
=
[]
writeM
aker
{
Writemaker
{
"full"
,
&
N
ewByteWriter
},
writeMaker
{
"full"
,
&
n
ewByteWriter
},
Writemaker
{
"half"
,
&
N
ewHalfByteWriter
},
writeMaker
{
"half"
,
&
n
ewHalfByteWriter
},
};
};
for
i
:=
0
;
i
<
len
(
data
);
i
++
{
for
i
:=
0
;
i
<
len
(
data
);
i
++
{
...
...
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