Created
November 16, 2012 20:32
-
-
Save lancepantz/4090649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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