1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow, 2003
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC extensions)
12 -- Implementations for the character predicates (isLower, isUpper, etc.)
13 -- and the conversions (toUpper, toLower). The implementation uses
14 -- libunicode on Unix systems if that is available.
16 -----------------------------------------------------------------------------
19 isAscii, isLatin1, isControl,
20 isAsciiUpper, isAsciiLower,
21 isPrint, isSpace, isUpper,
22 isLower, isAlpha, isDigit,
23 isOctDigit, isHexDigit, isAlphaNum,
28 import GHC.Real (fromIntegral)
31 import GHC.Num (fromInteger)
33 #include "ghcconfig.h"
34 #include "HsBaseConfig.h"
36 -- | Selects the first 128 characters of the Unicode character set,
37 -- corresponding to the ASCII character set.
38 isAscii :: Char -> Bool
39 isAscii c = c < '\x80'
41 -- | Selects the first 256 characters of the Unicode character set,
42 -- corresponding to the ISO 8859-1 (Latin-1) character set.
43 isLatin1 :: Char -> Bool
44 isLatin1 c = c <= '\xff'
46 isAsciiUpper, isAsciiLower :: Char -> Bool
47 isAsciiLower c = c >= 'a' && c <= 'z'
48 isAsciiUpper c = c >= 'A' && c <= 'Z'
50 -- | Selects control characters, which are the non-printing characters of
51 -- the Latin-1 subset of Unicode.
52 isControl :: Char -> Bool
54 -- | Selects printable Unicode characters
55 -- (letters, numbers, marks, punctuation, symbols and spaces).
56 isPrint :: Char -> Bool
58 -- | Selects white-space characters in the Latin-1 range.
59 -- (In Unicode terms, this includes spaces and some control characters.)
60 isSpace :: Char -> Bool
61 -- isSpace includes non-breaking space
62 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
63 -- recursion with GHC.List elem
64 isSpace c = c == ' ' ||
72 -- | Selects alphabetic Unicode characters (letters) that are not lower-case.
73 -- (In Unicode terms, this includes letters in upper and title cases,
74 -- as well as modifier letters and other letters.)
75 isUpper :: Char -> Bool
77 -- | Selects lower-case alphabetic Unicode characters (letters).
78 isLower :: Char -> Bool
80 -- | Selects alphabetic Unicode characters (letters).
82 -- Note: the Haskell 98 definition of 'isAlpha' is:
84 -- > isAlpha c = isUpper c || isLower c
86 -- the implementation here diverges from the Haskell 98
87 -- definition in the sense that Unicode alphabetic characters which
88 -- are neither upper nor lower case will still be identified as
89 -- alphabetic by 'isAlpha'.
90 isAlpha :: Char -> Bool
92 -- | Selects alphabetic or numeric digit Unicode characters.
94 -- Note that numeric digits outside the ASCII range are selected by this
95 -- function but not by 'isDigit'. Such digits may be part of identifiers
96 -- but are not used by the printer and reader to represent numbers.
97 isAlphaNum :: Char -> Bool
99 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
100 isDigit :: Char -> Bool
102 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
103 isOctDigit :: Char -> Bool
104 isOctDigit c = c >= '0' && c <= '7'
106 -- | Selects ASCII hexadecimal digits,
107 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
108 isHexDigit :: Char -> Bool
109 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
112 -- | Convert a letter to the corresponding upper-case letter, leaving any
113 -- other character unchanged. Any Unicode letter which has an upper-case
114 -- equivalent is transformed.
115 toUpper :: Char -> Char
117 -- | Convert a letter to the corresponding lower-case letter, leaving any
118 -- other character unchanged. Any Unicode letter which has a lower-case
119 -- equivalent is transformed.
120 toLower :: Char -> Char
122 -- -----------------------------------------------------------------------------
123 -- Win32 implementation
125 #if (defined(HAVE_WCTYPE_H) && HAVE_ISWSPACE && defined(HTYPE_WINT_T)) || mingw32_HOST_OS
127 -- Use the wide-char classification functions if available. Glibc
128 -- seems to implement these properly, even for chars > 0xffff, as long
129 -- as you call setlocale() to set the locale to something other than
130 -- "C". Therefore, we call setlocale() in hs_init().
132 -- Win32 uses UTF-16, so presumably the system-supplied iswlower() and
133 -- friends won't work properly with characters > 0xffff. These
134 -- characters are represented as surrogate pairs in UTF-16.
136 type WInt = HTYPE_WINT_T
137 type CInt = HTYPE_INT
139 isDigit c = iswdigit (fromIntegral (ord c)) /= 0
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))))
151 foreign import ccall unsafe "iswdigit"
152 iswdigit :: WInt -> CInt
154 foreign import ccall unsafe "iswalpha"
155 iswalpha :: WInt -> CInt
157 foreign import ccall unsafe "iswalnum"
158 iswalnum :: WInt -> CInt
160 foreign import ccall unsafe "iswcntrl"
161 iswcntrl :: WInt -> CInt
163 foreign import ccall unsafe "iswspace"
164 iswspace :: WInt -> CInt
166 foreign import ccall unsafe "iswprint"
167 iswprint :: WInt -> CInt
169 foreign import ccall unsafe "iswlower"
170 iswlower :: WInt -> CInt
172 foreign import ccall unsafe "iswupper"
173 iswupper :: WInt -> CInt
175 foreign import ccall unsafe "towlower"
176 towlower :: WInt -> WInt
178 foreign import ccall unsafe "towupper"
179 towupper :: WInt -> WInt
181 -- -----------------------------------------------------------------------------
182 -- No libunicode, so fall back to the ASCII-only implementation
186 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
187 isPrint c = not (isControl c)
189 -- The upper case ISO characters have the multiplication sign dumped
190 -- randomly in the middle of the range. Go figure.
191 isUpper c = c >= 'A' && c <= 'Z' ||
192 c >= '\xC0' && c <= '\xD6' ||
193 c >= '\xD8' && c <= '\xDE'
194 -- The lower case ISO characters have the division sign dumped
195 -- randomly in the middle of the range. Go figure.
196 isLower c = c >= 'a' && c <= 'z' ||
197 c >= '\xDF' && c <= '\xF6' ||
198 c >= '\xF8' && c <= '\xFF'
200 isAlpha c = isLower c || isUpper c
201 isDigit c = c >= '0' && c <= '9'
202 isAlphaNum c = isAlpha c || isDigit c
204 -- Case-changing operations
207 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
209 -- fall-through to the slower stuff.
210 | isLower c && c /= '\xDF' && c /= '\xFF'
211 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
217 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
219 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')