-% ------------------------------------------------------------------------------
-% $Id: Show.lhs,v 1.2 2001/07/03 11:37:50 simonmar Exp $
-%
-% (c) The University of Glasgow, 1992-2000
-%
-
-\section{Module @GHC.Show@}
-
-
\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module : GHC.Show
+-- Copyright : (c) The University of Glasgow, 1992-2002
+-- License : see libraries/base/LICENSE
+--
+-- Maintainer : cvs-ghc@haskell.org
+-- Stability : internal
+-- Portability : non-portable (GHC Extensions)
+--
+-- The 'Show' class, and related operations.
+--
+-----------------------------------------------------------------------------
module GHC.Show
(
-- Show support code
shows, showChar, showString, showParen, showList__, showSpace,
showLitChar, protectEsc,
- intToDigit, showSignedInt,
+ intToDigit, digitToInt, showSignedInt,
+ appPrec, appPrec1,
-- Character operations
isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
import {-# SOURCE #-} GHC.Err ( error )
import GHC.Base
-import GHC.Maybe
+import GHC.Enum
+import Data.Maybe
+import Data.Either
import GHC.List ( (!!), break, dropWhile
#ifdef USE_REPORT_PRELUDE
, concatMap, foldr1
where
showl [] = ']' : s
showl (y:ys) = ',' : showx y (showl ys)
+
+appPrec, appPrec1 :: Int
+ -- Use unboxed stuff because we don't have overloaded numerics yet
+appPrec = I# 10# -- Precedence of application:
+ -- one more than the maximum operator precedence of 9
+appPrec1 = I# 11# -- appPrec + 1
\end{code}
%*********************************************************
instance Show a => Show (Maybe a) where
showsPrec _p Nothing s = showString "Nothing" s
- showsPrec (I# p#) (Just x) s
- = (showParen (p# >=# 10#) $
+ showsPrec p (Just x) s
+ = (showParen (p > appPrec) $
showString "Just " .
- showsPrec (I# 10#) x) s
+ showsPrec appPrec1 x) s
instance (Show a, Show b) => Show (Either a b) where
- showsPrec (I# p#) e s =
- (showParen (p# >=# 10#) $
+ showsPrec p e s =
+ (showParen (p > appPrec) $
case e of
- Left a -> showString "Left " . showsPrec (I# 10#) a
- Right b -> showString "Right " . showsPrec (I# 10#) b)
+ Left a -> showString "Left " . showsPrec appPrec1 a
+ Right b -> showString "Right " . showsPrec appPrec1 b)
s
-
\end{code}
protectEsc p f = f . cont
where cont s@(c:_) | p c = "\\&" ++ s
cont s = s
+\end{code}
+
+Code specific for Ints.
+\begin{code}
intToDigit :: Int -> Char
intToDigit (I# i)
| i >=# 0# && i <=# 9# = unsafeChr (ord '0' `plusInt` I# i)
- | i >=# 10# && i <=# 15# = unsafeChr (ord 'a' `minusInt` I# 10# `plusInt` I# i)
+ | i >=# 10# && i <=# 15# = unsafeChr (ord 'a' `minusInt` ten `plusInt` I# i)
| otherwise = error ("Char.intToDigit: not a digit " ++ show (I# i))
-\end{code}
+digitToInt :: Char -> Int
+digitToInt c
+ | isDigit c = ord c `minusInt` ord '0'
+ | c >= 'a' && c <= 'f' = ord c `minusInt` ord 'a' `plusInt` ten
+ | c >= 'A' && c <= 'F' = ord c `minusInt` ord 'A' `plusInt` ten
+ | otherwise = error ("Char.digitToInt: not a digit " ++ show c) -- sigh
-Code specific for Ints.
+ten = I# 10#
-\begin{code}
showSignedInt :: Int -> Int -> ShowS
showSignedInt (I# p) (I# n) r
| n <# 0# && p ># 6# = '(' : itos n (')' : r)
itos' :: Int# -> String -> String
itos' n# cs
| n# <# 10# = C# (chr# (ord# '0'# +# n#)) : cs
- | otherwise = itos' (n# `quotInt#` 10#)
- (C# (chr# (ord# '0'# +# (n# `remInt#` 10#))) : cs)
+ | otherwise = case chr# (ord# '0'# +# (n# `remInt#` 10#)) of { c# ->
+ itos' (n# `quotInt#` 10#) (C# c# : cs) }
\end{code}
+
%*********************************************************
%* *
\subsection{Character stuff}
= c
-
toLower c@(C# c#)
| isAsciiUpper c = C# (chr# (ord# c# +# 32#))
| isAscii c = c