Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Why is there no proper ReadLine function?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 62 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter Kleiweg  
View profile  
 More options Oct 31 2012, 7:23 pm
From: Peter Kleiweg <pklei...@xs4all.nl>
Date: Wed, 31 Oct 2012 16:23:36 -0700 (PDT)
Local: Wed, Oct 31 2012 7:23 pm
Subject: Why is there no proper ReadLine function?
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.

Sure I can write this function myself, but I think it is a basic
enough functionality to belong in the standard library.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rory McGuire  
View profile  
 More options Nov 1 2012, 4:51 am
From: Rory McGuire <rjmcgu...@gmail.com>
Date: Thu, 1 Nov 2012 01:51:38 -0700 (PDT)
Local: Thurs, Nov 1 2012 4:51 am
Subject: Re: Why is there no proper ReadLine function?

Which limits should this new ReadLine function have?

The current built in solutions are the simplest and cleanest, and they make
people that
want to write their own version have to think about it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 5:04 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 20:03:54 +1100
Local: Thurs, Nov 1 2012 5:03 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

ReadString and ReadBytes don't have a limit. IIUC, Peter is asking for a
ReadLine that doesn't have a limit.

Andrew

On 1 November 2012 19:51, Rory McGuire <rjmcgu...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rory McGuire  
View profile  
 More options Nov 1 2012, 5:09 am
From: Rory McGuire <rjmcgu...@gmail.com>
Date: Thu, 1 Nov 2012 11:09:29 +0200
Local: Thurs, Nov 1 2012 5:09 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

Exactly, so anyone that can supply this fictional reader with data can
cause the OS to run out of memory.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 5:21 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 20:20:18 +1100
Local: Thurs, Nov 1 2012 5:20 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

Well, yes. But I don't see how that's different to ReadBytes or ReadString,
which already exist.

It's a shame that ReadLine is named ReadLine, because ReadLine would be a
great name for the suggested function.

Andrew

On 1 November 2012 20:09, Rory McGuire <rjmcgu...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rory McGuire  
View profile  
 More options Nov 1 2012, 5:54 am
From: Rory McGuire <rjmcgu...@gmail.com>
Date: Thu, 1 Nov 2012 11:54:40 +0200
Local: Thurs, Nov 1 2012 5:54 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

: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)?

Thanks,


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Gillette  
View profile  
 More options Nov 1 2012, 6:01 am
From: Kevin Gillette <extemporalgen...@gmail.com>
Date: Thu, 1 Nov 2012 03:01:00 -0700 (PDT)
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

ReadLine is the only one of the four (including ReadBytes, ReadSlice, and
ReadString) that is actually purposed for reading lines, and without
application-specific information, it's impossible to know how long a line
is expected to be, and in some applications, the line length is unknowable.
ReadLine retains the flexibility to be useful in the worst input case (when
you don't know the line length and it may be very long) or the best
algorithmic case (when you can process the line in one pass, without
needing to store it fully).

If an application has clear constraints on the input, such as a limited
length line, or the need to buffer the entire line, then ReadBytes,
ReadSlice, or ReadString might as well be used.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Gillette  
View profile  
 More options Nov 1 2012, 6:05 am
From: Kevin Gillette <extemporalgen...@gmail.com>
Date: Thu, 1 Nov 2012 03:05:24 -0700 (PDT)
Local: Thurs, Nov 1 2012 6:05 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

You would use it safely by constraining the input or using ReadLine.
ReadString and friends are convenience methods, and aren't for heavy
lifting. If you're not scanning for a line, you'd might as well use Read()
or ReadByte() or ReadRune(), depending.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rory McGuire  
View profile  
 More options Nov 1 2012, 6:17 am
From: Rory McGuire <rmcgu...@clearformat.com>
Date: Thu, 1 Nov 2012 12:17:57 +0200
Local: Thurs, Nov 1 2012 6:17 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

I'm saying that if the standard library wants to be used for servers surely
it should by default do the right thing
for something as simple as reading a line, even bradfitz on github used
bufio.ReadSlice with his smtp server.
Anyone making a server HTTP, SMTP, etc which use lines for part of the
input would likely use the standard
library functions, and in so doing make their servers vulnerable.

I think the simplest solution that gets everyone thinking about it is to
make the max line length an argument.
No confusion then. The best solution is that ReadBytes takes a slice as an
argument ReadBytes(make([]byte, 0, 1024), "\r\n")
 because that would have no hidden allocation.

Apologies for hijacking the thread.

:D Cheers,

On Thu, Nov 1, 2012 at 12:05 PM, Kevin Gillette
<extemporalgen...@gmail.com>wrote:

<div>
Rory McGuire
ClearFormat - Research and Development
UK : 44 870 224 0424
USA : 1 877 842 6286
RSA : 27 21 466 9400
Email: rmcgu...@clearformat.com
Website: www.clearformat.com
</div>
www.clearformat.com: http://fwd.clearformat.com/8XmSh

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Kleiweg  
View profile  
 More options Nov 1 2012, 7:03 am
From: Peter Kleiweg <pklei...@xs4all.nl>
Date: Thu, 1 Nov 2012 04:02:55 -0700 (PDT)
Local: Thurs, Nov 1 2012 7:02 am
Subject: Re: Why is there no proper ReadLine function?
On 1 nov, 00:23, Peter Kleiweg <pklei...@xs4all.nl> wrote:

> Sure I can write this function myself, but I think it is a basic
> enough functionality to belong in the standard library.

I wrote it:

https://github.com/pebbe/util/blob/master/readline.go

It may need some more testing.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
yy  
View profile  
 More options Nov 1 2012, 7:04 am
From: yy <yiyu....@gmail.com>
Date: Thu, 1 Nov 2012 12:04:39 +0100
Local: Thurs, Nov 1 2012 7:04 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

On 1 November 2012 10:20, Andrew Gerrand <a...@golang.org> wrote:

> It's a shame that ReadLine is named ReadLine, because ReadLine would be a

great name for the suggested function.


Actually, I'd prefer:

func (b *Reader) ReadLineBytes() (line []byte, err error)

--
- yiyus || JGL .


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Kleiweg  
View profile  
 More options Nov 1 2012, 7:05 am
From: Peter Kleiweg <pklei...@xs4all.nl>
Date: Thu, 1 Nov 2012 04:05:26 -0700 (PDT)
Local: Thurs, Nov 1 2012 7:05 am
Subject: Re: Why is there no proper ReadLine function?
On 1 nov, 10:21, Andrew Gerrand <a...@golang.org> wrote:

> It's a shame that ReadLine is named ReadLine, because ReadLine would be a
> great name for the suggested function.

ReadLine should be called ReadUnixOrDOSLine, because it doesn't work
with Mac lines.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 7:07 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 22:06:36 +1100
Local: Thurs, Nov 1 2012 7:06 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

On 1 November 2012 20:54, Rory McGuire <rjmcgu...@gmail.com> wrote:

> :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)?

One possible approach is to wrap the reader with an io.LimitedReader before
passing it to bufio.NewReader.

http://golang.org/pkg/io/#LimitedReader

And then you can re-set the N field after each read.

Or just use ReadLine.

Andrew


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 7:10 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 22:09:38 +1100
Local: Thurs, Nov 1 2012 7:09 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

On 1 November 2012 22:05, Peter Kleiweg <pklei...@xs4all.nl> wrote:

> On 1 nov, 10:21, Andrew Gerrand <a...@golang.org> wrote:

> > It's a shame that ReadLine is named ReadLine, because ReadLine would be a
> > great name for the suggested function.

> ReadLine should be called ReadUnixOrDOSLine, because it doesn't work
> with Mac lines.

MacOS 10 is Unix. Do you mean earlier versions? Does Go run on those
systems at all?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Kleiweg  
View profile  
 More options Nov 1 2012, 7:30 am
From: Peter Kleiweg <pklei...@xs4all.nl>
Date: Thu, 1 Nov 2012 04:30:53 -0700 (PDT)
Local: Thurs, Nov 1 2012 7:30 am
Subject: Re: Why is there no proper ReadLine function?
On 1 nov, 12:10, Andrew Gerrand <a...@golang.org> wrote:

> On 1 November 2012 22:05, Peter Kleiweg <pklei...@xs4all.nl> wrote:

> > On 1 nov, 10:21, Andrew Gerrand <a...@golang.org> wrote:

> > > It's a shame that ReadLine is named ReadLine, because ReadLine would be a
> > > great name for the suggested function.

> > ReadLine should be called ReadUnixOrDOSLine, because it doesn't work
> > with Mac lines.

> MacOS 10 is Unix. Do you mean earlier versions? Does Go run on those
> systems at all?

I am talking about file formats. I am working on Linux, with text
files that are generated on Linux, Windows, Mac. They all have
different line-endings. Mac line-ending is a single \r , without the
\n.

bufio.ReadLine() only handles \n and \r\n.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 7:47 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 22:46:56 +1100
Local: Thurs, Nov 1 2012 7:46 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

What Mac file formats use \r as line endings? Which programs generate them?

I just created a file in TextEdit on my Mac OS X system and it appears to
use \n for line breaks:

% hexdump -C test.txt
00000000  74 65 73 74 0a 74 65 73  74 0a 74 65 73 74 0a
|test.test.test.|
0000000f

Andrew

On 1 November 2012 22:30, Peter Kleiweg <pklei...@xs4all.nl> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 7:52 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 22:51:28 +1100
Local: Thurs, Nov 1 2012 7:51 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

I know that OS9 uses \r, but I am wondering what file formats Peter K is
interested in reading.

I think it's okay that Go's ReadLine doesn't support file formats generated
by an operating system that has been obsolete for 10 years.

On 1 November 2012 22:48, Peter Weinberger (温博格) <p...@google.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nate Finch  
View profile  
 More options Nov 1 2012, 7:54 am
From: Nate Finch <nate.fi...@gmail.com>
Date: Thu, 1 Nov 2012 04:54:50 -0700 (PDT)
Local: Thurs, Nov 1 2012 7:54 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Kappler  
View profile  
 More options Nov 1 2012, 8:32 am
From: Thomas Kappler <tkapp...@gmail.com>
Date: Thu, 1 Nov 2012 05:32:05 -0700 (PDT)
Local: Thurs, Nov 1 2012 8:32 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

On Thursday, November 1, 2012 12:54:50 PM UTC+1, Nate Finch wrote:

> 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.

For further processing you also need to strip the \r in case of \r\n. It's
not rocket-science, but a bit more boilerplate code than I'd want to paste
into every program dealing with lines.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Kane's Brain  
View profile  
 More options Nov 1 2012, 8:49 am
From: "Matt Kane's Brain" <mkb-pr...@hydrogenproject.com>
Date: Thu, 1 Nov 2012 08:48:37 -0400
Local: Thurs, Nov 1 2012 8:48 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

On Thu, Nov 1, 2012 at 7:46 AM, Andrew Gerrand <a...@golang.org> wrote:
> What Mac file formats use \r as line endings? Which programs generate them?

If you export a CSV or other text file from Microsoft Excel, it has \r
instead of \n for line endings. Unfortunate!

--
matt kane's brain
http://hydrogenproject.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patrick Mylund Nielsen  
View profile  
 More options Nov 1 2012, 8:57 am
From: Patrick Mylund Nielsen <patr...@patrickmylund.com>
Date: Thu, 1 Nov 2012 13:57:48 +0100
Local: Thurs, Nov 1 2012 8:57 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

I don't use a Mac, so I can't say exactly, but I still deal with this
problem regularly. IIRC the default text editor(s) (TextEdit?) let you save
"Mac" or "UNIX" text, or with "Mac" or "UNIX" linebreaks, and the linebreak
character becomes \r. The frequency at which I face this problem would
suggest that it's the default in at least some of those editors, and
Office--it's 50/50 when I'm dealing with user data from Macs. I'll try to
find out exactly which programs the next time I see it.

So far my fix has been something quick and ugly like perl -p -i -e
"s/\r/\n/g" foo.csv


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Gerrand  
View profile  
 More options Nov 1 2012, 8:58 am
From: Andrew Gerrand <a...@golang.org>
Date: Thu, 1 Nov 2012 23:57:29 +1100
Local: Thurs, Nov 1 2012 8:57 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

You could always write a converting reader that swaps lone \r bytes with
\n, and then continue to use ReadLine:

http://play.golang.org/p/-aYmMfYKnZ

On 1 November 2012 22:30, Peter Kleiweg <pklei...@xs4all.nl> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patrick Mylund Nielsen  
View profile  
 More options Nov 1 2012, 9:01 am
From: Patrick Mylund Nielsen <patr...@patrickmylund.com>
Date: Thu, 1 Nov 2012 14:00:58 +0100
Local: Thurs, Nov 1 2012 9:00 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

or throw your arms in the air and refuse to process anything but lines with
\n in your programs. It's 2013 soon -- it's ridiculous and sad that we're
still dealing with this problem.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Larry Clapp  
View profile  
 More options Nov 1 2012, 9:17 am
From: Larry Clapp <la...@theclapp.org>
Date: Thu, 1 Nov 2012 06:17:48 -0700 (PDT)
Local: Thurs, Nov 1 2012 9:17 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

Just as an aside, this is an awesome feature of Go that you can chain
Readers this way.  The more I use Go, the more I see how it embraces (what
I think of as) the Unix philosophy: write small things and chain them
together (e.g. pipes and such in the shell).  You can do this via Readers &
Writers, or channels, or probably lots of other things.

I guess this is really the CSP philosophy, not so much Unix, as such.

Regardless, it's cool.  Kudos to the stdlib authors for enabling it.

-- Larry


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Tarpine  
View profile  
 More options Nov 1 2012, 9:47 am
From: Ryan Tarpine <tarpi...@gmail.com>
Date: Thu, 1 Nov 2012 06:47:51 -0700 (PDT)
Local: Thurs, Nov 1 2012 9:47 am
Subject: Re: [go-nuts] Re: Why is there no proper ReadLine function?

This is an example of the decorator pattern (
http://en.wikipedia.org/wiki/Decorator_pattern) and this exact feature has
been present in Java since 1.0 back in '96 :-)

-Ryan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 62   Newer >
« Back to Discussions « Newer topic     Older topic »