views:

374

answers:

2

I got into an argument over on SuperUser.com about useless answers and found myself challenging the other poster to answer the question in brainfuck. He didn't take me up on it, but now I'm curious.

All the program needs to do is convert CRLF line endings to LF (dos-style to unix). Any bf coders around that can help out?

+17  A: 

Here you go:

,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.

Assumes that EOF is indicated by an input value of 0 (this is the default on beef, which I used to test, and is a reasonable choice; I think it might also support EOF leaving the character unchanged, but I didn't test that). Also assumes that the file ends with a LF (actually, it replaces the last character with LF). Handles CR correctly if not part of a CRLF pair (that is, it outputs lone CRs).

Took maybe an hour to write and test, and that includes learning Brainfuck from the beginning.

jk
ha! i haven't even gotten a compiler working yet. somehow i missed beef in the debian repositories. thanks!
quack quixote
works on my test case. you win!
quack quixote
Impressive! kudos +1
Toad
+1 nice. but it is still not easier to read than 'dos2unix' .. which was my point @su
akira
+13  A: 

This is quite a bit shorter, just 41 characters.

,[[->+>+<<]>-------------[>.<[-]]>[-]<<,]

It reads a value into a[0]. It copies the read value into a[1] and a[2] and subtracts 13 from a[1]. If a[1] is non-zero (meaning it's not a CR), it puts a[2] and clears a[1]. It then clears a[2] and reads into a[0] again and repeats.

This has the added advantage that--since it leaves a[0]=0 every time it reads--it should support BF virtual machines that read EOF as 0 or treat EOF as "no change", both of which are common.

Since this doesn't replace CRLF pairs with LF but rather just strips CR, this doesn't depend on assuming the file ends with an LF. My own testing with dos2unix (at least the Cygwin one) does not indicate that lone CRs are retained.

Alan Krueger
runs on beef, works with my testcase. it's pretty basic, no stray CRs or anything. nice job.
quack quixote