]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: scanning widths apply after leading spaces
authorRob Pike <r@golang.org>
Fri, 12 Jun 2015 23:17:08 +0000 (16:17 -0700)
committerRob Pike <r@golang.org>
Sat, 13 Jun 2015 04:33:26 +0000 (04:33 +0000)
When scanning with a width, as in %5s, C skips leading spaces
brefore counting the 5 characters. We should do the same.

Reword the documentation about widths to make this clear.

Fixes #9444

Change-Id: I443a6441adcf1c834057ef3977f9116a987a79cd
Reviewed-on: https://go-review.googlesource.com/10997
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/fmt/doc.go
src/fmt/scan.go
src/fmt/scan_test.go

index a5fb513f30fee92b521247655940909240375a8c..2efe6ee5b490db56583d93bc7bd2a61ceea0b5bb 100644 (file)
                Flags # and + are not implemented.
 
        The familiar base-setting prefixes 0 (octal) and 0x
-       (hexadecimal) are accepted when scanning integers without a
-       format or with the %v verb.
-
-       Width is interpreted in the input text (%5s means at most
-       five runes of input will be read to scan a string) but there
-       is no syntax for scanning with a precision (no %5.2f, just
-       %5f).
+       (hexadecimal) are accepted when scanning integers without
+       a format or with the %v verb.
+
+       Width is interpreted in the input text but there is no
+       syntax for scanning with a precision (no %5.2f, just %5f).
+       If width is provided, it applies after leading spaces are
+       trimmed and specifies the maximum number of runes to read
+       to satisfy the verb. For example,
+          Sscanf(" 1234567 ", "%5s%d", &s, &i)
+       will set s to "12345" and i to 67 while
+          Sscanf(" 12 34 567 ", "%5s%d", &s, &i)
+       will set s to "12" and i to 34.
 
        In all the scanning functions, a carriage return followed
        immediately by a newline is treated as a plain newline
index d6b9b79c6bc70974b0320913966c0770f484af6d..21ed091d80f6068b0d5df93f8d815e27574eddf8 100644 (file)
@@ -1165,6 +1165,7 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro
                if !widPresent {
                        s.maxWid = hugeWid
                }
+               s.SkipSpace()
                s.argLimit = s.limit
                if f := s.count + s.maxWid; f < s.argLimit {
                        s.argLimit = f
index 9e3e90a5c43a386316a8ad9c64561fb90780c09b..694f93e1aeb0a544a7f8fbc3351ce1b6eb36cb53 100644 (file)
@@ -340,6 +340,8 @@ var multiTests = []ScanfMultiTest{
        {"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), 2.5), ""},
        {"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
        {"%c%c%c", "2\u50c2X", args(&r1, &r2, &r3), args('2', '\u50c2', 'X'), ""},
+       {"%5s%d", " 1234567 ", args(&s, &i), args("12345", 67), ""},
+       {"%5s%d", " 12 34 567 ", args(&s, &i), args("12", 34), ""},
 
        // Custom scanners.
        {"%e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},