Polissage : CLI.Command.Balance : is_worth.
[comptalang.git] / lib / Hcompta / Lib / Regex.hs
index 2d3436de427c9b89eb06016e0ae6d7b02da83935..e3a07281fbbe441e0ff312f9c90cf4a7a4d89a69 100644 (file)
@@ -1,56 +1,73 @@
-{-# LANGUAGE DeriveDataTypeable #-}
 {-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE StandaloneDeriving #-}
-{-# LANGUAGE TypeSynonymInstances #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 module Hcompta.Lib.Regex where
 
-import Data.Data
-import Data.Typeable ()
-
 import qualified Data.Array
+import           Data.Data ()
 import qualified Data.Char
 import qualified Data.List
 import qualified Text.Regex.TDFA as R
+import qualified Text.Regex.TDFA.Text ()
+import           Text.Regex.TDFA.Common as R
+import qualified Text.Regex.TDFA.IntArrTrieSet as R.IntArrTrieSet
+import           Data.Typeable ()
+import           Data.Text (Text)
 
 -- * The 'Regex' type
 
-type Regex = R.Regex
+type Regex       = R.Regex
 type Replacement = String
 
 -- * Constructors
 
 -- | Parse the given 'String' to a 'Regex'.
-of_String :: String -> Regex
+of_String :: String -> R.Regex
 of_String = R.makeRegex
 
-instance Read Regex where
+instance Read R.Regex where
        readsPrec _ s = [(R.makeRegex s, "")]
-instance Show Regex where
+instance Show R.Regex where
        show _ = "Regex"
-
--- | Parse the given 'String' to a 'Regex' (monadic version).
-of_StringM :: Monad m => String -> m Regex
+-- instance Eq Regex where
+--     _x == _y = True
+deriving instance Eq a => Eq (R.IntArrTrieSet.TrieSet a)
+deriving instance Eq R.CompOption
+deriving instance Eq R.DFA
+deriving instance Eq R.DT
+deriving instance Eq R.ExecOption
+deriving instance Eq R.GroupInfo
+deriving instance Eq R.Instructions
+deriving instance Eq R.Transition
+instance Eq (R.Position -> R.OrbitTransformer) where
+       _x == _y = True
+deriving instance Eq R.Regex
+
+-- | Parse the given 'String' to a 'R.Regex' (monadic version).
+of_StringM :: Monad m => String -> m R.Regex
 of_StringM = R.makeRegexM
 
 -- * Matchers
 
--- | Synonym to Text.Regex.TDFA.'Text.Regex.TDFA.=~'.
-(=~) :: ( R.RegexMaker Regex R.CompOption R.ExecOption source
-        , R.RegexContext Regex source1 target )
+match :: R.Regex -> Text -> Bool
+match = R.match
+
+-- | Synonym to Text.R.Regex.TDFA.'Text.R.Regex.TDFA.=~'.
+(=~) :: ( R.RegexMaker R.Regex R.CompOption R.ExecOption source
+        , R.RegexContext R.Regex source1 target )
      => source1 -> source -> target
 (=~) = (R.=~)
 
--- | Synonym to Text.Regex.TDFA.'Text.Regex.TDFA.=~~'.
-(=~~) :: ( R.RegexMaker Regex R.CompOption R.ExecOption source
-         , R.RegexContext Regex source1 target,Monad m )
+-- | Synonym to Text.R.Regex.TDFA.'Text.R.Regex.TDFA.=~~'.
+(=~~) :: ( R.RegexMaker R.Regex R.CompOption R.ExecOption source
+         , R.RegexContext R.Regex source1 target,Monad m )
       => source1 -> source -> m target
 (=~~) = (R.=~~)
 
 -- * Replacers
 
-replace :: Regex -> Replacement -> String -> String
+replace :: R.Regex -> Replacement -> String -> String
 replace re repl s =
        Data.List.foldl (replace_match repl) s
        (reverse $ R.match re s :: [R.MatchText String])
@@ -76,11 +93,12 @@ replace_backref match_groups ('\\':s@(_:_))
 replace_backref _ s =
        error $ concat $ ["replace_backref called on non-numeric-backref \"", s, "\""]
 
-replace_all :: Regex -> (String -> String) -> String -> String
+replace_all :: R.Regex -> (String -> String) -> String -> String
 replace_all re f s =
        concat (reverse $ remaining:done)
        where
-               (_ind, remaining, done) = Data.List.foldl' go (0, s, []) $ R.getAllMatches $ R.match re s
+               (_ind, remaining, done) = Data.List.foldl' go (0, s, []) $
+                       (R.getAllMatches $ R.match re s::[(Int, Int)])
                go (ind, prev_ok_next, repl) (ofs, len) =
                        let (prev, ok_next) = Data.List.splitAt (ofs - ind) prev_ok_next
                            (ok, next)      = Data.List.splitAt len ok_next in