1 {-# OPTIONS -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"
35 -- | Selects the first 128 characters of the Unicode character set,
36 -- corresponding to the ASCII character set.
37 isAscii :: Char -> Bool
38 isAscii c = c < '\x80'
40 -- | Selects the first 256 characters of the Unicode character set,
41 -- corresponding to the ISO 8859-1 (Latin-1) character set.
42 isLatin1 :: Char -> Bool
43 isLatin1 c = c <= '\xff'
45 isAsciiUpper, isAsciiLower :: Char -> Bool
46 isAsciiLower c = c >= 'a' && c <= 'z'
47 isAsciiUpper c = c >= 'A' && c <= 'Z'
49 -- | Selects control characters, which are the non-printing characters of
50 -- the Latin-1 subset of Unicode.
51 isControl :: Char -> Bool
53 -- | Selects printable Unicode characters
54 -- (letters, numbers, marks, punctuation, symbols and spaces).
55 isPrint :: Char -> Bool
57 -- | Selects white-space characters in the Latin-1 range.
58 -- (In Unicode terms, this includes spaces and some control characters.)
59 isSpace :: Char -> Bool
60 -- isSpace includes non-breaking space
61 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
62 -- recursion with GHC.List elem
63 isSpace c = c == ' ' ||
71 -- | Selects alphabetic Unicode characters (letters) that are not lower-case.
72 -- (In Unicode terms, this includes letters in upper and title cases,
73 -- as well as modifier letters and other letters.)
74 isUpper :: Char -> Bool
76 -- | Selects lower-case alphabetic Unicode characters (letters).
77 isLower :: Char -> Bool
79 -- | Selects alphabetic Unicode characters (letters).
80 isAlpha :: Char -> Bool
82 -- | Selects alphabetic or numeric digit Unicode characters.
84 -- Note that numeric digits outside the ASCII range are selected by this
85 -- function but not by 'isDigit'. Such digits may be part of identifiers
86 -- but are not used by the printer and reader to represent numbers.
87 isAlphaNum :: Char -> Bool
89 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
90 isDigit :: Char -> Bool
92 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
93 isOctDigit :: Char -> Bool
94 isOctDigit c = c >= '0' && c <= '7'
96 -- | Selects ASCII hexadecimal digits,
97 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
98 isHexDigit :: Char -> Bool
99 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
102 -- | Convert a letter to the corresponding upper-case letter, leaving any
103 -- other character unchanged. Any Unicode letter which has an upper-case
104 -- equivalent is transformed.
105 toUpper :: Char -> Char
107 -- | Convert a letter to the corresponding lower-case letter, leaving any
108 -- other character unchanged. Any Unicode letter which has a lower-case
109 -- equivalent is transformed.
110 toLower :: Char -> Char
112 -- -----------------------------------------------------------------------------
113 -- Win32 implementation
115 #if (defined(HAVE_WCTYPE_H) && HAVE_ISWSPACE && defined(HTYPE_WINT_T)) || mingw32_TARGET_OS
117 -- Use the wide-char classification functions if available. Glibc
118 -- seems to implement these properly, even for chars > 0xffff, as long
119 -- as you call setlocale() to set the locale to something other than
120 -- "C". Therefore, we call setlocale() in hs_init().
122 -- Win32 uses UTF-16, so presumably the system-supplied iswlower() and
123 -- friends won't work properly with characters > 0xffff. These
124 -- characters are represented as surrogate pairs in UTF-16.
126 type WInt = HTYPE_WINT_T
127 type CInt = HTYPE_INT
129 isDigit c = iswdigit (fromIntegral (ord c)) /= 0
130 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
131 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
132 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
133 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
134 isPrint c = iswprint (fromIntegral (ord c)) /= 0
135 isUpper c = iswupper (fromIntegral (ord c)) /= 0
136 isLower c = iswlower (fromIntegral (ord c)) /= 0
138 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
139 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
141 foreign import ccall unsafe "iswdigit"
142 iswdigit :: WInt -> CInt
144 foreign import ccall unsafe "iswalpha"
145 iswalpha :: WInt -> CInt
147 foreign import ccall unsafe "iswalnum"
148 iswalnum :: WInt -> CInt
150 foreign import ccall unsafe "iswcntrl"
151 iswcntrl :: WInt -> CInt
153 foreign import ccall unsafe "iswspace"
154 iswspace :: WInt -> CInt
156 foreign import ccall unsafe "iswprint"
157 iswprint :: WInt -> CInt
159 foreign import ccall unsafe "iswlower"
160 iswlower :: WInt -> CInt
162 foreign import ccall unsafe "iswupper"
163 iswupper :: WInt -> CInt
165 foreign import ccall unsafe "towlower"
166 towlower :: WInt -> WInt
168 foreign import ccall unsafe "towupper"
169 towupper :: WInt -> WInt
171 -- -----------------------------------------------------------------------------
172 -- No libunicode, so fall back to the ASCII-only implementation
176 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
177 isPrint c = not (isControl c)
179 -- The upper case ISO characters have the multiplication sign dumped
180 -- randomly in the middle of the range. Go figure.
181 isUpper c = c >= 'A' && c <= 'Z' ||
182 c >= '\xC0' && c <= '\xD6' ||
183 c >= '\xD8' && c <= '\xDE'
184 -- The lower case ISO characters have the division sign dumped
185 -- randomly in the middle of the range. Go figure.
186 isLower c = c >= 'a' && c <= 'z' ||
187 c >= '\xDF' && c <= '\xF6' ||
188 c >= '\xF8' && c <= '\xFF'
190 isAlpha c = isLower c || isUpper c
191 isDigit c = c >= '0' && c <= '9'
192 isAlphaNum c = isAlpha c || isDigit c
194 -- Case-changing operations
197 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
199 -- fall-through to the slower stuff.
200 | isLower c && c /= '\xDF' && c /= '\xFF'
201 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
207 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
209 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')