I got a search function done in the decompression... It's sometimes getting it right..
Got to figure out how to do the search , first to last and backwards , last to first ....
Here's the "Test-Bed"
Code: Select all
Declare Function compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string
screen 19
'=====================================================================
'=====================================================================
'start program
'=====================================================================
'=====================================================================
dim as double time1 , time2 , time3 , time4
do
randomize
dim as string s = ""
For n As Long = 1 To 8
s+=chr(Int(Rnd*256))
Next
time1=timer
'begin compress
dim as string comp = s
'do
' dim as longint chk = len(comp) - 1
' comp = compress_loop(comp)
' if len(comp) >= chk then exit do
' if inkey = chr( 27 ) then end
'loop
for a as longint = 1 to 1 step 1
comp = compress_loop(comp)
next
'end compress
time2 = timer
time3=timer
'begin decompress
dim as string final_out = comp
for a as longint = 1 to 1 step 1
final_out = decompress_loop(final_out)
next
'end decompress
time4 = timer
'sleep
print string(99,"=")
print "inp = " ; (s)
print string(99,"=")
print "out = " ; (final_out)
print
print "compress time = "; time2-time1
print "decompress time = "; time4-time3
print
if s = final_out then print "Decompressed OK" else print "Decompression failed."
print string(99,"=")
sleep
loop until inkey = chr(27)
sleep
end
'===============================================================================
'===============================================================================
'compress
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
print "c inp = " ; len(chrs) ' , chrs
dim as string bits = ""
dim as string n1
dim as longint v1 , v2 , v3 , v4
for a as longint = 1 to len( chrs ) step 1
n1 = "00000000" + bin( chrs[ a - 1 ] )
n1 = right( n1 , 8 )
bits+= n1
next
print "c bin = " ; len( bits ) , bits
dim as string bits1 = ""
dim as string bits2 = ""
for a as longint = 1 to len( bits ) step 4
n1 = mid( bits , a , 4 )
if n1 = "0000" then bits1+= "0" : bits2+= "0"
if n1 = "0001" then bits1+= "1" : bits2+= "0"
if n1 = "0010" then bits1+= "2" : bits2+= "0"
if n1 = "0011" then bits1+= "3" : bits2+= "0"
if n1 = "0100" then bits1+= "0" : bits2+= "10"
if n1 = "0101" then bits1+= "1" : bits2+= "10"
if n1 = "0110" then bits1+= "2" : bits2+= "10"
if n1 = "0111" then bits1+= "3" : bits2+= "10"
if n1 = "1000" then bits1+= "0" : bits2+= "11"
if n1 = "1001" then bits1+= "1" : bits2+= "11"
if n1 = "1010" then bits1+= "2" : bits2+= "11"
if n1 = "1011" then bits1+= "3" : bits2+= "11"
if n1 = "1100" then bits1+= "00" : bits2+= "0"
if n1 = "1101" then bits1+= "11" : bits2+= "0"
if n1 = "1110" then bits1+= "22" : bits2+= "0"
if n1 = "1111" then bits1+= "33" : bits2+= "0"
next
print
print "c out = " ; len( bits1 ) , bits1
print "c out = " ; len( bits2 ) , bits2
dim as ubyte count1 = 0
dim as string str1 = ""
dim as ubyte dec1
do
str1 = str( len( bits1 ) / 4 )
dec1 = instr( 1 , str1 , "." )
if dec1 <> 0 then bits1+= "0" : count1+= 1
loop until dec1 = 0
dim as ubyte count2 = 0
dim as string str2 = ""
dim as ubyte dec2
do
str2 = str( len( bits2 ) / 8 )
dec2 = instr( 1 , str2 , "." )
if dec2 <> 0 then bits2+= "0" : count2+= 1
loop until dec2 = 0
dim as string final = ""
dim as string s , n
for b as longint = 1 to len( bits1 ) step 4
s = mid( bits1 , b , 4 )
n = ""
n+= right( "00" + bin( val( mid( s , 1 , 1 ) ) ) , 2 )
n+= right( "00" + bin( val( mid( s , 2 , 1 ) ) ) , 2 )
n+= right( "00" + bin( val( mid( s , 3 , 1 ) ) ) , 2 )
n+= right( "00" + bin( val( mid( s , 4 , 1 ) ) ) , 2 )
final+= chr( val( "&B" + n ) )
'final+= chr( val( "&B" + mid( bits1 , b , 8 ) ) )
next
final+= "END"
for b as longint = 1 to len( bits2 ) step 8
final+= chr( val( "&B" + mid( bits2 , b , 8 ) ) )
next
final = chr( count1 ) + chr( count2 ) + final
print "c fin = " ; len(final)
return final
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
print
print "d inp = " ; len( chrs )
dim as ubyte count1 = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
dim as ubyte count2 = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
dim as longint place = instr( 1 , chrs , "END" ) - 1
dim as string bits1 = left( chrs , place )
dim as string bits2 = mid( chrs , place + 4 )
dim as string outs1 = ""
dim as string n1
dim as string v1 , v2 , v3 , v4
for a as longint = 1 to len( bits1 ) step 1
n1 = "00000000" + bin( bits1[ a - 1 ] )
n1 = right( n1 , 8 )
v1 = str( val( "&B" + mid( n1 , 1 , 2 ) ) )
v2 = str( val( "&B" + mid( n1 , 3 , 2 ) ) )
v3 = str( val( "&B" + mid( n1 , 5 , 2 ) ) )
v4 = str( val( "&B" + mid( n1 , 7 , 2 ) ) )
outs1+= v1 + v2 + v3 + v4
next
outs1 = left( outs1 , len( outs1 ) - count1 )
dim as string outs2 = ""
for a as longint = 1 to len( bits2 ) step 1
n1 = "00000000" + bin( bits2[ a - 1 ] )
n1 = right( n1 , 8 )
outs2+= n1
next
outs2 = left( outs2 , len( outs2 ) - count2 )
print "d out = " ; len( outs1 ) , outs1
print "d out = " ; len( outs2 ) , outs2
dim as string show1 = ""
dim as string show2 = ""
dim as string n2
dim as longint pl1 = 1 , pl2 = 1
do
n1 = mid( outs2 , pl1 , 1 )
n2 = mid( outs1 , pl2 , 1 )
if n1 = "0" then
show2+= "_" + n1 + "_" : pl1+= 1
if mid( outs1 , pl2 + 1 , 1 ) = n2 then
show1+= n2 + n2 + "_" : pl2+= 2
else
show1+= "_" + n2 + "_" : pl2+=1
end if
end if
if n1 = "1" then
n1+= mid( outs2 , pl1 + 1 , 1 ) : pl1+= 2 : show2+= n1 + "_"
show1+= "_" + n2 + "_" : pl2+=1
end if
loop until pl1 > len( outs2 )
print
print "d sho = " ; len( show1 ) , show1
print "d sho = " ; len( show2 ) , show2
return chrs
end function