aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text/buffer.lux
blob: 54adf6087cdcabfae497a2e51d84314393780ff1 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(.require
 [library
  [lux (.except)
   ["[0]" ffi (.only import)]
   [control
    ["[0]" function]]
   [data
    ["[0]" product]
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" array]
     ["[0]" sequence (.only Sequence) (.use "[1]#[0]" mix)]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [type
     ["[0]" nominal (.except def)]]
    [compiler
     ["@" target]]]]]
 ["[0]" //])

(with_expansions [<jvm> (these (import java/lang/CharSequence
                                 "[1]::[0]")

                               (import java/lang/Appendable
                                 "[1]::[0]"
                                 (append [java/lang/CharSequence] java/lang/Appendable))

                               (import java/lang/String
                                 "[1]::[0]"
                                 (new [int])
                                 (toString [] java/lang/String))

                               (import java/lang/StringBuilder
                                 "[1]::[0]"
                                 (new [int])
                                 (toString [] java/lang/String)))]
  (`` (for @.old (these <jvm>)
           @.jvm (these <jvm>)
           @.js (these (import (JS_Array a)
                         "[1]::[0]"
                         (push [a] a)
                         (join [Text] Text)))
           @.lua (these (import (table/concat [(array.Array Text) Text] Text))
                        ...https://www.lua.org/manual/5.3/manual.html#pdf-table.concat
                        (import (table/insert [(array.Array Text) Text] "?" Nothing))
                        ... https://www.lua.org/manual/5.3/manual.html#pdf-table.insert
                        )
           (these))))

(`` (nominal.def .public Buffer
      (for @.old [Nat (-> java/lang/StringBuilder java/lang/StringBuilder)]
           @.jvm [Nat (-> java/lang/StringBuilder java/lang/StringBuilder)]
           @.js [Nat (-> (JS_Array Text) (JS_Array Text))]
           @.lua [Nat (-> (array.Array Text) (array.Array Text))]
           ... default
           (Sequence Text))

      (def .public empty
        Buffer
        (abstraction (with_expansions [<jvm> [0 function.identity]]
                       (for @.old <jvm>
                            @.jvm <jvm>
                            @.js [0 function.identity]
                            @.lua [0 function.identity]
                            ... default
                            sequence.empty))))

      (def .public (then chunk buffer)
        (-> Text Buffer Buffer)
        (with_expansions [<jvm> (let [[capacity transform] (representation buffer)
                                      then! (is (-> Text java/lang/StringBuilder java/lang/StringBuilder)
                                                (function (_ chunk builder)
                                                  (exec
                                                    (java/lang/Appendable::append (as java/lang/CharSequence chunk)
                                                                                  builder)
                                                    builder)))]
                                  (abstraction [(n.+ (//.size chunk) capacity)
                                                (|>> transform (then! chunk))]))]
          (for @.old <jvm>
               @.jvm <jvm>
               @.js (let [[capacity transform] (representation buffer)
                          then! (is (-> (JS_Array Text) (JS_Array Text))
                                    (function (_ array)
                                      (exec
                                        (JS_Array::push chunk array)
                                        array)))]
                      (abstraction [(n.+ (//.size chunk) capacity)
                                    (|>> transform then!)]))
               @.lua (let [[capacity transform] (representation buffer)
                           then! (is (-> (array.Array Text) (array.Array Text))
                                     (function (_ array)
                                       (exec
                                         (table/insert array chunk)
                                         array)))]
                       (abstraction [(n.+ (//.size chunk) capacity)
                                     (|>> transform then!)]))
               ... default
               (|> buffer representation (sequence.suffix chunk) abstraction))))

      (def .public size
        (-> Buffer Nat)
        (with_expansions [<jvm> (|>> representation product.left)]
          (for @.old <jvm>
               @.jvm <jvm>
               @.js <jvm>
               @.lua <jvm>
               ... default
               (|>> representation
                    (sequence#mix (function (_ chunk total)
                                    (n.+ (//.size chunk) total))
                                  0)))))

      (def .public (text buffer)
        (-> Buffer Text)
        (with_expansions [<jvm> (let [[capacity transform] (representation buffer)]
                                  (|> (java/lang/StringBuilder::new (ffi.as_int (.int capacity)))
                                      transform
                                      java/lang/StringBuilder::toString
                                      ffi.of_string))]
          (for @.old <jvm>
               @.jvm <jvm>
               @.js (let [[capacity transform] (representation buffer)]
                      (|> (array.empty 0)
                          (as (JS_Array Text))
                          transform
                          (JS_Array::join "")))
               @.lua (let [[capacity transform] (representation buffer)]
                       (table/concat (transform (array.empty 0)) ""))
               ... default
               (sequence#mix (function (_ chunk total)
                               (format total chunk))
                             ""
                             (representation buffer)))))
      ))