3
import Control.Applicative
5
import qualified Data.ByteString.Char8
7
import Text.Regex.Posix
9
--------------------------------------------------------------------------------
11
type MatchPair = (MatchOffset, MatchLength)
12
type MatchTriple = (BS.ByteString, (BS.ByteString, BS.ByteString))
13
type Substitution m = BS.ByteString -> m BS.ByteString
15
substitute :: (Functor m, Monad m)
20
substitute input regEx subst = output
21
$ splits 0 (getAllMatches (input =~ regEx)) input
23
splits :: Int -> [MatchPair] -> BS.ByteString -> [MatchTriple]
24
splits _o [] _str = []
25
splits o ((mo, ml):ms) str = splitter str
26
: splits o' ms (BS.drop (o' - o) str)
29
splitter = (***) id (BS.splitAt ml) . BS.splitAt (mo - o)
30
output mts = BS.concat <$> mapM joiner mts
33
v <- subst $ fst $ snd mt
34
return $ BS.concat [fst mt, v]