1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 {-# OPTIONS -#include "WCsubst.h" #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
6 -- Module : GHC.Unicode
7 -- Copyright : (c) The University of Glasgow, 2003
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC extensions)
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.
18 -----------------------------------------------------------------------------
22 isAscii, isLatin1, isControl,
23 isAsciiUpper, isAsciiLower,
24 isPrint, isSpace, isUpper,
25 isLower, isAlpha, isDigit,
26 isOctDigit, isHexDigit, isAlphaNum,
27 toUpper, toLower, toTitle,
32 import GHC.Real (fromIntegral)
35 import GHC.Num (fromInteger)
37 #include "HsBaseConfig.h"
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'
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'
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'
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'
59 -- | Selects control characters, which are the non-printing characters of
60 -- the Latin-1 subset of Unicode.
61 isControl :: Char -> Bool
63 -- | Selects printable Unicode characters
64 -- (letters, numbers, marks, punctuation, symbols and spaces).
65 isPrint :: Char -> Bool
67 -- | Selects white-space characters in the Latin-1 range.
68 -- (In Unicode terms, this includes spaces and some control characters.)
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 == ' ' ||
80 iswspace (fromIntegral (ord c)) /= 0
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
87 -- | Selects lower-case alphabetic Unicode characters (letters).
88 isLower :: Char -> Bool
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
95 -- | Selects alphabetic or numeric digit Unicode characters.
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
102 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
103 isDigit :: Char -> Bool
104 isDigit c = c >= '0' && c <= '9'
106 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
107 isOctDigit :: Char -> Bool
108 isOctDigit c = c >= '0' && c <= '7'
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' ||
116 -- | Convert a letter to the corresponding upper-case letter, if any.
117 -- Any other character is returned unchanged.
118 toUpper :: Char -> Char
120 -- | Convert a letter to the corresponding lower-case letter, if any.
121 -- Any other character is returned unchanged.
122 toLower :: Char -> Char
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
130 -- -----------------------------------------------------------------------------
131 -- Implementation with the supplied auto-generated Unicode character properties
136 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
138 type CInt = HTYPE_INT
140 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
141 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
142 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
143 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
144 isPrint c = iswprint (fromIntegral (ord c)) /= 0
145 isUpper c = iswupper (fromIntegral (ord c)) /= 0
146 isLower c = iswlower (fromIntegral (ord c)) /= 0
148 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
149 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
150 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
152 foreign import ccall unsafe "u_iswdigit"
153 iswdigit :: CInt -> CInt
155 foreign import ccall unsafe "u_iswalpha"
156 iswalpha :: CInt -> CInt
158 foreign import ccall unsafe "u_iswalnum"
159 iswalnum :: CInt -> CInt
161 foreign import ccall unsafe "u_iswcntrl"
162 iswcntrl :: CInt -> CInt
164 foreign import ccall unsafe "u_iswspace"
165 iswspace :: CInt -> CInt
167 foreign import ccall unsafe "u_iswprint"
168 iswprint :: CInt -> CInt
170 foreign import ccall unsafe "u_iswlower"
171 iswlower :: CInt -> CInt
173 foreign import ccall unsafe "u_iswupper"
174 iswupper :: CInt -> CInt
176 foreign import ccall unsafe "u_towlower"
177 towlower :: CInt -> CInt
179 foreign import ccall unsafe "u_towupper"
180 towupper :: CInt -> CInt
182 foreign import ccall unsafe "u_towtitle"
183 towtitle :: CInt -> CInt
185 foreign import ccall unsafe "u_gencat"
186 wgencat :: CInt -> CInt
188 -- -----------------------------------------------------------------------------
189 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
193 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
194 isPrint c = not (isControl c)
196 -- The upper case ISO characters have the multiplication sign dumped
197 -- randomly in the middle of the range. Go figure.
198 isUpper c = c >= 'A' && c <= 'Z' ||
199 c >= '\xC0' && c <= '\xD6' ||
200 c >= '\xD8' && c <= '\xDE'
201 -- The lower case ISO characters have the division sign dumped
202 -- randomly in the middle of the range. Go figure.
203 isLower c = c >= 'a' && c <= 'z' ||
204 c >= '\xDF' && c <= '\xF6' ||
205 c >= '\xF8' && c <= '\xFF'
207 isAlpha c = isLower c || isUpper c
208 isAlphaNum c = isAlpha c || isDigit c
210 -- Case-changing operations
213 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
215 -- fall-through to the slower stuff.
216 | isLower c && c /= '\xDF' && c /= '\xFF'
217 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
223 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
225 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')