[project @ 2005-03-15 17:18:24 by ross]
[ghc-base.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 CInt = HTYPE_INT
130
131 isAlpha    c = iswalpha (fromIntegral (ord c)) /= 0
132 isAlphaNum c = iswalnum (fromIntegral (ord c)) /= 0
133 --isSpace    c = iswspace (fromIntegral (ord c)) /= 0
134 isControl  c = iswcntrl (fromIntegral (ord c)) /= 0
135 isPrint    c = iswprint (fromIntegral (ord c)) /= 0
136 isUpper    c = iswupper (fromIntegral (ord c)) /= 0
137 isLower    c = iswlower (fromIntegral (ord c)) /= 0
138
139 toLower c = chr (fromIntegral (towlower (fromIntegral (ord c))))
140 toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))
141 toTitle c = chr (fromIntegral (towtitle (fromIntegral (ord c))))
142
143 foreign import ccall unsafe "u_iswdigit"
144   iswdigit :: CInt -> CInt
145
146 foreign import ccall unsafe "u_iswalpha"
147   iswalpha :: CInt -> CInt
148
149 foreign import ccall unsafe "u_iswalnum"
150   iswalnum :: CInt -> CInt
151
152 foreign import ccall unsafe "u_iswcntrl"
153   iswcntrl :: CInt -> CInt
154
155 foreign import ccall unsafe "u_iswspace"
156   iswspace :: CInt -> CInt
157
158 foreign import ccall unsafe "u_iswprint"
159   iswprint :: CInt -> CInt
160
161 foreign import ccall unsafe "u_iswlower"
162   iswlower :: CInt -> CInt
163
164 foreign import ccall unsafe "u_iswupper"
165   iswupper :: CInt -> CInt
166
167 foreign import ccall unsafe "u_towlower"
168   towlower :: CInt -> CInt
169
170 foreign import ccall unsafe "u_towupper"
171   towupper :: CInt -> CInt
172
173 foreign import ccall unsafe "u_towtitle"
174   towtitle :: CInt -> CInt
175
176 foreign import ccall unsafe "u_gencat"
177   wgencat :: CInt -> Int
178
179 -- -----------------------------------------------------------------------------
180 -- No libunicode, so fall back to the ASCII-only implementation (never used, indeed)
181
182 #else
183
184 isControl c             =  c < ' ' || c >= '\DEL' && c <= '\x9f'
185 isPrint c               =  not (isControl c)
186
187 -- The upper case ISO characters have the multiplication sign dumped
188 -- randomly in the middle of the range.  Go figure.
189 isUpper c               =  c >= 'A' && c <= 'Z' || 
190                            c >= '\xC0' && c <= '\xD6' ||
191                            c >= '\xD8' && c <= '\xDE'
192 -- The lower case ISO characters have the division sign dumped
193 -- randomly in the middle of the range.  Go figure.
194 isLower c               =  c >= 'a' && c <= 'z' ||
195                            c >= '\xDF' && c <= '\xF6' ||
196                            c >= '\xF8' && c <= '\xFF'
197
198 isAlpha c               =  isLower c || isUpper c
199 isAlphaNum c            =  isAlpha c || isDigit c
200
201 -- Case-changing operations
202
203 toUpper c@(C# c#)
204   | isAsciiLower c    = C# (chr# (ord# c# -# 32#))
205   | isAscii c         = c
206     -- fall-through to the slower stuff.
207   | isLower c   && c /= '\xDF' && c /= '\xFF'
208   = unsafeChr (ord c `minusInt` ord 'a' `plusInt` ord 'A')
209   | otherwise
210   = c
211
212
213 toLower c@(C# c#)
214   | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
215   | isAscii c      = c
216   | isUpper c      = unsafeChr (ord c `minusInt` ord 'A' `plusInt` ord 'a')
217   | otherwise      =  c
218
219 #endif
220