--
| Rory McGuire ClearFormat - Research and Development UK : 44 870 224 0424 USA : 1 877 842 6286 RSA : 27 21 466 9400 Email: rmcg...@clearformat.com Website: www.clearformat.com |
--
:D good point, you win.
I think there should at least be some limit, in those functions to though.I doubt a valid line will ever be longer than 10000 runes. I use 1.0.3 and there is no limit in ReadBytes or ReadSlice.How would one use ReadBytes safely esp. in a server that relies on it (such as telnet)?
--
> --
>
>
Pre-OSX Macs did that. That's the "Mac File Format". OSX uses the "Unix file format". Yes it's confusing. If you have to support it, it's an extra headache, because otherwise, ending a line with \n hits both windows \r\n and unix/linux \n, and you're just done.
--
--
--
| Rory McGuire ClearFormat - Research and Development UK : 44 870 224 0424 USA : 1 877 842 6286 RSA : 27 21 466 9400 Email: rmcg...@clearformat.com Website: www.clearformat.com |
--
There is bufio.ReadLine(), but that will only give you a partial line
if the line is very long. And it doesn't work with Mac line-endings,
because it only checks for Unix and DOS line-endings.
The doc for bufio.ReadLine() says most people should just use
bufio.ReadString('\n') instead, but that only works with Unix line-
endings.
What I would want is a function that returns a single line, all line-
endings removed, either \n, \r, or \r\n (even \n\r ?), and complete
lines, not that stuff with prefixes. And it should return the last
line without error if it has non-zero length, and is missing a line-
ending.
Read the code in bufio.go. To me it looks like ReadLine uses ReadSlice which appears (quick look) to use the defaultBufSize as the buffer size so I think you would get a prefix if your line was longer than 4096 bytes (1 <<20 bytes in your example). To test you could set your buffer size to 16(minReadBufferSize).
ReadLine and ReadSlice should be safe then, and ReadSlice does what I wanted.
ReadBytes allocates its own memory and repeatedly calls ReadSlice so it's not safe, same for ReadString because it uses ReadBytes. Neither of them have memory limits so you can't use them with unchecked user input, as Andrew said you would have to use a LimitedReader except not how the standard http implementation use it because it has a LimitedReader set to int64(1<<63)
>> --
>>
>>
The Go compiler can't handle source code with Mac line-endings:
go build test.go
can't load package: package :
test.go:1:15: expected ';', found 'import'
Python can.
I think that's quite foolish. If you want a function that reads an arbitrary amount, then have no limit. If you want a limit, have the caller specify it. What you think is obviously big enough will be insufficient for some case years from now that nobody has thought of.
Thomas
--
The Go compiler can't handle source code with Mac line-endings
Python can.
However highly-obsolete, it is still around. Reading files means
reading something that already exists.
An importent point is that line-end codings is not something that
separates one document from another like EBCDIC from ASCII from Shift-
JIS. The latter are very different beasts. But Unix, DOS or Mac ASCII
text files, they can and do live in the same environment, usually
without the user noticing the difference.
I think the point is that "line" is a vague and complex thing, capable of too much dangerous oversimplification. Better in this area to specify what line terminator you want and use that, better to think about what kind of allocation you want and use that.
--
Note this thread: https://groups.google.com/d/msg/golang-nuts/QiR85Z1W6fc/870wtfwP2q0JSounds like something new may be coming down the pipeline... though it sounds like the default will still not include \r as a standalone line delimiter. Also, I wasn't clear on when that code would make it into Go, since 1.1 is past feature freeze at this point.