aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDave Cheney <dave@cheney.net>2015-05-01 15:29:11 +1000
committerDave Cheney <dave@cheney.net>2015-05-01 23:08:18 +0000
commit8d16253c90aea7f0c6eaef7741e0f32ecc86100e (patch)
treea7975ba3f9fe3125f70651f19f095a4a17a41c18 /src
parentc723230e4a1ddb30b4822de8f795c16fd9aa90ff (diff)
downloadgo-8d16253c90aea7f0c6eaef7741e0f32ecc86100e.tar.xz
cmd/internal/obj: remove Biobuf unget
This change applies CL 9365 to the copy of Biobuf in cmd/internal/obj. In the process I discovered that some of the methods that should have been checking the unget buffer before reading were not and it was probably just dumb luck that we handn't hit these issues before; Bungetc is only used in one place in cmd/internal/gc and only an unlikely code path. Change-Id: Ifa0c5c08442e9fe951a5078c6e9ec77a8a4dc2ff Reviewed-on: https://go-review.googlesource.com/9529 Reviewed-by: Daniel Morsing <daniel.morsing@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dave Cheney <dave@cheney.net> Run-TryBot: Dave Cheney <dave@cheney.net>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/internal/gc/lex.go17
-rw-r--r--src/cmd/internal/obj/util.go29
2 files changed, 19 insertions, 27 deletions
diff --git a/src/cmd/internal/gc/lex.go b/src/cmd/internal/gc/lex.go
index edfb6ca7d7..c2c4207998 100644
--- a/src/cmd/internal/gc/lex.go
+++ b/src/cmd/internal/gc/lex.go
@@ -1865,20 +1865,21 @@ func getc() int {
curio.cp = curio.cp[1:]
}
} else {
- var c1 int
- var c2 int
loop:
c = obj.Bgetc(curio.bin)
if c == 0xef {
- c1 = obj.Bgetc(curio.bin)
- c2 = obj.Bgetc(curio.bin)
- if c1 == 0xbb && c2 == 0xbf {
+ buf, err := curio.bin.Peek(2)
+ if err != nil {
+ log.Fatalf("getc: peeking: %v", err)
+ }
+ if buf[0] == 0xbb && buf[1] == 0xbf {
yyerrorl(int(lexlineno), "Unicode (UTF-8) BOM in middle of file")
+
+ // consume BOM bytes
+ obj.Bgetc(curio.bin)
+ obj.Bgetc(curio.bin)
goto loop
}
-
- obj.Bungetc(curio.bin)
- obj.Bungetc(curio.bin)
}
}
diff --git a/src/cmd/internal/obj/util.go b/src/cmd/internal/obj/util.go
index 71587954f4..b44b9caf38 100644
--- a/src/cmd/internal/obj/util.go
+++ b/src/cmd/internal/obj/util.go
@@ -28,12 +28,10 @@ func Cputime() float64 {
}
type Biobuf struct {
- unget [2]int
- numUnget int
- f *os.File
- r *bufio.Reader
- w *bufio.Writer
- linelen int
+ f *os.File
+ r *bufio.Reader
+ w *bufio.Writer
+ linelen int
}
func Bopenw(name string) (*Biobuf, error) {
@@ -116,18 +114,11 @@ func Bread(b *Biobuf, p []byte) int {
}
func Bgetc(b *Biobuf) int {
- if b.numUnget > 0 {
- b.numUnget--
- return int(b.unget[b.numUnget])
- }
c, err := b.r.ReadByte()
- r := int(c)
if err != nil {
- r = -1
+ return -1
}
- b.unget[1] = b.unget[0]
- b.unget[0] = r
- return r
+ return int(c)
}
func Bgetrune(b *Biobuf) int {
@@ -146,6 +137,10 @@ func (b *Biobuf) Read(p []byte) (int, error) {
return b.r.Read(p)
}
+func (b *Biobuf) Peek(n int) ([]byte, error) {
+ return b.r.Peek(n)
+}
+
func Brdline(b *Biobuf, delim int) string {
s, err := b.r.ReadBytes(byte(delim))
if err != nil {
@@ -181,10 +176,6 @@ func Blinelen(b *Biobuf) int {
return b.linelen
}
-func Bungetc(b *Biobuf) {
- b.numUnget++
-}
-
func Bterm(b *Biobuf) error {
var err error
if b.w != nil {