[project @ 2002-04-11 12:03:43 by simonpj]
[ghc-base.git] / GHC / Show.lhs
index 2edd038..b0265be 100644 (file)
@@ -1,5 +1,5 @@
 % ------------------------------------------------------------------------------
-% $Id: Show.lhs,v 1.1 2001/06/28 14:15:03 simonmar Exp $
+% $Id: Show.lhs,v 1.5 2002/04/11 12:03:44 simonpj Exp $
 %
 % (c) The University of Glasgow, 1992-2000
 %
@@ -19,7 +19,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 +34,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 +217,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 +255,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 +319,6 @@ toUpper c@(C# c#)
   = c
 
 
-
 toLower c@(C# c#)
   | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
   | isAscii c      = c