aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/char.lux
blob: 0db90898e573ed4130d2bb4ed95dfc0588684446 (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
(;module:
  lux
  (lux/control eq
               [ord]
               codec
               hash)
  (.. [text "Text/" Monoid<Text>]))

## [Structures]
(struct: #export _ (Eq Char)
  (def: (= x y)
    (_lux_proc ["char" "="] [x y])))

(struct: #export _ (Hash Char)
  (def: eq Eq<Char>)
  (def: (hash input)
    (_lux_proc ["char" "to-nat"] [input])))

(struct: #export _ (ord;Ord Char)
  (def: eq Eq<Char>)

  (def: (< test subject)
    (_lux_proc ["char" "<"] [subject test]))

  (def: (<= test subject)
    (or (_lux_proc ["char" "="] [subject test])
        (_lux_proc ["char" "<"] [subject test])))

  (def: (> test subject)
    (_lux_proc ["char" "<"] [test subject]))

  (def: (>= test subject)
    (or (_lux_proc ["char" "="] [test subject])
        (_lux_proc ["char" "<"] [test subject])))
  )

(struct: #export _ (Codec Text Char)
  (def: (encode x)
    (let [as-text (case x
                    #"\t" "\\t"
                    #"\v" "\\v"
                    #"\b" "\\b"
                    #"\n" "\\n"
                    #"\r" "\\r"
                    #"\f" "\\f"
                    #"\"" "\\\""
                    #"\\" "\\\\"
                    _     (_lux_proc ["char" "to-text"] [x]))]
      ($_ Text/append "#\"" as-text "\"")))

  (def: (decode y)
    (let [size (text;size y)]
      (if (and (text;starts-with? "#\"" y)
               (text;ends-with? "\"" y)
               (or (n.= +4 size)
                   (n.= +5 size)))
        (if (n.= +4 size)
          (case (text;nth +2 y)
            #;None
            (#;Left (Text/append "Wrong syntax for Char: " y))

            (#;Some char)
            (#;Right char))
          (case [(text;nth +2 y) (text;nth +3 y)]
            [(#;Some #"\\") (#;Some char)]
            (case char
              #"t" (#;Right #"\t")
              #"v" (#;Right #"\v")
              #"b" (#;Right #"\b")
              #"n" (#;Right #"\n")
              #"r" (#;Right #"\r")
              #"f" (#;Right #"\f")
              #"\"" (#;Right #"\"")
              #"\\" (#;Right #"\\")
              _    (#;Left (Text/append "Wrong syntax for Char: " y)))

            _
            (#;Left (Text/append "Wrong syntax for Char: " y))))
        (#;Left (Text/append "Wrong syntax for Char: " y))))))

## [Values]
(def: #export (space? char)
  {#;doc "Checks whether the character is white-space."}
  (-> Char Bool)
  (case char
    (^or #"\t" #"\v" #" " #"\n" #"\r" #"\f")
    true

    _
    false))

(def: #export (as-text x)
  (-> Char Text)
  (_lux_proc ["char" "to-text"] [x]))

(def: #export (char x)
  (-> Nat Char)
  (_lux_proc ["nat" "to-char"] [x]))

(def: #export (code x)
  (-> Char Nat)
  (_lux_proc ["char" "to-nat"] [x]))