1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
10 -- Portability : portable
12 -- The Char type and associated operations.
14 -----------------------------------------------------------------------------
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
31 , isLetter, isMark, isNumber, isPunctuation, isSymbol, isSeparator
33 , GeneralCategory(..), generalCategory
37 , toUpper, toLower, toTitle -- :: Char -> Char
39 -- * Single digit characters
40 , digitToInt -- :: Char -> Int
41 , intToDigit -- :: Int -> Char
43 -- * Numeric representations
44 , ord -- :: Char -> Int
45 , chr -- :: Int -> Char
47 -- * String representations
48 , showLitChar -- :: Char -> ShowS
49 , lexLitChar -- :: ReadS String
50 , readLitChar -- :: ReadS Char
52 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
55 #ifdef __GLASGOW_HASKELL__
57 import GHC.Real (fromIntegral)
59 import GHC.Read (Read, readLitChar, lexLitChar)
71 import Prelude(Char,String)
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
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
86 #ifndef __GLASGOW_HASKELL__
87 isAsciiUpper, isAsciiLower :: Char -> Bool
88 isAsciiLower c = c >= 'a' && c <= 'z'
89 isAsciiUpper c = c >= 'A' && c <= 'Z'
93 -- | Unicode General Categories (column 2 of the UnicodeData table)
94 -- in the order they are listed in the Unicode standard.
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)
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)))
135 generalCategory c = toEnum (primUniGenCat c)
138 -- derived character classifiers
140 isLetter :: Char -> Bool
141 isLetter c = case generalCategory c of
142 UppercaseLetter -> True
143 LowercaseLetter -> True
144 TitlecaseLetter -> True
145 ModifierLetter -> True
149 isMark :: Char -> Bool
150 isMark c = case generalCategory c of
151 NonSpacingMark -> True
152 SpacingCombiningMark -> True
153 EnclosingMark -> True
156 isNumber :: Char -> Bool
157 isNumber c = case generalCategory c of
158 DecimalNumber -> True
163 isPunctuation :: Char -> Bool
164 isPunctuation c = case generalCategory c of
165 ConnectorPunctuation -> True
166 DashPunctuation -> True
167 OpenPunctuation -> True
168 ClosePunctuation -> True
171 OtherPunctuation -> True
174 isSymbol :: Char -> Bool
175 isSymbol c = case generalCategory c of
177 CurrencySymbol -> True
178 ModifierSymbol -> True
182 isSeparator :: Char -> Bool
183 isSeparator c = case generalCategory c of
185 LineSeparator -> True
186 ParagraphSeparator -> True
188 #endif /* !__NHC__ */
191 -- dummy implementation
192 toTitle :: Char -> Char