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