git.iko.soy/typeable/pandoc
on master help sign in
Use strict instead of lazy sum.

sum is lazy; replace with `foldl' (+) 0` to avoid stack overflow in Text.Pandoc.Pretty with very long strings. Closes #5401.

commit7fa5fbed9e0440694e9c52a8dc83f3e4f29c8a96 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 28 Mar 2019, 13:51 (8 years ago) parentb87a3ef
2 files changed+5−5
src/Text/Pandoc/Pretty.hs+3−3 src/Text/Pandoc/Writers/Shared.hs+2−2
src/Text/Pandoc/Pretty.hs+3−3
@@ -65,7 +65,7 @@ import Control.Monad
6565import Control.Monad.State.Strict
6666import Data.Char (isSpace)
6767import Data.Foldable (toList)
68−import Data.List (intersperse)
68+import Data.List (intersperse, foldl')
6969import Data.Sequence (Seq, ViewL (..), fromList, mapWithIndex, singleton, viewl,
7070 (<|))
7171import qualified Data.Sequence as Seq
@@ -305,7 +305,7 @@ renderList (BreakingSpace : xs) = do
305305 let xs' = dropWhile isBreakingSpace xs
306306 let next = takeWhile isText xs'
307307 st <- get
308− let off = sum $ map offsetOf next
308+ let off = foldl' (+) 0 $ map offsetOf next
309309 case lineLength st of
310310 Just l | column st + 1 + off > l -> do
311311 outp (-1) "\n"
@@ -540,4 +540,4 @@ charWidth c =
540540-- | Get real length of string, taking into account combining and double-wide
541541-- characters.
542542realLength :: String -> Int
543−realLength = sum . map charWidth
543+realLength = foldl' (+) 0 . map charWidth
src/Text/Pandoc/Writers/Shared.hs+2−2
@@ -41,7 +41,7 @@ import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
4141 encode, fromJSON)
4242import Data.Char (chr, ord, isSpace, isDigit)
4343import qualified Data.HashMap.Strict as H
44−import Data.List (groupBy, intersperse, transpose)
44+import Data.List (groupBy, intersperse, transpose, foldl')
4545import qualified Data.Map as M
4646import Data.Maybe (isJust)
4747import qualified Data.Text as T
@@ -279,7 +279,7 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
279279 -- handleGivenWidths
280280 let handleZeroWidths = do
281281 (widthsInChars', rawHeaders', rawRows') <- handleFullWidths
282− if sum widthsInChars' > writerColumns opts
282+ if foldl' (+) 0 widthsInChars' > writerColumns opts
283283 then -- use even widths
284284 handleGivenWidths
285285 (replicate numcols (1.0 / fromIntegral numcols) :: [Double])