mirror of
https://github.com/odin-lang/Odin.git
synced 2026-08-18 19:32:15 +00:00
Add non-allocating hex.decode_into_buffer
Also minor fixes to documentation of hex.decode(). Adds tests of the new procedure with buffers the correct size, larger, and too small.
This commit is contained in:
@@ -47,6 +47,60 @@ hex_decode :: proc(t: ^testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
hex_decode_into_buffer :: proc(t: ^testing.T) {
|
||||
for test in CASES {
|
||||
buffer := make([]u8, len(test[1]) / 2)
|
||||
defer delete(buffer)
|
||||
decoded, ok := hex.decode_into_buffer(transmute([]byte)test[1], buffer)
|
||||
testing.expect(t, ok, "decode_into_buffer: not ok")
|
||||
testing.expectf(
|
||||
t,
|
||||
ok,
|
||||
"decode: %q not ok",
|
||||
test[1],
|
||||
)
|
||||
testing.expectf(
|
||||
t,
|
||||
string(decoded) == test[0],
|
||||
"decode: %q -> %q (should be: %q)",
|
||||
test[1],
|
||||
string(decoded),
|
||||
test[0],
|
||||
)
|
||||
}
|
||||
|
||||
// destination buffer is larger
|
||||
for test in CASES {
|
||||
buffer := make([]u8, len(test[1]) / 2 + 1)
|
||||
defer delete(buffer)
|
||||
decoded, ok := hex.decode_into_buffer(transmute([]byte)test[1], buffer)
|
||||
testing.expect(t, ok, "decode_into_buffer: not ok")
|
||||
testing.expectf(
|
||||
t,
|
||||
ok,
|
||||
"decode: %q not ok",
|
||||
test[1],
|
||||
)
|
||||
testing.expectf(
|
||||
t,
|
||||
string(decoded) == test[0],
|
||||
"decode: %q -> %q (should be: %q)",
|
||||
test[1],
|
||||
string(decoded),
|
||||
test[0],
|
||||
)
|
||||
}
|
||||
|
||||
// destination buffer is too small
|
||||
for test in CASES {
|
||||
buffer := make([]u8, len(test[1]) / 2 - 1)
|
||||
defer delete(buffer)
|
||||
_, ok := hex.decode_into_buffer(transmute([]byte)test[1], buffer)
|
||||
testing.expect(t, !ok, "decode_into_buffer: should not be ok")
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
hex_decode_sequence :: proc(t: ^testing.T) {
|
||||
b, ok := hex.decode_sequence("0x23")
|
||||
@@ -83,4 +137,4 @@ hex_decode_sequence :: proc(t: ^testing.T) {
|
||||
|
||||
_, ok = hex.decode_sequence("123")
|
||||
testing.expect(t, !ok, "decode_sequence: 123 should be too long")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user