September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,10 +1,15 @@
import Numeric
import Numeric (readOct, showOct)
import Data.List (intercalate)
to :: Int -> String
to = flip showOct ""
from :: String -> Int
from = fst . head . readOct
main = do
fancy 2097152
fancy 2097151
where fancy i = putStrLn $ to i ++ " <-> " ++ show (from $ to i)
main :: IO ()
main =
mapM_
(putStrLn .
intercalate " <-> " . (pure (:) <*> to <*> (return . show . from . to)))
[2097152, 2097151]

View file

@ -1,11 +1,19 @@
import Data.List (intercalate)
base :: Int
base = 8
to :: Int -> [Int]
to 0 = []
to i = to (div i base) ++ [mod i base]
from = foldl1 (\x y -> x*base + y)
from :: [Int] -> Int
from = foldl1 ((+) . (base *))
main = do
fancy 2097152
fancy 2097151
where fancy i = putStrLn $ concatMap show (to i) ++ " <-> " ++ show (from $ to i)
main :: IO ()
main =
mapM_
(putStrLn .
intercalate " <-> " .
(((:) . concatMap show . to) <*> (return . show . from . to)))
[2097152, 2097151]

View file

@ -0,0 +1,31 @@
// version 1.0.6
fun Int.toOctets(): ByteArray {
var s = Integer.toBinaryString(this)
val r = s.length % 7
var z = s.length / 7
if (r > 0) {
z++
s = s.padStart(z * 7, '0')
}
s = Array(z) { "1" + s.slice(it * 7 until (it + 1) * 7) }.joinToString("")
s = s.take(s.length - 8) + "0" + s.takeLast(7)
return ByteArray(z) { Integer.parseInt(s.slice(it * 8 until (it + 1) * 8), 2).toByte() }
}
fun ByteArray.fromOctets(): Int {
var s = ""
for (b in this) s += Integer.toBinaryString(b.toInt()).padStart(7, '0').takeLast(7)
return Integer.parseInt(s, 2)
}
fun main(args: Array<String>) {
val tests = intArrayOf(0x7f, 0x3fff, 0x200000, 0x1fffff)
for (test in tests) {
val ba = test.toOctets()
print("${"0x%x".format(test).padEnd(8)} -> ")
var s = ""
ba.forEach { s += "0x%02x ".format(it) }
println("${s.padEnd(20)} <- ${"0x%x".format(ba.fromOctets())}")
}
}

View file

@ -1,26 +1,24 @@
/*REXX program to display (and also test/verify) some numbers as octets.*/
/*REXX program displays (and also tests/verifies) some numbers as octets. */
nums = x2d(200000) x2d(1fffff) 2097172 2097151
#=words(nums)
say ' number hex octet original'
say ' '
say ' number hex octet original'
say ' '
ok=1
do j=1 for #; @.j=word(nums,j)
do j=1 for #; @.j= word(nums,j)
onum.j=octet(@.j)
orig.j=x2d(space(onum.j,0))
say show(@.j) show(d2x(@.j)) show(onum.j) show(orig.j)
orig.j= x2d( space(onum.j, 0) )
w=10
say center(@.j, w) center(d2x(@.j), w) center(onum.j, w) center(orig.j, w)
if @.j\==orig.j then ok=0
end /*j*/
say
if ok then say 'All' # "numbers are OK." /*all numbers good*/
else say 'Trouble right here in River City.' /*some number ¬good*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────OCTET subroutine────────────────────*/
octet: procedure; parse arg a,_ /*obtain A from the passed arg. */
x=d2x(a) /*convert A to hexadecimal octet.*/
do j=length(x) by -2 to 1 /*process the "little" end first.*/
_=substr(x,j-1,2,0) _ /*pad odd hexcharacters with ··· */
end /*j*/ /* ··· a zero on the left. */
return _
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: return center(arg(1),9) /*justify via centering the text.*/
if ok then say 'All ' # " numbers are OK." /*all of the numbers are good. */
else say "Some numbers are not OK." /*some of the numbers are ¬good. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
octet: procedure; parse arg z,$ /*obtain Z from the passed arguments.*/
x=d2x(z) /*convert Z to a hexadecimal octet. */
do j=length(x) by -2 to 1 /*process the "little" end first. */
$= substr(x, j-1, 2, 0) $ /*pad odd hexadecimal characters with */
end /*j*/ /* ··· a zero on the left. */
return strip($)

View file

@ -0,0 +1,27 @@
func vlq_encode(num) {
var t = (0x7F & num)
var str = t.chr
while (num >>= 7) {
t = (0x7F & num)
str += chr(0x80 | t)
}
str.reverse
}
func vlq_decode(str) {
var num = ''
str.each_byte { |b|
num += ('%07b' % (b & 0x7F))
}
Num(num, 2)
}
var tests = [0, 0xa, 123, 254, 255, 256,
257, 65534, 65535, 65536, 65537, 0x1fffff,
0x200000]
tests.each { |t|
var vlq = vlq_encode(t)
printf("%8s %12s %8s\n", t,
vlq.bytes.join(':', { "%02X" % _ }), vlq_decode(vlq))
}

View file

@ -1,20 +0,0 @@
;; show the utf8 bytes from byte stream as hex
(defun put-utf8 (str : stream)
(set stream (or stream *stdout*))
(for ((s (make-string-byte-input-stream str)) byte)
((set byte (get-byte s)))
((format stream "\\x~,02x" byte))))
;; print
(put-utf8 (tostring 0))
(put-line "")
(put-utf8 (tostring 42))
(put-line "")
(put-utf8 (tostring #x200000))
(put-line "")
(put-utf8 (tostring #x1fffff))
(put-line "")
;; print to string and recover
(format t "~a\n" (read (tostring #x200000)))
(format t "~a\n" (read (tostring #x1f0000)))

View file

@ -0,0 +1,8 @@
fcn to_seq(x){ //--> list of ints
z:=(x.log2()/7);
(0).pump(z+1,List,'wrap(j){
x.shiftRight((z-j)*7).bitAnd(0x7f).bitOr((j!=z) and 0x80 or 0)
});
}
fcn from_seq(in){ in.reduce(fcn(p,n){ p.shiftLeft(7).bitOr(n.bitAnd(0x7f)) },0) }

View file

@ -0,0 +1,5 @@
ns:=T(0x7f, 0x4000, 0, 0x3ffffe, 0x1fffff, 0x200000, 0x3311a1234df31413);
ms:=ns.apply(to_seq);
ns.zipWith(fcn{"%8,x --> %s --> %,x".fmt(vm.arglist.xplode()).println()},
ms.apply("apply","%,x".fmt),
ms.apply(from_seq));