packages feed

phino-0.0.151 / src / Bytes.hs

{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Bytes  ( numToBts  , strToBts  , bytesToBts  , btsToStr  , unescapeStr  , btsToNum  , btsToUnescapedStr  , btsIsUtf8  , btsAnd  , btsOr  , btsNot  , btsConcat  , btsEqual  , btsSize  , btsSlice  , btsShift  , nonFinites  , nonFiniteName  , nonFiniteBts  , btsToNonFinite  , nonFiniteOf  , NonFinite (..)  , BytesException (..)  )whereimport ASTimport Control.Exception (Exception, throw)import Data.Binary.IEEE754import Data.Bits (Bits (complement, shiftL, shiftR), (.&.), (.|.))import qualified Data.ByteString as Bimport Data.ByteString.Builder (toLazyByteString, word64BE)import Data.ByteString.Lazy (unpack)import qualified Data.ByteString.Lazy.UTF8 as Uimport Data.Char (chr, isDigit, isPrint, ord)import Data.List (find)import qualified Data.Text as Timport qualified Data.Text.Encoding as Timport Data.Word (Word64, Word8)import Numeric (readHex)import Text.Printf (printf)newtype BytesException = InvalidNumberLength Int  deriving (Eq, Show)instance Exception BytesExceptionbtsToWord8 :: Bytes -> [Word8]btsToWord8 BtEmpty = []btsToWord8 (BtOne bt) = [hexByte bt]btsToWord8 (BtMany bts) = map hexByte btsbtsToWord8 (BtMeta mt) = error $ "Cannot convert meta bytes to Word8; " ++ T.unpack mtbtsToWord8 (BtAny _) = error "Cannot convert anonymous meta bytes to Word8"hexByte :: String -> Word8hexByte [hi, lo] = (nibble hi `shiftL` 4) .|. nibble lo  where    nibble :: Char -> Word8    nibble c      | isDigit c = fromIntegral (ord c - ord '0')      | c >= 'A' && c <= 'F' = fromIntegral (ord c - ord 'A' + 10)      | c >= 'a' && c <= 'f' = fromIntegral (ord c - ord 'a' + 10)      | otherwise = error ("Invalid hex digit: " ++ [c])hexByte bt = error $ "Invalid hex byte; " ++ btword8ToBytes :: [Word8] -> Bytesword8ToBytes [] = BtEmptyword8ToBytes [w8] = BtOne (toHex w8)word8ToBytes bts = BtMany (map toHex bts)toHex :: Word8 -> StringtoHex w = [digit (w `shiftR` 4), digit (w .&. 0x0F)]  where    digit :: Word8 -> Char    digit n      | n < 10 = chr (fromIntegral n + ord '0')      | otherwise = chr (fromIntegral n + ord 'A' - 10)btsToNum :: Bytes -> Either Int DoublebtsToNum hx =  let bytes = btsToWord8 hx   in if length bytes /= 8        then throw (InvalidNumberLength (length bytes))        else          let word = toWord64BE bytes              val = wordToDouble word           in if isNaN val || isInfinite val || isNegativeZero val                then Right val                else case properFraction val of                  (n, 0.0) -> Left n                  _ -> Right val  where    toWord64BE :: [Word8] -> Word64    toWord64BE [a, b, c, d, e, f, g, h] =      fromIntegral a `shiftL` 56        .|. fromIntegral b `shiftL` 48        .|. fromIntegral c `shiftL` 40        .|. fromIntegral d `shiftL` 32        .|. fromIntegral e `shiftL` 24        .|. fromIntegral f `shiftL` 16        .|. fromIntegral g `shiftL` 8        .|. fromIntegral h    toWord64BE _ = error "Expected 8 bytes for Double"numToBts :: Double -> BytesnumToBts num = word8ToBytes (unpack (toLazyByteString (word64BE (doubleToWord num))))data NonFinite = NfNan | NfPinf | NfNinf  deriving (Eq, Show)nonFinites :: [NonFinite]nonFinites = [NfNan, NfPinf, NfNinf]nonFiniteName :: NonFinite -> T.TextnonFiniteName NfNan = "nan"nonFiniteName NfPinf = "pinf"nonFiniteName NfNinf = "ninf"nonFiniteBts :: NonFinite -> BytesnonFiniteBts NfNan = BtMany ["7F", "F8", "00", "00", "00", "00", "00", "00"]nonFiniteBts NfPinf = BtMany ["7F", "F0", "00", "00", "00", "00", "00", "00"]nonFiniteBts NfNinf = BtMany ["FF", "F0", "00", "00", "00", "00", "00", "00"]btsToNonFinite :: Bytes -> Maybe NonFinitebtsToNonFinite (BtMeta _) = NothingbtsToNonFinite (BtAny _) = NothingbtsToNonFinite bts = find (btsEqual bts . nonFiniteBts) nonFinitesnonFiniteOf :: T.Text -> Maybe NonFinitenonFiniteOf name = find ((== name) . nonFiniteName) nonFinitesstrToBts :: String -> BytesstrToBts "" = BtEmptystrToBts [ch] = word8ToBytes (unpack (U.fromString [ch]))strToBts str = word8ToBytes (unpack (U.fromString str))bytesToBts :: String -> BytesbytesToBts "--" = BtEmptybytesToBts str  | length str == 3 && last str == '-' = BtOne (init str)  | not (null str) && last str == '-' = error $ "Invalid trailing separator in byte string; " ++ str  | otherwise = BtMany (map T.unpack (T.splitOn "-" (T.pack str)))btsToStr :: Bytes -> StringbtsToStr BtEmpty = ""btsToStr bytes = escapeStr (btsToUnescapedStr bytes)  where    escapeStr :: String -> String    escapeStr = concatMap escapeChar      where        escapeChar :: Char -> String        escapeChar '"' = "\\\""        escapeChar '\\' = "\\\\"        escapeChar '\n' = "\\n"        escapeChar '\t' = "\\t"        escapeChar c          | isPrint c && c /= '\\' && c /= '"' = [c]          | ord c <= 0xFF = printf "\\x%02x" (ord c)          | ord c <= 0xFFFF = printf "\\u%04x" (ord c)          | otherwise = surrogates (ord c)        surrogates :: Int -> String        surrogates code =          let rest = code - 0x10000              high = 0xD800 + rest `div` 0x400              low = 0xDC00 + rest `mod` 0x400           in printf "\\u%04x\\u%04x" high lowunescapeStr :: String -> StringunescapeStr = go  where    go :: String -> String    go "" = ""    go ('\\' : 'u' : digits) = goUnicode digits    go ('\\' : 'x' : high : low : rest)      | Just code <- hexPair high low = chr code : go rest    go ('\\' : escaped : rest)      | Just unescaped <- lookup escaped escapes = unescaped : go rest    go (char : rest) = char : go rest    goUnicode :: String -> String    goUnicode (h1 : h2 : h3 : h4 : rest)      | Just code <- hexQuad h1 h2 h3 h4 =          if code >= 0xD800 && code <= 0xDBFF            then case rest of              ('\\' : 'u' : l1 : l2 : l3 : l4 : rest')                | Just low <- hexQuad l1 l2 l3 l4                , low >= 0xDC00 && low <= 0xDFFF ->                    chr (0x10000 + (code - 0xD800) * 0x400 + (low - 0xDC00)) : go rest'              _ -> chr code : go rest            else chr code : go rest    goUnicode rest = go rest    hexQuad :: Char -> Char -> Char -> Char -> Maybe Int    hexQuad a b c d = case readHex [a, b, c, d] of      [(code, "")] -> Just code      _ -> Nothing    hexPair :: Char -> Char -> Maybe Int    hexPair high low = case readHex [high, low] of      [(code, "")] -> Just code      _ -> Nothing    escapes :: [(Char, Char)]    escapes = [('"', '"'), ('\\', '\\'), ('n', '\n'), ('t', '\t'), ('r', '\r'), ('b', '\b'), ('f', '\f')]btsToUnescapedStr :: Bytes -> StringbtsToUnescapedStr bytes = T.unpack (T.decodeUtf8 (B.pack (btsToWord8 bytes)))btsIsUtf8 :: Bytes -> BoolbtsIsUtf8 bytes =  case T.decodeUtf8' (B.pack (btsToWord8 bytes)) of    Left _ -> False    Right _ -> TruebtsAnd :: Bytes -> Bytes -> Maybe BytesbtsAnd = zipBytes (.&.)btsOr :: Bytes -> Bytes -> Maybe BytesbtsOr = zipBytes (.|.)zipBytes :: (Word8 -> Word8 -> Word8) -> Bytes -> Bytes -> Maybe ByteszipBytes op left right  | length lefts /= length rights = Nothing  | otherwise = Just (word8ToBytes (zipWith op lefts rights))  where    lefts :: [Word8]    lefts = btsToWord8 left    rights :: [Word8]    rights = btsToWord8 rightbtsNot :: Bytes -> BytesbtsNot = word8ToBytes . map complement . btsToWord8btsConcat :: Bytes -> Bytes -> BytesbtsConcat left right = word8ToBytes (btsToWord8 left ++ btsToWord8 right)btsEqual :: Bytes -> Bytes -> BoolbtsEqual left right = btsToWord8 left == btsToWord8 rightbtsSize :: Bytes -> IntbtsSize = length . btsToWord8btsSlice :: Int -> Int -> Bytes -> Maybe BytesbtsSlice start len bts  | start < 0 || len < 0 || start + len > length octets = Nothing  | otherwise = Just (word8ToBytes (take len (drop start octets)))  where    octets :: [Word8]    octets = btsToWord8 btsbtsShift :: Int -> Bytes -> BytesbtsShift bits bts  | magnitude >= toInteger size * 8 = word8ToBytes (replicate size 0)  | bits < 0 = word8ToBytes (map leftwards indices)  | otherwise = word8ToBytes (map rightwards indices)  where    magnitude :: Integer    magnitude = abs (toInteger bits)    octets :: [Word8]    octets = btsToWord8 bts    size :: Int    size = length octets    indices :: [Int]    indices = [0 .. size - 1]    modulo :: Int    modulo = fromInteger (magnitude `mod` 8)    offset :: Int    offset = fromInteger (magnitude `div` 8)    octet :: Int -> Word8    octet index = octets !! index    rightwards :: Int -> Word8    rightwards index      | source < 0 = 0      | source > 0 = shifted .|. ((octet (source - 1) `shiftL` (8 - modulo)) .&. carry)      | otherwise = shifted      where        source :: Int        source = index - offset        shifted :: Word8        shifted = octet source `shiftR` modulo        carry :: Word8        carry = 0xFF `shiftL` (8 - modulo)    leftwards :: Int -> Word8    leftwards index      | source >= size = 0      | source + 1 < size = shifted .|. ((octet (source + 1) `shiftR` (8 - modulo)) .&. carry)      | otherwise = shifted      where        source :: Int        source = index + offset        shifted :: Word8        shifted = octet source `shiftL` modulo        carry :: Word8        carry = (0x01 `shiftL` modulo) - 1