Merge pull request #3045 from laytan/cbor

encoding/cbor
This commit is contained in:
gingerBill
2024-04-15 14:28:52 +01:00
committed by GitHub
15 changed files with 4732 additions and 49 deletions
+24 -1
View File
@@ -29,7 +29,7 @@ Error :: enum i32 {
// Invalid_Write means that a write returned an impossible count
Invalid_Write,
// Short_Buffer means that a read required a longer buffer than was provided
// Short_Buffer means that a read/write required a longer buffer than was provided
Short_Buffer,
// No_Progress is returned by some implementations of `io.Reader` when many calls
@@ -359,6 +359,29 @@ read_at_least :: proc(r: Reader, buf: []byte, min: int) -> (n: int, err: Error)
return
}
// write_full writes until the entire contents of `buf` has been written or an error occurs.
write_full :: proc(w: Writer, buf: []byte) -> (n: int, err: Error) {
return write_at_least(w, buf, len(buf))
}
// write_at_least writes at least `buf[:min]` to the writer and returns the amount written.
// If an error occurs before writing everything it is returned.
write_at_least :: proc(w: Writer, buf: []byte, min: int) -> (n: int, err: Error) {
if len(buf) < min {
return 0, .Short_Buffer
}
for n < min && err == nil {
nn: int
nn, err = write(w, buf[n:])
n += nn
}
if err == nil && n < min {
err = .Short_Write
}
return
}
// copy copies from src to dst till either EOF is reached on src or an error occurs
// It returns the number of bytes copied and the first error that occurred whilst copying, if any.
copy :: proc(dst: Writer, src: Reader) -> (written: i64, err: Error) {