Last active
September 28, 2022 05:41
-
-
Save jeovazero/cd73400da35f7027deb24fa6868571ab to your computer and use it in GitHub Desktop.
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
import Data.Bits | |
import Data.Char | |
bin 0 n acc = (Prelude.take n (repeat 0)) ++ acc | |
bin x n acc = bin (div x 2) (n-1) $ (mod x 2):acc | |
bs n = Prelude.concat $ fmap show $ bin n 8 [] | |
ibs ls = ibs' ls 0 | |
where | |
ibs' [] acc = acc | |
ibs' (l:ls) acc = ibs' ls $ (shift acc 1) + ord l - ord '0' |
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
import Data.Bits (xor, (.&.)) | |
import Data.Int | |
import Data.Word | |
mask6, mask5, mask4, mask3, mask2, mask1 :: Word8 | |
mask6 = 63 | |
mask5 = 31 | |
mask4 = 15 | |
mask3 = 7 | |
mask2 = 3 | |
mask1 = 1 | |
b10 :: Word8 -> Bool | |
b10 n = n `xor` 64 .&. 192 == 192 -- extract with mask6 | |
b110 n = n `xor` 32 .&. 224 == 224 -- ... mask5 | |
b1110 n = n `xor` 16 .&. 240 == 240 -- ... mask4 | |
b11110 n = n `xor` 8 .&. 248 == 248 -- ... mask3 | |
consumeBits :: [Word8] -> ([Word8], Int32, Int32) | |
consumeBits (w:ws) | |
| w <= 127 = (ws, fromIntegral w, 1) -- tail, value, consumed | |
| b110 w = consumeUnicode ws (fromIntegral $ mask5 .&. w) 1 | |
| b1110 w = consumeUnicode ws (fromIntegral $ mask4 .&. w) 1 | |
| b11110 w = consumeUnicode ws (fromIntegral $ mask3 .&. w) 1 | |
consumeUnicode :: [Word8] -> Int32 -> Int32 -> ([Word8], Int32, Int32) | |
consumeUnicode ww@(w:ws) acc consumed | |
| b10 w = consumeUnicode ws (acc + fromIntegral (w .&. mask6)) (consumed + 1) | |
| otherwise = (ww, acc, consumed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment