76eb39c1a7781c8ea562c2610707f6d7720d9e96
[ghc-base.git] / Data / Char.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.Char
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  stable
10 -- Portability :  portable
11 --
12 -- The Char type and associated operations.
13 --
14 -----------------------------------------------------------------------------
15
16 module Data.Char 
17     (
18       Char
19
20     , String
21
22     -- * Character classification
23     -- | Unicode characters are divided into letters, numbers, marks,
24     -- punctuation, symbols, separators (including spaces) and others
25     -- (including control characters).
26     -- The full set of Unicode character attributes is not accessible
27     -- in this library.
28     , isAscii, isLatin1, isControl, isSpace
29     , isLower, isUpper,  isAlpha,   isAlphaNum, isPrint
30     , isDigit, isOctDigit, isHexDigit  -- :: Char -> Bool
31
32     -- * Case conversion
33     , toUpper, toLower  -- :: Char -> Char
34
35     -- * Single digit characters
36     , digitToInt        -- :: Char -> Int
37     , intToDigit        -- :: Int  -> Char
38
39     -- * Numeric representations
40     , ord               -- :: Char -> Int
41     , chr               -- :: Int  -> Char
42
43     -- * String representations
44     , showLitChar       -- :: Char -> ShowS
45     , lexLitChar        -- :: ReadS String
46     , readLitChar       -- :: ReadS Char 
47
48      -- Implementation checked wrt. Haskell 98 lib report, 1/99.
49     ) where
50
51 #ifdef __GLASGOW_HASKELL__
52 import GHC.Base
53 import GHC.Show
54 import GHC.Read (readLitChar, lexLitChar)
55 import GHC.Unicode
56 import GHC.Num
57 #endif
58
59 #ifdef __HUGS__
60 import Hugs.Char
61 #endif
62
63 #ifdef __NHC__
64 import Prelude
65 import Prelude(Char,String)
66 import Char
67 #endif
68
69 -- | Convert a single digit 'Char' to the corresponding 'Int'.  
70 -- This function fails unless its argument satisfies 'isHexDigit',
71 -- but recognises both upper and lower-case hexadecimal digits
72 -- (i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@).
73 digitToInt :: Char -> Int
74 digitToInt c
75  | isDigit c            =  ord c - ord '0'
76  | c >= 'a' && c <= 'f' =  ord c - ord 'a' + 10
77  | c >= 'A' && c <= 'F' =  ord c - ord 'A' + 10
78  | otherwise            =  error ("Char.digitToInt: not a digit " ++ show c) -- sigh