]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: treat \r\n as \n in Scan
authorRob Pike <r@golang.org>
Wed, 31 Jul 2013 05:00:08 +0000 (15:00 +1000)
committerRob Pike <r@golang.org>
Wed, 31 Jul 2013 05:00:08 +0000 (15:00 +1000)
When scanning input and "white space" is permitted, a carriage return
followed immediately by a newline (\r\n) is treated exactly the same
as a plain newline (\n). I hope this makes it work better on Windows.

We do it everywhere, not just on Windows, since why not?

Fixes #5391.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/12142043

src/pkg/fmt/doc.go
src/pkg/fmt/scan.go
src/pkg/fmt/scan_test.go

index 29fc6e5e3a091f4a3515e3cfa874eca5b8f8cad7..e1fca445e307a85edb582c02d0b4b1365a9d9bfd 100644 (file)
        stops if it does not, with the return value of the function
        indicating the number of arguments scanned.
 
+       In all the scanning functions, a carriage return followed
+       immediately by a newline is treated as a plain newline
+       (\r\n means the same as \n).
+
        In all the scanning functions, if an operand implements method
        Scan (that is, it implements the Scanner interface) that
        method will be used to scan the text for that operand.  Also,
index f223897a91101fe520ebab0c4388cbad95f574de..93621e24c8cda830b685b369f5696ed2af340431 100644 (file)
@@ -437,6 +437,9 @@ func (s *ss) skipSpace(stopAtNewline bool) {
                if r == eof {
                        return
                }
+               if r == '\r' && s.peek("\n") {
+                       continue
+               }
                if r == '\n' {
                        if stopAtNewline {
                                break
index 4e2c0feb2cbb65d9a07e43250a3d5eee2d25d2c1..e60cc347aa860388ff0d19315cc875a1071730b5 100644 (file)
@@ -192,6 +192,10 @@ var scanTests = []ScanTest{
        {"-.45e1-1e2i\n", &complex128Val, complex128(-.45e1 - 100i)},
        {"hello\n", &stringVal, "hello"},
 
+       // Carriage-return followed by newline. (We treat \r\n as \n always.)
+       {"hello\r\n", &stringVal, "hello"},
+       {"27\r\n", &uint8Val, uint8(27)},
+
        // Renamed types
        {"true\n", &renamedBoolVal, renamedBool(true)},
        {"F\n", &renamedBoolVal, renamedBool(false)},