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 WInt = HTYPE_WINT_T
130 type CInt = HTYPE_INT
132 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
133 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
134 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
135 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
136 isPrint c = iswprint (fromIntegral (ord c)) /= 0
137 isUpper c = iswupper (fromIntegral (ord c)) /= 0
138 isLower c = iswlower (fromIntegral (ord c)) /= 0
140 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
141 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
142 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
144 foreign import ccall unsafe "u_iswdigit"
145 iswdigit :: CInt -> CInt
147 foreign import ccall unsafe "u_iswalpha"
148 iswalpha :: CInt -> CInt
150 foreign import ccall unsafe "u_iswalnum"
151 iswalnum :: CInt -> CInt
153 foreign import ccall unsafe "u_iswcntrl"
154 iswcntrl :: CInt -> CInt
156 foreign import ccall unsafe "u_iswspace"
157 iswspace :: CInt -> CInt
159 foreign import ccall unsafe "u_iswprint"
160 iswprint :: CInt -> CInt
162 foreign import ccall unsafe "u_iswlower"
163 iswlower :: CInt -> CInt
165 foreign import ccall unsafe "u_iswupper"
166 iswupper :: CInt -> CInt
168 foreign import ccall unsafe "u_towlower"
169 towlower :: CInt -> CInt
171 foreign import ccall unsafe "u_towupper"
172 towupper :: CInt -> CInt
174 foreign import ccall unsafe "u_towtitle"
175 towtitle :: CInt -> CInt
177 foreign import ccall unsafe "u_gencat"
178 wgencat :: CInt -> Int
180 -- -----------------------------------------------------------------------------
181 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
185 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
186 isPrint c = not (isControl c)
188 -- The upper case ISO characters have the multiplication sign dumped
189 -- randomly in the middle of the range. Go figure.
190 isUpper c = c >= 'A' && c <= 'Z' ||
191 c >= '\xC0' && c <= '\xD6' ||
192 c >= '\xD8' && c <= '\xDE'
193 -- The lower case ISO characters have the division sign dumped
194 -- randomly in the middle of the range. Go figure.
195 isLower c = c >= 'a' && c <= 'z' ||
196 c >= '\xDF' && c <= '\xF6' ||
197 c >= '\xF8' && c <= '\xFF'
199 isAlpha c = isLower c || isUpper c
200 isAlphaNum c = isAlpha c || isDigit c
202 -- Case-changing operations
205 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
207 -- fall-through to the slower stuff.
208 | isLower c && c /= '\xDF' && c /= '\xFF'
209 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
215 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
217 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')