Created
September 29, 2021 00:10
-
-
Save duanebester/4f1f16801f732bb330ad59f5f74ece4c to your computer and use it in GitHub Desktop.
BinF C-String helpers
This file contains hidden or 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 wr-cstring [view string] | |
(binf/wr-string view (str string "\0")) | |
view) | |
(defn rr-cstring [view] | |
(loop [index (binf/position view) acc []] | |
(let [b (binf/rr-u8 view)] | |
(if (zero? b) | |
(.toString | |
(.decode binf.string/decoder-utf-8 | |
#?(:clj (ByteBuffer/wrap (byte-array acc)) | |
:cljs (.from js/Uint8Array acc)))) | |
(recur (inc index) (conj acc b)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat!
I guess you could even optimize as such:
And for reading, you can get a buffer in one go instead of having to create a vector. Something like: