packages feed

regex-do 3.1 → 3.2

raw patch · 4 files changed

+36/−21 lines, 4 files

Files

changelog.md view
@@ -1,3 +1,6 @@+#####   3.2+  fix bug in defaultReplacer  + #####   3.1   tidy up types:      
regex-do.cabal view
@@ -1,5 +1,5 @@ name:                regex-do-version:             3.1+version:             3.2 synopsis:            PCRE wrapper description:         format, search, replace (String | ByteString) with PCRE regex. Utf8-safe author:              Imants Cekusins@@ -43,15 +43,14 @@              ghc-options:  -fwarn-unused-imports     -  build-depends:  base <= 5.0,-              regex-base,-              regex-pcre,-              stringsearch,-              bytestring,-              tagged,-              array,-              text-+  build-depends:  base > 4.7 && <= 5.0,+                  array               >= 0.5.0,+                  bytestring          >= 0.10.0,+                  regex-base          >= 0.93.0,+                  regex-pcre          >= 0.94.0,+                  stringsearch        >= 0.3.0,+                  tagged              >= 0.8.0,+                  text                >= 1.2.0    hs-source-dirs:      src   default-language:    Haskell2010
src/Text/Regex/Do/Replace/Open.hs view
@@ -31,8 +31,8 @@     (Replace(..),     defaultReplacer,     getGroup,-    replaceMatch-    )+    replaceMatch,+    boundsOk)     where  import Text.Regex.Base.RegexLike as R@@ -44,8 +44,6 @@ import Text.Regex.Do.Type.Extract  -- class Replace f repl body where    replace::(Extract' body, ToArray arr) =>         f arr -> repl -> body -> body@@ -100,17 +98,27 @@     another custom dynamic replacer could e.g.     inspect all group matches before looking up a replacement.     -} defaultReplacer::Extract' a =>-        Int         -- ^ group idx. 1-based+        Int         -- ^ group idx. 0: full match, groups: 1.. see 'MatchArray'         -> (a -> a) -- ^ (group match -> replacement) lookup             -> GroupReplacer a defaultReplacer idx0 tweak0 = GroupReplacer fn1-    where fn1 (ma0::MatchArray) acc0 = maybe acc0 fn1 mval1-                where pl1 = ma0 A.! idx0 :: (R.MatchOffset, R.MatchLength)-                      mval1 = getGroup acc0 ma0 idx0-                      fn1 str1 = replaceMatch pl1 (str2, acc0)-                                 where str2 = tweak0 str1+    where fn1 (ma0::MatchArray) acc0 =+            if boundsOk ma0 idx0 then maybe acc0 fn1 mval1+            else acc0+            where pl1 = ma0 A.! idx0 :: (R.MatchOffset, R.MatchLength)+                  mval1 = getGroup acc0 ma0 idx0 +                  fn1 str1 = replaceMatch pl1 (str2, acc0) +                             where str2 = tweak0 str1  +{- | check if specified group index is within 'MatchArray' bounds++for use within 'GroupReplacer'+-}+boundsOk::MatchArray -> Int -> Bool+boundsOk ma0 = inRange (bounds ma0)                         ++ {- | get group content safely:      * non-existing group idx will not error but return 'Nothing'@@ -120,7 +128,7 @@     -} getGroup::R.Extract a =>     ReplaceAcc a -> MatchArray -> Int -> Maybe a-getGroup acc0 ma0 idx0 = if idx0 < 1 || idx0 > P.length ma0 then Nothing     --  safety catch+getGroup acc0 ma0 idx0 = if not (boundsOk ma0 idx0) then Nothing     --  safety catch     else Just val1     where pl1 = ma0 A.! idx0 :: (R.MatchOffset, R.MatchLength)           pl2 = adjustPoslen pl1 acc0
test/TestRegex/TestReplaceOpen.hs view
@@ -18,10 +18,15 @@             O.replace (Just [(4,3)::PosLen]) replacer ("abc 123 def"::Text) `shouldBe` ("abc [1-2-3] def"::Text)           it "case 3" $             O.replace ([[(4,3)::PosLen]]) replacer ("abc 123 def"::Text) `shouldBe` ("abc [1-2-3] def"::Text)+          it "case 4" $+            O.replace ([[]::[PosLen]]) replacer ("abc 123 def"::Text) `shouldBe` ("abc 123 def"::Text)+          it "case 5" $+            O.replace ([[(4,3)],[(8,3)],[(16,3)]]::[[PosLen]]) replacer ("abc 456 456 def 456"::Text) `shouldBe` ("abc 4 4 def 4"::Text)   replacer::GroupReplacer Text replacer = defaultReplacer 1 tweak1           where tweak1 str1 = case str1 of                                 "123" -> "[1-2-3]"+                                "456" -> "4"                                 otherwise -> traceShow str1 "?"