base16

Fast Haskell base16 encoding/decoding (docs.ppad.tech/base16).
git clone git://git.ppad.tech/base16.git
Log | Files | Refs | README | LICENSE

Base16.hs (1296B)


      1 {-# OPTIONS_HADDOCK prune #-}
      2 
      3 -- |
      4 -- Module: Data.ByteString.Base16
      5 -- Copyright: (c) 2025 Jared Tobin
      6 -- License: MIT
      7 -- Maintainer: Jared Tobin <jared@ppad.tech>
      8 --
      9 -- Base16 encoding and decoding of strict bytestrings.
     10 
     11 module Data.ByteString.Base16 (
     12     encode
     13   , decode
     14   ) where
     15 
     16 import qualified Data.ByteString as BS
     17 import qualified Data.ByteString.Base16.Arm as Arm
     18 import qualified Data.ByteString.Base16.Pure as Pure
     19 
     20 -- | Encode a base256 'ByteString' as base16.
     21 --
     22 --   Uses ARM NEON extensions when available, otherwise a pure
     23 --   Haskell scalar loop.
     24 --
     25 --   >>> encode "hello world"
     26 --   "68656c6c6f20776f726c64"
     27 encode :: BS.ByteString -> BS.ByteString
     28 encode bs
     29   | Arm.base16_arm_available = Arm.encode bs
     30   | otherwise = Pure.encode bs
     31 {-# INLINABLE encode #-}
     32 
     33 -- | Decode a base16 'ByteString' to base256.
     34 --
     35 --   Uses ARM NEON extensions when available, otherwise a pure
     36 --   Haskell scalar loop.  Invalid inputs (including odd-length
     37 --   inputs) will produce 'Nothing'.
     38 --
     39 --   >>> decode "68656c6c6f20776f726c64"
     40 --   Just "hello world"
     41 --   >>> decode "068656c6c6f20776f726c64" -- odd-length
     42 --   Nothing
     43 decode :: BS.ByteString -> Maybe BS.ByteString
     44 decode bs
     45   | Arm.base16_arm_available = Arm.decode bs
     46   | otherwise = Pure.decode bs
     47 {-# INLINABLE decode #-}