changed: moved `conv.encode_hex` into new package
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 12 Jun 2023 02:40:09 +0000 (21:40 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 12 Jun 2023 02:40:09 +0000 (21:40 -0500)
CHANGELOG
core/conv/conv.onyx
core/encoding/hex.onyx [new file with mode: 0644]
core/std.onyx
tests/hex_autocoder.onyx

index 447b40240b753349a001189db9da1082c14802f5..4270ee1d79dceab31c2f06ddbdb661ea2e69be50 100644 (file)
--- 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 `#()`.
index 8f8357207250b33d2c4c2b1c8c6cdaaec4ec9e98..80b96f35d81b4f2788e4226e144388be3f4b3a9d 100644 (file)
@@ -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 (file)
index 0000000..bb1c415
--- /dev/null
@@ -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
index da8023d1384db176a00407b8ee66cbd95e01e1d8..29cb384739cbb83a2ee6754b4a29f2fb46408b79 100644 (file)
@@ -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"
index cbcbbd3da180675194095f14dff573d273191d6b..d03924a3dab102fdea719480ef9669aa2c4ea441 100644 (file)
@@ -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);
 }