Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[CLOSED] Collection of beautiful one-liner codes


  • Please log in to reply
46 replies to this topic

Poll: Who's the winner? (16 member(s) have cast votes)

Who's the winner?

  1. SKAN (6 votes [37.50%])

    Percentage of vote: 37.50%

  2. Slanter (1 votes [6.25%])

    Percentage of vote: 6.25%

  3. Lexikos (2 votes [12.50%])

    Percentage of vote: 12.50%

  4. DerRaphael (7 votes [43.75%])

    Percentage of vote: 43.75%

  5. [VxE] (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote
sdsd
  • Guests
  • Last active:
  • Joined: --
Happy 1st day of june! :D

BoBo²
  • Guests
  • Last active:
  • Joined: --
One poll-option is missing, which I guess needs to be autoselected: "The AHK-community" :D

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
SKAN: The Null compression algorithm for hex data is useful and works without restrictions. There are no dirty tricks, but the task is simple.

Slanter: Cleverly adapted existing code. The result is useful for indexing with short text strings to make a dictionary (associative array). Because of the recursion there is a limit on the text size (less than 860 chars), which might be acceptable for this kind of applications. It cannot handle binary data, though.

Lexikos: Strictly speaking, it is not one line, but a very useful script demonstrating how to fool AHK to execute dynamic expressions.

DerRaphael: a few clever tricks, but not very useful result. The b64 script cannot handle binary data (and there is no point to Base64 encode ASCII text). Also, there is a limit for the depth of recursion. In my PC the script crashes at input texts over 860 characters. Its main strength is demonstrating various tricks to pack lines into a single line, and obfuscating simple algorithms to the point where no one understands and can modify them any more (a kind of copyright protection).

[VxE]: Working script, but better used as a timer subroutine. No advantage to write it in one line.


Which one would I use as it is? SKAN’s or Slanter’s script (although the later can be shortened). I might incorporate SKAN’s script into the hex conversion routine, which makes Slanter’s script the most useful one standing alone.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Would you, voters, tell why you voted for a particular script? How would you use the script or benefit from it?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

SKAN: The Null compression algorithm for hex data is useful and works without restrictions. There are no dirty tricks, but the task is simple.


Thank you, Sir!. I learnt this from you from one of your A-f-H post where you had demonstrated removing of redundant spaces from a string ( I am unable to locate that post :( )

I personally use the following version which offers tighter compression:

VarSetCapacity(Z,512,48), Nums:="512|256|128|64|32|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2" 
Loop, Parse, Nums, |                                  ;  Compressing nulls in hex data 
  StringReplace,hex,hex,% SubStr(Z,1,A_LoopField),% Chr(70+A_Index),All

empty.dll is 2562 bytes and weighs 5124 bytes when converted to hex. The given algorithm compresses the hex to a mere 380 bytes - far lesser than the original size of the binary file - which a85 or b64 does not offer.

This algorithm well suits only when the binary input contains too many nulls. I am able to include some simple icons as hex, weighing lesser than the original size.

CreateDLL()

CreateDLL( file="empty.dll" ) {

hex := "4D5A9X3U4VFFFFXB8N4KLMFF8BX8TE1FBA0EZB409CD21B8014CCD214175746F486F746B6579205363"
. "72697074696E67204C616E67756167652C205265736F757263652D6F6E6C7920444C4C2E245045X4C0103L"
. "REXE210B0103W2U2N2U2U1Q1Y1T2W4U1U4L4T4O2Y3V1W1S1LY1JLS3VCIKV2E64617461U4T1T2U4LP4WC02E"
. "74657874V11T2T2U6LP2W602E72656C6F63WCT3T2U8LP4W4JL02E737263T2D01V4T2UALP4Z8Y6GGGHI8B44"
. "2408A3Z1Y10B801VC20CGHIJKZ2VCU53GHIJKND0A"

VarSetCapacity(Z,512,48),   Nums:="512|256|128|64|32|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2" 
Loop, Parse, Nums, |                                  ;  Uncompressing nulls in hex data 
  StringReplace,hex,hex,% Chr(70+A_Index),% SubStr(Z,1,A_LoopField),All

VarSetCapacity( Bin,(cLen:=StrLen(hex)//2)), h:=DllCall("_lcreat",str,file,int,0)
Loop %cLen%
  NumPut("0x" SubStr(hex,2*a_index-1,2),Bin,a_index-1,"char")
b:=DllCall("_lwrite",uint,h,str,Bin,uint,cLen),DllCall("_lclose",uint,h), Bin:="", hex:=""

Return b ? file : ""
}

Crazy Scripting : Create ResourceOnly DLL (ICON+BITMAP+RCD) posted by SKAN
:)

PS: I did not vote and thanks for those who voted for me!

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
SKAN: If you convert bitmap images to hex, sometimes you can get better compression with the more general algorithm: the letters G..Z followed by any char 0-9A-Z code the number of times the preceding byte (two hex digits) repeat. Large monochrome areas in a row get very well compressed. However, it is probably better if you use Windows’ built in compression algorithms. Images can be first compressed to jpg or png, other files zipped and only after encoded to hex. There will be not many repetitions. At load time you can convert the hex back to binary and use some API calls to convert the data back to the original.

Oberon
  • Members
  • 442 posts
  • Last active: Jul 03 2008 11:34 PM
  • Joined: 18 Feb 2008

This algorithm well suits only when the binary input contains too many nulls.

Run length encoding can be applied to any sequence, your implementation just doesn't recognize it. With a few modifications your function could work similar to the range encoder used in LZMA. However this is trivial for your intended purpose as DLLs can be invoked from local file system, packed with a superior alternative to base64 such as deflate and yenc.

derRaphael
  • Members
  • 872 posts
  • Last active: Mar 19 2013 04:42 PM
  • Joined: 23 Nov 2007

((($:=(StrLen(a:=%[%(a,"[^a-zA-Z0-9+/]"))*6//8))&&($//3+1!=c)&&(z:=(!c)? %v%(b,$,32):1))? b64(mode,a,(b:=(NumPut((G:=((64*((64*((64*(%]%(@,SubStr(µ:=SubStr(a,++c*4-3,4),1,1))-1))+(%]%(@,SubStr(µ,2,1))-1)))+(StrLen(_:=SubStr(µ,3,1))? %]%(@,_)-1:0)))+(StrLen(_:=SubS

Your codes is fascinating it reminds me of perl. How long did you spend writing this?


it took 1 day to get it into this shape ..

greets
derRaphael

All scripts, unless otherwise noted, are hereby released under CC-BY

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

it took 1 day to get it into this shape

One could start with the Base64 script posted to the Forum. Replace the loop with a recursive call, and inline the calls to Code(). This should not take more than an hour. For the encoder part the result could be:
MsgBox % B64Encode("Blahblah")

B64Encode(S,m=1,B=0,out="",C="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
   If (S != "") {
      B := m=1 ? *&S<<16 : B+(*&S<<4*m)
      If m = 0
         out .= SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) SubStr(C,((B>>6)&63)+1,1) SubStr(C,(B&63)+1,1)
      Return B64Encode(SubStr(S,2),Mod(m+1,3),B,out)
   }
   Return out (m=1 ? "" : m=2 ? SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) "=="
   : SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) SubStr(C,((B>>6)&63)+1,1) "=")
}
Converting this form to a single line is then straightforward. Of course, this is not optimal, but still readable.

However, the question remains: what can we use the result for? It is not good for generating dynamic variable names from arbitrary strings, because names don’t distinguish between lower and upper case letters. It is not good for encoding binary data (although this can be fixed), and the recursion depth limit prevents the function to encode more than 860 bytes. A practical use case would be helpful.

The main strength of the script is the list of clever tricks to cram code to one line. Someone may employ them to write a useful function. Code obfuscation (use cryptic variable names like ‘[’) can be advanced further. You can find examples here.

This is a variant, which handles short binary data:
MsgBox % B64Encode(&In,StrLen(In))

B64Encode(addr,len,m=1,B=0,out="",C="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
   If (len > 0) {
      B := m=1 ? *addr<<16 : B+(*addr<<4*m)
      If m = 0
         out .= SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) SubStr(C,((B>>6)&63)+1,1) SubStr(C,(B&63)+1,1)
      Return B64Encode(addr+1,len-1,Mod(m+1,3),B,out)
   }
   Return out (m=1 ? "":m=2 ? SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) "=="
   :SubStr(C,(B>>18)+1,1) SubStr(C,((B>>12)&63)+1,1) SubStr(C,((B>>6)&63)+1,1) "=")
}


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

it is probably better if you use Windows’ built in compression algorithms.


Could you please throw more light on this, Sir ?

I tried with zlib1.dll ( Webpage: http://www.zlib.net/ )

A working example follows:

hmod := DllCall( "LoadLibrary", Str,"ZLIB1.DLL" )

String := "The Quick Brown-------------------------------------------------------Lazy Dog"
StrLen := StrLen( String )
VarSetCapacity( cString, Strlen )
VarSetCapacity( nStrLen,8,32 )
DllCall( "ZLIB1.dll\compress", Str,cString, Str,nStrLen, Str,String, UInt,StrLen, "Cdecl")

MsgBox, % "Original StrLen`t: " StrLen "`nCompressed`t: " NumGet( nStrLen )

VarSetCapacity( zString, Strlen )                               
VarSetCapacity( zStrLen,8,32 )
DllCall( "ZLIB1.dll\uncompress", Str,zString,Str,zStrLen,Str,cString,UInt,StrLen,"Cdecl" )

MsgBox, % NumGet( zStrLen ) "`n" zString

A catch is that we need to know the destination data length before we can uncompress it.

The idea is: To use zlib1.dll itself as the resource file. We may compress BITMAPs and store it RT_RCDATA. Not as good as PNG but the compression rate is commendable... and we can avoid the required GDI+ calls to load the PNG/JPEG data.
The source code is available for C, C++.
I find the DLL has redundant 16-BIT file IO functions and weighs 59KB.
If we could strip-off the redundant functions ( support for gzip etc ), I think we will have a much lighter DLL in which we could store our resources in compressed form.

(or)

Is it possible to have machine code for these functions -- just compress/uncompress ? I could implement it in my AxC format.

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
There are AHK scripts for image conversion using GDI+, e.g. here. The .net library contains the zip functionality, but I never used it from AHK. You can always control Explorer (context menu/Send to/Compressed Folder), but there must be a direct way (via COM, DOM…), too.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

There are AHK scripts for image conversion using GDI+, e.g. here.


Actually, Sean gave us the most simplest way here

I am trying to avoid GDI+. From what I learnt from google, BITMAP would be the fastest way to load an Image. PNG uses ZLIB compression, but I had seen people recommending to compress BITMAP with ZLIB and claim it is faster than PNG decoding ( this method would not help for transparent Images though ).

I have also been trying my luck with freeware JPEG/PNG decoders. But in all, our own fast machine code for compression/decompression would be the most ideal to store as resources or in AxC.

On a related note:
Sir, if you have a chance, please try my script posted here
Watching autohotkey.exe in task manager reveals that the memory usage bloats by 1 MB with every view of an image until the maximum physical memory is utilised, after which windows decides to reset/free the resources.

I hope someday you can give us some fast compress/uncompress algorithm.


:)

heresy
  • Members
  • 291 posts
  • Last active: Sep 26 2008 10:47 PM
  • Joined: 11 Mar 2008
Thanks to all the participants for joining this event and let us see beautiful codes

as chosen, SKAN and DerRaphael are the winners of this event.
Congratuations !
i'll pm you guys
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
:lol: Feel the love :lol:

'grats to teh winners.

I thought Laszlo's summaries were funny:-> "useful", "clever", "useful", "clever", and "no advantage" :p ... didn't want to say anything about it 'till the voting was over.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks Heresy and Thanks to all those who particiapted in this topic.
I did not get a hint that this was a time limited poll and was too late before I could vote.
Please consider my vote as +1 for DerRaphael and announce him as the sole winner.
Thanks again. It was very nice to participate in the event.

:D
kWo4Lk1.png