[project @ 2005-01-28 15:03:06 by simonmar]
[packages/pretty.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 "HsBaseConfig.h"
34
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'
39
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'
44
45 isAsciiUpper, isAsciiLower :: Char -> Bool
46 isAsciiLower c = c >= 'a' && c <= 'z'
47 isAsciiUpper c = c >= 'A' && c <= 'Z'
48
49 -- | Selects control characters, which are the non-printing characters of
50 -- the Latin-1 subset of Unicode.
51 isControl :: Char -> Bool
52
53 -- | Selects printable Unicode characters
54 -- (letters, numbers, marks, punctuation, symbols and spaces).
55 isPrint :: Char -> Bool
56
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 == ' ' ||
64 c == '\t' ||
65 c == '\n' ||
66 c == '\r' ||
67 c == '\f' ||
68 c == '\v' ||
69 c == '\xa0'
70
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
75
76 -- | Selects lower-case alphabetic Unicode characters (letters).
77 isLower :: Char -> Bool
78
79 -- | Selects alphabetic Unicode characters (letters).
80 --
81 -- Note: the Haskell 98 definition of 'isAlpha' is:
82 --
83 -- > isAlpha c = isUpper c || isLower c
84 --
85 -- the implementation here diverges from the Haskell 98
86 -- definition in the sense that Unicode alphabetic characters which
87 -- are neither upper nor lower case will still be identified as
88 -- alphabetic by 'isAlpha'.
89 isAlpha :: Char -> Bool
90
91 -- | Selects alphabetic or numeric digit Unicode characters.
92 --
93 -- Note that numeric digits outside the ASCII range are selected by this
94 -- function but not by 'isDigit'. Such digits may be part of identifiers
95 -- but are not used by the printer and reader to represent numbers.
96 isAlphaNum :: Char -> Bool
97
98 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
99 isDigit :: Char -> Bool
100
101 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
102 isOctDigit :: Char -> Bool
103 isOctDigit c = c >= '0' && c <= '7'
104
105 -- | Selects ASCII hexadecimal digits,
106 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
107 isHexDigit :: Char -> Bool
108 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
109 c >= 'a' && c <= 'f'
110
111 -- | Convert a letter to the corresponding upper-case letter, leaving any
112 -- other character unchanged. Any Unicode letter which has an upper-case
113 -- equivalent is transformed.
114 toUpper :: Char -> Char
115
116 -- | Convert a letter to the corresponding lower-case letter, leaving any
117 -- other character unchanged. Any Unicode letter which has a lower-case
118 -- equivalent is transformed.
119 toLower :: Char -> Char
120
121 -- -----------------------------------------------------------------------------
122 -- Win32 implementation
123
124 #if (defined(HAVE_WCTYPE_H) && HAVE_ISWSPACE && defined(HTYPE_WINT_T)) || mingw32_HOST_OS
125
126 -- Use the wide-char classification functions if available. Glibc
127 -- seems to implement these properly, even for chars > 0xffff, as long
128 -- as you call setlocale() to set the locale to something other than
129 -- "C". Therefore, we call setlocale() in hs_init().
130
131 -- Win32 uses UTF-16, so presumably the system-supplied iswlower() and
132 -- friends won't work properly with characters > 0xffff. These
133 -- characters are represented as surrogate pairs in UTF-16.
134
135 type WInt = HTYPE_WINT_T
136 type CInt = HTYPE_INT
137
138 isDigit c = iswdigit (fromIntegral (ord c)) /= 0
139 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
140 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
141 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
142 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
143 isPrint c = iswprint (fromIntegral (ord c)) /= 0
144 isUpper c = iswupper (fromIntegral (ord c)) /= 0
145 isLower c = iswlower (fromIntegral (ord c)) /= 0
146
147 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
148 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
149
150 foreign import ccall unsafe "iswdigit"
151 iswdigit :: WInt -> CInt
152
153 foreign import ccall unsafe "iswalpha"
154 iswalpha :: WInt -> CInt
155
156 foreign import ccall unsafe "iswalnum"
157 iswalnum :: WInt -> CInt
158
159 foreign import ccall unsafe "iswcntrl"
160 iswcntrl :: WInt -> CInt
161
162 foreign import ccall unsafe "iswspace"
163 iswspace :: WInt -> CInt
164
165 foreign import ccall unsafe "iswprint"
166 iswprint :: WInt -> CInt
167
168 foreign import ccall unsafe "iswlower"
169 iswlower :: WInt -> CInt
170
171 foreign import ccall unsafe "iswupper"
172 iswupper :: WInt -> CInt
173
174 foreign import ccall unsafe "towlower"
175 towlower :: WInt -> WInt
176
177 foreign import ccall unsafe "towupper"
178 towupper :: WInt -> WInt
179
180 -- -----------------------------------------------------------------------------
181 -- No libunicode, so fall back to the ASCII-only implementation
182
183 #else
184
185 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
186 isPrint c = not (isControl c)
187
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'
198
199 isAlpha c = isLower c || isUpper c
200 isDigit c = c >= '0' && c <= '9'
201 isAlphaNum c = isAlpha c || isDigit c
202
203 -- Case-changing operations
204
205 toUpper c@(C# c#)
206 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
207 | isAscii c = c
208 -- fall-through to the slower stuff.
209 | isLower c && c /= '\xDF' && c /= '\xFF'
210 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
211 | otherwise
212 = c
213
214
215 toLower c@(C# c#)
216 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
217 | isAscii c = c
218 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
219 | otherwise = c
220
221 #endif
222