Skip to content

Commit

Permalink
Add escaping functions (fixes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdornan committed Mar 5, 2017
1 parent 68ae0ca commit e1d229a
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 40 deletions.
21 changes: 21 additions & 0 deletions Text/RE/Internal/EscapeREString.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Text.RE.Internal.EscapeREString where

escapeREString :: String -> String
escapeREString = foldr esc []
where
esc c t | isMetaChar c = '\\' : c : t
| otherwise = c : t

isMetaChar :: Char -> Bool
isMetaChar c = case c of
'*' -> True
'?' -> True
'+' -> True
'[' -> True
']' -> True
'{' -> True
'}' -> True
'$' -> True
'^' -> True
'.' -> True
_ -> False
6 changes: 6 additions & 0 deletions Text/RE/PCRE/RE.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module Text.RE.PCRE.RE
, defaultOptions
, unpackSimpleRegexOptions
, compileRegex
, escape
, escapeREString
) where

import Data.Bits
Expand All @@ -50,6 +52,7 @@ import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Prelude.Compat
import Text.RE
import Text.RE.Internal.EscapeREString
import Text.RE.Internal.NamedCaptures
import Text.RE.Internal.PreludeMacros
import Text.RE.Internal.QQ
Expand Down Expand Up @@ -243,3 +246,6 @@ preludeSources = preludeMacroSources PCRE

preludeSource :: PreludeMacro -> String
preludeSource = preludeMacroSource PCRE

escape :: (String->String) -> String -> RE
escape f = unsafeCompileRegex () . f . escapeREString
6 changes: 6 additions & 0 deletions Text/RE/TDFA/RE.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ module Text.RE.TDFA.RE
, preludeSource
, unpackSimpleRegexOptions
, compileRegex
, escape
, escapeREString
) where

import Data.Functor.Identity
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Prelude.Compat
import Text.RE
import Text.RE.Internal.EscapeREString
import Text.RE.Internal.NamedCaptures
import Text.RE.Internal.PreludeMacros
import Text.RE.Internal.QQ
Expand Down Expand Up @@ -239,3 +242,6 @@ preludeSources = preludeMacroSources TDFA

preludeSource :: PreludeMacro -> String
preludeSource = preludeMacroSource TDFA

escape :: (String->String) -> String -> RE
escape f = unsafeCompileRegex () . f . escapeREString

0 comments on commit e1d229a

Please sign in to comment.