aboutsummaryrefslogtreecommitdiff
path: root/core/strings/builder.odin
blob: a910b09884c5dea7256f266658023673b8bc0778 (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
381
382
383
384
385
386
387
package strings

import "core:runtime"
import "core:unicode/utf8"
import "core:strconv"
import "core:io"

Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)

/*
	dynamic byte buffer / string builder with helper procedures
	the dynamic array is wrapped inside the struct to be more opaque
	you can use `fmt.sbprint*` procedures with a `^strings.Builder` directly
*/
Builder :: struct {
	buf: [dynamic]byte,
}

// return a builder, default length 0 / cap 16 are done through make
make_builder_none :: proc(allocator := context.allocator) -> Builder {
	return Builder{buf=make([dynamic]byte, allocator)}
}

// return a builder, with a set length `len` and cap 16 byte buffer
make_builder_len :: proc(len: int, allocator := context.allocator) -> Builder {
	return Builder{buf=make([dynamic]byte, len, allocator)}
}

// return a builder, with a set length `len` byte buffer and a custom `cap`
make_builder_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
	return Builder{buf=make([dynamic]byte, len, cap, allocator)}
}

// overload simple `make_builder_*` with or without len / cap parameters
make_builder :: proc{
	make_builder_none,
	make_builder_len,
	make_builder_len_cap,
}

// initialize a builder, default length 0 / cap 16 are done through make
// replaces the existing `buf`
init_builder_none :: proc(b: ^Builder, allocator := context.allocator) {
	b.buf = make([dynamic]byte, allocator)
}

// initialize a builder, with a set length `len` and cap 16 byte buffer
// replaces the existing `buf`
init_builder_len :: proc(b: ^Builder, len: int, allocator := context.allocator) {
	b.buf = make([dynamic]byte, len, allocator)
}

// initialize a builder, with a set length `len` byte buffer and a custom `cap`
// replaces the existing `buf`
init_builder_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) {
	b.buf = make([dynamic]byte, len, cap, allocator)
}

// overload simple `init_builder_*` with or without len / ap parameters
init_builder :: proc{
	init_builder_none,
	init_builder_len,
	init_builder_len_cap,
}

@(private)
_builder_stream_vtable := &io.Stream_VTable{
	impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
		b := (^Builder)(s.stream_data)
		n = write_bytes(b, p)
		if n < len(p) {
			err = .EOF
		}
		return
	},
	impl_write_byte = proc(s: io.Stream, c: byte) -> (err: io.Error) {
		b := (^Builder)(s.stream_data)
		n := write_byte(b, c)
		if n == 0 {
			err = .EOF
		}
		return
	},
	impl_size = proc(s: io.Stream) -> i64 {
		b := (^Builder)(s.stream_data)
		return i64(len(b.buf))
	},
	impl_destroy = proc(s: io.Stream) -> io.Error {
		b := (^Builder)(s.stream_data)
		delete(b.buf)
		return .None
	},
}

// return an `io.Stream` from a builder
to_stream :: proc(b: ^Builder) -> io.Stream {
	return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
}

// return an `io.Writer` from a builder
to_writer :: proc(b: ^Builder) -> io.Writer {
	return io.to_writer(to_stream(b))
}

// delete and clear the builder byte buffer content
destroy_builder :: proc(b: ^Builder) {
	delete(b.buf)
	clear(&b.buf)
}

// reserve the builfer byte buffer to a specific cap, when it's higher than before
grow_builder :: proc(b: ^Builder, cap: int) {
	reserve(&b.buf, cap)
}

// clear the builder byte buffer content
reset_builder :: proc(b: ^Builder) {
	clear(&b.buf)
}

/*
	create an empty builder with the same slice length as its cap
	uses the `mem.nil_allocator` to avoid allocation and keep a fixed length
	used in `fmt.bprint*`
	
	bytes: [8]byte // <-- gets filled
	builder := strings.builder_from_bytes(bytes[:])
	strings.write_byte(&builder, 'a') -> "a"
	strings.write_byte(&builder, 'b') -> "ab"
*/
builder_from_bytes :: proc(backing: []byte) -> Builder {
	s := transmute(runtime.Raw_Slice)backing
	d := runtime.Raw_Dynamic_Array{
		data = s.data,
		len  = 0,
		cap  = s.len,
		allocator = runtime.nil_allocator(),
	}
	return Builder{
		buf = transmute([dynamic]byte)d,
	}
}
builder_from_slice :: builder_from_bytes

// cast the builder byte buffer to a string and return it
to_string :: proc(b: Builder) -> string {
	return string(b.buf[:])
}

// return the length of the builder byte buffer
builder_len :: proc(b: Builder) -> int {
	return len(b.buf)
}

// return the cap of the builder byte buffer
builder_cap :: proc(b: Builder) -> int {
	return cap(b.buf)
}

// returns the space left in the builder byte buffer to use up
builder_space :: proc(b: Builder) -> int {
	return cap(b.buf) - len(b.buf)
}

/*
	appends a byte to the builder, returns the append diff

	builder := strings.make_builder()
	strings.write_byte(&builder, 'a') // 1
	strings.write_byte(&builder, 'b') // 1
	strings.write_byte(&builder, 'c') // 1
	fmt.println(strings.to_string(builder)) // -> abc
*/
write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
	n0 := len(b.buf)
	append(&b.buf, x)
	n1 := len(b.buf)
	return n1-n0
}

/*
	appends a slice of bytes to the builder, returns the append diff

	builder := strings.make_builder()
	bytes := [?]byte { 'a', 'b', 'c' }
	strings.write_bytes(&builder, bytes[:]) // 3
	fmt.println(strings.to_string(builder)) // -> abc
*/
write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
	n0 := len(b.buf)
	append(&b.buf, ..x)
	n1 := len(b.buf)
	return n1-n0
}

/*
	appends a single rune into the builder, returns written rune size and an `io.Error`

	builder := strings.make_builder()
	strings.write_rune_builder(&builder, 'ä') // 2 None
	strings.write_rune_builder(&builder, 'b') // 1 None
	strings.write_rune_builder(&builder, 'c') // 1 None
	fmt.println(strings.to_string(builder)) // -> äbc
*/
write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
	return io.write_rune(to_writer(b), r)
}

/*
	appends a quoted rune into the builder, returns written size

	builder := strings.make_builder()
	strings.write_string(&builder, "abc") // 3
	strings.write_quoted_rune_builder(&builder, 'ä') // 4
	strings.write_string(&builder, "abc") // 3
	fmt.println(strings.to_string(builder)) // -> abc'ä'abc
*/
write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) {
	return write_quoted_rune(to_writer(b), r)
}

@(private)
_write_byte :: proc(w: io.Writer, c: byte) -> int {
	err := io.write_byte(w, c)
	return 1 if err == nil else 0
}

// writer append a quoted rune into the byte buffer, return the written size
write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) {
	quote := byte('\'')
	n += _write_byte(w, quote)
	buf, width := utf8.encode_rune(r)
	if width == 1 && r == utf8.RUNE_ERROR {
		n += _write_byte(w, '\\')
		n += _write_byte(w, 'x')
		n += _write_byte(w, DIGITS_LOWER[buf[0]>>4])
		n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf])
	} else {
		i, _ := io.write_escaped_rune(w, r, quote)
		n += i
	}
	n += _write_byte(w, quote)
	return
}

// overload for `write_string_*` variants
write_string :: proc{
	write_string_builder,
	write_string_writer,
}

/*
	appends a string to the builder, return the written byte size
	
	builder := strings.make_builder()
	strings.write_string(&builder, "a") // 1
	strings.write_string(&builder, "bc") // 2	
	strings.write_string(&builder, "xyz") // 3
	fmt.println(strings.to_string(builder)) // -> abcxyz
*/
write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) {
	return write_string_writer(to_writer(b), s)
}

// appends a string to the writer
write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) {
	n, _ = io.write(w, transmute([]byte)s)
	return
}

// pops and returns the last byte in the builder
// returns 0 when the builder is empty
pop_byte :: proc(b: ^Builder) -> (r: byte) {
	if len(b.buf) == 0 {
		return 0
	}

	r = b.buf[len(b.buf)-1]
	d := cast(^runtime.Raw_Dynamic_Array)&b.buf
	d.len = max(d.len-1, 0)
	return
}

// pops the last rune in the builder and returns the popped rune and its rune width
// returns 0, 0 when the builder is empty
pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
	if len(b.buf) == 0 {
		return 0, 0
	}

	r, width = utf8.decode_last_rune(b.buf[:])
	d := cast(^runtime.Raw_Dynamic_Array)&b.buf
	d.len = max(d.len-width, 0)
	return
}

@(private)
DIGITS_LOWER := "0123456789abcdefx"

// overload for `write_quoted_string_*` variants
write_quoted_string :: proc{
	write_quoted_string_builder,
	write_quoted_string_writer,
}

/*
	append a quoted string into the builder, return the written byte size

	builder := strings.make_builder()
	strings.write_quoted_string(&builder, "a") // 3
	strings.write_quoted_string(&builder, "bc", '\'') // 4	
	strings.write_quoted_string(&builder, "xyz") // 5
	fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz"
*/
write_quoted_string_builder :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
	n, _ = io.write_quoted_string(to_writer(b), str, quote)
	return
}

@(deprecated="prefer io.write_quoted_string")
write_quoted_string_writer :: proc(w: io.Writer, str: string, quote: byte = '"') -> (n: int) {
	n, _ = io.write_quoted_string(w, str, quote)
	return	
}

// overload for `write_encoded_rune_*`
write_encoded_rune :: proc{
	write_encoded_rune_builder,
	write_encoded_rune_writer,
}

// appends a rune to the builder, optional `write_quote` boolean tag, returns the written rune size
write_encoded_rune_builder :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
	n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
	return

}
@(deprecated="prefer io.write_encoded_rune")
write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) -> (n: int) {
	n, _ = io.write_encoded_rune(w, r, write_quote)
	return
}

// overload for `write_escaped_rune_*`
write_escaped_rune :: proc{
	write_escaped_rune_builder,
	write_escaped_rune_writer,
}

// appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such
// when `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
// `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
write_escaped_rune_builder :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
	n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
	return
}

@(deprecated="prefer io.write_escaped_rune")
write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe := false) -> (n: int) {
	n, _ = io.write_escaped_rune(w, r, quote, html_safe)
	return
}

// writes a u64 value `i` in `base` = 10 into the builder, returns the written amount of characters
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
	buf: [32]byte
	s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
	return write_string(b, s)
}

// writes a i64 value `i` in `base` = 10 into the builder, returns the written amount of characters
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
	buf: [32]byte
	s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
	return write_string(b, s)
}

// writes a uint value `i` in `base` = 10 into the builder, returns the written amount of characters
write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
	return write_u64(b, u64(i), base)
}

// writes a int value `i` in `base` = 10 into the builder, returns the written amount of characters
write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
	return write_i64(b, i64(i), base)
}