Skip to content

Instantly share code, notes, and snippets.

@jeovazero
Last active September 28, 2022 05:41
Show Gist options
  • Save jeovazero/cd73400da35f7027deb24fa6868571ab to your computer and use it in GitHub Desktop.
Save jeovazero/cd73400da35f7027deb24fa6868571ab to your computer and use it in GitHub Desktop.
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'
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