This commit is contained in:
gingerBill
2022-07-14 11:56:13 +01:00
18 changed files with 115 additions and 153 deletions
+1 -1
View File
@@ -519,7 +519,7 @@ join_adjacent_string_literals :: proc(cpp: ^Preprocessor, initial_tok: ^Token) {
quote_string :: proc(s: string) -> []byte { quote_string :: proc(s: string) -> []byte {
b := strings.make_builder(0, len(s)+2) b := strings.builder_make(0, len(s)+2)
io.write_quoted_string(strings.to_writer(&b), s, '"') io.write_quoted_string(strings.to_writer(&b), s, '"')
return b.buf[:] return b.buf[:]
} }
+4 -4
View File
@@ -25,8 +25,8 @@ import "core:strings"
MAX_RUNE_CODEPOINT :: int(unicode.MAX_RUNE) MAX_RUNE_CODEPOINT :: int(unicode.MAX_RUNE)
write_rune :: strings.write_rune_builder write_rune :: strings.write_rune
write_string :: strings.write_string_builder write_string :: strings.write_string
Error :: enum u8 { Error :: enum u8 {
None = 0, None = 0,
@@ -94,8 +94,8 @@ decode_xml :: proc(input: string, options := XML_Decode_Options{}, allocator :=
l := len(input) l := len(input)
if l == 0 { return "", .None } if l == 0 { return "", .None }
builder := strings.make_builder() builder := strings.builder_make()
defer strings.destroy_builder(&builder) defer strings.builder_destroy(&builder)
t := Tokenizer{src=input} t := Tokenizer{src=input}
in_data := false in_data := false
@@ -8,7 +8,7 @@ import "core:time"
doc_print :: proc(doc: ^xml.Document) { doc_print :: proc(doc: ^xml.Document) {
buf: strings.Builder buf: strings.Builder
defer strings.destroy_builder(&buf) defer strings.builder_destroy(&buf)
w := strings.to_writer(&buf) w := strings.to_writer(&buf)
xml.print(w, doc) xml.print(w, doc)
+2 -2
View File
@@ -18,9 +18,9 @@ Marshal_Error :: union #shared_nil {
} }
marshal :: proc(v: any, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) { marshal :: proc(v: any, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) {
b := strings.make_builder(allocator) b := strings.builder_make(allocator)
defer if err != nil { defer if err != nil {
strings.destroy_builder(&b) strings.builder_destroy(&b)
} }
marshal_to_builder(&b, v) or_return marshal_to_builder(&b, v) or_return
+1 -1
View File
@@ -84,7 +84,7 @@ example :: proc() {
doc_hash :: proc(doc: ^xml.Document, print := false) -> (crc32: u32) { doc_hash :: proc(doc: ^xml.Document, print := false) -> (crc32: u32) {
buf: strings.Builder buf: strings.Builder
defer strings.destroy_builder(&buf) defer strings.builder_destroy(&buf)
w := strings.to_writer(&buf) w := strings.to_writer(&buf)
xml.print(w, doc) xml.print(w, doc)
+7 -7
View File
@@ -77,7 +77,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist
// They must be freed accordingly // They must be freed accordingly
aprint :: proc(args: ..any, sep := " ") -> string { aprint :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str) strings.builder_init(&str)
sbprint(buf=&str, args=args, sep=sep) sbprint(buf=&str, args=args, sep=sep)
return strings.to_string(str) return strings.to_string(str)
} }
@@ -85,7 +85,7 @@ aprint :: proc(args: ..any, sep := " ") -> string {
// They must be freed accordingly // They must be freed accordingly
aprintln :: proc(args: ..any, sep := " ") -> string { aprintln :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str) strings.builder_init(&str)
sbprintln(buf=&str, args=args, sep=sep) sbprintln(buf=&str, args=args, sep=sep)
return strings.to_string(str) return strings.to_string(str)
} }
@@ -93,7 +93,7 @@ aprintln :: proc(args: ..any, sep := " ") -> string {
// They must be freed accordingly // They must be freed accordingly
aprintf :: proc(fmt: string, args: ..any) -> string { aprintf :: proc(fmt: string, args: ..any) -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str) strings.builder_init(&str)
sbprintf(&str, fmt, ..args) sbprintf(&str, fmt, ..args)
return strings.to_string(str) return strings.to_string(str)
} }
@@ -102,21 +102,21 @@ aprintf :: proc(fmt: string, args: ..any) -> string {
// tprint procedure return a string that was allocated with the current context's temporary allocator // tprint procedure return a string that was allocated with the current context's temporary allocator
tprint :: proc(args: ..any, sep := " ") -> string { tprint :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprint(buf=&str, args=args, sep=sep) sbprint(buf=&str, args=args, sep=sep)
return strings.to_string(str) return strings.to_string(str)
} }
// tprintln procedure return a string that was allocated with the current context's temporary allocator // tprintln procedure return a string that was allocated with the current context's temporary allocator
tprintln :: proc(args: ..any, sep := " ") -> string { tprintln :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprintln(buf=&str, args=args, sep=sep) sbprintln(buf=&str, args=args, sep=sep)
return strings.to_string(str) return strings.to_string(str)
} }
// tprintf procedure return a string that was allocated with the current context's temporary allocator // tprintf procedure return a string that was allocated with the current context's temporary allocator
tprintf :: proc(fmt: string, args: ..any) -> string { tprintf :: proc(fmt: string, args: ..any) -> string {
str: strings.Builder str: strings.Builder
strings.init_builder(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprintf(&str, fmt, ..args) sbprintf(&str, fmt, ..args)
return strings.to_string(str) return strings.to_string(str)
} }
@@ -776,7 +776,7 @@ fmt_rune :: proc(fi: ^Info, r: rune, verb: rune) {
case 'c', 'r', 'v': case 'c', 'r', 'v':
io.write_rune(fi.writer, r, &fi.n) io.write_rune(fi.writer, r, &fi.n)
case 'q': case 'q':
fi.n += strings.write_quoted_rune(fi.writer, r) fi.n += io.write_quoted_rune(fi.writer, r)
case: case:
fmt_int(fi, u64(r), false, 32, verb) fmt_int(fi, u64(r), false, 32, verb)
} }
+2 -2
View File
@@ -1,8 +1,8 @@
package hash package hash
@(optimization_mode="speed") @(optimization_mode="speed")
crc64_ecma_182 :: proc(data: []byte, seed := u32(0)) -> u64 #no_bounds_check { crc64_ecma_182 :: proc(data: []byte, seed := u64(0)) -> (result: u64) #no_bounds_check {
result := u64(seed) result = seed
#no_bounds_check for b in data { #no_bounds_check for b in data {
result = result<<8 ~ _crc64_table_ecma_182[((result>>56) ~ u64(b)) & 0xff] result = result<<8 ~ _crc64_table_ecma_182[((result>>56) ~ u64(b)) & 0xff]
} }
+2 -2
View File
@@ -130,7 +130,7 @@ save_to_buffer :: proc(img: ^Image, custom_info: Info = {}, allocator := context
// we will write to a string builder // we will write to a string builder
data: strings.Builder data: strings.Builder
strings.init_builder(&data) strings.builder_init(&data)
// all PNM headers start with the format // all PNM headers start with the format
fmt.sbprintf(&data, "%s\n", header.format) fmt.sbprintf(&data, "%s\n", header.format)
@@ -409,7 +409,7 @@ _parse_header_pam :: proc(data: []byte, allocator := context.allocator) -> (head
// string buffer for the tupltype // string buffer for the tupltype
tupltype: strings.Builder tupltype: strings.Builder
strings.init_builder(&tupltype, context.temp_allocator); defer strings.destroy_builder(&tupltype) strings.builder_init(&tupltype, context.temp_allocator); defer strings.builder_destroy(&tupltype)
fmt.sbprint(&tupltype, "") fmt.sbprint(&tupltype, "")
// PAM uses actual lines, so we can iterate easily // PAM uses actual lines, so we can iterate easily
+24
View File
@@ -247,6 +247,30 @@ write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"', n_written
return return
} }
// writer append a quoted rune into the byte buffer, return the written size
write_quoted_rune :: proc(w: Writer, r: rune) -> (n: int) {
_write_byte :: #force_inline proc(w: Writer, c: byte) -> int {
err := write_byte(w, c)
return 1 if err == nil else 0
}
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, _ := write_escaped_rune(w, r, quote)
n += i
}
n += _write_byte(w, quote)
return
}
Tee_Reader :: struct { Tee_Reader :: struct {
+1 -1
View File
@@ -151,7 +151,7 @@ print :: proc(p: ^Printer, file: ^ast.File) -> string {
fix_lines(p) fix_lines(p)
builder := strings.make_builder(0, 5 * mem.Megabyte, p.allocator) builder := strings.builder_make(0, 5 * mem.Megabyte, p.allocator)
last_line := 0 last_line := 0
+3 -3
View File
@@ -71,7 +71,7 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
return 0 return 0
} else { } else {
builder := strings.make_builder(context.temp_allocator) builder := strings.builder_make(context.temp_allocator)
c_len := len(comment.text) c_len := len(comment.text)
trim_space := true trim_space := true
@@ -90,12 +90,12 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
continue continue
case c == '\r' && comment.text[min(c_len - 1, i + 1)] == '\n': case c == '\r' && comment.text[min(c_len - 1, i + 1)] == '\n':
append(&multilines, strings.to_string(builder)) append(&multilines, strings.to_string(builder))
builder = strings.make_builder(context.temp_allocator) builder = strings.builder_make(context.temp_allocator)
trim_space = true trim_space = true
i += 1 i += 1
case c == '\n': case c == '\n':
append(&multilines, strings.to_string(builder)) append(&multilines, strings.to_string(builder))
builder = strings.make_builder(context.temp_allocator) builder = strings.builder_make(context.temp_allocator)
trim_space = true trim_space = true
case c == '/' && comment.text[min(c_len - 1, i + 1)] == '*': case c == '/' && comment.text[min(c_len - 1, i + 1)] == '*':
strings.write_string(&builder, "/*") strings.write_string(&builder, "/*")
+43 -105
View File
@@ -17,50 +17,53 @@ Builder :: struct {
} }
// return a builder, default length 0 / cap 16 are done through make // 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 Builder{buf=make([dynamic]byte, allocator)}
} }
// return a builder, with a set length `len` and cap 16 byte buffer // 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 Builder{buf=make([dynamic]byte, len, allocator)}
} }
// return a builder, with a set length `len` byte buffer and a custom `cap` // 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)} return Builder{buf=make([dynamic]byte, len, cap, allocator)}
} }
// overload simple `make_builder_*` with or without len / cap parameters // overload simple `builder_make_*` with or without len / cap parameters
make_builder :: proc{ builder_make :: proc{
make_builder_none, builder_make_none,
make_builder_len, builder_make_len,
make_builder_len_cap, builder_make_len_cap,
} }
// initialize a builder, default length 0 / cap 16 are done through make // initialize a builder, default length 0 / cap 16 are done through make
// replaces the existing `buf` // 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) b.buf = make([dynamic]byte, allocator)
return b
} }
// initialize a builder, with a set length `len` and cap 16 byte buffer // initialize a builder, with a set length `len` and cap 16 byte buffer
// replaces the existing `buf` // 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) b.buf = make([dynamic]byte, len, allocator)
return b
} }
// initialize a builder, with a set length `len` byte buffer and a custom `cap` // initialize a builder, with a set length `len` byte buffer and a custom `cap`
// replaces the existing `buf` // 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) b.buf = make([dynamic]byte, len, cap, allocator)
return b
} }
// overload simple `init_builder_*` with or without len / ap parameters // overload simple `builder_init_*` with or without len / ap parameters
init_builder :: proc{ builder_init :: proc{
init_builder_none, builder_init_none,
init_builder_len, builder_init_len,
init_builder_len_cap, builder_init_len_cap,
} }
@(private) @(private)
@@ -103,18 +106,18 @@ to_writer :: proc(b: ^Builder) -> io.Writer {
} }
// delete and clear the builder byte buffer content // delete and clear the builder byte buffer content
destroy_builder :: proc(b: ^Builder) { builder_destroy :: proc(b: ^Builder) {
delete(b.buf) delete(b.buf)
clear(&b.buf) clear(&b.buf)
} }
// reserve the builfer byte buffer to a specific cap, when it's higher than before // 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) reserve(&b.buf, cap)
} }
// clear the builder byte buffer content // clear the builder byte buffer content
reset_builder :: proc(b: ^Builder) { builder_reset :: proc(b: ^Builder) {
clear(&b.buf) clear(&b.buf)
} }
@@ -165,7 +168,7 @@ builder_space :: proc(b: Builder) -> int {
/* /*
appends a byte to the builder, returns the append diff 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, 'a') // 1
strings.write_byte(&builder, 'b') // 1 strings.write_byte(&builder, 'b') // 1
strings.write_byte(&builder, 'c') // 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 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' } bytes := [?]byte { 'a', 'b', 'c' }
strings.write_bytes(&builder, bytes[:]) // 3 strings.write_bytes(&builder, bytes[:]) // 3
fmt.println(strings.to_string(builder)) // -> abc 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` appends a single rune into the builder, returns written rune size and an `io.Error`
builder := strings.make_builder() builder := strings.builder_make()
strings.write_rune_builder(&builder, 'ä') // 2 None strings.write_rune(&builder, 'ä') // 2 None
strings.write_rune_builder(&builder, 'b') // 1 None strings.write_rune(&builder, 'b') // 1 None
strings.write_rune_builder(&builder, 'c') // 1 None strings.write_rune(&builder, 'c') // 1 None
fmt.println(strings.to_string(builder)) // -> äbc 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) return io.write_rune(to_writer(b), r)
} }
/* /*
appends a quoted rune into the builder, returns written size 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_string(&builder, "abc") // 3
strings.write_quoted_rune_builder(&builder, 'ä') // 4 strings.write_quoted_rune(&builder, 'ä') // 4
strings.write_string(&builder, "abc") // 3 strings.write_string(&builder, "abc") // 3
fmt.println(strings.to_string(builder)) // -> abc'ä'abc fmt.println(strings.to_string(builder)) // -> abc'ä'abc
*/ */
write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) { write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
return write_quoted_rune(to_writer(b), r) 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 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, "a") // 1
strings.write_string(&builder, "bc") // 2 strings.write_string(&builder, "bc") // 2
strings.write_string(&builder, "xyz") // 3 strings.write_string(&builder, "xyz") // 3
fmt.println(strings.to_string(builder)) // -> abcxyz fmt.println(strings.to_string(builder)) // -> abcxyz
*/ */
write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) { write_string :: proc(b: ^Builder, s: string) -> (n: int) {
return write_string_writer(to_writer(b), s) 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 // pops and returns the last byte in the builder
// returns 0 when the builder is empty // returns 0 when the builder is empty
@@ -297,70 +269,36 @@ pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
@(private) @(private)
DIGITS_LOWER := "0123456789abcdefx" 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 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, "a") // 3
strings.write_quoted_string(&builder, "bc", '\'') // 4 strings.write_quoted_string(&builder, "bc", '\'') // 4
strings.write_quoted_string(&builder, "xyz") // 5 strings.write_quoted_string(&builder, "xyz") // 5
fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz" 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) n, _ = io.write_quoted_string(to_writer(b), str, quote)
return 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 // 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) n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
return 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 // 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 // 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` // `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) n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
return 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 // 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) { write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
buf: [32]byte buf: [32]byte
+10 -10
View File
@@ -10,7 +10,7 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) ->
} }
b: Builder b: Builder
init_builder(&b, 0, 0, allocator) builder_init(&b, 0, 0, allocator)
s := s s := s
for c, i in s { for c, i in s {
@@ -20,7 +20,7 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) ->
_, w := utf8.decode_rune_in_string(s[i:]) _, w := utf8.decode_rune_in_string(s[i:])
if w == 1 { if w == 1 {
grow_builder(&b, len(s) + len(replacement)) builder_grow(&b, len(s) + len(replacement))
write_string(&b, s[:i]) write_string(&b, s[:i])
s = s[i:] s = s[i:]
break break
@@ -67,9 +67,9 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) ->
*/ */
to_lower :: proc(s: string, allocator := context.allocator) -> string { to_lower :: proc(s: string, allocator := context.allocator) -> string {
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
for r in s { for r in s {
write_rune_builder(&b, unicode.to_lower(r)) write_rune(&b, unicode.to_lower(r))
} }
return to_string(b) return to_string(b)
} }
@@ -83,9 +83,9 @@ to_lower :: proc(s: string, allocator := context.allocator) -> string {
*/ */
to_upper :: proc(s: string, allocator := context.allocator) -> string { to_upper :: proc(s: string, allocator := context.allocator) -> string {
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
for r in s { for r in s {
write_rune_builder(&b, unicode.to_upper(r)) write_rune(&b, unicode.to_upper(r))
} }
return to_string(b) return to_string(b)
} }
@@ -147,7 +147,7 @@ to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
s := s s := s
s = trim_space(s) s = trim_space(s)
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
w := to_writer(&b) w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) { string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
@@ -172,7 +172,7 @@ to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
s := s s := s
s = trim_space(s) s = trim_space(s)
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
w := to_writer(&b) w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) { string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
@@ -203,7 +203,7 @@ to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allo
s := s s := s
s = trim_space(s) s = trim_space(s)
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
w := to_writer(&b) w := to_writer(&b)
adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower
@@ -272,7 +272,7 @@ to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
s := s s := s
s = trim_space(s) s = trim_space(s)
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
w := to_writer(&b) w := to_writer(&b)
prev, curr: rune prev, curr: rune
+8 -8
View File
@@ -1553,7 +1553,7 @@ split_multi_iterate :: proc(using sm: ^Split_Multi) -> (res: string, ok: bool) #
scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string { scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
str := s str := s
b: Builder b: Builder
init_builder(&b, 0, len(s), allocator) builder_init(&b, 0, len(s), allocator)
has_error := false has_error := false
cursor := 0 cursor := 0
@@ -1622,7 +1622,7 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) ->
} }
b: Builder b: Builder
init_builder(&b, allocator) builder_init(&b, allocator)
writer := to_writer(&b) writer := to_writer(&b)
str := s str := s
column: int column: int
@@ -1690,8 +1690,8 @@ centre_justify :: proc(str: string, length: int, pad: string, allocator := conte
pad_len := rune_count(pad) pad_len := rune_count(pad)
b: Builder b: Builder
init_builder(&b, allocator) builder_init(&b, allocator)
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad))
w := to_writer(&b) w := to_writer(&b)
@@ -1713,8 +1713,8 @@ left_justify :: proc(str: string, length: int, pad: string, allocator := context
pad_len := rune_count(pad) pad_len := rune_count(pad)
b: Builder b: Builder
init_builder(&b, allocator) builder_init(&b, allocator)
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad))
w := to_writer(&b) w := to_writer(&b)
@@ -1735,8 +1735,8 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex
pad_len := rune_count(pad) pad_len := rune_count(pad)
b: Builder b: Builder
init_builder(&b, allocator) builder_init(&b, allocator)
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad))
w := to_writer(&b) w := to_writer(&b)
@@ -45,7 +45,7 @@ generate_encoding_entity_table :: proc() {
printf("\"%v\" loaded and parsed.\n", filename) printf("\"%v\" loaded and parsed.\n", filename)
generated_buf: strings.Builder generated_buf: strings.Builder
defer strings.destroy_builder(&generated_buf) defer strings.builder_destroy(&generated_buf)
w := strings.to_writer(&generated_buf) w := strings.to_writer(&generated_buf)
charlist, charlist_ok := xml.find_child_by_ident(doc.root, "charlist") charlist, charlist_ok := xml.find_child_by_ident(doc.root, "charlist")
@@ -79,8 +79,8 @@ test_leb128 :: proc(t: ^testing.T) {
} }
} }
for num_bytes in 1..uint(16) { for num_bytes in 1..=uint(16) {
for _ in 0..RANDOM_TESTS { for _ in 0..=RANDOM_TESTS {
unsigned, signed := get_random(num_bytes) unsigned, signed := get_random(num_bytes)
{ {
@@ -109,7 +109,7 @@ test_leb128 :: proc(t: ^testing.T) {
get_random :: proc(byte_count: uint) -> (u: u128, i: i128) { get_random :: proc(byte_count: uint) -> (u: u128, i: i128) {
assert(byte_count >= 0 && byte_count <= size_of(u128)) assert(byte_count >= 0 && byte_count <= size_of(u128))
for _ in 1..byte_count { for _ in 1..=byte_count {
u <<= 8 u <<= 8
u |= u128(rand.uint32() & 0xff) u |= u128(rand.uint32() & 0xff)
} }
+1 -1
View File
@@ -281,7 +281,7 @@ doc_to_string :: proc(doc: ^xml.Document) -> (result: string) {
} }
buf: strings.Builder buf: strings.Builder
defer strings.destroy_builder(&buf) defer strings.builder_destroy(&buf)
print(strings.to_writer(&buf), doc) print(strings.to_writer(&buf), doc)
return strings.clone(strings.to_string(buf)) return strings.clone(strings.to_string(buf))
+1 -1
View File
@@ -353,7 +353,7 @@ end :: proc(ctx: ^Context) {
/* reset input state */ /* reset input state */
ctx.key_pressed_bits = {} // clear ctx.key_pressed_bits = {} // clear
strings.reset_builder(&ctx.text_input) strings.builder_reset(&ctx.text_input)
ctx.mouse_pressed_bits = {} // clear ctx.mouse_pressed_bits = {} // clear
ctx.mouse_released_bits = {} // clear ctx.mouse_released_bits = {} // clear
ctx.scroll_delta = Vec2{0, 0} ctx.scroll_delta = Vec2{0, 0}