From 221af5c12fe9769b723b8af2f000ed5f39a5dbb3 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 31 Jul 2013 15:00:08 +1000 Subject: [PATCH] fmt: treat \r\n as \n in Scan 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 | 4 ++++ src/pkg/fmt/scan.go | 3 +++ src/pkg/fmt/scan_test.go | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/src/pkg/fmt/doc.go b/src/pkg/fmt/doc.go index 29fc6e5e3a..e1fca445e3 100644 --- a/src/pkg/fmt/doc.go +++ b/src/pkg/fmt/doc.go @@ -215,6 +215,10 @@ 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, diff --git a/src/pkg/fmt/scan.go b/src/pkg/fmt/scan.go index f223897a91..93621e24c8 100644 --- a/src/pkg/fmt/scan.go +++ b/src/pkg/fmt/scan.go @@ -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 diff --git a/src/pkg/fmt/scan_test.go b/src/pkg/fmt/scan_test.go index 4e2c0feb2c..e60cc347aa 100644 --- a/src/pkg/fmt/scan_test.go +++ b/src/pkg/fmt/scan_test.go @@ -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)}, -- 2.48.1