1 module PreludeCore ( Char(..) ) where
3 import Prel ( (.), (&&), chr, ord, otherwise, maxChar, minChar, not )
8 import List ( (++), map, takeWhile )
9 import PS ( _PackedString, _unpackPS )
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
21 ---------------------------------------------------------------
23 instance Eq Char where
27 instance Ord Char where
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 }
36 _tagCmp (C# a#) (C# b#)
37 = if (eqChar# a# b#) then _EQ
38 else if (ltChar# a# b#) then _LT else _GT
40 instance Ix Char where
41 range (c,c') = [c..c']
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'
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))
56 instance Text Char where
57 readsPrec p = readParen False
58 (\r -> [(c,t) | ('\'':s,t)<- lex r,
59 (c,_) <- readLitChar s])
61 showsPrec p '\'' = showString "'\\''"
62 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
64 readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
66 where readl ('"':s) = [("",s)]
67 readl ('\\':'&':s) = readl s
68 readl s = [(c:cs,u) | (c ,t) <- readLitChar s,
71 showList cs = showChar '"' . showl cs
72 where showl "" = showChar '"'
73 showl ('"':cs) = showString "\\\"" . showl cs
74 showl (c:cs) = showLitChar c . showl cs
76 instance _CCallable Char
77 instance _CReturnable Char
79 #if defined(__UNBOXED_INSTANCES__)
80 ---------------------------------------------------------------
81 -- Instances for Char#
82 ---------------------------------------------------------------
84 instance Eq Char# where
85 (==) x y = eqChar# x y
86 (/=) x y = neChar# x y
88 instance Ord Char# where
89 (<=) x y = leChar# x y
91 (>=) x y = geChar# x y
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 }
98 = if (eqChar# a b) then _EQ
99 else if (ltChar# a b) then _LT else _GT
101 instance Ix Char# where
102 range (c,c') = [c..c']
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'
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))
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)
124 instance _CCallable Char#
125 instance _CReturnable Char#
127 #endif {-UNBOXED INSTANCES-}