From e9dd77e82b98d27bcbb2d9e8e73e6dd8e3e9d1db Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Wed, 15 Dec 2021 15:58:27 +1100 Subject: [PATCH] fix: Ensure head and tail are not called on empty account names. --- hledger-lib/Hledger/Data/Posting.hs | 4 ++-- hledger-lib/Hledger/Utils/Text.hs | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index 13873a9d8..c74697700 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -404,8 +404,8 @@ accountNamePostingType a accountNameWithoutPostingType :: AccountName -> AccountName accountNameWithoutPostingType a = case accountNamePostingType a of - BalancedVirtualPosting -> T.init $ T.tail a - VirtualPosting -> T.init $ T.tail a + BalancedVirtualPosting -> textUnbracket a + VirtualPosting -> textUnbracket a RegularPosting -> a accountNameWithPostingType :: PostingType -> AccountName -> AccountName diff --git a/hledger-lib/Hledger/Utils/Text.hs b/hledger-lib/Hledger/Utils/Text.hs index e14517699..a91f5433f 100644 --- a/hledger-lib/Hledger/Utils/Text.hs +++ b/hledger-lib/Hledger/Utils/Text.hs @@ -169,15 +169,17 @@ stripquotes s = if isSingleQuoted s || isDoubleQuoted s then T.init $ T.tail s e isSingleQuoted :: Text -> Bool isSingleQuoted s = - T.length (T.take 2 s) == 2 && T.head s == '\'' && T.last s == '\'' + T.length s >= 2 && T.head s == '\'' && T.last s == '\'' isDoubleQuoted :: Text -> Bool isDoubleQuoted s = - T.length (T.take 2 s) == 2 && T.head s == '"' && T.last s == '"' + T.length s >= 2 && T.head s == '"' && T.last s == '"' textUnbracket :: Text -> Text textUnbracket s - | (T.head s == '[' && T.last s == ']') || (T.head s == '(' && T.last s == ')') = T.init $ T.tail s + | T.null s = s + | T.head s == '[' && T.last s == ']' = T.init $ T.tail s + | T.head s == '(' && T.last s == ')' = T.init $ T.tail s | otherwise = s -- | Join several multi-line strings as side-by-side rectangular strings of the same height, top-padded.