|
1 From 71387ff53f95ddd5f0bb91d7e4c92ccdc65238e7 Mon Sep 17 00:00:00 2001 |
|
2 From: Rob Pike <[email protected]> |
|
3 Date: Sun, 23 Aug 2015 17:45:58 +1000 |
|
4 Subject: [PATCH 20/63] [release-branch.go1.5] fmt: in Scanf, %c can scan a |
|
5 space, so don't skip spaces at %c |
|
6 |
|
7 In short, %c should just give you the next rune, period. |
|
8 Apparently this is the design. I use the term loosely. |
|
9 |
|
10 Fixes #12275 |
|
11 |
|
12 Change-Id: I6f30bed442c0e88eac2244d465c7d151b29cf393 |
|
13 Reviewed-on: https://go-review.googlesource.com/13821 |
|
14 Reviewed-by: Andrew Gerrand <[email protected]> |
|
15 Reviewed-on: https://go-review.googlesource.com/14395 |
|
16 --- |
|
17 src/fmt/scan.go | 13 +++++++++---- |
|
18 src/fmt/scan_test.go | 11 +++++++---- |
|
19 2 files changed, 16 insertions(+), 8 deletions(-) |
|
20 |
|
21 diff --git a/src/fmt/scan.go b/src/fmt/scan.go |
|
22 index 5b9b516..e3e0fd0 100644 |
|
23 --- a/src/fmt/scan.go |
|
24 +++ b/src/fmt/scan.go |
|
25 @@ -83,6 +83,8 @@ func Scanln(a ...interface{}) (n int, err error) { |
|
26 // the format. It returns the number of items successfully scanned. |
|
27 // If that is less than the number of arguments, err will report why. |
|
28 // Newlines in the input must match newlines in the format. |
|
29 +// The one exception: the verb %c always scans the next rune in the |
|
30 +// input, even if it is a space (or tab etc.) or newline. |
|
31 func Scanf(format string, a ...interface{}) (n int, err error) { |
|
32 return Fscanf(os.Stdin, format, a...) |
|
33 } |
|
34 @@ -1164,15 +1166,18 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro |
|
35 if !widPresent { |
|
36 s.maxWid = hugeWid |
|
37 } |
|
38 - s.SkipSpace() |
|
39 + |
|
40 + c, w := utf8.DecodeRuneInString(format[i:]) |
|
41 + i += w |
|
42 + |
|
43 + if c != 'c' { |
|
44 + s.SkipSpace() |
|
45 + } |
|
46 s.argLimit = s.limit |
|
47 if f := s.count + s.maxWid; f < s.argLimit { |
|
48 s.argLimit = f |
|
49 } |
|
50 |
|
51 - c, w := utf8.DecodeRuneInString(format[i:]) |
|
52 - i += w |
|
53 - |
|
54 if numProcessed >= len(a) { // out of operands |
|
55 s.errorString("too few operands for format %" + format[i-w:]) |
|
56 break |
|
57 diff --git a/src/fmt/scan_test.go b/src/fmt/scan_test.go |
|
58 index a378436..334c4a6 100644 |
|
59 --- a/src/fmt/scan_test.go |
|
60 +++ b/src/fmt/scan_test.go |
|
61 @@ -300,10 +300,13 @@ var scanfTests = []ScanfTest{ |
|
62 {"%2s", "sssss", &xVal, Xs("ss")}, |
|
63 |
|
64 // Fixed bugs |
|
65 - {"%d\n", "27\n", &intVal, 27}, // ok |
|
66 - {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline" |
|
67 - {"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted. |
|
68 - {"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted. |
|
69 + {"%d\n", "27\n", &intVal, 27}, // ok |
|
70 + {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline" |
|
71 + {"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted. |
|
72 + {"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted. |
|
73 + {"%c", " ", &uintVal, uint(' ')}, // %c must accept a blank. |
|
74 + {"%c", "\t", &uintVal, uint('\t')}, // %c must accept any space. |
|
75 + {"%c", "\n", &uintVal, uint('\n')}, // %c must accept any space. |
|
76 } |
|
77 |
|
78 var overflowTests = []ScanTest{ |
|
79 -- |
|
80 2.6.1 |
|
81 |