|
1 | 1 | -- | A type and functions for single characters. |
2 | 2 | module Data.Char |
3 | | - ( Char(), |
4 | | - charString, |
5 | | - fromCharCode, |
6 | | - toCharCode |
| 3 | + ( Char() |
| 4 | + , charString |
| 5 | + , fromCharCode |
| 6 | + , toCharCode |
7 | 7 | ) where |
8 | 8 |
|
9 | | - --| A unicode character. |
10 | | - newtype Char = Char String |
11 | | - |
12 | | - -- | Returns the string of length `1` containing only the given character. |
13 | | - charString :: Char -> String |
14 | | - charString (Char s) = s |
15 | | - |
16 | | - -- | Returns the numeric Unicode value of the character. |
17 | | - foreign import toCharCode |
18 | | - """ |
19 | | - function toCharCode(c) { |
20 | | - return c.charCodeAt(0); |
21 | | - } |
22 | | - """ :: Char -> Number |
23 | | - |
24 | | - -- | Constructs a character from the given Unicode numeric value. |
25 | | - foreign import fromCharCode |
26 | | - """ |
27 | | - function fromCharCode(c) { |
28 | | - return String.fromCharCode(c); |
29 | | - } |
30 | | - """ :: Number -> Char |
31 | | - |
32 | | - -- | Characters can be compared for equality with `==` and `/=`. |
33 | | - instance eqChar :: Eq Char where |
34 | | - (==) (Char a) (Char b) = a == b |
35 | | - |
36 | | - (/=) a b = not (a == b) |
37 | | - |
38 | | - -- | Characters can be compared with `compare`, `>`, `>=`, `<` and `<=`. |
39 | | - instance ordChar :: Ord Char where |
40 | | - compare (Char a) (Char b) = a `compare` b |
41 | | - |
42 | | - -- | Characters can be rendered as a string with `show`. |
43 | | - instance showChar :: Show Char where |
44 | | - show (Char s) = "Char " ++ show s |
| 9 | +--| A unicode character. |
| 10 | +newtype Char = Char String |
| 11 | + |
| 12 | +-- | Returns the string of length `1` containing only the given character. |
| 13 | +charString :: Char -> String |
| 14 | +charString (Char s) = s |
| 15 | + |
| 16 | +-- | Returns the numeric Unicode value of the character. |
| 17 | +foreign import toCharCode |
| 18 | + """ |
| 19 | + function toCharCode(c) { |
| 20 | + return c.charCodeAt(0); |
| 21 | + } |
| 22 | + """ :: Char -> Number |
| 23 | + |
| 24 | +-- | Constructs a character from the given Unicode numeric value. |
| 25 | +foreign import fromCharCode |
| 26 | + """ |
| 27 | + function fromCharCode(c) { |
| 28 | + return String.fromCharCode(c); |
| 29 | + } |
| 30 | + """ :: Number -> Char |
| 31 | + |
| 32 | +-- | Characters can be compared for equality with `==` and `/=`. |
| 33 | +instance eqChar :: Eq Char where |
| 34 | + (==) (Char a) (Char b) = a == b |
| 35 | + |
| 36 | + (/=) a b = not (a == b) |
| 37 | + |
| 38 | +-- | Characters can be compared with `compare`, `>`, `>=`, `<` and `<=`. |
| 39 | +instance ordChar :: Ord Char where |
| 40 | + compare (Char a) (Char b) = a `compare` b |
| 41 | + |
| 42 | +instance boundedChar :: Bounded Char where |
| 43 | + top = fromCharCode 0 |
| 44 | + bottom = fromCharCode 65535 |
| 45 | + |
| 46 | +-- | Characters can be rendered as a string with `show`. |
| 47 | +instance showChar :: Show Char where |
| 48 | + show (Char s) = "Char " ++ show s |
0 commit comments