Port dos2unix to brainfuck
Asked Answered
I

3

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?

Ieyasu answered 7/10, 2009 at 15:12 Comment(2)
+1 from me for anyone who posts working code ;^)Crasis
i accepted the first working answer but anyone who posts a (different, obviously) working program still gets upvotes.Ieyasu
K
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.

Katydid answered 7/10, 2009 at 21:28 Comment(3)
ha! i haven't even gotten a compiler working yet. somehow i missed beef in the debian repositories. thanks!Ieyasu
Impressive! kudos +1Crasis
+1 nice. but it is still not easier to read than 'dos2unix' .. which was my point @suPredesignate
M
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.

Marquesan answered 8/10, 2009 at 23:42 Comment(3)
runs on beef, works with my testcase. it's pretty basic, no stray CRs or anything. nice job.Ieyasu
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.Euplastic
Is a language really useless if it can be put to use? =)Marquesan
K
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.

Katydid answered 7/10, 2009 at 21:28 Comment(3)
ha! i haven't even gotten a compiler working yet. somehow i missed beef in the debian repositories. thanks!Ieyasu
Impressive! kudos +1Crasis
+1 nice. but it is still not easier to read than 'dos2unix' .. which was my point @suPredesignate
L
4

Slightly shorter and simpler CR-stripper:

,[-------------[+++++++++++++.[-]],]
Listen answered 1/8, 2015 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.