|
@@ -17,50 +17,53 @@ Builder :: struct {
|
|
|
}
|
|
|
|
|
|
// return a builder, default length 0 / cap 16 are done through make
|
|
|
-make_builder_none :: proc(allocator := context.allocator) -> Builder {
|
|
|
+builder_make_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 {
|
|
|
+builder_make_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 {
|
|
|
+builder_make_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,
|
|
|
+// overload simple `builder_make_*` with or without len / cap parameters
|
|
|
+builder_make :: proc{
|
|
|
+ builder_make_none,
|
|
|
+ builder_make_len,
|
|
|
+ builder_make_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) {
|
|
|
+builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> ^Builder {
|
|
|
b.buf = make([dynamic]byte, allocator)
|
|
|
+ return b
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
+builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> ^Builder {
|
|
|
b.buf = make([dynamic]byte, len, allocator)
|
|
|
+ return b
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
+builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> ^Builder {
|
|
|
b.buf = make([dynamic]byte, len, cap, allocator)
|
|
|
+ return b
|
|
|
}
|
|
|
|
|
|
-// overload simple `init_builder_*` with or without len / ap parameters
|
|
|
-init_builder :: proc{
|
|
|
- init_builder_none,
|
|
|
- init_builder_len,
|
|
|
- init_builder_len_cap,
|
|
|
+// overload simple `builder_init_*` with or without len / ap parameters
|
|
|
+builder_init :: proc{
|
|
|
+ builder_init_none,
|
|
|
+ builder_init_len,
|
|
|
+ builder_init_len_cap,
|
|
|
}
|
|
|
|
|
|
@(private)
|
|
@@ -103,18 +106,18 @@ to_writer :: proc(b: ^Builder) -> io.Writer {
|
|
|
}
|
|
|
|
|
|
// delete and clear the builder byte buffer content
|
|
|
-destroy_builder :: proc(b: ^Builder) {
|
|
|
+builder_destroy :: 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) {
|
|
|
+builder_grow :: proc(b: ^Builder, cap: int) {
|
|
|
reserve(&b.buf, cap)
|
|
|
}
|
|
|
|
|
|
// clear the builder byte buffer content
|
|
|
-reset_builder :: proc(b: ^Builder) {
|
|
|
+builder_reset :: proc(b: ^Builder) {
|
|
|
clear(&b.buf)
|
|
|
}
|
|
|
|
|
@@ -165,7 +168,7 @@ builder_space :: proc(b: Builder) -> int {
|
|
|
/*
|
|
|
appends a byte to the builder, returns the append diff
|
|
|
|
|
|
- builder := strings.make_builder()
|
|
|
+ builder := strings.builder_make()
|
|
|
strings.write_byte(&builder, 'a') // 1
|
|
|
strings.write_byte(&builder, 'b') // 1
|
|
|
strings.write_byte(&builder, 'c') // 1
|
|
@@ -181,7 +184,7 @@ write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
|
|
|
/*
|
|
|
appends a slice of bytes to the builder, returns the append diff
|
|
|
|
|
|
- builder := strings.make_builder()
|
|
|
+ builder := strings.builder_make()
|
|
|
bytes := [?]byte { 'a', 'b', 'c' }
|
|
|
strings.write_bytes(&builder, bytes[:]) // 3
|
|
|
fmt.println(strings.to_string(builder)) // -> abc
|
|
@@ -196,77 +199,46 @@ write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
|
|
|
/*
|
|
|
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
|
|
|
+ builder := strings.builder_make()
|
|
|
+ strings.write_rune(&builder, 'ä') // 2 None
|
|
|
+ strings.write_rune(&builder, 'b') // 1 None
|
|
|
+ strings.write_rune(&builder, 'c') // 1 None
|
|
|
fmt.println(strings.to_string(builder)) // -> äbc
|
|
|
*/
|
|
|
-write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
|
|
|
+write_rune :: 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()
|
|
|
+ builder := strings.builder_make()
|
|
|
strings.write_string(&builder, "abc") // 3
|
|
|
- strings.write_quoted_rune_builder(&builder, 'ä') // 4
|
|
|
+ strings.write_quoted_rune(&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)
|
|
|
+write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
|
|
|
+ return io.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()
|
|
|
+ builder := strings.builder_make()
|
|
|
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)
|
|
|
+write_string :: proc(b: ^Builder, s: string) -> (n: int) {
|
|
|
+ n0 := len(b.buf)
|
|
|
+ append(&b.buf, s)
|
|
|
+ n1 := len(b.buf)
|
|
|
+ return n1-n0
|
|
|
}
|
|
|
|
|
|
-// 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
|
|
@@ -297,70 +269,36 @@ pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
|
|
|
@(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()
|
|
|
+ builder := strings.builder_make()
|
|
|
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) {
|
|
|
+write_quoted_string :: 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) {
|
|
|
+write_encoded_rune :: 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) {
|
|
|
+write_escaped_rune :: 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
|