replaceRegexAll :: Source -> Match -> ([(Source, (MatchOffset, MatchLength))] -> Maybe Source) -> Source
replaceRegexAll source match trans = case source =~+ match of
      [] -> source
      m@((_, (offset, length)):_) -> case trans m of
                                        Just m' -> let before = LBS.take (fromIntegral offset) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> m' <> replaceRegexAll after match trans
                                        Nothing -> let before = LBS.take (fromIntegral $ offset + length) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> replaceRegexAll after match trans
replaceRegexAll :: Source -> Match -> ([(Source, (MatchOffset, MatchLength))] -> Maybe Source) -> Source
replaceRegexAll source match trans = case source =~+ match of
      [] -> source
      m@((_, (offset, length)):_) -> case trans m of
                                        Just m' -> let before = LBS.take (fromIntegral offset) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> m' <> replaceRegexAll after match trans
                                        Nothing -> let before = LBS.take (fromIntegral $ offset + length) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> replaceRegexAll after match trans
replaceRegexAll source match trans = case source =~+ match of
      [] -> source
      m@((_, (offset, length)):_) -> case trans m of
                                        Just m' -> let before = LBS.take (fromIntegral offset) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> m' <> replaceRegexAll after match trans
                                        Nothing -> let before = LBS.take (fromIntegral $ offset + length) source
                                                       after  = LBS.drop (fromIntegral $ offset + length) source
                                                   in before <> replaceRegexAll after match trans