[project @ 2002-04-26 12:48:16 by simonmar]
[haskell-directory.git] / GHC / Show.lhs
index 2edd038..dd931b8 100644 (file)
@@ -1,14 +1,18 @@
-% ------------------------------------------------------------------------------
-% $Id: Show.lhs,v 1.1 2001/06/28 14:15:03 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
        (
@@ -19,7 +23,7 @@ module GHC.Show
        -- Show support code
        shows, showChar, showString, showParen, showList__, showSpace,
        showLitChar, protectEsc, 
-       intToDigit, showSignedInt,
+       intToDigit, digitToInt, showSignedInt,
 
        -- Character operations
        isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
@@ -34,8 +38,9 @@ module GHC.Show
 
 import {-# SOURCE #-} GHC.Err ( error )
 import GHC.Base
-import GHC.Tup
-import GHC.Maybe
+import GHC.Enum
+import Data.Maybe
+import Data.Either
 import GHC.List        ( (!!), break, dropWhile
 #ifdef USE_REPORT_PRELUDE
                 , concatMap, foldr1
@@ -216,18 +221,26 @@ protectEsc :: (Char -> Bool) -> ShowS -> ShowS
 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)
@@ -246,10 +259,11 @@ itos n# cs
     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}
@@ -309,7 +323,6 @@ toUpper c@(C# c#)
   = c
 
 
-
 toLower c@(C# c#)
   | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
   | isAscii c      = c