1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:24:41 -07:00

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
This commit is contained in:
Rob Pike 2013-07-31 15:00:08 +10:00
parent a696ae56db
commit 221af5c12f
3 changed files with 11 additions and 0 deletions

View File

@ -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,

View 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

View 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)},