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)
35 isAscii, isLatin1, isAsciiUpper, isAsciiLower :: Char -> Bool
36 isAscii c = c < '\x80'
37 isLatin1 c = c <= '\xff'
38 isAsciiLower c = c >= 'a' && c <= 'z'
39 isAsciiUpper c = c >= 'A' && c <= 'Z'
41 isControl, isPrint, isSpace, isUpper,
42 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphaNum
45 isOctDigit c = c >= '0' && c <= '7'
46 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
49 -- -----------------------------------------------------------------------------
50 -- Win32 implementation
52 #if defined(HAVE_WCTYPE_H) || mingw32_TARGET_OS
54 -- Use the wide-char classification functions if available. Glibc
55 -- seems to implement these properly, even for chars > 0xffff, as long
56 -- as you call setlocale() to set the locale to something other than
57 -- "C". Therefore, we call setlocale() in hs_init().
59 -- Win32 uses UTF-16, so presumably the system-supplied iswlower() and
60 -- friends won't work properly with characters > 0xffff. These
61 -- characters are represented as surrogate pairs in UTF-16.
63 type WInt = (#type wint_t)
64 type CInt = (#type int)
66 isDigit c = iswdigit (fromIntegral (ord c)) /= 0
67 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
68 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
69 isSpace c = iswspace (fromIntegral (ord c)) /= 0
70 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
71 isPrint c = iswprint (fromIntegral (ord c)) /= 0
72 isUpper c = iswupper (fromIntegral (ord c)) /= 0
73 isLower c = iswlower (fromIntegral (ord c)) /= 0
75 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
76 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
78 foreign import ccall unsafe "iswdigit"
79 iswdigit :: WInt -> CInt
81 foreign import ccall unsafe "iswalpha"
82 iswalpha :: WInt -> CInt
84 foreign import ccall unsafe "iswalnum"
85 iswalnum :: WInt -> CInt
87 foreign import ccall unsafe "iswcntrl"
88 iswcntrl :: WInt -> CInt
90 foreign import ccall unsafe "iswspace"
91 iswspace :: WInt -> CInt
93 foreign import ccall unsafe "iswprint"
94 iswprint :: WInt -> CInt
96 foreign import ccall unsafe "iswlower"
97 iswlower :: WInt -> CInt
99 foreign import ccall unsafe "iswupper"
100 iswupper :: WInt -> CInt
102 foreign import ccall unsafe "towlower"
103 towlower :: WInt -> WInt
105 foreign import ccall unsafe "towupper"
106 towupper :: WInt -> WInt
108 -- -----------------------------------------------------------------------------
109 -- No libunicode, so fall back to the ASCII-only implementation
113 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
114 isPrint c = not (isControl c)
116 -- isSpace includes non-breaking space
117 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
118 -- recursion with GHC.List elem
119 isSpace c = c == ' ' ||
127 -- The upper case ISO characters have the multiplication sign dumped
128 -- randomly in the middle of the range. Go figure.
129 isUpper c = c >= 'A' && c <= 'Z' ||
130 c >= '\xC0' && c <= '\xD6' ||
131 c >= '\xD8' && c <= '\xDE'
132 -- The lower case ISO characters have the division sign dumped
133 -- randomly in the middle of the range. Go figure.
134 isLower c = c >= 'a' && c <= 'z' ||
135 c >= '\xDF' && c <= '\xF6' ||
136 c >= '\xF8' && c <= '\xFF'
138 isAlpha c = isLower c || isUpper c
139 isDigit c = c >= '0' && c <= '9'
140 isAlphaNum c = isAlpha c || isDigit c
142 -- Case-changing operations
144 toUpper, toLower :: Char -> Char
146 | isAsciiLower c = C## (chr## (ord## c## -## 32##))
148 -- fall-through to the slower stuff.
149 | isLower c && c /= '\xDF' && c /= '\xFF'
150 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
156 | isAsciiUpper c = C## (chr## (ord## c## +## 32##))
158 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')