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