1 {-# OPTIONS -fglasgow-exts #-}
3 -- Replacement for GHC.Unicode module
5 {-# OPTIONS -fno-implicit-prelude #-}
6 {-# OPTIONS -#include "WCsubst.h" #-}
7 {-# OPTIONS_HADDOCK hide #-}
8 -----------------------------------------------------------------------------
10 -- Module : GHC.Unicode
11 -- Copyright : (c) The University of Glasgow, 2003
12 -- License : see libraries/base/LICENSE
14 -- Maintainer : cvs-ghc@haskell.org
15 -- Stability : internal
16 -- Portability : non-portable (GHC extensions)
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.
22 -----------------------------------------------------------------------------
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,
36 import GHC.Real (fromIntegral)
39 import GHC.Num (fromInteger)
41 #include "HsBaseConfig.h"
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'
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'
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'
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'
63 -- | Selects control characters, which are the non-printing characters of
64 -- the Latin-1 subset of Unicode.
65 isControl :: Char -> Bool
67 -- | Selects printable Unicode characters
68 -- (letters, numbers, marks, punctuation, symbols and spaces).
69 isPrint :: Char -> Bool
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 == ' ' ||
85 iswspace (fromIntegral (ord c)) /= 0
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
93 -- | Selects lower-case alphabetic Unicode characters (letters).
94 isLower :: Char -> Bool
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
101 -- | Selects alphabetic or numeric digit Unicode characters.
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
108 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
109 isDigit :: Char -> Bool
110 isDigit c = c >= '0' && c <= '9'
112 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
113 isOctDigit :: Char -> Bool
114 isOctDigit c = c >= '0' && c <= '7'
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' ||
122 -- | Convert a letter to the corresponding upper-case letter, if any.
123 -- Any other character is returned unchanged.
124 toUpper :: Char -> Char
126 -- | Convert a letter to the corresponding lower-case letter, if any.
127 -- Any other character is returned unchanged.
128 toLower :: Char -> Char
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
136 -- -----------------------------------------------------------------------------
137 -- Implementation with the supplied auto-generated Unicode character properties
142 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
144 type CInt = HTYPE_INT
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
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))))
157 foreign import ccall unsafe "u_iswdigit"
158 iswdigit :: CInt -> CInt
160 foreign import ccall unsafe "u_iswalpha"
161 iswalpha :: CInt -> CInt
163 foreign import ccall unsafe "u_iswalnum"
164 iswalnum :: CInt -> CInt
166 foreign import ccall unsafe "u_iswcntrl"
167 iswcntrl :: CInt -> CInt
169 foreign import ccall unsafe "u_iswspace"
170 iswspace :: CInt -> CInt
172 foreign import ccall unsafe "u_iswprint"
173 iswprint :: CInt -> CInt
175 foreign import ccall unsafe "u_iswlower"
176 iswlower :: CInt -> CInt
178 foreign import ccall unsafe "u_iswupper"
179 iswupper :: CInt -> CInt
181 foreign import ccall unsafe "u_towlower"
182 towlower :: CInt -> CInt
184 foreign import ccall unsafe "u_towupper"
185 towupper :: CInt -> CInt
187 foreign import ccall unsafe "u_towtitle"
188 towtitle :: CInt -> CInt
190 foreign import ccall unsafe "u_gencat"
191 wgencat :: CInt -> CInt
193 -- -----------------------------------------------------------------------------
194 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
198 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
199 isPrint c = not (isControl c)
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'
212 isAlpha c = isLower c || isUpper c
213 isAlphaNum c = isAlpha c || isDigit c
215 -- Case-changing operations
218 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
220 -- fall-through to the slower stuff.
221 | isLower c && c /= '\xDF' && c /= '\xFF'
222 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
228 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
230 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
235 -------------------------------------------------------------
237 isSpace :: Char -> Bool
238 isSpace (C# c) = isSpace# c
240 isAlpha (C# c) = isAlpha# c
242 ------------------------------------------------------------
243 -- fake stubs for primops to fool GHC into typechecking this
244 ------------------------------------------------------------
245 {-# NOINLINE isSpace# #-}
246 isSpace# :: Char# -> Bool
249 {-# NOINLINE isAlpha# #-}
250 isAlpha# :: Char# -> Bool