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