From 9bf5228fdc1937f44901a945553eea3cb0f14faa Mon Sep 17 00:00:00 2001 From: David Feuer Date: Wed, 1 Oct 2014 23:34:29 +0200 Subject: Use dropWhileEndLE p instead of reverse . dropWhile p . reverse Summary: Using `dropWhileEndLE` tends to be faster and easier to read than the `reverse . dropWhile p . reverse` idiom. This also cleans up some other, nearby, messes. Fix #9616 (incorrect number formatting potentially leading to incorrect numbers in output). Test Plan: Run validate Reviewers: thomie, rwbarton, nomeata, austin Reviewed By: nomeata, austin Subscribers: simonmar, ezyang, carter, thomie Projects: #ghc Differential Revision: https://phabricator.haskell.org/D259 GHC Trac Issues: #9623, #9616 Conflicts: compiler/basicTypes/OccName.lhs --- utils/hpc/HpcMarkup.hs | 25 +++++++++++++++++++------ utils/hpc/HpcUtils.hs | 4 ++++ 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'utils/hpc') diff --git a/utils/hpc/HpcMarkup.hs b/utils/hpc/HpcMarkup.hs index 8fd9e4226c..c294b6a94e 100644 --- a/utils/hpc/HpcMarkup.hs +++ b/utils/hpc/HpcMarkup.hs @@ -140,6 +140,16 @@ charEncodingTag = "" +-- Add characters to the left of a string until it is at least as +-- large as requested. +padLeft :: Int -> Char -> String -> String +padLeft n c str = go n str + where + -- If the string is already long enough, stop traversing it. + go 0 _ = str + go k [] = replicate k c ++ str + go k (_:xs) = go (k-1) xs + genHtmlFromMod :: String -> Flags @@ -210,8 +220,7 @@ genHtmlFromMod dest_dir flags tix theFunTotals invertOutput = do content <- readFileFromPath (hpcError markup_plugin) origFile theHsPath let content' = markup tabStop info content - let show' = reverse . take 5 . (++ " ") . reverse . show - let addLine n xs = "" ++ show' n ++ " " ++ xs + let addLine n xs = "" ++ padLeft 5 ' ' (show n) ++ " " ++ xs let addLines = unlines . map (uncurry addLine) . zip [1 :: Int ..] . lines let fileName = modName0 ++ ".hs.html" putStrLn $ "Writing: " ++ fileName @@ -363,10 +372,14 @@ openTick (TopLevelDecl True 1) openTick (TopLevelDecl True n0) = "-- entered " ++ showBigNum n0 ++ " times" ++ openTopDecl where showBigNum n | n <= 9999 = show n - | otherwise = showBigNum' (n `div` 1000) ++ "," ++ showWith (n `mod` 1000) + | otherwise = case n `quotRem` 1000 of + (q, r) -> showBigNum' q ++ "," ++ showWith r showBigNum' n | n <= 999 = show n - | otherwise = showBigNum' (n `div` 1000) ++ "," ++ showWith (n `mod` 1000) - showWith n = take 3 $ reverse $ ("000" ++) $ reverse $ show n + | otherwise = case n `quotRem` 1000 of + (q, r) -> showBigNum' q ++ "," ++ showWith r + showWith n = padLeft 3 '0' $ show n + + closeTick :: String closeTick = "" @@ -462,7 +475,7 @@ instance Monoid ModuleSummary where writeFileUsing :: String -> String -> IO () writeFileUsing filename text = do - let dest_dir = reverse . dropWhile (\ x -> x /= '/') . reverse $ filename + let dest_dir = dropWhileEndLE (\ x -> x /= '/') $ filename -- We need to check for the dest_dir each time, because we use sub-dirs for -- packages, and a single .tix file might contain information about diff --git a/utils/hpc/HpcUtils.hs b/utils/hpc/HpcUtils.hs index 5655f837f3..73d9cd3a87 100644 --- a/utils/hpc/HpcUtils.hs +++ b/utils/hpc/HpcUtils.hs @@ -3,6 +3,10 @@ module HpcUtils where import Trace.Hpc.Util import qualified Data.Map as Map +dropWhileEndLE :: (a -> Bool) -> [a] -> [a] +-- Spec: dropWhileEndLE p = reverse . dropWhileEnd . reverse +dropWhileEndLE p = foldr (\x r -> if null r && p x then [] else x:r) [] + -- turns \n into ' ' -- | grab's the text behind a HpcPos; grabHpcPos :: Map.Map Int String -> HpcPos -> String -- cgit v1.2.1