summaryrefslogtreecommitdiff
path: root/ghc/lib/prelude/IChar.hs
blob: 01d30425aab6052827892d6d4d71cd12b20e6a9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module PreludeCore ( Char(..) ) where

import Prel		( (.), (&&), chr, ord, otherwise, maxChar, minChar, not )
import Cls
import Core
import IInt
import IList
import List		( map, (++), foldr )
import PS		( _PackedString, _unpackPS )
import Text

gtChar	(C# x) (C# y) = gtChar# x y
geChar	(C# x) (C# y) = geChar# x y
eqChar	(C# x) (C# y) = eqChar# x y
neChar	(C# x) (C# y) = neChar# x y
ltChar	(C# x) (C# y) = ltChar# x y
leChar	(C# x) (C# y) = leChar# x y

---------------------------------------------------------------

instance  Eq Char  where
    (==) x y = eqChar x y
    (/=) x y = neChar x y

instance  Ord Char  where
    (<=) x y = leChar x y
    (<)	 x y = ltChar x y
    (>=) x y = geChar x y
    (>)  x y = gtChar x y

    max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
    min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }

    _tagCmp (C# a#) (C# b#)
      = if      (eqChar# a# b#) then _EQ
	else if (ltChar# a# b#) then _LT else _GT

instance  Ix Char  where
    range (c,c')	=  [c..c']
    index b@(c,c') ci
	| inRange b ci	=  ord ci - ord c
	| otherwise	=  error "Ix.Char.index{PreludeCore}: Index out of range\n"
    inRange (c,c') ci	=  ord c <= i && i <= ord c'
			   where i = ord ci

instance  Enum Char  where
    enumFrom c		=  map chr [ord c .. ord maxChar]
    enumFromThen c c'	=  map chr [ord c, ord c' .. ord lastChar]
			   where lastChar = if c' < c then minChar else maxChar

instance  Text Char  where
    readsPrec p      = readParen False
    	    	    	    (\r -> [(c,t) | ('\'':s,t)<- lex r,
					    (c,_)     <- readLitChar s])

    showsPrec p '\'' = showString "'\\''"
    showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''

    readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
					       (l,_)      <- readl s ])
	       where readl ('"':s)	= [("",s)]
		     readl ('\\':'&':s)	= readl s
		     readl s		= [(c:cs,u) | (c ,t) <- readLitChar s,
						      (cs,u) <- readl t	      ]

    showList cs = showChar '"' . showl cs
		 where showl ""       = showChar '"'
		       showl ('"':cs) = showString "\\\"" . showl cs
		       showl (c:cs)   = showLitChar c . showl cs

instance _CCallable   Char
instance _CReturnable Char

#if defined(__UNBOXED_INSTANCES__)
---------------------------------------------------------------
-- Instances for Char#
---------------------------------------------------------------

instance  Eq Char#  where
    (==) x y = eqChar# x y
    (/=) x y = neChar# x y

instance  Ord Char#  where
    (<=) x y = leChar# x y
    (<)	 x y = ltChar# x y
    (>=) x y = geChar# x y
    (>)  x y = gtChar# x y

    max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
    min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }

    _tagCmp a b
      = if      (eqChar# a b) then _EQ
	else if (ltChar# a b) then _LT else _GT

instance  Ix Char#  where
    range (c,c')	=  [c..c']
    index b@(c,c') ci
	| inRange b ci	=  I# (ord# ci - ord# c)
	| otherwise	=  error "Ix.Char#.index{PreludeCore}: Index out of range\n"
    inRange (c,c') ci	=  ord# c <= i && i <= ord# c'
			   where i = ord# ci

instance  Enum Char#  where
    enumFrom c		 =  map chr# [ord# c .. ord# '\255'#]
    enumFromThen c c'	 =  map chr# [ord# c, ord# c' .. ord# lastChar#]
			    where lastChar# = if c' < c then '\0'# else '\255'#
    -- default methods not specialised!
    enumFromTo n m	 =  takeWhile (<= m) (enumFrom n)
    enumFromThenTo n m p =  takeWhile (if m >= n then (<= p) else (>= p))
				      (enumFromThen n m)

-- ToDo: efficient Text Char# instance
instance  Text Char#  where
    readsPrec p s = map (\ (C# c#, s) -> (c#, s)) (readsPrec p s)
    showsPrec p c = showsPrec p (C# c)
    readList s = map (\ (x, s) -> (map (\ (C# c#) -> c#) x, s)) (readList s)
    showList l = showList (map C# l)

instance _CCallable   Char#
instance _CReturnable Char#

#endif {-UNBOXED INSTANCES-}