[GHC] #9654: Clean up stringify in util/hsc2hs/CrossCodegen

GHC ghc-devs at haskell.org
Wed Oct 1 05:05:58 UTC 2014


#9654: Clean up stringify in util/hsc2hs/CrossCodegen
-------------------------------------+-------------------------------------
       Reporter:  dfeuer             |                   Owner:
           Type:  task               |                  Status:  new
       Priority:  normal             |               Milestone:  7.10.1
      Component:  Code Coverage      |                 Version:  7.9
       Keywords:                     |        Operating System:
   Architecture:  Unknown/Multiple   |  Unknown/Multiple
     Difficulty:  Easy (less than 1  |         Type of failure:
  hour)                              |  None/Unknown
     Blocked By:                     |               Test Case:
Related Tickets:                     |                Blocking:
                                     |  Differential Revisions:
-------------------------------------+-------------------------------------
 Currently, it's defined

 {{{#!hs
 stringify :: String -> String
 stringify s = reverse .  dropWhile isSpace . reverse -- drop trailing
 space
               . dropWhile isSpace                    -- drop leading space
               . compressSpaces                       -- replace each span
 of
                                                      -- whitespace with a
 single space
               $ s
     where compressSpaces [] = []
           compressSpaces (a:as) | isSpace a = ' ' : compressSpaces
 (dropWhile isSpace as)
           compressSpaces (a:as) = a : compressSpaces as
 }}}

 If we're going to go to the trouble of doing this by hand, we might as
 well take the efficiency advantage doing so can give us:

 {{{#!hs
 stringify :: String -> String
 -- Spec: stringify = unwords . words
 stringify xs = go False (dropWhile isSpace xs)
   where
     go _haveSpace [] = []
     go  haveSpace (x:xs)
       | isSpace x = go True xs
       | otherwise = if haveSpace
                     then ' ' : x : go False xs
                     else x : go False xs
 }}}

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9654>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list