1 {-# OPTIONS -fno-implicit-prelude #-}
2 {-# OPTIONS -#include "WCsubst.h" #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.Unicode
6 -- Copyright : (c) The University of Glasgow, 2003
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC extensions)
13 -- Implementations for the character predicates (isLower, isUpper, etc.)
14 -- and the conversions (toUpper, toLower). The implementation uses
15 -- libunicode on Unix systems if that is available.
17 -----------------------------------------------------------------------------
21 isAscii, isLatin1, isControl,
22 isAsciiUpper, isAsciiLower,
23 isPrint, isSpace, isUpper,
24 isLower, isAlpha, isDigit,
25 isOctDigit, isHexDigit, isAlphaNum,
26 toUpper, toLower, toTitle,
31 import GHC.Real (fromIntegral)
34 import GHC.Num (fromInteger)
36 #include "HsBaseConfig.h"
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'
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'
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'
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'
58 -- | Selects control characters, which are the non-printing characters of
59 -- the Latin-1 subset of Unicode.
60 isControl :: Char -> Bool
62 -- | Selects printable Unicode characters
63 -- (letters, numbers, marks, punctuation, symbols and spaces).
64 isPrint :: Char -> Bool
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 == ' ' ||
79 iswspace (fromIntegral (ord c)) /= 0
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
86 -- | Selects lower-case alphabetic Unicode characters (letters).
87 isLower :: Char -> Bool
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
94 -- | Selects alphabetic or numeric digit Unicode characters.
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
101 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
102 isDigit :: Char -> Bool
103 isDigit c = c >= '0' && c <= '9'
105 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
106 isOctDigit :: Char -> Bool
107 isOctDigit c = c >= '0' && c <= '7'
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' ||
115 -- | Convert a letter to the corresponding upper-case letter, if any.
116 -- Any other character is returned unchanged.
117 toUpper :: Char -> Char
119 -- | Convert a letter to the corresponding lower-case letter, if any.
120 -- Any other character is returned unchanged.
121 toLower :: Char -> Char
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
129 -- -----------------------------------------------------------------------------
130 -- Implementation with the supplied auto-generated Unicode character properties
135 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
137 type CInt = HTYPE_INT
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
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))))
151 foreign import ccall unsafe "u_iswdigit"
152 iswdigit :: CInt -> CInt
154 foreign import ccall unsafe "u_iswalpha"
155 iswalpha :: CInt -> CInt
157 foreign import ccall unsafe "u_iswalnum"
158 iswalnum :: CInt -> CInt
160 foreign import ccall unsafe "u_iswcntrl"
161 iswcntrl :: CInt -> CInt
163 foreign import ccall unsafe "u_iswspace"
164 iswspace :: CInt -> CInt
166 foreign import ccall unsafe "u_iswprint"
167 iswprint :: CInt -> CInt
169 foreign import ccall unsafe "u_iswlower"
170 iswlower :: CInt -> CInt
172 foreign import ccall unsafe "u_iswupper"
173 iswupper :: CInt -> CInt
175 foreign import ccall unsafe "u_towlower"
176 towlower :: CInt -> CInt
178 foreign import ccall unsafe "u_towupper"
179 towupper :: CInt -> CInt
181 foreign import ccall unsafe "u_towtitle"
182 towtitle :: CInt -> CInt
184 foreign import ccall unsafe "u_gencat"
185 wgencat :: CInt -> Int
187 -- -----------------------------------------------------------------------------
188 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
192 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
193 isPrint c = not (isControl c)
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'
206 isAlpha c = isLower c || isUpper c
207 isAlphaNum c = isAlpha c || isDigit c
209 -- Case-changing operations
212 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
214 -- fall-through to the slower stuff.
215 | isLower c && c /= '\xDF' && c /= '\xFF'
216 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
222 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
224 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')