Remove unused imports
[ghc-base.git] / GHC / Unicode.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 {-# OPTIONS -#include "WCsubst.h" #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module      :  GHC.Unicode
7 -- Copyright   :  (c) The University of Glasgow, 2003
8 -- License     :  see libraries/base/LICENSE
9 -- 
10 -- Maintainer  :  cvs-ghc@haskell.org
11 -- Stability   :  internal
12 -- Portability :  non-portable (GHC extensions)
13 --
14 -- Implementations for the character predicates (isLower, isUpper, etc.)
15 -- and the conversions (toUpper, toLower).  The implementation uses
16 -- libunicode on Unix systems if that is available.
17 --
18 -----------------------------------------------------------------------------
19
20 -- #hide
21 module GHC.Unicode (
22     isAscii, isLatin1, isControl,
23     isAsciiUpper, isAsciiLower,
24     isPrint, isSpace,  isUpper,
25     isLower, isAlpha,  isDigit,
26     isOctDigit, isHexDigit, isAlphaNum,
27     toUpper, toLower, toTitle,
28     wgencat,
29   ) where
30
31 import GHC.Base
32 import GHC.Real  (fromIntegral)
33 import GHC.Int
34 import GHC.Num   (fromInteger)
35
36 #include "HsBaseConfig.h"
37
38 -- | Selects the first 128 characters of the Unicode character set,
39 -- corresponding to the ASCII character set.
40 isAscii                 :: Char -> Bool
41 isAscii c               =  c <  '\x80'
42
43 -- | Selects the first 256 characters of the Unicode character set,
44 -- corresponding to the ISO 8859-1 (Latin-1) character set.
45 isLatin1                :: Char -> Bool
46 isLatin1 c              =  c <= '\xff'
47
48 -- | Selects ASCII lower-case letters,
49 -- i.e. characters satisfying both 'isAscii' and 'isLower'.
50 isAsciiLower :: Char -> Bool
51 isAsciiLower c          =  c >= 'a' && c <= 'z'
52
53 -- | Selects ASCII upper-case letters,
54 -- i.e. characters satisfying both 'isAscii' and 'isUpper'.
55 isAsciiUpper :: Char -> Bool
56 isAsciiUpper c          =  c >= 'A' && c <= 'Z'
57
58 -- | Selects control characters, which are the non-printing characters of
59 -- the Latin-1 subset of Unicode.
60 isControl               :: Char -> Bool
61
62 -- | Selects printable Unicode characters
63 -- (letters, numbers, marks, punctuation, symbols and spaces).
64 isPrint                 :: Char -> Bool
65
66 -- | Selects white-space characters in the Latin-1 range.
67 -- (In Unicode terms, this includes spaces and some control characters.)
68 isSpace                 :: Char -> Bool
69 -- isSpace includes non-breaking space
70 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
71 -- recursion with GHC.List elem
72 isSpace c               =  c == ' '     ||
73                            c == '\t'    ||
74                            c == '\n'    ||
75                            c == '\r'    ||
76                            c == '\f'    ||
77                            c == '\v'    ||
78                            c == '\xa0'  ||
79                            iswspace (fromIntegral (ord c)) /= 0
80
81 -- | Selects upper-case or title-case alphabetic Unicode characters (letters).
82 -- Title case is used by a small number of letter ligatures like the
83 -- single-character form of /Lj/.
84 isUpper                 :: Char -> Bool
85
86 -- | Selects lower-case alphabetic Unicode characters (letters).
87 isLower                 :: Char -> Bool
88
89 -- | Selects alphabetic Unicode characters (lower-case, upper-case and
90 -- title-case letters, plus letters of caseless scripts and modifiers letters).
91 -- This function is equivalent to 'Data.Char.isLetter'.
92 isAlpha                 :: Char -> Bool
93
94 -- | Selects alphabetic or numeric digit Unicode characters.
95 --
96 -- Note that numeric digits outside the ASCII range are selected by this
97 -- function but not by 'isDigit'.  Such digits may be part of identifiers
98 -- but are not used by the printer and reader to represent numbers.
99 isAlphaNum              :: Char -> Bool
100
101 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
102 isDigit                 :: Char -> Bool
103 isDigit c               =  c >= '0' && c <= '9'
104
105 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
106 isOctDigit              :: Char -> Bool
107 isOctDigit c            =  c >= '0' && c <= '7'
108
109 -- | Selects ASCII hexadecimal digits,
110 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
111 isHexDigit              :: Char -> Bool
112 isHexDigit c            =  isDigit c || c >= 'A' && c <= 'F' ||
113                                         c >= 'a' && c <= 'f'
114
115 -- | Convert a letter to the corresponding upper-case letter, if any.
116 -- Any other character is returned unchanged.
117 toUpper                 :: Char -> Char
118
119 -- | Convert a letter to the corresponding lower-case letter, if any.
120 -- Any other character is returned unchanged.
121 toLower                 :: Char -> Char
122
123 -- | Convert a letter to the corresponding title-case or upper-case
124 -- letter, if any.  (Title case differs from upper case only for a small
125 -- number of ligature letters.)
126 -- Any other character is returned unchanged.
127 toTitle                 :: Char -> Char
128
129 -- -----------------------------------------------------------------------------
130 -- Implementation with the supplied auto-generated Unicode character properties
131 -- table (default)
132
133 #if 1
134
135 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
136
137 type CInt = HTYPE_INT
138
139 isAlpha    c = iswalpha (fromIntegral (ord c)) /= 0
140 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
141 --isSpace    c = iswspace (fromIntegral (ord c)) /= 0
142 isControl  c = iswcntrl (fromIntegral (ord c)) /= 0
143 isPrint    c = iswprint (fromIntegral (ord c)) /= 0
144 isUpper    c = iswupper (fromIntegral (ord c)) /= 0
145 isLower    c = iswlower (fromIntegral (ord c)) /= 0
146
147 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
148 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
149 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
150
151 foreign import ccall unsafe "u_iswdigit"
152   iswdigit :: CInt -> CInt
153
154 foreign import ccall unsafe "u_iswalpha"
155   iswalpha :: CInt -> CInt
156
157 foreign import ccall unsafe "u_iswalnum"
158   iswalnum :: CInt -> CInt
159
160 foreign import ccall unsafe "u_iswcntrl"
161   iswcntrl :: CInt -> CInt
162
163 foreign import ccall unsafe "u_iswspace"
164   iswspace :: CInt -> CInt
165
166 foreign import ccall unsafe "u_iswprint"
167   iswprint :: CInt -> CInt
168
169 foreign import ccall unsafe "u_iswlower"
170   iswlower :: CInt -> CInt
171
172 foreign import ccall unsafe "u_iswupper"
173   iswupper :: CInt -> CInt
174
175 foreign import ccall unsafe "u_towlower"
176   towlower :: CInt -> CInt
177
178 foreign import ccall unsafe "u_towupper"
179   towupper :: CInt -> CInt
180
181 foreign import ccall unsafe "u_towtitle"
182   towtitle :: CInt -> CInt
183
184 foreign import ccall unsafe "u_gencat"
185   wgencat :: CInt -> CInt
186
187 -- -----------------------------------------------------------------------------
188 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
189
190 #else
191
192 isControl c             =  c < ' ' || c >= '\DEL' && c <= '\x9f'
193 isPrint c               =  not (isControl c)
194
195 -- The upper case ISO characters have the multiplication sign dumped
196 -- randomly in the middle of the range.  Go figure.
197 isUpper c               =  c >= 'A' && c <= 'Z' ||
198                            c >= '\xC0' && c <= '\xD6' ||
199                            c >= '\xD8' && c <= '\xDE'
200 -- The lower case ISO characters have the division sign dumped
201 -- randomly in the middle of the range.  Go figure.
202 isLower c               =  c >= 'a' && c <= 'z' ||
203                            c >= '\xDF' && c <= '\xF6' ||
204                            c >= '\xF8' && c <= '\xFF'
205
206 isAlpha c               =  isLower c || isUpper c
207 isAlphaNum c            =  isAlpha c || isDigit c
208
209 -- Case-changing operations
210
211 toUpper c@(C# c#)
212   | isAsciiLower c    = C# (chr# (ord# c# -# 32#))
213   | isAscii c         = c
214     -- fall-through to the slower stuff.
215   | isLower c   && c /= '\xDF' && c /= '\xFF'
216   = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
217   | otherwise
218   = c
219
220
221 toLower c@(C# c#)
222   | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
223   | isAscii c      = c
224   | isUpper c      = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
225   | otherwise      =  c
226
227 #endif
228