aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/text.lux
blob: 85d5d9dd510d4542532f7b644875e7a88bf33b41 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  lux
  (lux (control monoid
                eq
                [ord]
                monad
                codec
                hash)
       (data (struct [list])
             maybe)))

## [Functions]
(def: #export (size x)
  (-> Text Nat)
  (int-to-nat (_lux_proc ["jvm" "i2l"] [(_lux_proc ["jvm" "invokevirtual:java.lang.String:length:"] [x])])))

(def: #export (at idx x)
  (-> Nat Text (Maybe Char))
  (if (n.< (size x) idx)
    (#;Some (_lux_proc ["jvm" "invokevirtual:java.lang.String:charAt:int"] [x (_lux_proc ["jvm" "l2i"] [(nat-to-int idx)])]))
    #;None))

(def: #export (contains? sub text)
  (-> Text Text Bool)
  (_lux_proc ["jvm" "invokevirtual:java.lang.String:contains:java.lang.CharSequence"] [text sub]))

(do-template [<name> <proc>]
  [(def: #export (<name> x)
     (-> Text Text)
     (_lux_proc ["jvm" <proc>] [x]))]
  [lower-case "invokevirtual:java.lang.String:toLowerCase:"]
  [upper-case "invokevirtual:java.lang.String:toUpperCase:"]
  [trim       "invokevirtual:java.lang.String:trim:"]
  )

(def: #export (sub from to x)
  (-> Nat Nat Text (Maybe Text))
  (if (and (n.< to from)
           (n.<= (size x) to))
    (#;Some (_lux_proc ["jvm" "invokevirtual:java.lang.String:substring:int,int"]
                       [x
                        (_lux_proc ["jvm" "l2i"] [(nat-to-int from)])
                        (_lux_proc ["jvm" "l2i"] [(nat-to-int to)])]))
    #;None))

(def: #export (sub' from x)
  (-> Nat Text (Maybe Text))
  (sub from (size x) x))

(def: #export (replace pattern value template)
  (-> Text Text Text Text)
  (_lux_proc ["jvm" "invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence"] [template pattern value]))

(do-template [<common> <common-proc> <general> <general-proc>]
  [(def: #export (<common> pattern x)
     (-> Text Text (Maybe Nat))
     (case (_lux_proc ["jvm" "i2l"] [(_lux_proc ["jvm" <common-proc>] [x pattern])])
       -1  #;None
       idx (#;Some (int-to-nat idx))))

   (def: #export (<general> pattern from x)
     (-> Text Nat Text (Maybe Nat))
     (if (n.< (size x) from)
       (case (_lux_proc ["jvm" "i2l"] [(_lux_proc ["jvm" <general-proc>] [x pattern (_lux_proc ["jvm" "l2i"] [(nat-to-int from)])])])
         -1  #;None
         idx (#;Some (int-to-nat idx)))
       #;None))]

  [index-of      "invokevirtual:java.lang.String:indexOf:java.lang.String"     index-of'      "invokevirtual:java.lang.String:indexOf:java.lang.String,int"]
  [last-index-of "invokevirtual:java.lang.String:lastIndexOf:java.lang.String" last-index-of' "invokevirtual:java.lang.String:lastIndexOf:java.lang.String,int"]
  )

(def: #export (starts-with? prefix x)
  (-> Text Text Bool)
  (case (index-of prefix x)
    (#;Some +0)
    true

    _
    false))

(def: #export (ends-with? postfix x)
  (-> Text Text Bool)
  (case (last-index-of postfix x)
    (#;Some n)
    (n.= (size x)
         (n.+ (size postfix) n))

    _
    false))

(def: #export (split at x)
  (-> Nat Text (Maybe [Text Text]))
  (if (n.<= (size x) at)
    (let [pre (_lux_proc ["jvm" "invokevirtual:java.lang.String:substring:int,int"] [x (_lux_proc ["jvm" "l2i"] [0]) (_lux_proc ["jvm" "l2i"] [(nat-to-int at)])])
          post (_lux_proc ["jvm" "invokevirtual:java.lang.String:substring:int"] [x (_lux_proc ["jvm" "l2i"] [(nat-to-int at)])])]
      (#;Some [pre post]))
    #;None))

(def: #export (split-with token sample)
  (-> Text Text (Maybe [Text Text]))
  (do Monad<Maybe>
    [index (index-of token sample)
     [pre post'] (split index sample)
     [_ post] (split (size token) post')]
    (wrap [pre post])))

(def: #export (split-all-with token sample)
  (-> Text Text (List Text))
  (case (split-with token sample)
    (#;Some [pre post])
    (#;Cons pre (split-all-with token post))

    #;None
    (#;Cons sample #;Nil)))

(def: #export split-lines
  (split-all-with "\n"))

## [Structures]
(struct: #export _ (Eq Text)
  (def: (= test subject)
    (_lux_proc ["jvm" "invokevirtual:java.lang.Object:equals:java.lang.Object"] [subject test])))

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

  (do-template [<name> <op>]
    [(def: (<name> test subject)
       (<op> 0
             (_lux_proc ["jvm" "i2l"]  [(_lux_proc ["jvm" "invokevirtual:java.lang.String:compareTo:java.lang.String"] [subject test])])))]

    [<  i.<]
    [<= i.<=]
    [>  i.>]
    [>= i.>=]))

(struct: #export _ (Monoid Text)
  (def: unit "")
  (def: (append x y)
    (_lux_proc ["jvm" "invokevirtual:java.lang.String:concat:java.lang.String"] [x y])))

(open Monoid<Text>)

(struct: #export _ (Codec Text Text)
  (def: (encode original)
    (let [escaped (|> original
                      (replace "\\" "\\\\")
                      (replace "\t" "\\t")
                      (replace "\b" "\\b")
                      (replace "\n" "\\n")
                      (replace "\r" "\\r")
                      (replace "\f" "\\f")
                      (replace "\"" "\\\"")
                      )]
      ($_ append "\"" escaped "\"")))

  (def: (decode input)
    (if (and (starts-with? "\"" input)
             (ends-with? "\"" input))
      (case (sub +1 (n.dec (size input)) input)
        (#;Some input')
        (|> input'
            (replace "\\\\" "\\")
            (replace "\\t" "\t")
            (replace "\\b" "\b")
            (replace "\\n" "\n")
            (replace "\\r" "\r")
            (replace "\\f" "\f")
            (replace "\\\"" "\"")
            #;Some)

        #;None
        (#;Left "Couldn't decode text"))
      (#;Left "Couldn't decode text"))))

(struct: #export _ (Hash Text)
  (def: eq Eq<Text>)
  
  (def: hash
    (|>. []
         (_lux_proc ["jvm" "invokevirtual:java.lang.Object:hashCode:"])
         []
         (_lux_proc ["jvm" "i2l"])
         int-to-nat)))

(def: #export concat
  (-> (List Text) Text)
  (let [(^open) list;Fold<List>
        (^open) Monoid<Text>]
    (|>. list;reverse (fold append unit))))

(def: #export (join-with sep texts)
  (-> Text (List Text) Text)
  (|> texts (list;interpose sep) concat))

(def: #export (empty? text)
  (-> Text Bool)
  (case text
    "" true
    _  false))

(def: #export (replace-once pattern value template)
  (-> Text Text Text Text)
  (default template
    (do Monad<Maybe>
      [[pre post] (split-with pattern template)]
      (let [(^open) Monoid<Text>]
        (wrap ($_ append pre value post))))))

(def: #export (enclose [left right] content)
  {#;doc "Surrounds the given content text with left and right side additions."}
  (-> [Text Text] Text Text)
  (let [(^open) Monoid<Text>]
    ($_ append left content right)))

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