From 9694b1b6ae3763c44d3b1361b5faa0a7b27e77a9 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sun, 13 Oct 2019 04:53:58 -0400 Subject: Remove trailing whitespace in the to-hash content. --- Codec/Encryption/OpenPGP/ASCIIArmor/Utils.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Utils.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Utils.hs index f1068fe..e36b8db 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor/Utils.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Utils.hs @@ -14,11 +14,23 @@ import qualified Data.ByteString.Char8 as BC8 import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BLC8 import Data.List (intersperse) +import Data.Char (isSpace) + +removeTrailingSpaces :: ByteString -> ByteString +removeTrailingSpaces bs = fst $ BC8.spanEnd isSpace bs + +toLast :: (a -> a) -> [a] -> [a] +toLast _ [] = [] +toLast f [x] = [f x] +toLast f (x:xs) = x : toLast f xs + +removeTrailingSpacesLazy :: BL.ByteString -> BL.ByteString +removeTrailingSpacesLazy bs = BL.fromChunks $ toLast removeTrailingSpaces $ BL.toChunks bs crlfUnlines :: [ByteString] -> ByteString crlfUnlines [] = B.empty -crlfUnlines ss = B.concat $ intersperse (BC8.pack "\r\n") ss +crlfUnlines ss = B.concat $ intersperse (BC8.pack "\r\n") $ map removeTrailingSpaces ss crlfUnlinesLazy :: [BL.ByteString] -> BL.ByteString crlfUnlinesLazy [] = BL.empty -crlfUnlinesLazy ss = BL.concat $ intersperse (BLC8.pack "\r\n") ss +crlfUnlinesLazy ss = BL.concat $ intersperse (BLC8.pack "\r\n") $ map removeTrailingSpacesLazy ss -- cgit v1.2.3