-- |-- Module : Data.ByteString.Search.Substitution-- Copyright : Daniel Fischer-- Licence : BSD3-- Maintainer : Daniel Fischer <daniel.is.fischer@googlemail.com>-- Stability : Provisional-- Portability : portable---- Class for values to be substituted into strict and lazy 'S.ByteString's-- by the @replace@ functions defined in this package.--
module Data.ByteString.Search.Substitution ( Substitution(..)) where
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as LI
-- | Type class of meaningful substitutions for replace functions-- on ByteStrings. Instances for strict and lazy ByteStrings are-- provided here.
class Substitutiona where
-- | @'substitution'@ transforms a value to a substitution function.substitution :: a -> ([S.ByteString] -> [S.ByteString])
-- | @'prependCycle' sub lazyBS@ shall prepend infinitely many copies-- of @sub@ to @lazyBS@ without entering an infinite loop in case-- of an empty @sub@, so e.g.---- @-- 'prependCycle' \"\" \"ab\" == \"ab\"-- @---- shall (quickly) evaluate to 'True'.-- For non-empty @sub@, the cycle shall be constructed efficiently.prependCycle :: a -> (L.ByteString -> L.ByteString)
instance SubstitutionS.ByteString where
{-# INLINE substitution #-}substitutionsub = if S.nullsub then id else (sub:)
{-# INLINE prependCycle #-}prependCyclesub
| S.nullsub = id
| otherwise = let c = LI.Chunksubc in constc
instance SubstitutionL.ByteString where
{-# INLINE substitution #-}substitutionLI.Empty = idsubstitution (LI.Chunkct) = (c:) .flip (LI.foldrChunks(:)) t{-# INLINE prependCycle #-}prependCyclesub
| L.nullsub = idprependCyclesub = let cyc = LI.foldrChunksLI.Chunkcycsub in constcyc