aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text.lux
blob: 2670bdae3962a5b4d8761c0dcf25bf51604c6e90 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(.module:
  [library
   [lux (#- char)
    ["@" target]
    [abstract
     [hash (#+ Hash)]
     [monoid (#+ Monoid)]
     [equivalence (#+ Equivalence)]
     [order (#+ Order)]
     [monad (#+ do)]
     [codec (#+ Codec)]]
    [control
     ["." maybe]]
    [data
     [collection
      ["." list ("#\." fold)]]]
    [math
     [number
      ["n" nat]
      ["." i64]]]]])

(type: .public Char
  {#.doc (example "A character code number.")}
  Nat)

... TODO: Instead of ints, chars should be produced fron nats.
... (The JVM specifies chars as 16-bit unsigned integers)
(def: .public of_char
  (-> Char Text)
  (|>> .int "lux i64 char"))

(template [<code> <short> <long>]
  [(def: .public <long> (..of_char <code>))
   (def: .public <short> <long>)]

  [00 \0  null]
  [07 \a  alarm]
  [08 \b  back_space]
  [09 \t  tab]
  [10 \n  new_line]
  [11 \v  vertical_tab]
  [12 \f  form_feed]
  [13 \r  carriage_return]
  [34 \'' double_quote]
  )

(def: .public line_feed
  {#.doc (example "Same as 'new_line'.")}
  ..new_line)

(def: .public size
  (-> Text Nat)
  (|>> "lux text size"))

(def: .public (char index input)
  {#.doc (example "Yields the character at the specified index.")}
  (-> Nat Text (Maybe Char))
  (if (n.< ("lux text size" input) index)
    (#.Some ("lux text char" index input))
    #.None))

(def: .public (index_of' from pattern input)
  (-> Nat Text Text (Maybe Nat))
  ("lux text index" from pattern input))

(def: .public (index_of pattern input)
  (-> Text Text (Maybe Nat))
  (index_of' 0 pattern input))

(def: (last_index_of' from part text)
  (-> Nat Text Text (Maybe Nat))
  (loop [from from
         output (: (Maybe Nat)
                   #.None)]
    (let [output' ("lux text index" from part text)]
      (case output'
        #.None
        output

        (#.Some from')
        (recur (inc from') output')))))

(def: .public (last_index_of part text)
  (-> Text Text (Maybe Nat))
  (last_index_of' 0 part text))

(def: .public (starts_with? prefix x)
  (-> Text Text Bit)
  (case (index_of prefix x)
    (#.Some 0)
    true

    _
    false))

(def: .public (ends_with? postfix x)
  (-> Text Text Bit)
  (case (last_index_of postfix x)
    (#.Some n)
    (n.= (size x)
         (n.+ (size postfix) n))

    _
    false))

(def: .public (enclosed_by? boundary value)
  (-> Text Text Bit)
  (and (starts_with? boundary value)
       (ends_with? boundary value)))

(def: .public (contains? sub text)
  (-> Text Text Bit)
  (case ("lux text index" 0 sub text)
    (#.Some _)
    true

    _
    false))

(def: .public (prefix param subject)
  (-> Text Text Text)
  ("lux text concat" param subject))

(def: .public (suffix param subject)
  (-> Text Text Text)
  ("lux text concat" subject param))

(def: .public (enclosed [left right] content)
  {#.doc "Surrounds the given content text with left and right side additions."}
  (-> [Text Text] Text Text)
  ($_ "lux text concat" left content right))

(def: .public (enclosed' boundary content)
  {#.doc "Surrounds the given content text with the same boundary text."}
  (-> Text Text Text)
  (enclosed [boundary boundary] content))

(def: .public format
  (-> Text Text)
  (..enclosed' ..double_quote))

(def: .public (clip offset size input)
  {#.doc (example "Clips a chunk of text from the input at the specified offset and of the specified size.")}
  (-> Nat Nat Text (Maybe Text))
  (if (|> size (n.+ offset) (n.<= ("lux text size" input)))
    (#.Some ("lux text clip" offset size input))
    #.None))

(def: .public (clip' offset input)
  {#.doc (example "Clips the remaining text from the input at the specified offset.")}
  (-> Nat Text (Maybe Text))
  (let [size ("lux text size" input)]
    (if (n.<= size offset)
      (#.Some ("lux text clip" offset (n.- offset size) input))
      #.None)))

(def: .public (split_at at x)
  (-> Nat Text (Maybe [Text Text]))
  (case [(..clip 0 at x) (..clip' at x)]
    [(#.Some pre) (#.Some post)]
    (#.Some [pre post])

    _
    #.None))

(def: .public (split_by token sample)
  (-> Text Text (Maybe [Text Text]))
  (do maybe.monad
    [index (index_of token sample)
     [pre post'] (split_at index sample)
     [_ post] (split_at (size token) post')]
    (in [pre post])))

(def: .public (all_split_by token sample)
  (-> Text Text (List Text))
  (loop [input sample
         output (: (List Text) (list))]
    (case (..split_by token input)
      (#.Some [pre post])
      (|> output
          (#.Item pre)
          (recur post))

      #.None
      (|> output
          (#.Item input)
          list.reversed))))

(def: .public (replaced/1 pattern replacement template)
  (-> Text Text Text Text)
  (<| (maybe.else template)
      (do maybe.monad
        [[pre post] (..split_by pattern template)]
        (in ($_ "lux text concat" pre replacement post)))))

(def: .public (replaced pattern replacement template)
  (-> Text Text Text Text)
  (for {@.old
        (:as Text
             ("jvm invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence"
              (:as (primitive "java.lang.String") template)
              (:as (primitive "java.lang.CharSequence") pattern)
              (:as (primitive "java.lang.CharSequence") replacement)))
        @.jvm
        (:as Text
             ("jvm member invoke virtual" [] "java.lang.String" "replace" []
              (:as (primitive "java.lang.String") template)
              ["Ljava/lang/CharSequence;" (:as (primitive "java.lang.CharSequence") pattern)]
              ["Ljava/lang/CharSequence;" (:as (primitive "java.lang.CharSequence") replacement)]))
        ... TODO: Comment/turn-off when generating a JS compiler using a JVM-based compiler because Nashorn's implementation of "replaceAll" is incorrect. 
        @.js
        (:as Text
             ("js object do" "replaceAll" template [pattern replacement]))
        @.python
        (:as Text
             ("python object do" "replace" template pattern replacement))
        ... TODO @.lua
        @.ruby
        (:as Text
             ("ruby object do" "gsub" template pattern replacement))
        @.php
        (:as Text
             ("php apply" (:expected ("php constant" "str_replace"))
              pattern replacement template))
        ... TODO @.scheme
        ... TODO @.common_lisp
        ... TODO @.r
        }
       ... Inefficient default
       (loop [left ""
              right template]
         (case (..split_by pattern right)
           (#.Some [pre post])
           (recur ($_ "lux text concat" left pre replacement) post)

           #.None
           ("lux text concat" left right)))))

(implementation: .public equivalence
  (Equivalence Text)
  
  (def: (= reference sample)
    ("lux text =" reference sample)))

(implementation: .public order
  (Order Text)
  
  (def: &equivalence ..equivalence)

  (def: (< reference sample)
    ("lux text <" reference sample)))

(implementation: .public monoid
  (Monoid Text)
  
  (def: identity "")
  
  (def: (compose left right)
    ("lux text concat" left right)))

(implementation: .public hash
  (Hash Text)
  
  (def: &equivalence ..equivalence)
  
  (def: (hash input)
    (for {@.old
          (|> input
              (: (primitive "java.lang.String"))
              "jvm invokevirtual:java.lang.String:hashCode:"
              "jvm convert int-to-long"
              (:as Nat))

          @.jvm
          (|> input
              (:as (primitive "java.lang.String"))
              ("jvm member invoke virtual" [] "java.lang.String" "hashCode" [])
              "jvm conversion int-to-long"
              "jvm object cast"
              (: (primitive "java.lang.Long"))
              (:as Nat))}
         ... Platform-independent default.
         (let [length ("lux text size" input)]
           (loop [index 0
                  hash 0]
             (if (n.< length index)
               (recur (inc index)
                      (|> hash
                          (i64.left_shifted 5)
                          (n.- hash)
                          (n.+ ("lux text char" index input))))
               hash))))))

(def: .public joined
  (-> (List Text) Text)
  (let [(^open ".") ..monoid]
    (|>> list.reversed
         (list\fold compose identity))))

(def: .public (interposed separator texts)
  (-> Text (List Text) Text)
  (case separator
    "" (..joined texts)
    _ (|> texts (list.interposed separator) ..joined)))

(def: .public (empty? text)
  (-> Text Bit)
  (case text
    "" true
    _  false))

(def: .public space
  Text
  " ")

(def: .public (space? char)
  {#.doc "Checks whether the character is white-space."}
  (-> Char Bit)
  (with_expansions [<options> (template [<char>]
                                [(^ (.char (~~ (static <char>))))]

                                [..tab]
                                [..vertical_tab]
                                [..space]
                                [..new_line]
                                [..carriage_return]
                                [..form_feed]
                                )]
    (`` (case char
          (^or <options>)
          true

          _
          false))))

(def: .public (lower_cased value)
  (-> Text Text)
  (for {@.old
        (:as Text
             ("jvm invokevirtual:java.lang.String:toLowerCase:"
              (:as (primitive "java.lang.String") value)))
        @.jvm
        (:as Text
             ("jvm member invoke virtual" [] "java.lang.String" "toLowerCase" []
              (:as (primitive "java.lang.String") value)))
        @.js
        (:as Text
             ("js object do" "toLowerCase" value []))
        @.python
        (:as Text
             ("python object do" "lower" value))
        @.lua
        (:as Text
             ("lua apply" ("lua constant" "string.lower") value))
        @.ruby
        (:as Text
             ("ruby object do" "downcase" value))}))

(def: .public (upper_cased value)
  (-> Text Text)
  (for {@.old
        (:as Text
             ("jvm invokevirtual:java.lang.String:toUpperCase:"
              (:as (primitive "java.lang.String") value)))
        @.jvm
        (:as Text
             ("jvm member invoke virtual" [] "java.lang.String" "toUpperCase" []
              (:as (primitive "java.lang.String") value)))
        @.js
        (:as Text
             ("js object do" "toUpperCase" value []))
        @.python
        (:as Text
             ("python object do" "upper" value))
        @.lua
        (:as Text
             ("lua apply" ("lua constant" "string.upper") value))
        @.ruby
        (:as Text
             ("ruby object do" "upcase" value))}))