From e4dba5760720c6d640d22418b37c924e0dc9958c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 11 Jun 2023 21:40:09 -0500 Subject: [PATCH] changed: moved `conv.encode_hex` into new package --- CHANGELOG | 4 ++-- core/conv/conv.onyx | 38 +------------------------------------- core/encoding/hex.onyx | 34 ++++++++++++++++++++++++++++++++++ core/std.onyx | 1 + tests/hex_autocoder.onyx | 4 ++-- 5 files changed, 40 insertions(+), 41 deletions(-) create mode 100644 core/encoding/hex.onyx diff --git a/CHANGELOG b/CHANGELOG index 447b4024..4270ee1d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -14,8 +14,8 @@ Additions: - `iter.flatten` - `-Dvariable=value` command line option to add symbols to the `runtime.vars` package. - `--no-type-info` command line option to omit type information from the binary. -- `conv.encode_hex` and `conv.decode_hex` - - Quickly convrt a byte array to and from its hex equivalent. +- `core.encoding.hex` + - Quickly convert a byte array to and from its hex equivalent. Removals: - Remove old syntax for quoted blocks, `#quote` and `#()`. diff --git a/core/conv/conv.onyx b/core/conv/conv.onyx index 8f835720..80b96f35 100644 --- a/core/conv/conv.onyx +++ b/core/conv/conv.onyx @@ -330,40 +330,4 @@ parse_float :: str_to_f64 format_int :: i64_to_str format_uint :: u64_to_str -format_float :: f64_to_str - - - - -encode_hex :: (s: str, allocator := context.allocator) -> str { - #persist encode_map := u8.['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; - - new_str := make([] u8, s.count * 2, allocator); - for i: s.count { - new_str[2 * i + 0] = encode_map[(s[i] & 0xf0) >> 4]; - new_str[2 * i + 1] = encode_map[s[i] & 0xf]; - } - - return new_str; -} - -decode_hex :: (s: str, allocator := context.allocator) -> str { - assert(s.count & 1 == 0, "Expected string of even length"); - - new_str := make([] u8, s.count >> 1, allocator); - for i: range.{0, s.count, 2} { - new_str[i >> 1] = ~~((digit_to_value(s[i + 0]) << 4) | (digit_to_value(s[i + 1]))); - } - - return new_str; - - digit_to_value :: (it: u8) -> u32 { - switch it { - case #char "0" .. #char "9" do return ~~(it - #char "0"); - case #char "a" .. #char "f" do return ~~(it - #char "a" + 10); - case #char "A" .. #char "F" do return ~~(it - #char "A" + 10); - } - - return 0; - } -} \ No newline at end of file +format_float :: f64_to_str \ No newline at end of file diff --git a/core/encoding/hex.onyx b/core/encoding/hex.onyx new file mode 100644 index 00000000..bb1c4158 --- /dev/null +++ b/core/encoding/hex.onyx @@ -0,0 +1,34 @@ +package core.encoding.hex + +encode :: (s: str, allocator := context.allocator) -> str { + #persist encode_map := u8.['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; + + new_str := make([] u8, s.count * 2, allocator); + for i: s.count { + new_str[2 * i + 0] = encode_map[(s[i] & 0xf0) >> 4]; + new_str[2 * i + 1] = encode_map[s[i] & 0xf]; + } + + return new_str; +} + +decode :: (s: str, allocator := context.allocator) -> str { + assert(s.count & 1 == 0, "Expected string of even length"); + + new_str := make([] u8, s.count >> 1, allocator); + for i: range.{0, s.count, 2} { + new_str[i >> 1] = ~~((digit_to_value(s[i + 0]) << 4) | (digit_to_value(s[i + 1]))); + } + + return new_str; + + digit_to_value :: (it: u8) -> u32 { + switch it { + case #char "0" .. #char "9" do return ~~(it - #char "0"); + case #char "a" .. #char "f" do return ~~(it - #char "a" + 10); + case #char "A" .. #char "F" do return ~~(it - #char "A" + 10); + } + + return 0; + } +} \ No newline at end of file diff --git a/core/std.onyx b/core/std.onyx index da8023d1..29cb3847 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -59,6 +59,7 @@ use runtime #load "./time/date" #load "./encoding/base64" +#load "./encoding/hex" #load "./encoding/utf8" #load "./encoding/osad" #load_all "./encoding/json" diff --git a/tests/hex_autocoder.onyx b/tests/hex_autocoder.onyx index cbcbbd3d..d03924a3 100644 --- a/tests/hex_autocoder.onyx +++ b/tests/hex_autocoder.onyx @@ -5,9 +5,9 @@ main :: () { s := "The quick brown fox jumps over the lazy dog"; println(s); - encoded := conv.encode_hex(s); + encoded := encoding.hex.encode(s); println(encoded); - decoded := conv.decode_hex(encoded); + decoded := encoding.hex.decode(encoded); println(decoded); } -- 2.25.1