packages feed

iri-0.5.1.2: library/Iri/PercentEncoding.hs

module Iri.PercentEncoding where

import Iri.Prelude

{-# INLINE matchHexByte #-}
matchHexByte :: a -> (Word8 -> a) -> Word8 -> a
matchHexByte failure success x =
  if x >= 48 && x <= 57
    then success (x - 48)
    else
      if x >= 65 && x <= 70
        then success (x - 55)
        else
          if x >= 97 && x <= 102
            then success (x - 87)
            else failure

{-# INLINE matchPercentEncodedBytes #-}
matchPercentEncodedBytes :: a -> (Word8 -> a) -> Word8 -> Word8 -> a
matchPercentEncodedBytes failure success byte1 byte2 =
  matchHexByte failure firstByteSuccess byte1
  where
    firstByteSuccess decodedByte1 =
      matchHexByte failure secondByteSuccess byte2
      where
        secondByteSuccess decodedByte2 =
          success (shiftL decodedByte1 4 .|. decodedByte2)