Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,86 @@
|
|||
on DecToVLQ
|
||||
Ask "Enter base 10 value:" -- input dialog box
|
||||
if it is not empty then
|
||||
if it is a number then
|
||||
put it into theString
|
||||
if isWholeNumString(theString) is false then -- I think there is built in equivalent for this but I rolled my own!
|
||||
answer "Only Whole Decimal Numbers Are Allowed!"
|
||||
exit DecToVLQ
|
||||
end if
|
||||
if theString>4294967295 then
|
||||
answer "This function fails with whole numbers over 4294967295!"&cr\
|
||||
& "4294967295 is the maximum allowed value for 32bits (4 bytes)"
|
||||
exit DecToVLQ
|
||||
end if
|
||||
if theString>268435455 then
|
||||
answer "This function is not accurate with whole numbers over 268435455!"&cr\
|
||||
& "268435455 is the maximum allowed value for 28bit (7bits per byte) MIDI delta-time VLQ"
|
||||
end if
|
||||
put "Original Whole Number="& theString & cr & \
|
||||
"Original Whole Number in Hex="& baseConvert(theString,10,16) & cr & \ --- LC's built in baseConvert function
|
||||
"Variable Length Quantity in Hex=" & wholeNumToVLQ(theString) into fld "Output"
|
||||
else
|
||||
answer "Only Whole Decimal Numbers Are Allowed!"
|
||||
end if
|
||||
end if
|
||||
end DecToVLQ
|
||||
|
||||
function wholeNumToVLQ theWholeNum
|
||||
-- baseConvert(number,originalBase,destinationBase) -- there is also bitwise operations in LC but I took the long road
|
||||
if theWholeNum < 127 then -- if it fits into a single 7bit byte value and theres no need to process it
|
||||
put baseConvert(theWholeNum,10,16) into VQLinHex
|
||||
if the number of chars in VQLinHex=1 then put "0" before VQLinHex
|
||||
return VQLinHex
|
||||
exit wholeNumToVLQ
|
||||
end if
|
||||
put baseConvert(theWholeNum,10,2) into theBits
|
||||
put number of chars in theBits into x
|
||||
put 0 into bitCounter
|
||||
put empty into the7bitBytes
|
||||
repeat
|
||||
if char x of theBits is not empty then
|
||||
put char x theBits before the7bitBytes
|
||||
delete char x of theBits
|
||||
if theBits is empty then exit repeat
|
||||
put number of chars in theBits into x
|
||||
add 1 to bitCounter
|
||||
if bitCounter=7 then
|
||||
put "," before the7bitBytes
|
||||
put 0 into bitCounter
|
||||
next repeat
|
||||
end if
|
||||
else
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
get the number of chars in item 1 of the7bitBytes
|
||||
if it<7 then
|
||||
put 7 - it into x
|
||||
repeat x
|
||||
put "0" before item 1 of the7bitBytes
|
||||
end repeat
|
||||
end if
|
||||
put the number of items in the7bitBytes into y
|
||||
repeat with x = 1 to y
|
||||
if x is not y then
|
||||
put "1" before item x of the7bitBytes
|
||||
else
|
||||
put "0" before item x of the7bitBytes
|
||||
end if
|
||||
put baseConvert(item x of the7bitBytes,2,16) into item x of the7bitBytes
|
||||
if the number of chars in item x of the7bitBytes<2 then put "0" before item x of the7bitBytes
|
||||
put item x of the7bitBytes after VQLinHex
|
||||
end repeat
|
||||
return VQLinHex
|
||||
end wholeNumToVLQ
|
||||
|
||||
function isWholeNumString theString
|
||||
put the number of chars in theString into y
|
||||
repeat with x = 1 to y
|
||||
if char x of theString is not in "0123456789" then
|
||||
return false
|
||||
exit isWholeNumString
|
||||
end if
|
||||
end repeat
|
||||
return true
|
||||
end isWholeNumString
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
function VLQtoWholeNum theHexVLQ
|
||||
-- The number must be an integer between zero and 4,294,967,295
|
||||
put baseConvert(theHexVLQ,16,2) into theBits
|
||||
put 0 into bitCounter
|
||||
put empty into the8bitBytes
|
||||
repeat
|
||||
if char 1 of theBits is not empty then
|
||||
put char 1 theBits after the8bitBytes
|
||||
delete char 1 of theBits
|
||||
if theBits is empty then exit repeat
|
||||
add 1 to bitCounter
|
||||
if bitCounter=8 then
|
||||
put "," after the8bitBytes
|
||||
put 0 into bitCounter
|
||||
next repeat
|
||||
end if
|
||||
else
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
put the number of items in the8bitBytes into y
|
||||
repeat with x = 1 to y
|
||||
put char 1 of item x of the8bitBytes into lengthCntrlBit
|
||||
delete char 1 of item x of the8bitBytes
|
||||
if the number of chars in item x of the8bitBytes < 7 then
|
||||
repeat 7 - (the number of chars in item x of the8bitBytes)
|
||||
put "0" before item x of the8bitBytes
|
||||
end repeat
|
||||
end if
|
||||
put item x of the8bitBytes after WholeNumInBinary
|
||||
switch lengthCntrlBit
|
||||
case "1"
|
||||
next repeat
|
||||
break
|
||||
case "0"
|
||||
exit repeat
|
||||
break
|
||||
end switch
|
||||
end repeat
|
||||
return baseConvert(WholeNumInBinary,2,10)
|
||||
end VLQtoWholeNum
|
||||
|
||||
function isHexString theString
|
||||
---again there is probably an easier way to do this:
|
||||
if char 1 to 2 of theString is "0x" then delete char 1 to 2 of theString
|
||||
put the number of chars in theString into y
|
||||
repeat with x = 1 to y
|
||||
if char x of theString is not in "abcdefABCDEF0123456789" then
|
||||
return false
|
||||
end if
|
||||
end repeat
|
||||
end isHexString
|
||||
|
||||
on VLQHexToWholeNum
|
||||
Ask "Enter Variable Length Quantity Hex Value:" -- input dialog
|
||||
if it is not empty then
|
||||
if char 1 to 2 of it is "0x" then delete char 1 to 2 of it
|
||||
put it into hexString
|
||||
if isHexString(hexString) is false then
|
||||
answer "Only Valid Hex Digits Are Allowed!"
|
||||
exit VLQHexToWholeNum
|
||||
else
|
||||
put "Original Variable Length Quantity in Hex="& hexString & cr & \
|
||||
"Whole Number=" & VLQtoWholeNum(hexString) into fld "Output"
|
||||
end if
|
||||
end if
|
||||
end VLQHexToWholeNum
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import unsigned, strutils
|
||||
|
||||
proc toSeq(x: uint64): seq[uint8] =
|
||||
var x = x
|
||||
result = @[]
|
||||
var f = 0
|
||||
for i in countdown(9, 1):
|
||||
if (x and (127'u64 shl uint((i * 7)))) > 0'u64:
|
||||
f = i
|
||||
break
|
||||
for j in 0..f:
|
||||
result.add(uint8((x shr uint64((f - j) * 7)) and 127) or 128)
|
||||
|
||||
result[f] = result[f] xor 128'u8
|
||||
|
||||
proc fromSeq(xs): uint64 =
|
||||
result = 0
|
||||
for x in xs:
|
||||
result = (result shl 7) or (x and 127)
|
||||
|
||||
for x in [0x7f'u64, 0x4000'u64, 0'u64, 0x3ffffe'u64, 0x1fffff'u64,
|
||||
0x200000'u64, 0x3311a1234df31413'u64]:
|
||||
let c = toSeq(x)
|
||||
echo "seq from $#: $# back: $#".format(x, c, fromSeq(c))
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
function vlq_encode(sequence s)
|
||||
sequence res = {}
|
||||
integer n, msb
|
||||
for i=length(s) to 1 by -1 do
|
||||
n = s[i]
|
||||
if n<0 then crash("unsigned integers only!") end if
|
||||
msb = 0
|
||||
while 1 do
|
||||
res = prepend(res,msb+and_bits(n,#7F))
|
||||
n = floor(n/#80)
|
||||
if n=0 then exit end if
|
||||
msb = #80
|
||||
end while
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function vlq_decode(sequence s)
|
||||
sequence res = {}
|
||||
integer n = 0, byte
|
||||
for i=1 to length(s) do
|
||||
byte = s[i]
|
||||
n = n*#80+and_bits(byte,#7F)
|
||||
if not and_bits(byte,#80) then
|
||||
res = append(res,n)
|
||||
n = 0
|
||||
end if
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function svlg(sequence s)
|
||||
string res = ""
|
||||
for i=1 to length(s) do
|
||||
res &= sprintf("#%02x:",{s[i]})
|
||||
end for
|
||||
return res[1..$-1]
|
||||
end function
|
||||
|
||||
constant testNumbers = { #200000, #1FFFFF, 1, 127, 128 }
|
||||
sequence s = vlq_encode(testNumbers)
|
||||
sequence decoded = vlq_decode(s)
|
||||
printf(1,"%s -> %s -> %s\n",{svlg(testNumbers),svlg(s),svlg(decoded)})
|
||||
if decoded!=testNumbers then crash("something wrong") end if
|
||||
Loading…
Add table
Add a link
Reference in a new issue