From 7cefecfc3d491ae668a32cbc89668a055c0268de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 16 Jul 2022 21:28:37 +0200 Subject: Do not escape alphanumeric chars for regexes It is not needed and some of the backslashed variants actually have special meaning. --- src/Test.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') 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 -- cgit v1.2.3