[project @ 2005-03-14 18:02:48 by ross]
[packages/pretty.git] / GHC / Unicode.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 {-# OPTIONS -#include "WCsubst.h" #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module : GHC.Unicode
6 -- Copyright : (c) The University of Glasgow, 2003
7 -- License : see libraries/base/LICENSE
8 --
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC extensions)
12 --
13 -- Implementations for the character predicates (isLower, isUpper, etc.)
14 -- and the conversions (toUpper, toLower). The implementation uses
15 -- libunicode on Unix systems if that is available.
16 --
17 -----------------------------------------------------------------------------
18
19 module GHC.Unicode (
20 isAscii, isLatin1, isControl,
21 isAsciiUpper, isAsciiLower,
22 isPrint, isSpace, isUpper,
23 isLower, isAlpha, isDigit,
24 isOctDigit, isHexDigit, isAlphaNum,
25 toUpper, toLower, toTitle,
26 wgencat,
27 ) where
28
29 import GHC.Base
30 import GHC.Real (fromIntegral)
31 import GHC.Int
32 import GHC.Word
33 import GHC.Num (fromInteger)
34
35 #include "HsBaseConfig.h"
36
37 -- | Selects the first 128 characters of the Unicode character set,
38 -- corresponding to the ASCII character set.
39 isAscii :: Char -> Bool
40 isAscii c = c < '\x80'
41
42 -- | Selects the first 256 characters of the Unicode character set,
43 -- corresponding to the ISO 8859-1 (Latin-1) character set.
44 isLatin1 :: Char -> Bool
45 isLatin1 c = c <= '\xff'
46
47 isAsciiUpper, isAsciiLower :: Char -> Bool
48 isAsciiLower c = c >= 'a' && c <= 'z'
49 isAsciiUpper c = c >= 'A' && c <= 'Z'
50
51 -- | Selects control characters, which are the non-printing characters of
52 -- the Latin-1 subset of Unicode.
53 isControl :: Char -> Bool
54
55 -- | Selects printable Unicode characters
56 -- (letters, numbers, marks, punctuation, symbols and spaces).
57 isPrint :: Char -> Bool
58
59 -- | Selects white-space characters in the Latin-1 range.
60 -- (In Unicode terms, this includes spaces and some control characters.)
61 isSpace :: Char -> Bool
62 -- isSpace includes non-breaking space
63 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
64 -- recursion with GHC.List elem
65 isSpace c = c == ' ' ||
66 c == '\t' ||
67 c == '\n' ||
68 c == '\r' ||
69 c == '\f' ||
70 c == '\v' ||
71 c == '\xa0' ||
72 iswspace (fromIntegral (ord c)) /= 0
73
74 -- | Selects alphabetic Unicode characters (letters) that are not lower-case.
75 -- (In Unicode terms, this includes letters in upper and title cases,
76 -- as well as modifier letters and other letters.)
77 isUpper :: Char -> Bool
78
79 -- | Selects lower-case alphabetic Unicode characters (letters).
80 isLower :: Char -> Bool
81
82 -- | Selects alphabetic Unicode characters (letters).
83 isAlpha :: Char -> Bool
84
85 -- | Selects alphabetic or numeric digit Unicode characters.
86 --
87 -- Note that numeric digits outside the ASCII range are selected by this
88 -- function but not by 'isDigit'. Such digits may be part of identifiers
89 -- but are not used by the printer and reader to represent numbers.
90 isAlphaNum :: Char -> Bool
91
92 -- | Selects ASCII digits, i.e. @\'0\'@..@\'9\'@.
93 isDigit :: Char -> Bool
94 isDigit c = c >= '0' && c <= '9'
95
96 -- | Selects ASCII octal digits, i.e. @\'0\'@..@\'7\'@.
97 isOctDigit :: Char -> Bool
98 isOctDigit c = c >= '0' && c <= '7'
99
100 -- | Selects ASCII hexadecimal digits,
101 -- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
102 isHexDigit :: Char -> Bool
103 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
104 c >= 'a' && c <= 'f'
105
106 -- | Convert a letter to the corresponding upper-case letter, leaving any
107 -- other character unchanged. Any Unicode letter which has an upper-case
108 -- equivalent is transformed.
109 toUpper :: Char -> Char
110
111 -- | Convert a letter to the corresponding lower-case letter, leaving any
112 -- other character unchanged. Any Unicode letter which has a lower-case
113 -- equivalent is transformed.
114 toLower :: Char -> Char
115
116 -- | Convert a letter to the corresponding title-case letter, leaving any
117 -- other character unchanged. Any Unicode letter which has a lower-case
118 -- equivalent is transformed.
119 toTitle :: Char -> Char
120
121 -- -----------------------------------------------------------------------------
122 -- Implementation with the supplied auto-generated Unicode character properties
123 -- table (default)
124
125 #if 1
126
127 -- Regardless of the O/S and Library, use the functions contained in WCsubst.c
128
129 type WInt = HTYPE_WINT_T
130 type CInt = HTYPE_INT
131
132 isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
133 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
134 --isSpace c = iswspace (fromIntegral (ord c)) /= 0
135 isControl c = iswcntrl (fromIntegral (ord c)) /= 0
136 isPrint c = iswprint (fromIntegral (ord c)) /= 0
137 isUpper c = iswupper (fromIntegral (ord c)) /= 0
138 isLower c = iswlower (fromIntegral (ord c)) /= 0
139
140 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
141 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
142 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
143
144 foreign import ccall unsafe "u_iswdigit"
145 iswdigit :: CInt -> CInt
146
147 foreign import ccall unsafe "u_iswalpha"
148 iswalpha :: CInt -> CInt
149
150 foreign import ccall unsafe "u_iswalnum"
151 iswalnum :: CInt -> CInt
152
153 foreign import ccall unsafe "u_iswcntrl"
154 iswcntrl :: CInt -> CInt
155
156 foreign import ccall unsafe "u_iswspace"
157 iswspace :: CInt -> CInt
158
159 foreign import ccall unsafe "u_iswprint"
160 iswprint :: CInt -> CInt
161
162 foreign import ccall unsafe "u_iswlower"
163 iswlower :: CInt -> CInt
164
165 foreign import ccall unsafe "u_iswupper"
166 iswupper :: CInt -> CInt
167
168 foreign import ccall unsafe "u_towlower"
169 towlower :: CInt -> CInt
170
171 foreign import ccall unsafe "u_towupper"
172 towupper :: CInt -> CInt
173
174 foreign import ccall unsafe "u_towtitle"
175 towtitle :: CInt -> CInt
176
177 foreign import ccall unsafe "u_gencat"
178 wgencat :: CInt -> Int
179
180 -- -----------------------------------------------------------------------------
181 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
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 isAlphaNum c = isAlpha c || isDigit c
201
202 -- Case-changing operations
203
204 toUpper c@(C# c#)
205 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
206 | isAscii c = c
207 -- fall-through to the slower stuff.
208 | isLower c && c /= '\xDF' && c /= '\xFF'
209 = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
210 | otherwise
211 = c
212
213
214 toLower c@(C# c#)
215 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
216 | isAscii c = c
217 | isUpper c = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
218 | otherwise = c
219
220 #endif
221