Re: #87 [Ver]: readLine() method has problems with "\n" line endings

From: Date: Sat, 18 Oct 2003 18:14:46 +0000
Subject: Re: #87 [Ver]: readLine() method has problems with "\n" line endings
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-22795@lists.php.net to get a copy of this message
This would also eat trailing \r's when they should not be eaten (when the line is only terminated by \n). Perhaps the right solution is a property which the user could set to either \n or \r\n (or perhaps even \r ;-). Btw I don't see why the line length have to be tested; substr($line, -2) returns _up to_ two characters, in which case the line have to be at least two chars long to be able to match "\r\n"... Heino ----- Original Message ----- From: "Greg Beaver" <greg@chiaraquartet.net> To: "Heino H. Gehlsen" <heino@gehlsen.dk> Cc: <pear-dev@lists.php.net>; <ssb@fast.no>; <chuck@horde.org>; <neufeind@php.net> Sent: Saturday, October 18, 2003 4:11 PM Subject: Re: [PEAR-DEV] [PEAR-BUG] #87 [Ver]: readLine() method has problems with "\n" line endings > try this code (untested) > > if (strlen($line) >= 1 && substr($line, -1) == "\n") { > return rtrim($line, "\r\n"); > } > > Greg > > Heino H. Gehlsen wrote: > > rtrim() eats more than \r and \n - a \t at the end of the line (just before > > \r or \n) would be trim'ed as well... > > > > > > ----- Original Message ----- > > From: <neufeind@php.net> > > To: <pear-dev@lists.php.net>; <ssb@fast.no>; <chuck@horde.org> > > Sent: Saturday, October 18, 2003 12:57 PM > > Subject: [PEAR-DEV] [PEAR-BUG] #87 [Ver]: readLine() method has problems > > with "\n" line endings > > > > > > > >>ID: 87 > >> Updated by: neufeind@php.net > >> Reported By: avb@php.net > >> Status: Verified > >> Bug Type: Net_Socket > >> Operating System: Any > >> PHP Version: Irrelevant > >> New Comment: > >> > >>How about this code (lines 396-400) in Net_Socket > >> > >> if (strlen($line) >= 1 && > >> (substr($line, -1) == "\n")) { > >> return rtrim($line); > >> } > >> > >>Should be enough, right? > >> > >> > >>Previous Comments: > >>------------------------------------------------------------------------ > >> > >>[2003-10-11 09:06:27] avb@php.net > >> > >>Description: > >>------------ > >>This code (lines 396-400) in Net_Socket > >> > >> if (strlen($line) >= 2 && > >> (substr($line, -2) == "\r\n" || > >> substr($line, -1) == "\n")) { > >> return rtrim($line); > >> } > >> > >>prevent the method from correctly returning an empty string if a line > >>containing just "\n" is read from socket. > >> > >> > >>Reproduce code: > >>--------------- > >>sock-bug.php: > >><?php > >>require_once 'Net/Socket.php'; > >> > >>$sock =& new Net_Socket(); > >>// To be able to test with local file instead of real socket... > >>$sock->fp = fopen('sock-bug.txt', 'rb'); > >> > >>$i = 1; > >>while (!$sock->eof()) { > >> $line = $sock->readLine(); > >> echo $i++ . ':' . $line . "\n"; > >>} // while > >>?> > >>sock-bug.txt (should have \n line endings!): > >>foo > >> > >>bar > >> > >> > >>baz > >> > >>Expected result: > >>---------------- > >>1:foo > >>2: > >>3:bar > >>4: > >>5: > >>6:baz > >> > >> > >>Actual result: > >>-------------- > >>1:foo > >>2: > >>bar > >>3: > >>4:baz > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >> > >>-- > >>Edit this bug report at > >>http://pear.php.net/bugs/bug.php?id=87&edit=1 > >> > >>-- > >>PEAR Development Mailing List (http://pear.php.net/) > >>To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > >> > > >

« previous php.pear.dev (#22795) next »