1 module PreludeCore ( Char(..) ) where
3 import Prel ( (.), (&&), chr, ord, otherwise, maxChar, minChar, not )
8 import List ( map, (++), foldr )
9 import PS ( _PackedString, _unpackPS )
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
19 ---------------------------------------------------------------
21 instance Eq Char where
25 instance Ord Char where
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 }
34 _tagCmp (C# a#) (C# b#)
35 = if (eqChar# a# b#) then _EQ
36 else if (ltChar# a# b#) then _LT else _GT
38 instance Ix Char where
39 range (c,c') = [c..c']
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'
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
51 instance Text Char where
52 readsPrec p = readParen False
53 (\r -> [(c,t) | ('\'':s,t)<- lex r,
54 (c,_) <- readLitChar s])
56 showsPrec p '\'' = showString "'\\''"
57 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
59 readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
61 where readl ('"':s) = [("",s)]
62 readl ('\\':'&':s) = readl s
63 readl s = [(c:cs,u) | (c ,t) <- readLitChar s,
66 showList cs = showChar '"' . showl cs
67 where showl "" = showChar '"'
68 showl ('"':cs) = showString "\\\"" . showl cs
69 showl (c:cs) = showLitChar c . showl cs
71 instance _CCallable Char
72 instance _CReturnable Char
74 #if defined(__UNBOXED_INSTANCES__)
75 ---------------------------------------------------------------
76 -- Instances for Char#
77 ---------------------------------------------------------------
79 instance Eq Char# where
80 (==) x y = eqChar# x y
81 (/=) x y = neChar# x y
83 instance Ord Char# where
84 (<=) x y = leChar# x y
86 (>=) x y = geChar# x y
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 }
93 = if (eqChar# a b) then _EQ
94 else if (ltChar# a b) then _LT else _GT
96 instance Ix Char# where
97 range (c,c') = [c..c']
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'
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))
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)
120 instance _CCallable Char#
121 instance _CReturnable Char#
123 #endif {-UNBOXED INSTANCES-}