fix: aliases: Make sure we can escape forward slashes in account

aliases, but otherwise the regular expression handler handles escapes.
This commit is contained in:
Stephen Morgan 2022-03-13 19:02:44 +11:00 committed by Simon Michael
parent cbdd86f4dc
commit e6bf04fce3
2 changed files with 35 additions and 3 deletions

View File

@ -1517,15 +1517,17 @@ regexaliasp = do
-- dbgparse 0 "regexaliasp"
(off1, off2, re) <- between (char '/') (char '/') $ do
off1 <- getOffset
re <- some $ noneOf ("/\\\n\r" :: [Char]) -- paranoid: don't try to read past line end
<|> (char '\\' *> anySingle) -- allow escaping any character
re <- fmap T.concat . some $
(T.singleton <$> noneOf ("/\\\n\r" :: [Char])) -- paranoid: don't try to read past line end
<|> string "\\/" -- allow escaping forward slashes
<|> (liftM2 T.cons (char '\\') (T.singleton <$> anySingle)) -- Otherwise leave backslashes in
off2 <- getOffset
return (off1, off2, re)
skipNonNewlineSpaces
char '='
skipNonNewlineSpaces
repl <- anySingle `manyTill` eolof
case toRegexCI $ T.pack re of
case toRegexCI re of
Right r -> return $! RegexAlias r repl
Left e -> customFailure $! parseErrorAtRegion off1 off2 e

View File

@ -267,3 +267,33 @@ $ hledger -f- print
b
>=0
# 19. Make sure you can escape special regexp characters.
<
alias /\./ = :
2021-01-01
hi.there 1
b
$ hledger -f- print
2021-01-01
hi:there 1
b
>=0
# 20. Including backslashes
<
alias /\\/ = :
2021-01-01
hi\there 1
b
$ hledger -f- print
2021-01-01
hi:there 1
b
>=0