hs-re (empty) → 0.1.0
raw patch · 5 files changed
+147/−0 lines, 5 filesdep +arraydep +basedep +regex-basesetup-changed
Dependencies added: array, base, regex-base, regex-posix
Files
- LICENSE +20/−0
- README.md +23/−0
- Setup.hs +2/−0
- hs-re.cabal +27/−0
- src/Re.hs +75/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Hiroki Sato++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,23 @@+# Re++Easy to use Regex for Haskell++```hs+import Re+import Data.Array ((!))++main :: IO ()+main = do+ putStrLn $ show $ matchCount "abc" "abcde abcde"+ -- 2+ putStrLn $ show $ matchTest "abc" "abcde abcde"+ -- True+ putStrLn $ concatMap (\m -> fst $ fst m ) $ matchAll "a(b.c)" "abbcdeabmcde"+ -- abbcabmc+ putStrLn $ replace "abc" "abcde abcde" ""+ -- de de+ putStrLn $ replace "(b)(c)" "abcd abcd" "--\\1--\\2--"+ -- a--b--c--d a--b--c--d+ putStrLn $ replaceMap "(b)(c)" "abcd abcd" (\ps -> "--" ++ (ps!!1) ++ "--" ++ (ps!!2) ++ "--")+ -- a--b--c--d a--b--c--d+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hs-re.cabal view
@@ -0,0 +1,27 @@+-- Initial hs-replace.cabal generated by cabal init. For further+-- documentation, see http://haskell.org/cabal/users-guide/++name: hs-re+version: 0.1.0+synopsis: Easy to use Regex+description: https://github.com/SKAhack/hs-re+license: MIT+license-file: LICENSE+author: Hiroki Sato+maintainer: m@skahack.com+-- copyright:+-- category:+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: Re+ -- other-modules:+ -- other-extensions:+ build-depends: base >=4.7 && <4.8,+ regex-base >= 0.93.0,+ regex-posix >= 0.95.0,+ array >= 0.5.0.0+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Re.hs view
@@ -0,0 +1,75 @@+module Re (+ matchOnce,+ matchAll,+ matchCount,+ matchTest,+ replace,+ replaceMap+) where++import qualified Text.Regex.Base as B+ ( RegexMaker(..)+ , RegexLike(..)+ , RegexContext(..)+ , defaultExecOpt+ , MatchText+ , MatchArray+ )+import Text.Regex.Posix as P+ ( Regex+ , compNewline+ , compIgnoreCase+ , compExtended+ )+import Data.Array ((!), indices, bounds, elems)++makeRegex :: String -> Regex+makeRegex = B.makeRegex++matchOnce :: String -> String -> Maybe (String, B.MatchText String, String)+matchOnce r s = B.matchOnceText (makeRegex r) s++matchAll :: String -> String -> [((String, Int), [String])]+matchAll r s = do+ let matches = B.matchAllText (makeRegex r) s+ map (\m -> ((substr m, count m), values m)) matches+ where+ values m = map (\n -> fst (m!n)) (drop 1 $ indices m)+ count m = snd $ bounds m+ substr m = fst (m!0)++matchCount :: String -> String -> Int+matchCount r s = B.matchCount (makeRegex r) s++matchTest :: String -> String -> Bool+matchTest r s = B.matchTest (makeRegex r) s++replace :: String -> String -> String -> String+replace r s m = replaceMap r s (func m)+ where+ func tmpl ps =+ compile "\\\\([0-9])" tmpl ps++compile :: String -> String -> [String] -> String+compile r s m = replaceMap r s (func m)+ where+ func ms ps = ms !! (read $ (ps!!1) :: Int)++replaceMap :: String -> String -> ([String] -> String) -> String+replaceMap r s f = do+ let matches = B.matchAllText (makeRegex r) s+ let splited = _split 0 s (map (\m -> m!0) matches)+ concat $ _replacer splited matches f+ where+ _replacer [] _ _ = []+ _replacer s [] _ = s+ _replacer ss ms f = do+ let ps = map (\m -> fst m) $ elems (head ms)+ [head ss] ++ [f ps] ++ (_replacer (tail ss) (tail ms) f)++ _split _ s [] = [s]+ _split i s m = do+ let (offset, len) = snd (head m)+ let pre = take (offset - i) s+ let left = drop (offset + len - i) s+ pre : ( _split (offset + len) left (tail m) )