diff options
| author | Feoramund <161657516+Feoramund@users.noreply.github.com> | 2024-06-22 15:14:14 -0400 |
|---|---|---|
| committer | Feoramund <161657516+Feoramund@users.noreply.github.com> | 2024-06-22 18:21:32 -0400 |
| commit | 339b2b23f695fb22d2b91fc79f75a288b27a9572 (patch) | |
| tree | 1227077f07b236d3f013f46c59adbb9f8588ed0d /core/encoding | |
| parent | 8b8f8c7f7d8fd9184e376a119730ad392d3aa4d1 (diff) | |
Add `unsafe_write` to `uuid` package
Diffstat (limited to 'core/encoding')
| -rw-r--r-- | core/encoding/uuid/writing.odin | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/core/encoding/uuid/writing.odin b/core/encoding/uuid/writing.odin index 27dc789c2..499cba72b 100644 --- a/core/encoding/uuid/writing.odin +++ b/core/encoding/uuid/writing.odin @@ -8,12 +8,53 @@ import "core:strings" /* Write a UUID in the 8-4-4-4-12 format. +This procedure performs error checking with every byte written. + +If you can guarantee beforehand that your stream has enough space to hold the +UUID (32 bytes), then it is better to use `unsafe_write` instead as that will +be faster. + +Inputs: +- w: A writable stream. +- id: The identifier to convert. + +Returns: +- error: An `io` error, if one occurred, otherwise `nil`. +*/ +write :: proc(w: io.Writer, id: Identifier) -> (error: io.Error) #no_bounds_check { + write_octet :: proc (w: io.Writer, octet: u8) -> io.Error #no_bounds_check { + high_nibble := octet >> 4 + low_nibble := octet & 0xF + + io.write_byte(w, strconv.digits[high_nibble]) or_return + io.write_byte(w, strconv.digits[low_nibble]) or_return + return nil + } + + for index in 0 ..< 4 { write_octet(w, id[index]) or_return } + io.write_byte(w, '-') or_return + for index in 4 ..< 6 { write_octet(w, id[index]) or_return } + io.write_byte(w, '-') or_return + for index in 6 ..< 8 { write_octet(w, id[index]) or_return } + io.write_byte(w, '-') or_return + for index in 8 ..< 10 { write_octet(w, id[index]) or_return } + io.write_byte(w, '-') or_return + for index in 10 ..< 16 { write_octet(w, id[index]) or_return } + + return nil +} + +/* +Write a UUID in the 8-4-4-4-12 format. + +This procedure performs no error checking on the underlying stream. + Inputs: - w: A writable stream. - id: The identifier to convert. */ -write :: proc(w: io.Writer, id: Identifier) #no_bounds_check { - write_octet :: proc (w: io.Writer, octet: u8) { +unsafe_write :: proc(w: io.Writer, id: Identifier) #no_bounds_check { + write_octet :: proc (w: io.Writer, octet: u8) #no_bounds_check { high_nibble := octet >> 4 low_nibble := octet & 0xF @@ -56,7 +97,7 @@ to_string_allocated :: proc( ) #optional_allocator_error { buf := make([]byte, EXPECTED_LENGTH, allocator, loc) or_return builder := strings.builder_from_bytes(buf[:]) - write(strings.to_writer(&builder), id) + unsafe_write(strings.to_writer(&builder), id) return strings.to_string(builder), nil } @@ -80,7 +121,7 @@ to_string_buffer :: proc( ) { assert(len(buffer) >= EXPECTED_LENGTH, "The buffer provided is not at least 32 bytes large.", loc) builder := strings.builder_from_bytes(buffer) - write(strings.to_writer(&builder), id) + unsafe_write(strings.to_writer(&builder), id) return strings.to_string(builder) } |