Change strings.Builder to be distinct [dynamic]byte from a struct wrapper

This commit is contained in:
gingerBill
2026-06-09 13:56:10 +01:00
parent d80720e30d
commit 2f3aefbb3d
12 changed files with 108 additions and 111 deletions

View File

@@ -207,7 +207,7 @@ encode :: encode_into
encode_into_bytes :: proc(v: Value, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (data: []byte, err: Encode_Error) {
b := strings.builder_make(allocator, loc) or_return
encode_into_builder(&b, v, flags, temp_allocator) or_return
return b.buf[:], nil
return b[:], nil
}
// Encodes the CBOR value into binary CBOR written to the given builder.
@@ -396,7 +396,7 @@ _decode_bytes :: proc(d: Decoder, add: Add, type: Major = .Bytes, allocator := c
if iter_n == -1 {
return nil, .Nested_Indefinite_Length
}
reserve(&buf.buf, len(buf.buf) + iter_cap) or_return
reserve(&buf, len(buf) + iter_cap) or_return
io.copy_n(buf_stream, d.reader, i64(iter_n)) or_return
case .Other:
@@ -411,12 +411,12 @@ _decode_bytes :: proc(d: Decoder, add: Add, type: Major = .Bytes, allocator := c
io.copy_n(buf_stream, d.reader, i64(n)) or_return
}
v = buf.buf[:]
v = buf[:]
// Write zero byte so this can be converted to cstring.
strings.write_byte(&buf, 0)
if .Shrink_Excess in d.flags { shrink(&buf.buf) }
if .Shrink_Excess in d.flags { shrink(&buf) }
return
}
@@ -561,7 +561,7 @@ _encode_map :: proc(e: Encoder, m: Map) -> (err: Encode_Error) {
ke.writer = strings.to_stream(&buf)
encode(ke, entry.entry.key) or_return
entry.encoded_key = buf.buf[:]
entry.encoded_key = buf[:]
}
// Sort lexicographic on the bytes of the key.

View File

@@ -58,7 +58,7 @@ marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.a
return
}
return b.buf[:], nil
return b[:], nil
}
// Marshals the given value into a CBOR byte stream written to the given builder.
@@ -379,7 +379,7 @@ _marshal_into_encoder :: proc(e: Encoder, v: any, ti: ^runtime.Type_Info) -> (er
err := _encode_u64(e, u64(len(str)), .Text)
assert(err == nil)
res[9] = u8(len(builder.buf))
res[9] = u8(len(builder))
assert(res[9] < 10)
return
}
@@ -476,7 +476,7 @@ _marshal_into_encoder :: proc(e: Encoder, v: any, ti: ^runtime.Type_Info) -> (er
key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
key_builder := strings.builder_make(0, 8, e.temp_allocator) or_return
marshal_into(Encoder{e.flags, strings.to_stream(&key_builder), e.temp_allocator}, any{ key, info.key.id }) or_return
append(&entries, Encoded_Entry{ &key_builder.buf, bucket_index }) or_return
append(&entries, Encoded_Entry{ (^[dynamic]byte)(&key_builder), bucket_index }) or_return
}
slice.sort_by_cmp(entries[:], proc(a, b: Encoded_Entry) -> slice.Ordering {
@@ -555,7 +555,7 @@ _marshal_into_encoder :: proc(e: Encoder, v: any, ti: ^runtime.Type_Info) -> (er
key_builder := strings.builder_make(e.temp_allocator) or_return
err_conv(_encode_text(Encoder{e.flags, strings.to_stream(&key_builder), e.temp_allocator}, fname)) or_return
append(&entries, Name{key_builder.buf[:], i}) or_return
append(&entries, Name{key_builder[:], i}) or_return
}
// Sort lexicographic on the bytes of the key.

View File

@@ -311,7 +311,7 @@ tag_base64_unmarshal :: proc(_: ^Tag_Implementation, d: Decoder, _: Tag_Number,
b64_decode_into(strings.to_stream(&builder), bytes) or_return
raw := (^cstring)(v.data)
raw^ = cstring(raw_data(builder.buf))
raw^ = cstring(raw_data(builder))
} else {
raw := (^string)(v.data)
raw^ = string(b64_decode(bytes) or_return)

View File

@@ -146,8 +146,8 @@ marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocato
opt := opt
marshal_to_builder(&b, v, &opt) or_return
if len(b.buf) != 0 {
data = b.buf[:]
if len(b) != 0 {
data = b[:]
}
return data, nil

View File

@@ -161,7 +161,7 @@ decode :: proc(data: []byte, allocator := context.allocator) -> (blk: ^Block, re
@(require_results)
encode :: proc(label: string, data: []byte, newline := false, allocator := context.allocator) -> (res: []byte, err: runtime.Allocator_Error) #optional_allocator_error {
sanitize_sb := proc(sb: ^strings.Builder) {
buf := sb.buf[:]
buf := sb[:]
b, l := raw_data(buf), len(buf)
crypto.zero_explicit(b, l)
strings.builder_destroy(sb)