开发者

The missing quines: Visual Basic (for Applications)

开发者 https://www.devze.com 2023-02-28 10:01 出处:网络
Today I surfed some random geek-stuff articles on wikipedia to get my daily dose of useless knowledge. I stumbled accross quines, which are programs that print their own source-code. I found t开发者_S

Today I surfed some random geek-stuff articles on wikipedia to get my daily dose of useless knowledge. I stumbled accross quines, which are programs that print their own source-code. I found t开发者_StackOverflowhat a great way to make my brain hurt, so I began working on a quine in VBA. I had two good reasons:

  1. I couldn't find a quine written in VBA
  2. VBA encourages you to write awkward code which makes your brain hurt

Here is my masterpiece:

Sub q()
c = "Sub q();c = #;Debug.Print Replace(Replace(c, Chr(59), vbNewLine), Chr(35), Chr(34) & c & Chr(34));End Sub"
Debug.Print Replace(Replace(c, Chr(59), vbNewLine), Chr(35), Chr(34) & c & Chr(34))
End Sub

My challenge: Can you make it even shorter (and preferably more awkward)?


How about

Sub q() '//in mdl1
Debug.Print Workbooks(1).VBProject.VBComponents(5).CodeModule.Lines(1, 3)
End Sub


I don't know if anybody reads this thread anomore but here's an even shorter one, based on the quine of das_weezul. It's independent of "Option Explicit" (unlike das_weezul's) and it's independent of the Office App you are working in (i.e. Excel, Access, Word etc.) - unlike Alex K's. Use it in the immediate window (Ctrl+G):

c="c=#:?replace(c,chr(35),chr(34) &c &chr(34))":?replace(c,chr(35),chr(34) &c &chr(34))


Some further modifications of @Dorian I's response gets the bytecount for the VBA quine to come down to 77 bytes

c=Chr(34):q="c=Chr(34):q=:?Replace(q,Chr(7),c+q+c)":?Replace(q,Chr(7),c+q+c)

The key tricks that allow for this to happen are the use of character 7, , over character 35, #, and using string addition rather than string concatenation.

0

精彩评论

暂无评论...
验证码 换一张
取 消