[project @ 2005-01-26 14:55:41 by simonmar]
[packages/old-time.git] / GHC / Unicode.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : GHC.Unicde
5 -- Copyright : (c) The University of Glasgow, 2003
6 -- License : see libraries/base/LICENSE
7 --
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC extensions)
11 --
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.
15 --
16 -----------------------------------------------------------------------------
17
18 module GHC.Unicode (
19 isAscii, isLatin1, isControl,
20 isAsciiUpper, isAsciiLower,
21 isPrint, isSpace, isUpper,
22 isLower, isAlpha, isDigit,
23 isOctDigit, isHexDigit, isAlphaNum,
24 toUpper, toLower,
25 ) where
26
27 import GHC.Base
28 import GHC.Real (fromIntegral)
29 import GHC.Int
30 import GHC.Word
31 import GHC.Num (fromInteger)
32
33 #include "ghcconfig.h"
34 #include "HsBaseConfig.h"
35
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'
40
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'
45
46 isAsciiUpper, isAsciiLower :: Char -> Bool
47 isAsciiLower c = c >= 'a' && c <= 'z'
48 isAsciiUpper c = c >= 'A' && c <= 'Z'
49
50 -- | Selects control characters, which are the non-printing characters of
51 -- the Latin-1 subset of Unicode.
52 isControl :: Char -> Bool
53
54 -- | Selects printable Unicode characters
55 -- (letters, numbers, marks, punctuation, symbols and spaces).
56 isPrint :: Char -> Bool
57
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 == ' ' ||
65 c == '\t' ||
66 c == '\n' ||
67 c == '\r' ||
68 c == '\f' ||
69 c == '\v' ||
70 c == '\xa0'
71
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
76
77 -- | Selects lower-case alphabetic Unicode characters (letters).
78 isLower :: Char -> Bool
79
80 -- | Selects alphabetic Unicode characters (letters).
81 --
82 -- Note: the Haskell 98 definition of 'isAlpha' is:
83 --
84 -- > isAlpha c = isUpper c || isLower c
85 --
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
91
92 -- | Selects alphabetic or numeric digit Unicode characters.
93 --
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
98
99 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
100 isDigit :: Char -> Bool
101
102 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
103 isOctDigit :: Char -> Bool
104 isOctDigit c = c >= '0' && c <= '7'
105
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' ||
110 c >= 'a' && c <= 'f'
111
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
116
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
121
122 -- -----------------------------------------------------------------------------
123 -- Win32 implementation
124
125 #if (defined(HAVE_WCTYPE_H) && HAVE_ISWSPACE && defined(HTYPE_WINT_T)) || mingw32_TARGET_OS
126
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().
131
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.
135
136 type WInt = HTYPE_WINT_T
137 type CInt = HTYPE_INT
138
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
147
148 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
149 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
150
151 foreign import ccall unsafe "iswdigit"
152 iswdigit :: WInt -> CInt
153
154 foreign import ccall unsafe "iswalpha"
155 iswalpha :: WInt -> CInt
156
157 foreign import ccall unsafe "iswalnum"
158 iswalnum :: WInt -> CInt
159
160 foreign import ccall unsafe "iswcntrl"
161 iswcntrl :: WInt -> CInt
162
163 foreign import ccall unsafe "iswspace"
164 iswspace :: WInt -> CInt
165
166 foreign import ccall unsafe "iswprint"
167 iswprint :: WInt -> CInt
168
169 foreign import ccall unsafe "iswlower"
170 iswlower :: WInt -> CInt
171
172 foreign import ccall unsafe "iswupper"
173 iswupper :: WInt -> CInt
174
175 foreign import ccall unsafe "towlower"
176 towlower :: WInt -> WInt
177
178 foreign import ccall unsafe "towupper"
179 towupper :: WInt -> WInt
180
181 -- -----------------------------------------------------------------------------
182 -- No libunicode, so fall back to the ASCII-only implementation
183
184 #else
185
186 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
187 isPrint c = not (isControl c)
188
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'
199
200 isAlpha c = isLower c || isUpper c
201 isDigit c = c >= '0' && c <= '9'
202 isAlphaNum c = isAlpha c || isDigit c
203
204 -- Case-changing operations
205
206 toUpper c@(C# c#)
207 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
208 | isAscii c = c
209 -- fall-through to the slower stuff.
210 | isLower c && c /= '\xDF' && c /= '\xFF'
211 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
212 | otherwise
213 = c
214
215
216 toLower c@(C# c#)
217 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
218 | isAscii c = c
219 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
220 | otherwise = c
221
222 #endif
223