~ubuntu-branches/ubuntu/wily/agda/wily-proposed

« back to all changes in this revision

Viewing changes to src/pkg/Utils.hs

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-08-05 06:38:12 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140805063812-io8e77niomivhd49
Tags: 2.4.0.2-1
* [6e140ac] Imported Upstream version 2.4.0.2
* [2049fc8] Update Build-Depends to match control
* [93dc4d4] Install the new primitives
* [e48f40f] Fix typo dev→doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Utils where
2
 
 
3
 
import           Control.Applicative
4
 
import           Control.Arrow
5
 
import qualified Data.ByteString.Char8
6
 
  as BS
7
 
import           Text.Regex.Posix
8
 
 
9
 
--------------------------------------------------------------------------------
10
 
 
11
 
type MatchPair      = (MatchOffset, MatchLength)
12
 
type MatchTriple    = (BS.ByteString, (BS.ByteString, BS.ByteString))
13
 
type Substitution m = BS.ByteString -> m BS.ByteString
14
 
 
15
 
substitute :: (Functor m, Monad m)
16
 
           => BS.ByteString
17
 
           -> BS.ByteString
18
 
           -> Substitution m
19
 
           -> m BS.ByteString
20
 
substitute input regEx subst = output
21
 
                             $ splits 0 (getAllMatches (input =~ regEx)) input
22
 
  where
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)
27
 
      where
28
 
        o'       = mo + ml
29
 
        splitter = (***) id (BS.splitAt ml) . BS.splitAt (mo - o)
30
 
    output mts = BS.concat <$> mapM joiner mts
31
 
      where
32
 
        joiner mt = do
33
 
          v <- subst $ fst $ snd mt
34
 
          return $ BS.concat [fst mt, v]