Is possible Ruby to use CRLF instead of LF?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIEFGQUlLIFJ1YnkgdXNlcyBMRiA9IFxuIHRvIGRldGVjdCBuZXdsaW5lLiBCdXQgbm93IEkn
bSBjb2RpbmcgYQpwYXJzZXIgZm9yIGEgcHJvdG9jb2wgdGhhdCB1c2VzIENSTEYgPSBcclxuIGZv
ciBuZXdsaW5lLiBJbiBmYWN0LCBcbgppcyBub3QgY29uc2lkZXJlZCBhIG5ld2xpbmUuCgpJcyBp
dCBwb3NzaWJsZSB0byBnZXQgUnVieSB3b3JraW5nIHdpdGggQ1JMRiBpbnN0ZWFkIG9mIExGPwoK
Rm9yIGV4YW1wbGUsIEkgd2FudCB0byBkZWNsYXJlIHRoaXMgc3RyaW5nIChmb3IgdGVzdGluZyk6
CgpleGFtcGxlID08LS0gRU5EX1NUUklORwpWZXJzaW9uIDQKUmVxdWVzdF9UeXBlOiBjYWxsCkZy
b206IHNzc3NzCkVORF9TVFJJTkcKCmFuZCBJIHdhbnQgdGhhdCBzdHJpbmcgdG8gbWF0Y2ggXHJc
biBhdCB0aGUgZW5kIG9mIGVhY2ggbGluZSBpbnN0ZWFkCm9mIFxuLCBpcyBpdCBwb3NzaWJsZT8K
ClBEOiBBbHNvIGl0IHdvdWxkIGJlIGdyZWF0IGlmIEkgY291bGQgZW50ZXIgXHJcbiBpbiBhIExp
bnV4IHRlbG5ldAppc250ZWFkIG9mIGp1c3QgXG4gZm9yIGFtbnVhbCB0ZXN0aW5nLCBidXQgYXNz
dW1lIGl0J3Mgbm90IGFwcHJvcGlhdGUKcXVlc3Rpb24gaW4gdGhpcyBtYWlsbGlzdCA7KQoKClRo
YW5rcyBpbiBhZHZhbmNlLgoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0
Pgo=
 
M

Mateusz Tybura

To change \n to \r\n you can use:

a.insert(index("\n"),"\r")

It will put \r before \n but only once. If there is more than one \n in tex=
t
you may use it in loop.
 
I

Iñaki Baz Castillo

MjAwOC8zLzI1LCBNYXRldXN6IFR5YnVyYSA8d3VqY2lvbEBnbWFpbC5jb20+Ogo+IFRvIGNoYW5n
ZSBcbiB0byBcclxuIHlvdSBjYW4gdXNlOgo+Cj4gIGEuaW5zZXJ0KGluZGV4KCJcbiIpLCJcciIp
Cj4KPiAgSXQgd2lsbCBwdXQgXHIgYmVmb3JlIFxuIGJ1dCBvbmx5IG9uY2UuIElmIHRoZXJlIGlz
IG1vcmUgdGhhbiBvbmUgXG4gaW4gdGV4dAo+ICB5b3UgbWF5IHVzZSBpdCBpbiBsb29wLgoKT2ss
IEknbGwgdHJ5IGl0LiBUaGFua3MuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4
Lm5ldD4K
 
G

Gerardo Santana Gómez Garrido

Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
parser for a protocol that uses CRLF =3D \r\n for newline. In fact, \n
is not considered a newline.

Is it possible to get Ruby working with CRLF instead of LF?

For example, I want to declare this string (for testing):

example =3D<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

Well, you could type that text in Notepad and then copy/pasting the
text to your code. Or even better, use unix2dos.

PD: Also it would be great if I could enter \r\n in a Linux telnet
isntead of just \n for amnual testing, but assume it's not appropiate
question in this maillist ;)

You know, you can always redirect the input. Instead of typing the
lines, store them in a text file, convert them with unix2dos and then
feed the file to your program:

yourprog < textfile


--=20
Gerardo Santana
 
I

Iñaki Baz Castillo

El Martes, 25 de Marzo de 2008, Gerardo Santana G=F3mez Garrido escribi=F3:
Well, you could type that text in Notepad and then copy/pasting the
text to your code. Or even better, use unix2dos.

Hi, finally I did it (note also that I don't use Windows ;) ):

example =3D<-- END_STRING
Version 4
Request_Type: call
=46rom: sssss
END_STRING

example.gsub!(/\n/,"\r\n")


XD



Thanks a lot.

=2D-=20
I=F1aki Baz Castillo
 
X

Xavier Noria

Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
parser for a protocol that uses CRLF =3D \r\n for newline. In fact, \n
is not considered a newline.

Is it possible to get Ruby working with CRLF instead of LF?

For example, I want to declare this string (for testing):

example =3D<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

The whole question is a bit generic.

In the source code a hard-newline like the one in a here-document has =20=

only LFs as long as the file has the newline conventions of the =20
runtime platform. That's because the Ruby interpreter itself reads the =20=

program as a text file in text mode.

If you want to force CRLF in a here-document you can use a trick like =20=

this (off the top of my head):

example =3D <<EOS.lf_to_crlf
...
EOS

class String
def lf_to_crlf
gsub(/\012/, "\015\012")
end
end

Then the I/O channel used to send the data needs to be in binary mode, =20=

etc.

-- fxn
 
I

Iñaki Baz Castillo

El Martes, 25 de Marzo de 2008, Xavier Noria escribi=F3:
In the source code a hard-newline like the one in a here-document has
only LFs as long as the file has the newline conventions of the
runtime platform. That's because the Ruby interpreter itself reads the
program as a text file in text mode.

If you want to force CRLF in a here-document you can use a trick like
this (off the top of my head):

example =3D <<EOS.lf_to_crlf
...
EOS

class String
def lf_to_crlf
gsub(/\012/, "\015\012")
end
end

Then the I/O channel used to send the data needs to be in binary mode,
etc.

Thanks, finally that it what I did (similar):


example =3D<-- END_STRING
Version 4
Request_Type: call
=46rom: sssss
END_STRING

example.gsub!(/\n/,"\r\n")



Thanks a lot.



=2D-=20
I=F1aki Baz Castillo
 
X

Xavier Noria

The dot matches a single char, no care 4 what the character is. The
exception is newline characters. In regex things, the dot will 'not
match' a newline character by default. So by default, the dot is short
for the negated character class [^\n] (UNIX regex flavors) or [^\r\n]
(Windows regex flavors)...

Careful, the dot does match \r. The dot is exactly [^\n] (modulus /m).

What happens is that in ordinary line-oriented, portable code, you
don't see a single \r in Ruby strings. The CRs on Windows are on the
disk but don't go up because the I/O layer filters them.

-- fxn
 
J

John Smith

Iñaki Baz Castillo said:
Hi, AFAIK Ruby uses LF = \n to detect newline. But now I'm coding a
parser for a protocol that uses CRLF = \r\n for newline. In fact, \n
is not considered a newline.

Is it possible to get Ruby working with CRLF instead of LF?

For example, I want to declare this string (for testing):

example =<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

PD: Also it would be great if I could enter \r\n in a Linux telnet
isntead of just \n for amnual testing, but assume it's not appropiate
question in this maillist ;)


Thanks in advance.
As a newbee regarding the development in ruby I am somewhat surprised
that the CRLF issue is not covered by something like a ruby "pragma".

With perl I am used to write something like:

use open IN => ":crlf", OUT => ":bytes";
use open IN => ":bytes", OUT => ":crlf";
use open OUT => ':utf8';
... and so on ...

this "pragma" specifies how IO is *generally* to be handled.

don't we have a comparable syntax ?
 
I

Iñaki Baz Castillo

El Martes, 25 de Marzo de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:
Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
parser for a protocol that uses CRLF =3D \r\n for newline. In fact, \n
is not considered a newline.

Is it possible to get Ruby working with CRLF instead of LF?

For example, I want to declare this string (for testing):

example =3D<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?


Ok, using io.gets or io.readline I can set as parameter the lines separator=
=20
string:

ios.gets(sep_string=3D$/)

And in Ruby by default (at least in Linux) variable $/ is \n. But I can set=
=20
this variable:
$/ =3D "\r\n"
so I assume it would work.


=2D-=20
I=C3=B1aki Baz Castillo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top