19

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?

2
  • 3
    +1 from me for anyone who posts working code ;^)
    – Toad
    Oct 7, 2009 at 15:16
  • i accepted the first working answer but anyone who posts a (different, obviously) working program still gets upvotes. Oct 7, 2009 at 22:02

3 Answers 3

26

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.

3
  • runs on beef, works with my testcase. it's pretty basic, no stray CRs or anything. nice job. Oct 9, 2009 at 5:40
  • Here it is! A nice useful application written in a useless language. Congrats! +1. BTW, I just create a fork compiler called Brain Force. I'll present it somewhere soon. :D I will use this code to test it.
    – DrBeco
    Apr 6, 2011 at 10:16
  • Is a language really useless if it can be put to use? =) Apr 19, 2011 at 14:20
25

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.

3
  • ha! i haven't even gotten a compiler working yet. somehow i missed beef in the debian repositories. thanks! Oct 7, 2009 at 21:56
  • Impressive! kudos +1
    – Toad
    Oct 8, 2009 at 9:08
  • +1 nice. but it is still not easier to read than 'dos2unix' .. which was my point @su
    – akira
    Oct 8, 2009 at 14:53
4

Slightly shorter and simpler CR-stripper:

,[-------------[+++++++++++++.[-]],]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.