[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / lib / prelude / IChar.hs
1 module PreludeCore ( Char(..) ) where
2
3 import Prel             ( (.), (&&), chr, ord, otherwise, maxChar, minChar, not )
4 import Cls
5 import Core
6 import IInt
7 import IList
8 import List             ( map, (++), foldr )
9 import PS               ( _PackedString, _unpackPS )
10 import Text
11
12 gtChar  (C# x) (C# y) = gtChar# x y
13 geChar  (C# x) (C# y) = geChar# x y
14 eqChar  (C# x) (C# y) = eqChar# x y
15 neChar  (C# x) (C# y) = neChar# x y
16 ltChar  (C# x) (C# y) = ltChar# x y
17 leChar  (C# x) (C# y) = leChar# x y
18
19 ---------------------------------------------------------------
20
21 instance  Eq Char  where
22     (==) x y = eqChar x y
23     (/=) x y = neChar x y
24
25 instance  Ord Char  where
26     (<=) x y = leChar x y
27     (<)  x y = ltChar x y
28     (>=) x y = geChar x y
29     (>)  x y = gtChar x y
30
31     max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
32     min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }
33
34     _tagCmp (C# a#) (C# b#)
35       = if      (eqChar# a# b#) then _EQ
36         else if (ltChar# a# b#) then _LT else _GT
37
38 instance  Ix Char  where
39     range (c,c')        =  [c..c']
40     index b@(c,c') ci
41         | inRange b ci  =  ord ci - ord c
42         | otherwise     =  error "Ix.Char.index{PreludeCore}: Index out of range\n"
43     inRange (c,c') ci   =  ord c <= i && i <= ord c'
44                            where i = ord ci
45
46 instance  Enum Char  where
47     enumFrom c          =  map chr [ord c .. ord maxChar]
48     enumFromThen c c'   =  map chr [ord c, ord c' .. ord lastChar]
49                            where lastChar = if c' < c then minChar else maxChar
50
51 instance  Text Char  where
52     readsPrec p      = readParen False
53                             (\r -> [(c,t) | ('\'':s,t)<- lex r,
54                                             (c,_)     <- readLitChar s])
55
56     showsPrec p '\'' = showString "'\\''"
57     showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''
58
59     readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
60                                                (l,_)      <- readl s ])
61                where readl ('"':s)      = [("",s)]
62                      readl ('\\':'&':s) = readl s
63                      readl s            = [(c:cs,u) | (c ,t) <- readLitChar s,
64                                                       (cs,u) <- readl t       ]
65
66     showList cs = showChar '"' . showl cs
67                  where showl ""       = showChar '"'
68                        showl ('"':cs) = showString "\\\"" . showl cs
69                        showl (c:cs)   = showLitChar c . showl cs
70
71 instance _CCallable   Char
72 instance _CReturnable Char
73
74 #if defined(__UNBOXED_INSTANCES__)
75 ---------------------------------------------------------------
76 -- Instances for Char#
77 ---------------------------------------------------------------
78
79 instance  Eq Char#  where
80     (==) x y = eqChar# x y
81     (/=) x y = neChar# x y
82
83 instance  Ord Char#  where
84     (<=) x y = leChar# x y
85     (<)  x y = ltChar# x y
86     (>=) x y = geChar# x y
87     (>)  x y = gtChar# x y
88
89     max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
90     min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }
91
92     _tagCmp a b
93       = if      (eqChar# a b) then _EQ
94         else if (ltChar# a b) then _LT else _GT
95
96 instance  Ix Char#  where
97     range (c,c')        =  [c..c']
98     index b@(c,c') ci
99         | inRange b ci  =  I# (ord# ci - ord# c)
100         | otherwise     =  error "Ix.Char#.index{PreludeCore}: Index out of range\n"
101     inRange (c,c') ci   =  ord# c <= i && i <= ord# c'
102                            where i = ord# ci
103
104 instance  Enum Char#  where
105     enumFrom c           =  map chr# [ord# c .. ord# '\255'#]
106     enumFromThen c c'    =  map chr# [ord# c, ord# c' .. ord# lastChar#]
107                             where lastChar# = if c' < c then '\0'# else '\255'#
108     -- default methods not specialised!
109     enumFromTo n m       =  takeWhile (<= m) (enumFrom n)
110     enumFromThenTo n m p =  takeWhile (if m >= n then (<= p) else (>= p))
111                                       (enumFromThen n m)
112
113 -- ToDo: efficient Text Char# instance
114 instance  Text Char#  where
115     readsPrec p s = map (\ (C# c#, s) -> (c#, s)) (readsPrec p s)
116     showsPrec p c = showsPrec p (C# c)
117     readList s = map (\ (x, s) -> (map (\ (C# c#) -> c#) x, s)) (readList s)
118     showList l = showList (map C# l)
119
120 instance _CCallable   Char#
121 instance _CReturnable Char#
122
123 #endif {-UNBOXED INSTANCES-}