diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2022-07-16 21:28:37 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2022-07-16 21:35:48 +0200 |
commit | 7cefecfc3d491ae668a32cbc89668a055c0268de (patch) | |
tree | 536d84644835f7a9c06e9e90c5f99aa11e85f9a8 | |
parent | 00da3541b7bf1b01de543db8283e9fd88634a903 (diff) |
Do not escape alphanumeric chars for regexes
It is not needed and some of the backslashed variants actually have
special meaning.
-rw-r--r-- | src/Test.hs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/Test.hs b/src/Test.hs index e21eaee..25f3c09 100644 --- a/src/Test.hs +++ b/src/Test.hs @@ -14,6 +14,7 @@ module Test ( import Control.Monad +import Data.Char import Data.List import Data.Text (Text) import qualified Data.Text as T @@ -74,7 +75,11 @@ eval (Concat xs) = T.concat <$> mapM eval xs eval (Regex xs) = do parts <- forM xs $ \case StringLit str -> return str - expr -> T.concatMap (\c -> T.pack ['\\', c]) <$> eval expr + expr -> T.concatMap escapeChar <$> eval expr + where + escapeChar c | isAlphaNum c = T.singleton c + | c `elem` "`'<>" = T.singleton c + | otherwise = T.pack ['\\', c] case compile defaultCompOpt defaultExecOpt $ T.concat $ concat [[T.singleton '^'], parts, [T.singleton '$']] of Left err -> fail err Right re -> return re |