Skip to content

Instantly share code, notes, and snippets.

@lancepantz
Created November 16, 2012 20:32
Show Gist options
  • Save lancepantz/4090649 to your computer and use it in GitHub Desktop.
Save lancepantz/4090649 to your computer and use it in GitHub Desktop.
(defn parse-vlf [buffer]
(loop [bytes 0, first true, number 0, multiplier 1, byte (.get buffer)]
(let [number (+ number (* (bit-and byte 0x7f)
(int (Math/pow 2 (* bytes 7)))))
[multiplier number] (if (and first (not= 0 (bit-and number 1)))
[-1 (- number 1)]
[1 number])]
(if (= 0 (bit-and 0x80 byte))
[(* (/ number 2) multiplier) buffer]
(recur (+ 1 bytes) false number multiplier (.get buffer))))))
(defn vlf []
(reify
Reader
(read-bytes [_ buffer]
(let [[value remainder] (parse-vlf (contiguous buffer))]
[true value (to-buf-seq remainder)]))
Writer
(sizeof [_] nil)
(write-bytes [_ _ _])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment