1
0
mirror of https://github.com/golang/go synced 2024-11-12 06:20:22 -07:00

fmt.Scan: %c

R=rsc
CC=golang-dev
https://golang.org/cl/1518042
This commit is contained in:
Rob Pike 2010-06-03 17:03:22 -07:00
parent 1d282a8eb2
commit 9a70762ac9
2 changed files with 23 additions and 5 deletions

View File

@ -219,7 +219,6 @@ func (s *ss) Token() (tok string, err os.Error) {
// readRune is a structure to enable reading UTF-8 encoded code points
// from an io.Reader. It is used if the Reader given to the scanner does
// not already implement ReadRuner.
// TODO: readByteRune for things that can read bytes.
type readRune struct {
reader io.Reader
buf [utf8.UTFMax]byte
@ -435,9 +434,22 @@ func (s *ss) scanNumber(digits string) string {
return s.buf.String()
}
// scanRune returns the next rune value in the input.
func (s *ss) scanRune(bitSize uint) int64 {
rune := int64(s.mustGetRune())
x := (rune << (64 - bitSize)) >> (64 - bitSize)
if x != rune {
s.errorString("overflow on character value " + string(rune))
}
return rune
}
// scanInt returns the value of the integer represented by the next
// token, checking for overflow. Any error is stored in s.err.
func (s *ss) scanInt(verb int, bitSize uint) int64 {
if verb == 'c' {
return s.scanRune(bitSize)
}
base, digits := s.getBase(verb)
s.skipSpace()
s.accept(sign) // If there's a sign, it will be left in the token buffer.
@ -456,6 +468,9 @@ func (s *ss) scanInt(verb int, bitSize uint) int64 {
// scanUint returns the value of the unsigned integer represented
// by the next token, checking for overflow. Any error is stored in s.err.
func (s *ss) scanUint(verb int, bitSize uint) uint64 {
if verb == 'c' {
return uint64(s.scanRune(bitSize))
}
base, digits := s.getBase(verb)
s.skipSpace()
tok := s.scanNumber(digits)

View File

@ -198,6 +198,8 @@ var scanfTests = []ScanfTest{
ScanfTest{"%t", "false\n", &boolVal, false},
ScanfTest{"%v", "-71\n", &intVal, -71},
ScanfTest{"%d", "72\n", &intVal, 72},
ScanfTest{"%c", "a\n", &intVal, 'a'},
ScanfTest{"%c", "\u1234\n", &intVal, '\u1234'},
ScanfTest{"%d", "73\n", &int8Val, int8(73)},
ScanfTest{"%d", "+74\n", &int16Val, int16(74)},
ScanfTest{"%d", "75\n", &int32Val, int32(75)},
@ -299,12 +301,13 @@ var multiTests = []ScanfMultiTest{
ScanfMultiTest{"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
// Custom scanner.
ScanfMultiTest{"%2e%f", "eefffff", []interface{}{&x, &y}, []interface{}{Xs("ee"), Xs("fffff")}, ""},
ScanfMultiTest{"%2e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},
// Errors
ScanfMultiTest{"%t", "23 18", []interface{}{&i}, nil, "bad verb"},
ScanfMultiTest{"%d %d %d", "23 18", []interface{}{&i, &j}, []interface{}{23, 18}, "too few operands"},
ScanfMultiTest{"%d %d", "23 18 27", []interface{}{&i, &j, &k}, []interface{}{23, 18}, "too many operands"},
ScanfMultiTest{"%t", "23 18", args(&i), nil, "bad verb"},
ScanfMultiTest{"%d %d %d", "23 18", args(&i, &j), args(23, 18), "too few operands"},
ScanfMultiTest{"%d %d", "23 18 27", args(&i, &j, &k), args(23, 18), "too many operands"},
ScanfMultiTest{"%c", "\u0100", args(&int8Val), nil, "overflow"},
}
func testScan(t *testing.T, scan func(r io.Reader, a ...interface{}) (int, os.Error)) {