-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
string_builder.gleam
356 lines (307 loc) Β· 8.04 KB
/
string_builder.gleam
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
import gleam/list
/// `StringBuilder` is a type used for efficiently building strings.
///
/// When we append one string to another the strings must be copied to a
/// new location in memory so that they can sit together. This behaviour
/// enables efficient reading of the string but copying can be expensive,
/// especially if we want to join many strings together.
///
/// `StringBuilder` is different in that it can be joined together in constant time
/// using minimal memory, and then can be efficiently converted to a string
/// using the `to_string` function.
///
/// On Erlang this type is compatible with Erlang's iodata. On JavaScript this
/// type is compatible with normal strings.
///
pub external type StringBuilder
/// Create an empty `StringBuilder`. Useful as the start of a pipe chaining many
/// builders together.
///
pub fn new() -> StringBuilder {
do_from_strings([])
}
/// Prepends a `String` onto the start of some `StringBuilder`.
///
/// Runs in constant time.
///
pub fn prepend(
to builder: StringBuilder,
prefix prefix: String,
) -> StringBuilder {
append_builder(from_string(prefix), builder)
}
/// Appends a `String` onto the end of some `StringBuilder`.
///
/// Runs in constant time.
///
pub fn append(to builder: StringBuilder, suffix second: String) -> StringBuilder {
append_builder(builder, from_string(second))
}
/// Prepends some `StringBuilder` onto the start of another.
///
/// Runs in constant time.
///
pub fn prepend_builder(
to builder: StringBuilder,
prefix prefix: StringBuilder,
) -> StringBuilder {
do_append(prefix, builder)
}
/// Appends some `StringBuilder` onto the end of another.
///
/// Runs in constant time.
///
pub fn append_builder(
to builder: StringBuilder,
suffix suffix: StringBuilder,
) -> StringBuilder {
do_append(builder, suffix)
}
if erlang {
external fn do_append(StringBuilder, StringBuilder) -> StringBuilder =
"gleam_stdlib" "iodata_append"
}
if javascript {
external fn do_append(StringBuilder, StringBuilder) -> StringBuilder =
"../gleam_stdlib.mjs" "add"
}
/// Converts a list of strings into a builder.
///
/// Runs in constant time.
///
pub fn from_strings(strings: List(String)) -> StringBuilder {
do_from_strings(strings)
}
if erlang {
external fn do_from_strings(List(String)) -> StringBuilder =
"gleam_stdlib" "identity"
}
if javascript {
external fn do_from_strings(List(String)) -> StringBuilder =
"../gleam_stdlib.mjs" "join"
}
/// Joins a list of builders into a single builder.
///
/// Runs in constant time.
///
pub fn concat(builders: List(StringBuilder)) -> StringBuilder {
do_concat(builders)
}
if erlang {
external fn do_concat(List(StringBuilder)) -> StringBuilder =
"gleam_stdlib" "identity"
}
if javascript {
external fn do_concat(List(StringBuilder)) -> StringBuilder =
"../gleam_stdlib.mjs" "join"
}
/// Converts a string into a builder.
///
/// Runs in constant time.
///
pub fn from_string(string: String) -> StringBuilder {
do_from_string(string)
}
if erlang {
external fn do_from_string(String) -> StringBuilder =
"gleam_stdlib" "identity"
}
if javascript {
external fn do_from_string(String) -> StringBuilder =
"../gleam_stdlib.mjs" "identity"
}
/// Turns an `StringBuilder` into a `String`
///
/// This function is implemented natively by the virtual machine and is highly
/// optimised.
///
pub fn to_string(builder: StringBuilder) -> String {
do_to_string(builder)
}
if erlang {
external fn do_to_string(StringBuilder) -> String =
"unicode" "characters_to_binary"
}
if javascript {
external fn do_to_string(StringBuilder) -> String =
"../gleam_stdlib.mjs" "identity"
}
/// Returns the size of the `StringBuilder` in bytes.
///
pub fn byte_size(builder: StringBuilder) -> Int {
do_byte_size(builder)
}
if erlang {
external fn do_byte_size(StringBuilder) -> Int =
"erlang" "iolist_size"
}
if javascript {
external fn do_byte_size(StringBuilder) -> Int =
"../gleam_stdlib.mjs" "length"
}
/// Joins the given builders into a new builder separated with the given string
///
pub fn join(builders: List(StringBuilder), with sep: String) -> StringBuilder {
builders
|> list.intersperse(from_string(sep))
|> concat
}
/// Converts a builder to a new builder where the contents have been
/// lowercased.
///
pub fn lowercase(builder: StringBuilder) -> StringBuilder {
do_lowercase(builder)
}
if erlang {
external fn do_lowercase(StringBuilder) -> StringBuilder =
"string" "lowercase"
}
if javascript {
external fn do_lowercase(StringBuilder) -> StringBuilder =
"../gleam_stdlib.mjs" "lowercase"
}
/// Converts a builder to a new builder where the contents have been
/// uppercased.
///
pub fn uppercase(builder: StringBuilder) -> StringBuilder {
do_uppercase(builder)
}
if erlang {
external fn do_uppercase(StringBuilder) -> StringBuilder =
"string" "uppercase"
}
if javascript {
external fn do_uppercase(StringBuilder) -> StringBuilder =
"../gleam_stdlib.mjs" "uppercase"
}
/// Converts a builder to a new builder with the contents reversed.
///
pub fn reverse(builder: StringBuilder) -> StringBuilder {
do_reverse(builder)
}
if erlang {
external fn do_reverse(StringBuilder) -> StringBuilder =
"string" "reverse"
}
if javascript {
fn do_reverse(builder: StringBuilder) -> StringBuilder {
builder
|> to_string
|> do_to_graphemes
|> list.reverse
|> from_strings
}
external fn do_to_graphemes(string: String) -> List(String) =
"../gleam_stdlib.mjs" "graphemes"
}
/// Splits a builder on a given pattern into a list of builders.
///
pub fn split(iodata: StringBuilder, on pattern: String) -> List(StringBuilder) {
do_split(iodata, pattern)
}
if erlang {
type Direction {
All
}
external fn erl_split(StringBuilder, String, Direction) -> List(StringBuilder) =
"string" "split"
fn do_split(iodata: StringBuilder, pattern: String) -> List(StringBuilder) {
erl_split(iodata, pattern, All)
}
}
if javascript {
external fn do_split(
builder: StringBuilder,
pattern: String,
) -> List(StringBuilder) =
"../gleam_stdlib.mjs" "split"
}
/// Replaces all instances of a pattern with a given string substitute.
///
pub fn replace(
in builder: StringBuilder,
each pattern: String,
with substitute: String,
) -> StringBuilder {
do_replace(builder, pattern, substitute)
}
if erlang {
fn do_replace(
iodata: StringBuilder,
pattern: String,
substitute: String,
) -> StringBuilder {
erl_replace(iodata, pattern, substitute, All)
}
external fn erl_replace(
StringBuilder,
String,
String,
Direction,
) -> StringBuilder =
"string" "replace"
}
if javascript {
external fn do_replace(StringBuilder, String, String) -> StringBuilder =
"../gleam_stdlib.mjs" "string_replace"
}
/// Compares two builders to determine if they have the same textual content.
///
/// Comparing two iodata using the `==` operator may return `False` even if they
/// have the same content as they may have been build in different ways, so
/// using this function is often preferred.
///
/// ## Examples
///
/// ```gleam
/// > from_strings(["a", "b"]) == from_string("ab")
/// False
/// ```
///
/// ```gleam
/// > is_equal(from_strings(["a", "b"]), from_string("ab"))
/// True
/// ```
///
pub fn is_equal(a: StringBuilder, b: StringBuilder) -> Bool {
do_is_equal(a, b)
}
if erlang {
external fn do_is_equal(StringBuilder, StringBuilder) -> Bool =
"string" "equal"
}
if javascript {
external fn do_is_equal(StringBuilder, StringBuilder) -> Bool =
"../gleam_stdlib.mjs" "equal"
}
/// Inspects a builder to determine if it is equivalent to an empty string.
///
/// ## Examples
///
/// ```gleam
/// > from_string("ok") |> is_empty
/// False
/// ```
///
/// ```gleam
/// > from_string("") |> is_empty
/// True
/// ```
///
/// ```gleam
/// > from_strings([]) |> is_empty
/// True
/// ```
///
pub fn is_empty(builder: StringBuilder) -> Bool {
do_is_empty(builder)
}
if erlang {
external fn do_is_empty(StringBuilder) -> Bool =
"string" "is_empty"
}
if javascript {
fn do_is_empty(builder: StringBuilder) -> Bool {
from_string("") == builder
}
}