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 -----------------------------------------------------------------------------
20 isAscii, isLatin1, isControl,
21 isAsciiUpper, isAsciiLower,
22 isPrint, isSpace, isUpper,
23 isLower, isAlpha, isDigit,
24 isOctDigit, isHexDigit, isAlphaNum,
25 toUpper, toLower, toTitle,
30 import GHC.Real (fromIntegral)
33 import GHC.Num (fromInteger)
35 #include "HsBaseConfig.h"
37 -- | Selects the first 128 characters of the Unicode character set,
38 -- corresponding to the ASCII character set.
39 isAscii :: Char -> Bool
40 isAscii c = c < '\x80'
42 -- | Selects the first 256 characters of the Unicode character set,
43 -- corresponding to the ISO 8859-1 (Latin-1) character set.
44 isLatin1 :: Char -> Bool
45 isLatin1 c = c <= '\xff'
47 isAsciiUpper, isAsciiLower :: Char -> Bool
48 isAsciiLower c = c >= 'a' && c <= 'z'
49 isAsciiUpper c = c >= 'A' && c <= 'Z'
51 -- | Selects control characters, which are the non-printing characters of
52 -- the Latin-1 subset of Unicode.
53 isControl :: Char -> Bool
55 -- | Selects printable Unicode characters
56 -- (letters, numbers, marks, punctuation, symbols and spaces).
57 isPrint :: Char -> Bool
59 -- | Selects white-space characters in the Latin-1 range.
60 -- (In Unicode terms, this includes spaces and some control characters.)
61 isSpace :: Char -> Bool
62 -- isSpace includes non-breaking space
63 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
64 -- recursion with GHC.List elem
65 isSpace c = c == ' ' ||
72 iswspace (fromIntegral (ord c)) /= 0
74 -- | Selects alphabetic Unicode characters (letters) that are not lower-case.
75 -- (In Unicode terms, this includes letters in upper and title cases,
76 -- as well as modifier letters and other letters.)
77 isUpper :: Char -> Bool
79 -- | Selects lower-case alphabetic Unicode characters (letters).
80 isLower :: Char -> Bool
82 -- | Selects alphabetic Unicode characters (letters).
83 isAlpha :: Char -> Bool
85 -- | Selects alphabetic or numeric digit Unicode characters.
87 -- Note that numeric digits outside the ASCII range are selected by this
88 -- function but not by 'isDigit'. Such digits may be part of identifiers
89 -- but are not used by the printer and reader to represent numbers.
90 isAlphaNum :: Char -> Bool
92 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
93 isDigit :: Char -> Bool
94 isDigit c = c >= '0' && c <= '9'
96 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
97 isOctDigit :: Char -> Bool
98 isOctDigit c = c >= '0' && c <= '7'
100 -- | Selects ASCII hexadecimal digits,
101 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
102 isHexDigit :: Char -> Bool
103 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
106 -- | Convert a letter to the corresponding upper-case letter, leaving any
107 -- other character unchanged. Any Unicode letter which has an upper-case
108 -- equivalent is transformed.
109 toUpper :: Char -> Char
111 -- | Convert a letter to the corresponding lower-case letter, leaving any
112 -- other character unchanged. Any Unicode letter which has a lower-case
113 -- equivalent is transformed.
114 toLower :: Char -> Char
116 -- | Convert a letter to the corresponding title-case letter, leaving any
117 -- other character unchanged. Any Unicode letter which has a lower-case
118 -- equivalent is transformed.
119 toTitle :: Char -> Char
121 -- -----------------------------------------------------------------------------
122 -- Implementation with the supplied auto-generated Unicode character properties
127 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
129 type CInt = HTYPE_INT
131 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
132 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
133 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
134 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
135 isPrint c = iswprint (fromIntegral (ord c)) /= 0
136 isUpper c = iswupper (fromIntegral (ord c)) /= 0
137 isLower c = iswlower (fromIntegral (ord c)) /= 0
139 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
140 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
141 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
143 foreign import ccall unsafe "u_iswdigit"
144 iswdigit :: CInt -> CInt
146 foreign import ccall unsafe "u_iswalpha"
147 iswalpha :: CInt -> CInt
149 foreign import ccall unsafe "u_iswalnum"
150 iswalnum :: CInt -> CInt
152 foreign import ccall unsafe "u_iswcntrl"
153 iswcntrl :: CInt -> CInt
155 foreign import ccall unsafe "u_iswspace"
156 iswspace :: CInt -> CInt
158 foreign import ccall unsafe "u_iswprint"
159 iswprint :: CInt -> CInt
161 foreign import ccall unsafe "u_iswlower"
162 iswlower :: CInt -> CInt
164 foreign import ccall unsafe "u_iswupper"
165 iswupper :: CInt -> CInt
167 foreign import ccall unsafe "u_towlower"
168 towlower :: CInt -> CInt
170 foreign import ccall unsafe "u_towupper"
171 towupper :: CInt -> CInt
173 foreign import ccall unsafe "u_towtitle"
174 towtitle :: CInt -> CInt
176 foreign import ccall unsafe "u_gencat"
177 wgencat :: CInt -> Int
179 -- -----------------------------------------------------------------------------
180 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
184 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
185 isPrint c = not (isControl c)
187 -- The upper case ISO characters have the multiplication sign dumped
188 -- randomly in the middle of the range. Go figure.
189 isUpper c = c >= 'A' && c <= 'Z' ||
190 c >= '\xC0' && c <= '\xD6' ||
191 c >= '\xD8' && c <= '\xDE'
192 -- The lower case ISO characters have the division sign dumped
193 -- randomly in the middle of the range. Go figure.
194 isLower c = c >= 'a' && c <= 'z' ||
195 c >= '\xDF' && c <= '\xF6' ||
196 c >= '\xF8' && c <= '\xFF'
198 isAlpha c = isLower c || isUpper c
199 isAlphaNum c = isAlpha c || isDigit c
201 -- Case-changing operations
204 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
206 -- fall-through to the slower stuff.
207 | isLower c && c /= '\xDF' && c /= '\xFF'
208 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
214 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
216 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')