Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port dos2unix to brainfuck

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?

like image 898
quack quixote Avatar asked Oct 07 '09 15:10

quack quixote


3 Answers

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.

like image 94
Alan Krueger Avatar answered Nov 16 '22 12:11

Alan Krueger


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.

like image 42
JaakkoK Avatar answered Nov 16 '22 12:11

JaakkoK


Slightly shorter and simpler CR-stripper:

,[-------------[+++++++++++++.[-]],]
like image 4
Daniel Cristofani Avatar answered Nov 16 '22 14:11

Daniel Cristofani