fix indentation and simplify hex.decode_sequence

This commit is contained in:
Laytan Laats
2023-05-15 18:40:36 +02:00
parent a381846034
commit 6e4fab19a2
+10 -11
View File
@@ -36,22 +36,21 @@ decode :: proc(src: []byte, allocator := context.allocator) -> (dst: []byte, ok:
} }
// Decodes the given sequence into one byte. // Decodes the given sequence into one byte.
// Should be called with one rune worth of the source, eg: 0x23 -> '#'. // Should be called with one byte worth of the source, eg: 0x23 -> '#'.
decode_sequence :: proc(str: string) -> (byte, bool) { decode_sequence :: proc(str: string) -> (res: byte, ok: bool) {
no_prefix_str := strings.trim_prefix(str, "0x") str := str
val: byte if strings.has_prefix(str, "0x") || strings.has_prefix(str, "0X") {
for i := 0; i < len(no_prefix_str); i += 1 { str = str[2:]
index := (len(no_prefix_str) - 1) - i // reverse the loop. }
hd, ok := hex_digit(no_prefix_str[i]) if len(str) != 2 {
if !ok {
return 0, false return 0, false
} }
val += u8(hd) << uint(4 * index) upper := hex_digit(str[0]) or_return
} lower := hex_digit(str[1]) or_return
return val, true return upper << 4 | lower, true
} }
@(private) @(private)