[project @ 2005-03-14 18:02:48 by ross]
[ghc-base.git] / Data / Char.hs
1 {-# OPTIONS_GHC -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     , isAscii, isLatin1, isControl, isSpace
27     , isLower, isUpper,  isAlpha,   isAlphaNum, isPrint
28     , isDigit, isOctDigit, isHexDigit
29     , isAsciiUpper, isAsciiLower
30 #ifndef __NHC__
31     , isLetter, isMark, isNumber, isPunctuation, isSymbol, isSeparator
32
33     , GeneralCategory(..), generalCategory
34 #endif
35
36     -- * Case conversion
37     , toUpper, toLower, toTitle  -- :: Char -> Char
38
39     -- * Single digit characters
40     , digitToInt        -- :: Char -> Int
41     , intToDigit        -- :: Int  -> Char
42
43     -- * Numeric representations
44     , ord               -- :: Char -> Int
45     , chr               -- :: Int  -> Char
46
47     -- * String representations
48     , showLitChar       -- :: Char -> ShowS
49     , lexLitChar        -- :: ReadS String
50     , readLitChar       -- :: ReadS Char 
51
52      -- Implementation checked wrt. Haskell 98 lib report, 1/99.
53     ) where
54
55 #ifdef __GLASGOW_HASKELL__
56 import GHC.Base
57 import GHC.Real (fromIntegral)
58 import GHC.Show
59 import GHC.Read (Read, readLitChar, lexLitChar)
60 import GHC.Unicode
61 import GHC.Num
62 import GHC.Enum
63 #endif
64
65 #ifdef __HUGS__
66 import Hugs.Char
67 #endif
68
69 #ifdef __NHC__
70 import Prelude
71 import Prelude(Char,String)
72 import Char
73 #endif
74
75 -- | Convert a single digit 'Char' to the corresponding 'Int'.  
76 -- This function fails unless its argument satisfies 'isHexDigit',
77 -- but recognises both upper and lower-case hexadecimal digits
78 -- (i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@).
79 digitToInt :: Char -> Int
80 digitToInt c
81  | isDigit c            =  ord c - ord '0'
82  | c >= 'a' && c <= 'f' =  ord c - ord 'a' + 10
83  | c >= 'A' && c <= 'F' =  ord c - ord 'A' + 10
84  | otherwise            =  error ("Char.digitToInt: not a digit " ++ show c) -- sigh
85
86 #ifndef __GLASGOW_HASKELL__
87 isAsciiUpper, isAsciiLower :: Char -> Bool
88 isAsciiLower c          =  c >= 'a' && c <= 'z'
89 isAsciiUpper c          =  c >= 'A' && c <= 'Z'
90 #endif
91
92 #ifndef __NHC__
93 -- | Unicode General Categories (column 2 of the UnicodeData table)
94 -- in the order they are listed in the Unicode standard.
95
96 data GeneralCategory
97         = UppercaseLetter       -- ^ Lu: Letter, Uppercase
98         | LowercaseLetter       -- ^ Ll: Letter, Lowercase
99         | TitlecaseLetter       -- ^ Lt: Letter, Titlecase
100         | ModifierLetter        -- ^ Lm: Letter, Modifier
101         | OtherLetter           -- ^ Lo: Letter, Other
102         | NonSpacingMark        -- ^ Mn: Mark, Non-Spacing
103         | SpacingCombiningMark  -- ^ Mc: Mark, Spacing Combining
104         | EnclosingMark         -- ^ Me: Mark, Enclosing
105         | DecimalNumber         -- ^ Nd: Number, Decimal
106         | LetterNumber          -- ^ Nl: Number, Letter
107         | OtherNumber           -- ^ No: Number, Other
108         | ConnectorPunctuation  -- ^ Pc: Punctuation, Connector
109         | DashPunctuation       -- ^ Pd: Punctuation, Dash
110         | OpenPunctuation       -- ^ Ps: Punctuation, Open
111         | ClosePunctuation      -- ^ Pe: Punctuation, Close
112         | InitialQuote          -- ^ Pi: Punctuation, Initial quote
113         | FinalQuote            -- ^ Pf: Punctuation, Final quote
114         | OtherPunctuation      -- ^ Po: Punctuation, Other
115         | MathSymbol            -- ^ Sm: Symbol, Math
116         | CurrencySymbol        -- ^ Sc: Symbol, Currency
117         | ModifierSymbol        -- ^ Sk: Symbol, Modifier
118         | OtherSymbol           -- ^ So: Symbol, Other
119         | Space                 -- ^ Zs: Separator, Space
120         | LineSeparator         -- ^ Zl: Separator, Line
121         | ParagraphSeparator    -- ^ Zp: Separator, Paragraph
122         | Control               -- ^ Cc: Other, Control
123         | Format                -- ^ Cf: Other, Format
124         | Surrogate             -- ^ Cs: Other, Surrogate
125         | PrivateUse            -- ^ Co: Other, Private Use
126         | NotAssigned           -- ^ Cn: Other, Not Assigned
127         deriving (Eq, Ord, Enum, Read, Show, Bounded)
128
129 -- | Retrieves the general Unicode category of the character.
130 generalCategory :: Char -> GeneralCategory
131 #ifdef __GLASGOW_HASKELL__
132 generalCategory c = toEnum (wgencat (fromIntegral (ord c)))
133 #endif
134 #ifdef __HUGS__
135 generalCategory c = toEnum (primUniGenCat c)
136 #endif
137
138 -- derived character classifiers
139
140 isLetter :: Char -> Bool
141 isLetter c = case generalCategory c of
142         UppercaseLetter         -> True
143         LowercaseLetter         -> True
144         TitlecaseLetter         -> True
145         ModifierLetter          -> True
146         OtherLetter             -> True
147         _                       -> False
148
149 isMark :: Char -> Bool
150 isMark c = case generalCategory c of
151         NonSpacingMark          -> True
152         SpacingCombiningMark    -> True
153         EnclosingMark           -> True
154         _                       -> False
155
156 isNumber :: Char -> Bool
157 isNumber c = case generalCategory c of
158         DecimalNumber           -> True
159         LetterNumber            -> True
160         OtherNumber             -> True
161         _                       -> False
162
163 isPunctuation :: Char -> Bool
164 isPunctuation c = case generalCategory c of
165         ConnectorPunctuation    -> True
166         DashPunctuation         -> True
167         OpenPunctuation         -> True
168         ClosePunctuation        -> True
169         InitialQuote            -> True
170         FinalQuote              -> True
171         OtherPunctuation        -> True
172         _                       -> False
173
174 isSymbol :: Char -> Bool
175 isSymbol c = case generalCategory c of
176         MathSymbol              -> True
177         CurrencySymbol          -> True
178         ModifierSymbol          -> True
179         OtherSymbol             -> True
180         _                       -> False
181
182 isSeparator :: Char -> Bool
183 isSeparator c = case generalCategory c of
184         Space                   -> True
185         LineSeparator           -> True
186         ParagraphSeparator      -> True
187         _                       -> False
188 #endif /* !__NHC__ */
189
190 #ifdef __NHC__
191 -- dummy implementation
192 toTitle :: Char -> Char
193 toTitle = toUpper
194 #endif