Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,63 @@
-- parent script "BitArray"
property ancestor
property bitSize
property _pow2
----------------------------------------
-- @constructor
-- @param {integer} [bSize=0]
----------------------------------------
on new (me, bSize)
if voidP(bitSize) then bitSize=0
me.bitSize = bSize
byteSize = bitSize/8 + (bitSize mod 8>0)
me._pow2 = [128,64,32,16,8,4,2,1] -- pow2 lookup list
me.ancestor = ByteArray(byteSize)
return me
end
----------------------------------------
-- Sets bit at position <bitPos> to <bitValue>.
-- @param {integer} bitPos - starts at 1, as ByteArray's native byte access functions
-- @param {boolean} bitValue
----------------------------------------
on setBit (me, bitPos, bitValue)
bytePos = (bitPos-1)/8 + 1
bitPos = (bitPos-1) mod 8 + 1
if bitValue then
me[bytePos] = bitOr(me[bytePos], me._pow2[bitPos])
else
me[bytePos] = bitAnd(me[bytePos], bitNot(me._pow2[bitPos]))
end if
end
----------------------------------------
-- Gets bit value at position <bitPos>.
-- @param {integer} bitPos - starts at 1, as ByteArray's native byte access functions
-- @return {boolean} bitValue
----------------------------------------
on getBit (me, bitPos)
bytePos = (bitPos-1)/8 + 1
bitPos = (bitPos-1) mod 8 + 1
return bitAnd(me[bytePos], me._pow2[bitPos])<>0
end
----------------------------------------
-- Returns all bits as string. To be in accordance with ByteArray's native toHexString(),
-- returned string is separated with SPACE (e.g. "0 1 1 0...")
-- @param {integer} [bitSizeOnly=FALSE] - if TRUE, only <bitSize> bits without byte-padding
-- @return {string}
----------------------------------------
on toBinString (me, bitSizeOnly)
res = ""
repeat with i = 1 to me.length
byte = me[i]
repeat with j = 1 to 8
put (bitAnd(byte, me._pow2[j])<>0)&" " after res
if bitSizeOnly and (i-1)*8+j=me.bitSize then exit repeat
end repeat
end repeat
delete the last char of res
return res
end

View file

@ -0,0 +1,37 @@
----------------------------------------
-- @param {string} str - ASCII string
-- @return {instance} BitArray
----------------------------------------
on crunchASCII (str)
ba = script("BitArray").new(str.length * 7)
pow2 = [64,32,16,8,4,2,1]
pos = 1
repeat with i = 1 to str.length
n = chartonum(str.char[i])
repeat with j = 1 to 7
ba.setBit(pos, bitAnd(n, pow2[j])<>0)
pos = pos+1
end repeat
end repeat
return ba
end
----------------------------------------
-- @param {instance} bitArray
-- @return {string} ASCII string
----------------------------------------
on decrunchASCII (bitArray)
str = ""
pow2 = [64,32,16,8,4,2,1]
pos = 1
cnt = bitArray.bitSize/7
repeat with i = 1 to cnt
n = 0
repeat with j = 1 to 7
n = n + bitArray.getBit(pos)*pow2[j]
pos = pos+1
end repeat
put numtochar(n) after str
end repeat
return str
end

View file

@ -0,0 +1,11 @@
str = "ABC"
ba = crunchASCII(str)
put ba.toBinString()
-- "1 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0"
put ba.toBinString(TRUE)
-- "1 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 1"
put decrunchASCII(ba)
-- "ABC"