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