packages feed

lens-regex-pcre 0.1.0.1 → 0.1.1.0

raw patch · 5 files changed

+181/−122 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Control.Lens.Regex: grouped :: Traversal' Match [Text]
+ Control.Lens.Regex: matchAndGroups :: Getter Match (Text, [Text])

Files

ChangeLog.md view
@@ -1,3 +1,10 @@ # Changelog for lens-regex-pcre -## Unreleased changes+# 1.1.0 +Adds `grouped` and `matchAndGroups`++# 1.0.1 +Doc fixes++# 1.0.0 +Initial Release
README.md view
@@ -65,117 +65,130 @@ import Control.Lens import Control.Lens.Regex -describe "regex" $ do-    describe "match" $ do-        describe "getting" $ do-            it "should find one match" $ do-                "abc" ^.. regex [rx|b|] . match-                `shouldBe` ["b"]--            it "should find many matches" $ do-                "a b c" ^.. regex [rx|\w|] . match-                `shouldBe` ["a", "b", "c"]--            it "should fold" $ do-                "a b c" ^. regex [rx|\w|] . match-                `shouldBe` "abc"--            it "should match with a group" $ do-                "a b c" ^.. regex [rx|(\w)|] . match-                `shouldBe` ["a", "b", "c"]--            it "should match with many groups" $ do-                "a b c" ^.. regex [rx|(\w) (\w)|] . match-                `shouldBe` ["a b"]--            it "should be greedy when overlapping" $ do-                "abc" ^.. regex [rx|\w+|] . match-                `shouldBe`["abc"]--            it "should respect lazy modifiers" $ do-                "abc" ^.. regex [rx|\w+?|] . match-                `shouldBe`["a", "b", "c"]--        describe "setting" $ do-            it "should allow setting" $ do-                ("one two three" & regex [rx|two|] . match .~ "new")-                `shouldBe` "one new three"--            it "should allow setting many" $ do-                ("one <two> three" & regex [rx|\w+|] . match .~ "new")-                `shouldBe` "new <new> new"--            it "should allow mutating" $ do-                ("one two three" & regex [rx|two|] . match %~ (<> "!!"). T.toUpper)-                `shouldBe` "one TWO!! three"--            it "should allow mutating many" $ do-                ("one two three" & regex [rx|two|] . match %~ T.toUpper)-                `shouldBe` "one TWO three"--    describe "groups" $ do-        describe "getting" $ do-            it "should get a group" $ do-                "a b c" ^.. regex [rx|(\w)|] . groups-                `shouldBe` ["a", "b", "c"]--            it "should get many groups" $ do-                "one two three" ^.. regex [rx|(\w+) (\w+)|] . groups-                `shouldBe` ["one", "two"]--        describe "setting" $ do-            it "should allow setting" $ do-                ("one two three" & regex [rx|(\w+) (\w+)|] . groups .~ "new")-                `shouldBe` "new new three"--            it "should allow setting many" $ do-                ("one two three four" & regex [rx|(\w+) (\w+)|] . groups .~ "new")-                `shouldBe` "new new new new"--            it "should allow mutating" $ do-                ("one two three four" & regex [rx|one (two) three|] . groups %~ (<> "!!") . T.toUpper)-                `shouldBe` "one TWO!! three four"--            it "should allow mutating" $ do-                ("one two three four" & regex [rx|one (two) (three)|] . groups %~ (<> "!!") . T.toUpper)-                `shouldBe` "one TWO!! THREE!! four"--describe "iregex" $ do-    describe "match" $ do-        it "should allow folding with index" $ do-            ("one two three" ^.. (iregex [rx|\w+|] <. match) . withIndex)-            `shouldBe` [(0, "one"), (1, "two"), (2, "three")]--        it "should allow getting with index" $ do-            ("one two three" ^.. iregex [rx|\w+|] . index 1 . match)-            `shouldBe` ["two"]--        it "should allow setting with index" $ do-            ("one two three" & iregex [rx|\w+|] <. match .@~ pack . show)-            `shouldBe` "0 1 2"--        it "should allow mutating with index" $ do-            ("one two three" & iregex [rx|\w+|] <. match %@~ \i s -> (pack $ show i) <> ": " <> s)-            `shouldBe` "0: one 1: two 2: three"--describe "igroups" $ do-    it "should allow folding with index" $ do-        ("one two three four" ^.. regex [rx|(\w+) (\w+)|] . igroups . withIndex)-        `shouldBe` [(0, "one"), (1, "two"), (0, "three"), (1, "four")]--    it "should allow getting a specific index" $ do-        ("one two three four" ^.. regex [rx|(\w+) (\w+)|] . igroups . index 1)-        `shouldBe` ["two", "four"]--    it "should allow setting with index" $ do-        ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups .@~ pack . show)-        `shouldBe` "0 1 0 1"--    it "should allow mutating with index" $ do-        ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups %@~ \i s -> (pack $ show i) <> ": " <> s)-        `shouldBe` "0: one 1: two 0: three 1: four"--    it "should compose indices with matches" $ do-        ("one two three four" ^.. (iregex [rx|(\w+) (\w+)|] <.> igroups) . withIndex)-        `shouldBe` [((0, 0), "one"), ((0, 1), "two"), ((1, 0), "three"), ((1, 1), "four")]+describe "regex" $ do                                                                                            +    describe "match" $ do                                                                                        +        describe "getting" $ do                                                                                  +            it "should find one match" $ do                                                                      +                "abc" ^.. regex [rx|b|] . match                                                                  +                `shouldBe` ["b"]                                                                                 +                                                                                                                 +            it "should find many matches" $ do                                                                   +                "a b c" ^.. regex [rx|\w|] . match                                                               +                `shouldBe` ["a", "b", "c"]                                                                       +                                                                                                                 +            it "should fold" $ do                                                                                +                "a b c" ^. regex [rx|\w|] . match                                                                +                `shouldBe` "abc"                                                                                 +                                                                                                                 +            it "should match with a group" $ do                                                                  +                "a b c" ^.. regex [rx|(\w)|] . match                                                             +                `shouldBe` ["a", "b", "c"]                                                                       +                                                                                                                 +            it "should match with many groups" $ do                                                              +                "a b c" ^.. regex [rx|(\w) (\w)|] . match                                                        +                `shouldBe` ["a b"]                                                                               +                                                                                                                 +            it "should be greedy when overlapping" $ do                                                          +                "abc" ^.. regex [rx|\w+|] . match                                                                +                `shouldBe`["abc"]                                                                                +                                                                                                                 +            it "should respect lazy modifiers" $ do                                                              +                "abc" ^.. regex [rx|\w+?|] . match                                                               +                `shouldBe`["a", "b", "c"]                                                                        +                                                                                                                 +        describe "setting" $ do                                                                                  +            it "should allow setting" $ do                                                                       +                ("one two three" & regex [rx|two|] . match .~ "new")                                             +                `shouldBe` "one new three"                                                                       +                                                                                                                 +            it "should allow setting many" $ do                                                                  +                ("one <two> three" & regex [rx|\w+|] . match .~ "new")                                           +                `shouldBe` "new <new> new"                                                                       +                                                                                                                 +            it "should allow mutating" $ do                                                                      +                ("one two three" & regex [rx|two|] . match %~ (<> "!!"). T.toUpper)                              +                `shouldBe` "one TWO!! three"                                                                     +                                                                                                                 +            it "should allow mutating many" $ do                                                                 +                ("one two three" & regex [rx|two|] . match %~ T.toUpper)                                         +                `shouldBe` "one TWO three"                                                                       +                                                                                                                 +    describe "groups" $ do                                                                                       +        describe "getting" $ do                                                                                  +            it "should get a group" $ do                                                                         +                "a b c" ^.. regex [rx|(\w)|] . groups                                                            +                `shouldBe` ["a", "b", "c"]                                                                       +                                                                                                                 +            it "should get many groups" $ do                                                                     +                "one two three" ^.. regex [rx|(\w+) (\w+)|] . groups                                             +                `shouldBe` ["one", "two"]                                                                        +                                                                                                                 +        describe "setting" $ do                                                                                  +            it "should allow setting" $ do                                                                       +                ("one two three" & regex [rx|(\w+) (\w+)|] . groups .~ "new")                                    +                `shouldBe` "new new three"                                                                       +                                                                                                                 +            it "should allow setting many" $ do                                                                  +                ("one two three four" & regex [rx|(\w+) (\w+)|] . groups .~ "new")                               +                `shouldBe` "new new new new"                                                                     +                                                                                                                 +            it "should allow mutating" $ do                                                                      +                ("one two three four" & regex [rx|one (two) three|] . groups %~ (<> "!!") . T.toUpper)           +                `shouldBe` "one TWO!! three four"                                                                +                                                                                                                 +            it "should allow mutating" $ do                                                                      +                ("one two three four" & regex [rx|one (two) (three)|] . groups %~ (<> "!!") . T.toUpper)         +                `shouldBe` "one TWO!! THREE!! four"                                                              +                                                                                                                 +describe "iregex" $ do                                                                                           +    describe "match" $ do                                                                                        +        it "should allow folding with index" $ do                                                                +            ("one two three" ^.. (iregex [rx|\w+|] <. match) . withIndex)                                        +            `shouldBe` [(0, "one"), (1, "two"), (2, "three")]                                                    +                                                                                                                 +        it "should allow getting with index" $ do                                                                +            ("one two three" ^.. iregex [rx|\w+|] . index 1 . match)                                             +            `shouldBe` ["two"]                                                                                   +                                                                                                                 +        it "should allow setting with index" $ do                                                                +            ("one two three" & iregex [rx|\w+|] <. match .@~ T.pack . show)                                      +            `shouldBe` "0 1 2"                                                                                   +                                                                                                                 +        it "should allow mutating with index" $ do                                                               +            ("one two three" & iregex [rx|\w+|] <. match %@~ \i s -> (T.pack $ show i) <> ": " <> s)             +            `shouldBe` "0: one 1: two 2: three"                                                                  +                                                                                                                 +describe "igroups" $ do                                                                                          +    it "should allow folding with index" $ do                                                                    +        ("one two three four" ^.. regex [rx|(\w+) (\w+)|] . igroups . withIndex)                                 +        `shouldBe` [(0, "one"), (1, "two"), (0, "three"), (1, "four")]                                           +                                                                                                                 +    it "should allow getting a specific index" $ do                                                              +        ("one two three four" ^.. regex [rx|(\w+) (\w+)|] . igroups . index 1)                                   +        `shouldBe` ["two", "four"]                                                                               +                                                                                                                 +    it "should allow setting with index" $ do                                                                    +        ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups .@~ T.pack . show)                             +        `shouldBe` "0 1 0 1"                                                                                     +                                                                                                                 +    it "should allow mutating with index" $ do                                                                   +        ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups %@~ \i s -> (T.pack $ show i) <> ": " <> s)    +        `shouldBe` "0: one 1: two 0: three 1: four"                                                              +                                                                                                                 +    it "should compose indices with matches" $ do                                                                +        ("one two three four" ^.. (iregex [rx|(\w+) (\w+)|] <.> igroups) . withIndex)                            +        `shouldBe` [((0, 0), "one"), ((0, 1), "two"), ((1, 0), "three"), ((1, 1), "four")]                       +                                                                                                                 +describe "grouped" $ do                                                                                          +    it "should get all groups in batches" $ do                                                                   +        "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . grouped                    +        `shouldBe` [["raindrops","roses"],["whiskers","kittens"]]                                                +    it "should allow editing when result list is the same length" $ do                                           +        ("raindrops on roses and whiskers on kittens" & regex [rx|(\w+) on (\w+)|] . grouped %~ reverse)         +        `shouldBe` "roses on raindrops and kittens on whiskers"                                                  +                                                                                                                 +describe "matchAndGroups" $ do                                                                                   +    it "should get match and groups" $ do                                                                        +        "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . matchAndGroups             +        `shouldBe` [("raindrops on roses",["raindrops","roses"]),("whiskers on kittens",["whiskers","kittens"])]  ```
lens-regex-pcre.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 99b9284dc756fdcf7fac76739cacfef2e68b962c0addd32d8a322a687fed38c7+-- hash: 847ca083cc4bffe2ad8319d0811ffdd549a436724924f1106c25966743c16fc6  name:           lens-regex-pcre-version:        0.1.0.1+version:        0.1.1.0 description:    Please see the README on GitHub at <https://github.com/ChrisPenner/lens-regex-pcre#readme> homepage:       https://github.com/ChrisPenner/lens-regex-pcre#readme bug-reports:    https://github.com/ChrisPenner/lens-regex-pcre/issues
src/Control/Lens/Regex.hs view
@@ -21,6 +21,8 @@     , match     , groups     , igroups+    , grouped+    , matchAndGroups      -- * QuasiQuoter     , rx@@ -58,10 +60,24 @@ igroups :: IndexedTraversal' Int Match T.Text igroups = indexing groups --- | traverse each group within a match. See 'igroups' for selecting specific groups.+-- | traverse each group within a match. See 'igroups' for selecting specific groups or+-- 'grouped' for handling all groups at once. groups :: Traversal' Match T.Text groups = traversed . _Right +-- | Access all groups of a match at once.+--+-- Note that this uses 'partsOf'; and is only a valid traversal if you don't+-- alter the length of the list. It is valid as a Fold or Getter however.+--+-- > > "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . grouped+-- > [["raindrops","roses"],["whiskers","kittens"]]+--+-- > > "raindrops on roses and whiskers on kittens" & regex [rx|(\w+) on (\w+)|] . grouped %~ reverse+-- > "roses on raindrops and kittens on whiskers"+grouped :: Traversal' Match [T.Text]+grouped = partsOf (traversed . _Right)+ -- | Traverse each match as a whole -- -- Use with 'regex' or 'iregex'@@ -132,6 +148,15 @@     collapse xs = xs ^. folded . beside id (traversed . chosen)     -- apply :: [Either Text [Either Text Text]] -> _ [Either Text [Either Text Text]]     apply xs = xs & traversed . _Right %%~ f++-- | Collect both the match text AND all the matching groups+--+-- > > "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . matchAndGroups+-- > [ ("raindrops on roses", ["raindrops","roses"])+-- > , ("whiskers on kittens", ["whiskers","kittens"])+-- > ]+matchAndGroups :: Getter Match (T.Text, [T.Text])+matchAndGroups = to $ \m -> (m ^. traversed . chosen, m ^. grouped)  splitter :: Text -> [(MatchRange, GroupRanges)] -> [Either T.Text (T.Text, GroupRanges)] splitter t [] | T.null t = []
test/Spec.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} import Control.Lens import Control.Lens.Regex-import Data.Text as T hiding (index)+import qualified Data.Text as T import Test.Hspec  main :: IO ()@@ -93,11 +93,11 @@                 `shouldBe` ["two"]              it "should allow setting with index" $ do-                ("one two three" & iregex [rx|\w+|] <. match .@~ pack . show)+                ("one two three" & iregex [rx|\w+|] <. match .@~ T.pack . show)                 `shouldBe` "0 1 2"              it "should allow mutating with index" $ do-                ("one two three" & iregex [rx|\w+|] <. match %@~ \i s -> (pack $ show i) <> ": " <> s)+                ("one two three" & iregex [rx|\w+|] <. match %@~ \i s -> (T.pack $ show i) <> ": " <> s)                 `shouldBe` "0: one 1: two 2: three"      describe "igroups" $ do@@ -110,13 +110,27 @@             `shouldBe` ["two", "four"]          it "should allow setting with index" $ do-            ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups .@~ pack . show)+            ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups .@~ T.pack . show)             `shouldBe` "0 1 0 1"          it "should allow mutating with index" $ do-            ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups %@~ \i s -> (pack $ show i) <> ": " <> s)+            ("one two three four" & regex [rx|(\w+) (\w+)|] . igroups %@~ \i s -> (T.pack $ show i) <> ": " <> s)             `shouldBe` "0: one 1: two 0: three 1: four"          it "should compose indices with matches" $ do             ("one two three four" ^.. (iregex [rx|(\w+) (\w+)|] <.> igroups) . withIndex)             `shouldBe` [((0, 0), "one"), ((0, 1), "two"), ((1, 0), "three"), ((1, 1), "four")]++    describe "grouped" $ do+        it "should get all groups in batches" $ do+            "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . grouped+            `shouldBe` [["raindrops","roses"],["whiskers","kittens"]]+        it "should allow editing when result list is the same length" $ do+            ("raindrops on roses and whiskers on kittens" & regex [rx|(\w+) on (\w+)|] . grouped %~ reverse)+            `shouldBe` "roses on raindrops and kittens on whiskers"++    describe "matchAndGroups" $ do+        it "should get match and groups" $ do+            "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . matchAndGroups+            `shouldBe` [("raindrops on roses",["raindrops","roses"]),("whiskers on kittens",["whiskers","kittens"])]+