packages feed

regex-do 1.4 → 1.5

raw patch · 5 files changed

+50/−26 lines, 5 files

Files

regex-do.cabal view
@@ -1,5 +1,5 @@ name:                regex-do-version:             1.4+version:             1.5 synopsis:            PCRE wrapper description:         format, search, replace (String | ByteString) with PCRE regex. Utf8-safe author:              Imants Cekusins
src/Text/Regex/Do/Pcre/Match.hs view
@@ -7,26 +7,47 @@ import Text.Regex.Do.Pcre.Option as O import Text.Regex.Do.TypeRegex import Data.ByteString+import Text.Regex.Do.Pcre.Result  -{- | see "Text.Regex.Do.Pcre.Result"-    to convert 'MatchArray' to something useful-    -}+{- | 'match' covers all 'ExplicitMatch' funs +    compiler looks up the appropriate function depending on the result type    -}+class ExplicitMatch n h => Match n h out where+    match::Pattern n -> Body h -> out++-- | 'matchOnce'+instance ExplicitMatch n h => Match n h [h] where match = matchOnce+-- | 'matchOnce''+instance ExplicitMatch n h => Match n h (Maybe MatchArray) where match = matchOnce'+-- | 'matchTest'+instance ExplicitMatch n h => Match n h Bool where match = matchTest+-- | 'matchAll'+instance ExplicitMatch n h => Match n h [[h]] where match = matchAll+-- | 'matchAll''+instance ExplicitMatch n h => Match n h [MatchArray] where match = matchAll'++ class Rx_ n h =>-            Match n h where+            ExplicitMatch n h where -   matchOnce::Pattern n -> Body h -> Maybe MatchArray-   matchOnce r0 (Body b0) = R.matchOnce (r_ r0) b0+   matchOnce::Pattern n -> Body h -> [h]      -- ^ matched content+   matchOnce r0 b0 = maybe [] id $ allMatches b0 $ matchOnce' r0 b0 +   matchOnce'::Pattern n -> Body h -> Maybe MatchArray  -- ^ see "Text.Regex.Do.Pcre.Result"+   matchOnce' r0 (Body b0) = R.matchOnce (r_ r0) b0+    matchTest::Pattern n -> Body h -> Bool    matchTest r0 (Body b0) = R.matchTest (r_ r0) b0 -   matchAll::Pattern n -> Body h -> [MatchArray]-   matchAll r0 (Body b0) = R.matchAll (r_ r0) b0+   matchAll::Pattern n -> Body h -> [[h]]       -- ^ matched content+   matchAll r0 b0 = allMatches b0 $ matchAll' r0 b0 +   matchAll'::Pattern n -> Body h -> [MatchArray]       -- ^ see "Text.Regex.Do.Pcre.Result"+   matchAll' r0 (Body b0) = R.matchAll (r_ r0) b0  + -- | tweak Regex with options makeRegexOpts::Opt_ n =>     [O.Comp] -> [O.Exec] -> Pattern n -> Regex@@ -42,17 +63,17 @@      True -}-instance Match String String+instance ExplicitMatch String String -- | accepts regex 'String'-instance Match String ByteString+instance ExplicitMatch String ByteString -- | accepts regex 'ByteString'-instance Match ByteString ByteString+instance ExplicitMatch ByteString ByteString -- | accepts regex 'ByteString'-instance Match ByteString String+instance ExplicitMatch ByteString String -- | accepts 'Regex' made with 'makeRegexOpts'-instance Match Regex String+instance ExplicitMatch Regex String -- | accepts 'Regex' made with 'makeRegexOpts'-instance Match Regex ByteString+instance ExplicitMatch Regex ByteString   
src/Text/Regex/Do/Pcre/Replace.hs view
@@ -117,7 +117,7 @@ ronce::Mr_ a =>     (Pattern Regex, Replacement a) -> Body a -> a ronce (pat1, Replacement repl1) h1@(Body h0) =-      let pl2 = let m1 = matchOnce pat1 h1+      let pl2 = let m1 = matchOnce' pat1 h1                 in poslen m1       in case pl2 of          Nothing -> h0@@ -127,7 +127,7 @@ rall::Mr_ a =>     (Pattern Regex, Replacement a) -> Body a -> a rall (pat1, Replacement repl1) h1@(Body h0) =-      let lpl1 = let m1 = matchAll pat1 h1+      let lpl1 = let m1 = matchAll' pat1 h1                  in poslen m1::[[PosLen]]           foldFn1 lpl1 acc1 = firstGroup lpl1 (repl1,acc1)       in P.foldr foldFn1 h0 lpl1@@ -174,10 +174,10 @@ adjustPoslen (p0,l0) acc0  = (p0 + pos_adj acc0, l0)  -ronceGroup::Match Regex a =>+ronceGroup::ExplicitMatch Regex a =>     Pattern Regex -> GroupReplacer a -> Body a -> a ronceGroup pat0 repl0 h1@(Body h0) =-     let m1 = matchOnce pat0 h1::Maybe MatchArray+     let m1 = matchOnce' pat0 h1::Maybe MatchArray      in case m1 of             Nothing -> h0             Just ma1 -> let a1 = ReplaceAcc {@@ -187,10 +187,10 @@                         in acc $ repl0 ma1 a1  -rallGroup::Match Regex a =>+rallGroup::ExplicitMatch Regex a =>     Pattern Regex -> GroupReplacer a -> Body a -> a rallGroup pat0 repl0 b1@(Body b0) =-    let ma1 = matchAll pat0 b1::[MatchArray]+    let ma1 = matchAll' pat0 b1::[MatchArray]         acc1 = ReplaceAcc { acc = b0, pos_adj = 0 }     in acc $ P.foldl (flip repl0) acc1 ma1 @@ -230,4 +230,4 @@ isUtf8 case0 = Utf8 `P.elem` case0  -type Mr_ a = (Match Regex a, Replace_ a, Opt_ a)+type Mr_ a = (ExplicitMatch Regex a, Replace_ a, Opt_ a)
test/TestRegex/TestPcre.hs view
@@ -11,10 +11,10 @@  main::IO() main = do-   test " ?String " Pattern id M.matchOnce-   test " [String] " Pattern id M.matchAll-   test " ?Bs " (Pattern . b) (b <$>) M.matchOnce-   test " [Bs] " (Pattern . b) (b <$>) M.matchAll+   test " ?String " Pattern id M.matchOnce'+   test " [String] " Pattern id M.matchAll'+   test " ?Bs " (Pattern . b) (b <$>) M.matchOnce'+   test " [Bs] " (Pattern . b) (b <$>) M.matchAll'    hspec $ do     describe " matchTest " $ do      it " matchTest " $ do
test/TestRegex/TestReplace.hs view
@@ -31,6 +31,9 @@             replace [Once,Utf8] (Pattern "менее") (Replacement  "более") (Body "менее менее")                 `shouldBe` "более менее"           it "replace 4" $ do+            replace [All,Utf8] (Pattern "менее") (Replacement  "боле") (Body "менее менее")+                `shouldBe` "боле боле"+          it "replace 5" $ do             replace [Once,Utf8] (Pattern "^a\\s") (Replacement "A") (Body "a bc хол.гор.")                 `shouldBe` "Abc хол.гор."