{- | see also "Text.Regex.Do.Replace.Latin"

    __'toByteString'__ converts String to 'ByteString'     -}

module Text.Regex.Do.Replace.Utf8
    (Replace(..))  where

import Prelude as P
import Text.Regex.Do.Type.Do
import Text.Regex.Do.Match.Matchf
import qualified Text.Regex.Do.Replace.Open as O
import Text.Regex.Do.Match.Regex as T
import Text.Regex.Do.Type.Convert
import Data.ByteString
import Text.Regex.Do.Type.Extract
import Text.Regex.Do.Type.Reexport as R
import Text.Regex.Base.RegexLike as R
import Text.Regex.Do.Match.Option


{- | see "Text.Regex.Do.Replace.Latin" for implemented types

    'GroupReplacer' is implemented only for 'ByteString'    -}

class Replace hint pattern repl body out where
    replace::(Extract' body, RegexLike R.Regex body) =>
        hint pattern -> repl -> body -> out


instance Replace Once R.Regex String String String where
    replace :: Once Regex -> String -> String -> String
replace (Once p0 :: Regex
p0) r0 :: String
r0 b0 :: String
b0 =    
        let b1 :: ByteString
b1 = String -> ByteString
toByteString String
b0
            r1 :: ByteString
r1 = String -> ByteString
toByteString String
r0 
            Right ma1 :: Maybe MatchArray
ma1 = E Regex -> Tagged Once ByteString -> H (Tagged Once)
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ (Regex -> E Regex
forall a b. b -> Either a b
Right Regex
p0) (ByteString -> Tagged Once ByteString
forall b. b -> Tagged Once b
tagOnce ByteString
b1)  
        in ByteString -> String
toString (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ Maybe MatchArray -> ByteString -> ByteString -> ByteString
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace Maybe MatchArray
ma1 ByteString
r1 ByteString
b1
-- ^ always succeeds

instance Replace All R.Regex String String String where
    replace :: All Regex -> String -> String -> String
replace (All p0 :: Regex
p0) r0 :: String
r0 b0 :: String
b0 =
        let b1 :: ByteString
b1 = String -> ByteString
toByteString String
b0
            r1 :: ByteString
r1 = String -> ByteString
toByteString String
r0
            Right ma1 :: [MatchArray]
ma1 = E Regex -> Tagged All ByteString -> H (Tagged All)
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ (Regex -> E Regex
forall a b. b -> Either a b
Right Regex
p0) (ByteString -> Tagged All ByteString
forall b. b -> Tagged All b
tagAll ByteString
b1)
        in ByteString -> String
toString (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ [MatchArray] -> ByteString -> ByteString -> ByteString
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace [MatchArray]
ma1 ByteString
r1 ByteString
b1
-- ^ always succeeds


instance O.Replace Maybe repl ByteString => 
    Replace Once R.Regex repl ByteString ByteString where
    replace :: Once Regex -> repl -> ByteString -> ByteString
replace (Once p0 :: Regex
p0) r0 :: repl
r0 b0 :: ByteString
b0 =
        let Right ma1 :: Maybe MatchArray
ma1 = E Regex -> Tagged Once ByteString -> H (Tagged Once)
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ (Regex -> E Regex
forall a b. b -> Either a b
Right Regex
p0) (ByteString -> Tagged Once ByteString
forall b. b -> Tagged Once b
tagOnce ByteString
b0)
        in Maybe MatchArray -> repl -> ByteString -> ByteString
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace Maybe MatchArray
ma1 repl
r0 ByteString
b0
{- ^ succeeds unless 'GroupReplacer' fails due to mismatched pattern etc 

repl: 'ByteString' | 'GroupReplacer' 'ByteString'    
-}


instance O.Replace [] repl ByteString => 
    Replace All R.Regex repl ByteString ByteString where
    replace :: All Regex -> repl -> ByteString -> ByteString
replace (All p0 :: Regex
p0) r0 :: repl
r0 b0 :: ByteString
b0 =
        let Right ma1 :: [MatchArray]
ma1 = E Regex -> Tagged All ByteString -> H (Tagged All)
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ (Regex -> E Regex
forall a b. b -> Either a b
Right Regex
p0) (ByteString -> Tagged All ByteString
forall b. b -> Tagged All b
tagAll ByteString
b0)
        in [MatchArray] -> repl -> ByteString -> ByteString
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace [MatchArray]
ma1 repl
r0 ByteString
b0
{- ^ succeeds unless 'GroupReplacer' fails due to mismatched pattern etc 

repl: 'ByteString' | 'GroupReplacer' 'ByteString'    
-}



instance Replace Once String String String (E String) where
    replace :: Once String -> String -> String -> E String
replace (Once p0 :: String
p0) = String
-> (String -> Tagged Once String) -> String -> String -> E String
forall a (hint :: * -> *) b (f :: * -> *) arr a.
(Regex a, Matchf hint, RegexLike Regex b,
 Replace f ByteString ByteString, ToArray arr,
 H hint ~ Either a (f arr)) =>
a -> (String -> hint b) -> String -> String -> Either a String
replace_str String
p0 String -> Tagged Once String
forall b. b -> Tagged Once b
tagOnce


instance Replace All String String String (E String) where
    replace :: All String -> String -> String -> E String
replace (All p0 :: String
p0) = String
-> (String -> Tagged All String) -> String -> String -> E String
forall a (hint :: * -> *) b (f :: * -> *) arr a.
(Regex a, Matchf hint, RegexLike Regex b,
 Replace f ByteString ByteString, ToArray arr,
 H hint ~ Either a (f arr)) =>
a -> (String -> hint b) -> String -> String -> Either a String
replace_str String
p0 String -> Tagged All String
forall b. b -> Tagged All b
tagAll 


instance Replace Once ByteString ByteString ByteString (E ByteString) where
    replace :: Once ByteString -> ByteString -> ByteString -> E ByteString
replace (Once p0 :: ByteString
p0) = ByteString
-> (ByteString -> Tagged Once ByteString)
-> ByteString
-> ByteString
-> E ByteString
forall (hint :: * -> *) b a (f :: * -> *) repl b arr a.
(Matchf hint, RegexLike Regex b, Regex a, Replace f repl b,
 Extract' b, ToArray arr, H hint ~ Either a (f arr)) =>
a -> (b -> hint b) -> repl -> b -> Either a b
replace_bs ByteString
p0 ByteString -> Tagged Once ByteString
forall b. b -> Tagged Once b
tagOnce


instance Replace All ByteString ByteString ByteString (E ByteString)where
    replace :: All ByteString -> ByteString -> ByteString -> E ByteString
replace (All p0 :: ByteString
p0) = ByteString
-> (ByteString -> Tagged All ByteString)
-> ByteString
-> ByteString
-> E ByteString
forall (hint :: * -> *) b a (f :: * -> *) repl b arr a.
(Matchf hint, RegexLike Regex b, Regex a, Replace f repl b,
 Extract' b, ToArray arr, H hint ~ Either a (f arr)) =>
a -> (b -> hint b) -> repl -> b -> Either a b
replace_bs ByteString
p0 ByteString -> Tagged All ByteString
forall b. b -> Tagged All b
tagAll


instance Replace Once ByteString (GroupReplacer ByteString) ByteString (E ByteString) where
    replace :: Once ByteString
-> GroupReplacer ByteString -> ByteString -> E ByteString
replace (Once p0 :: ByteString
p0) = ByteString
-> (ByteString -> Tagged Once ByteString)
-> GroupReplacer ByteString
-> ByteString
-> E ByteString
forall (hint :: * -> *) b a (f :: * -> *) repl b arr a.
(Matchf hint, RegexLike Regex b, Regex a, Replace f repl b,
 Extract' b, ToArray arr, H hint ~ Either a (f arr)) =>
a -> (b -> hint b) -> repl -> b -> Either a b
replace_bs ByteString
p0 ByteString -> Tagged Once ByteString
forall b. b -> Tagged Once b
tagOnce

instance Replace All ByteString (GroupReplacer ByteString) ByteString (E ByteString) where
    replace :: All ByteString
-> GroupReplacer ByteString -> ByteString -> E ByteString
replace (All p0 :: ByteString
p0) = ByteString
-> (ByteString -> Tagged All ByteString)
-> GroupReplacer ByteString
-> ByteString
-> E ByteString
forall (hint :: * -> *) b a (f :: * -> *) repl b arr a.
(Matchf hint, RegexLike Regex b, Regex a, Replace f repl b,
 Extract' b, ToArray arr, H hint ~ Either a (f arr)) =>
a -> (b -> hint b) -> repl -> b -> Either a b
replace_bs ByteString
p0 ByteString -> Tagged All ByteString
forall b. b -> Tagged All b
tagAll
{- ^ 
@
replacer::GroupReplacer ByteString
replacer = defaultReplacer 1 tweak1
      where tweak1 bs1 = toByteString' $
                        if bs1 == toByteString "左" then
                              "ー右ー"
                               else "?"


    runFn1 \`shouldBe\` toByteString "100メートルー右ー折後、左"
        where runFn1 = let rx1 = toByteString "(?<=ル)(左)"
                           body1 = toByteString "100メートル左折後、左"
                       in replace (All rx1) replacer body1    
@    -}

replace_str :: a -> (String -> hint b) -> String -> String -> Either a String
replace_str p0 :: a
p0 tag0 :: String -> hint b
tag0 r0 :: String
r0 b0 :: String
b0 =
   let b1 :: ByteString
b1 = String -> ByteString
toByteString String
b0
       r1 :: ByteString
r1 = String -> ByteString
toByteString String
r0
       p1 :: E Regex
p1 = a -> [Comp] -> [Exec] -> E Regex
forall a. Regex a => a -> [Comp] -> [Exec] -> E Regex
T.makeRegexOpt a
p0 [Comp
Utf8] []  
   in E Regex -> hint b -> H hint
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ E Regex
p1 (String -> hint b
tag0 String
b0) Either a (f arr) -> (f arr -> Either a String) -> Either a String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma1 :: f arr
ma1 ->
    String -> Either a String
forall a b. b -> Either a b
Right (String -> Either a String) -> String -> Either a String
forall a b. (a -> b) -> a -> b
$ ByteString -> String
toString (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ f arr -> ByteString -> ByteString -> ByteString
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace f arr
ma1 ByteString
r1 ByteString
b1


replace_bs :: a -> (b -> hint b) -> repl -> b -> Either a b
replace_bs p0 :: a
p0 tag0 :: b -> hint b
tag0 r0 :: repl
r0 b0 :: b
b0 =
        E Regex -> hint b -> H hint
forall (hint :: * -> *) b.
(Matchf hint, R_ b) =>
E Regex -> hint b -> H hint
marray_ (a -> [Comp] -> [Exec] -> E Regex
forall a. Regex a => a -> [Comp] -> [Exec] -> E Regex
T.makeRegexOpt a
p0 [Comp
Utf8] []) (b -> hint b
tag0 b
b0) Either a (f arr) -> (f arr -> Either a b) -> Either a b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma1 :: f arr
ma1 ->
    b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> b -> Either a b
forall a b. (a -> b) -> a -> b
$ f arr -> repl -> b -> b
forall (f :: * -> *) repl body arr.
(Replace f repl body, Extract' body, ToArray arr) =>
f arr -> repl -> body -> body
O.replace f arr
ma1 repl
r0 b
b0