Skip to content

Commit

Permalink
fmt: fix size returned on fast path of ReadRune
Browse files Browse the repository at this point in the history
Fixes #8512.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/130090043
  • Loading branch information
robpike committed Aug 15, 2014
1 parent c5e648d commit 4edcbe0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pkg/fmt/scan.go
Expand Up @@ -360,6 +360,7 @@ func (r *readRune) ReadRune() (rr rune, size int, err error) {
}
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
rr = rune(r.buf[0])
size = 1 // Known to be 1.
return
}
var n int
Expand Down
32 changes: 32 additions & 0 deletions src/pkg/fmt/scan_test.go
Expand Up @@ -842,6 +842,38 @@ func TestLineByLineFscanf(t *testing.T) {
}
}

// TestScanStateCount verifies the correct byte count is returned. Issue 8512.

// runeScanner implements the Scanner interface for TestScanStateCount.
type runeScanner struct {
rune rune
size int
}

func (rs *runeScanner) Scan(state ScanState, verb rune) error {
r, size, err := state.ReadRune()
rs.rune = r
rs.size = size
return err
}

func TestScanStateCount(t *testing.T) {
var a, b, c runeScanner
n, err := Sscanf("12➂", "%c%c%c", &a, &b, &c)
if err != nil {
t.Fatal(err)
}
if n != 3 {
t.Fatalf("expected 3 items consumed, got %d")
}
if a.rune != '1' || b.rune != '2' || c.rune != '➂' {
t.Errorf("bad scan rune: %q %q %q should be '1' '2' '➂'", a.rune, b.rune, c.rune)
}
if a.size != 1 || b.size != 1 || c.size != 3 {
t.Errorf("bad scan size: %q %q %q should be 1 1 3", a.size, b.size, c.size)
}
}

// RecursiveInt accepts a string matching %d.%d.%d....
// and parses it into a linked list.
// It allows us to benchmark recursive descent style scanners.
Expand Down

0 comments on commit 4edcbe0

Please sign in to comment.