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
Hide 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 (
// - buffered output
const
(
D
efaultBufSize
=
4096
d
efaultBufSize
=
4096
)
export
var
(
...
...
@@ -30,7 +30,7 @@ export var (
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
++
{
dst
[
i
]
=
src
[
i
]
}
...
...
@@ -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
)
{
// 6g BUG return NewBufReadSize(rd, DefaultBufSize)
r
,
e
:=
NewBufReadSize
(
rd
,
DefaultBufSize
);
return
r
,
e
return
NewBufReadSize
(
rd
,
defaultBufSize
);
}
// Read a new chunk into the buffer.
...
...
@@ -70,7 +68,7 @@ func (b *BufRead) Fill() *os.Error {
// Slide existing data to beginning.
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
;
}
else
{
b
.
w
=
0
...
...
@@ -107,7 +105,7 @@ func (b *BufRead) Read(p []byte) (nn int, err *os.Error) {
if
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
)];
b
.
r
+=
n
;
nn
+=
n
...
...
@@ -170,7 +168,7 @@ func (b *BufRead) ReadRune() (rune int, size int, err *os.Error) {
// Helper function: look for byte c in array p,
// 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
++
{
if
p
[
i
]
==
c
{
return
i
...
...
@@ -197,7 +195,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
}
// 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
];
b
.
r
+=
i
+
1
;
return
line1
,
nil
...
...
@@ -217,7 +215,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
}
// 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
];
b
.
r
=
n
+
i
+
1
;
return
line
,
nil
...
...
@@ -304,10 +302,10 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err *os.Error) {
buf
:=
make
([]
byte
,
n
);
n
=
0
;
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
])
}
C
opySlice
(
buf
[
n
:
n
+
len
(
frag
)],
frag
);
c
opySlice
(
buf
[
n
:
n
+
len
(
frag
)],
frag
);
return
buf
,
err
}
...
...
@@ -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
)
{
// 6g BUG return NewBufWriteSize(wr, DefaultBufSize)
r
,
e
:=
NewBufWriteSize
(
wr
,
DefaultBufSize
);
return
r
,
e
return
NewBufWriteSize
(
wr
,
defaultBufSize
);
}
// Flush the output buffer.
...
...
@@ -365,7 +361,7 @@ func (b *BufWrite) Flush() *os.Error {
}
if
e
!=
nil
{
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
.
err
=
e
;
...
...
@@ -400,7 +396,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err *os.Error) {
if
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
;
nn
+=
n
;
p
=
p
[
n
:
len
(
p
)]
...
...
src/lib/bufio_test.go
View file @
33f3ed77
...
...
@@ -13,55 +13,47 @@ import (
"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!
func
C
opy
(
p
[]
byte
,
q
[]
byte
)
{
func
c
opy
(
p
[]
byte
,
q
[]
byte
)
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
p
[
i
]
=
q
[
i
]
}
}
// Reads from p.
type
B
yteReader
struct
{
type
b
yteReader
struct
{
p
[]
byte
}
func
N
ewByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
B
yteReader
);
func
n
ewByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
b
yteReader
);
b
.
p
=
p
;
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
);
if
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
)];
return
n
,
nil
}
// Reads from p but only returns half of what you asked for.
type
H
alfByteReader
struct
{
type
h
alfByteReader
struct
{
p
[]
byte
}
func
N
ewHalfByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
H
alfByteReader
);
func
n
ewHalfByteReader
(
p
[]
byte
)
io
.
Read
{
b
:=
new
(
h
alfByteReader
);
b
.
p
=
p
;
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
;
if
n
==
0
&&
len
(
p
)
>
0
{
n
=
1
...
...
@@ -69,23 +61,23 @@ func (b *HalfByteReader) Read(p []byte) (int, *os.Error) {
if
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
)];
return
n
,
nil
}
// Reads from a reader and rot13s the result.
type
R
ot13Reader
struct
{
type
r
ot13Reader
struct
{
r
io
.
Read
}
func
NewRot13Reader
(
r
io
.
Read
)
*
R
ot13Reader
{
r13
:=
new
(
R
ot13Reader
);
func
newRot13Reader
(
r
io
.
Read
)
*
r
ot13Reader
{
r13
:=
new
(
r
ot13Reader
);
r13
.
r
=
r
;
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
);
if
e
!=
nil
{
return
n
,
e
...
...
@@ -102,18 +94,18 @@ func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) {
return
n
,
nil
}
type
Readm
aker
struct
{
type
readM
aker
struct
{
name
string
;
fn
*
([]
byte
)
io
.
Read
;
}
var
read
makers
=
[]
Readm
aker
{
Readmaker
{
"full"
,
func
(
p
[]
byte
)
io
.
Read
{
return
N
ewByteReader
(
p
)
}
},
Readmaker
{
"half"
,
func
(
p
[]
byte
)
io
.
Read
{
return
N
ewHalfByteReader
(
p
)
}
},
var
read
Makers
=
[]
readM
aker
{
readMaker
{
"full"
,
func
(
p
[]
byte
)
io
.
Read
{
return
n
ewByteReader
(
p
)
}
},
readMaker
{
"half"
,
func
(
p
[]
byte
)
io
.
Read
{
return
n
ewHalfByteReader
(
p
)
}
},
}
// Call ReadLineString (which ends up calling everything else)
// to accumulate the text of a file.
func
R
eadLines
(
b
*
BufRead
)
string
{
func
r
eadLines
(
b
*
BufRead
)
string
{
s
:=
""
;
for
{
s1
,
e
:=
b
.
ReadLineString
(
'\n'
,
true
);
...
...
@@ -129,7 +121,7 @@ func ReadLines(b *BufRead) string {
}
// 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
;
nb
:=
0
;
for
{
...
...
@@ -148,7 +140,7 @@ func ReadBytes(buf *BufRead) string {
}
// 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
;
nb
:=
0
;
for
{
...
...
@@ -161,19 +153,19 @@ func Reads(buf *BufRead, m int) string {
return
string
(
b
[
0
:
nb
])
}
type
Bufr
eader
struct
{
type
bufR
eader
struct
{
name
string
;
fn
*
(
*
BufRead
)
string
;
}
var
bufreaders
=
[]
Bufr
eader
{
Bufreader
{
"1"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
1
)
}
},
Bufreader
{
"2"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
2
)
}
},
Bufreader
{
"3"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
3
)
}
},
Bufreader
{
"4"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
4
)
}
},
Bufreader
{
"5"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
5
)
}
},
Bufreader
{
"7"
,
func
(
b
*
BufRead
)
string
{
return
R
eads
(
b
,
7
)
}
},
Bufreader
{
"bytes"
,
&
R
eadBytes
},
Bufreader
{
"lines"
,
&
R
eadLines
},
var
bufreaders
=
[]
bufR
eader
{
bufReader
{
"1"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
1
)
}
},
bufReader
{
"2"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
2
)
}
},
bufReader
{
"3"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
3
)
}
},
bufReader
{
"4"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
4
)
}
},
bufReader
{
"5"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
5
)
}
},
bufReader
{
"7"
,
func
(
b
*
BufRead
)
string
{
return
r
eads
(
b
,
7
)
}
},
bufReader
{
"bytes"
,
&
r
eadBytes
},
bufReader
{
"lines"
,
&
r
eadLines
},
}
var
bufsizes
=
[]
int
{
...
...
@@ -182,13 +174,13 @@ var bufsizes = []int {
}
export
func
TestBufReadSimple
(
t
*
testing
.
T
)
{
b
,
e
:=
NewBufRead
(
NewByteReader
(
StringTo
Bytes
(
"hello world"
)));
if
s
:=
R
eadBytes
(
b
);
s
!=
"hello world"
{
b
,
e
:=
NewBufRead
(
newByteReader
(
io
.
String
Bytes
(
"hello world"
)));
if
s
:=
r
eadBytes
(
b
);
s
!=
"hello world"
{
t
.
Errorf
(
"simple hello world test failed: got %q"
,
s
);
}
b
,
e
=
NewBufRead
(
NewRot13Reader
(
NewByteReader
(
StringTo
Bytes
(
"hello world"
))));
if
s
:=
R
eadBytes
(
b
);
s
!=
"uryyb jbeyq"
{
b
,
e
=
NewBufRead
(
newRot13Reader
(
newByteReader
(
io
.
String
Bytes
(
"hello world"
))));
if
s
:=
r
eadBytes
(
b
);
s
!=
"uryyb jbeyq"
{
t
.
Error
(
"rot13 hello world test failed: got %q"
,
s
);
}
}
...
...
@@ -206,11 +198,11 @@ export func TestBufRead(t *testing.T) {
for
h
:=
0
;
h
<
len
(
texts
);
h
++
{
text
:=
texts
[
h
];
textbytes
:=
StringTo
Bytes
(
text
);
for
i
:=
0
;
i
<
len
(
read
m
akers
);
i
++
{
textbytes
:=
io
.
String
Bytes
(
text
);
for
i
:=
0
;
i
<
len
(
read
M
akers
);
i
++
{
for
j
:=
0
;
j
<
len
(
bufreaders
);
j
++
{
for
k
:=
0
;
k
<
len
(
bufsizes
);
k
++
{
readmaker
:=
read
m
akers
[
i
];
readmaker
:=
read
M
akers
[
i
];
bufreader
:=
bufreaders
[
j
];
bufsize
:=
bufsizes
[
k
];
read
:=
readmaker
.
fn
(
textbytes
);
...
...
@@ -226,72 +218,72 @@ export func TestBufRead(t *testing.T) {
}
}
type
W
riteBuffer
interface
{
type
w
riteBuffer
interface
{
Write
(
p
[]
byte
)
(
int
,
*
os
.
Error
);
GetBytes
()
[]
byte
}
// Accumulates bytes into a byte array.
type
B
yteWriter
struct
{
type
b
yteWriter
struct
{
p
[]
byte
;
n
int
}
func
NewByteWriter
()
W
riteBuffer
{
return
new
(
B
yteWriter
)
func
newByteWriter
()
w
riteBuffer
{
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
{
w
.
p
=
make
([]
byte
,
len
(
p
)
+
100
)
}
else
if
w
.
n
+
len
(
p
)
>=
len
(
w
.
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
}
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
);
return
len
(
p
),
nil
}
func
(
w
*
B
yteWriter
)
GetBytes
()
[]
byte
{
func
(
w
*
b
yteWriter
)
GetBytes
()
[]
byte
{
return
w
.
p
[
0
:
w
.
n
]
}
// Accumulates bytes written into a byte array
// but Write only takes half of what you give it.
// TODO: Could toss this -- Write() is not supposed to do that.
type
H
alfByteWriter
struct
{
bw
W
riteBuffer
type
h
alfByteWriter
struct
{
bw
w
riteBuffer
}
func
NewHalfByteWriter
()
W
riteBuffer
{
w
:=
new
(
H
alfByteWriter
);
w
.
bw
=
N
ewByteWriter
();
func
newHalfByteWriter
()
w
riteBuffer
{
w
:=
new
(
h
alfByteWriter
);
w
.
bw
=
n
ewByteWriter
();
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
;
// BUG return w.bw.Write(p[0:n])
r
,
e
:=
w
.
bw
.
Write
(
p
[
0
:
n
]);
return
r
,
e
}
func
(
w
*
H
alfByteWriter
)
GetBytes
()
[]
byte
{
func
(
w
*
h
alfByteWriter
)
GetBytes
()
[]
byte
{
return
w
.
bw
.
GetBytes
()
}
type
Writem
aker
struct
{
type
writeM
aker
struct
{
name
string
;
fn
*
()
W
riteBuffer
;
fn
*
()
w
riteBuffer
;
}
export
func
TestBufWrite
(
t
*
testing
.
T
)
{
var
data
[
8192
]
byte
;
var
writers
=
[]
Writem
aker
{
Writemaker
{
"full"
,
&
N
ewByteWriter
},
Writemaker
{
"half"
,
&
N
ewHalfByteWriter
},
var
writers
=
[]
writeM
aker
{
writeMaker
{
"full"
,
&
n
ewByteWriter
},
writeMaker
{
"half"
,
&
n
ewHalfByteWriter
},
};
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