src/fmt/scan.go | 13 +++++++++---- src/fmt/scan_test.go | 11 +++++++---- diff --git a/src/fmt/scan.go b/src/fmt/scan.go index 5b9b516353b94c18ff3bf7538f61aac7b7acf897..e3e0fd0b5858d79d84a8888159aa567da7c594ee 100644 --- a/src/fmt/scan.go +++ b/src/fmt/scan.go @@ -83,6 +83,8 @@ // space-separated values into successive arguments as determined by // the format. It returns the number of items successfully scanned. // If that is less than the number of arguments, err will report why. // Newlines in the input must match newlines in the format. +// The one exception: the verb %c always scans the next rune in the +// input, even if it is a space (or tab etc.) or newline. func Scanf(format string, a ...interface{}) (n int, err error) { return Fscanf(os.Stdin, format, a...) } @@ -1164,14 +1166,17 @@ s.maxWid, widPresent, i = parsenum(format, i, end) if !widPresent { s.maxWid = hugeWid } - s.SkipSpace() + + c, w := utf8.DecodeRuneInString(format[i:]) + i += w + + if c != 'c' { + s.SkipSpace() + } s.argLimit = s.limit if f := s.count + s.maxWid; f < s.argLimit { s.argLimit = f } - - c, w := utf8.DecodeRuneInString(format[i:]) - i += w if numProcessed >= len(a) { // out of operands s.errorString("too few operands for format %" + format[i-w:]) diff --git a/src/fmt/scan_test.go b/src/fmt/scan_test.go index a3784364e630329d9b9214dd8af80dd426e92321..334c4a6b2428b944f9c71b9904067862f0a72b89 100644 --- a/src/fmt/scan_test.go +++ b/src/fmt/scan_test.go @@ -300,10 +300,13 @@ {"%s", " sss ", &xVal, Xs("sss")}, {"%2s", "sssss", &xVal, Xs("ss")}, // Fixed bugs - {"%d\n", "27\n", &intVal, 27}, // ok - {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline" - {"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted. - {"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted. + {"%d\n", "27\n", &intVal, 27}, // ok + {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline" + {"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted. + {"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted. + {"%c", " ", &uintVal, uint(' ')}, // %c must accept a blank. + {"%c", "\t", &uintVal, uint('\t')}, // %c must accept any space. + {"%c", "\n", &uintVal, uint('\n')}, // %c must accept any space. } var overflowTests = []ScanTest{