From b6e498279b18dedfca99b1f7164b4bb76187df09 Mon Sep 17 00:00:00 2001 From: Ynit <393227384@qq.com> Date: Sun, 21 Feb 2021 18:36:56 +0800 Subject: [PATCH] New hex_decode and hex_encode command #165 --- .../src/sdk/std/math/hex_decode/help.md | 24 ++++++++++ .../src/sdk/std/math/hex_decode/mod.rs | 46 +++++++++++++++++++ .../src/sdk/std/math/hex_decode/mod_test.rs | 26 +++++++++++ .../src/sdk/std/math/hex_encode/help.md | 22 +++++++++ .../src/sdk/std/math/hex_encode/mod.rs | 46 +++++++++++++++++++ .../src/sdk/std/math/hex_encode/mod_test.rs | 22 +++++++++ duckscript_sdk/src/sdk/std/math/mod.rs | 4 ++ test/std/math/hex_decode_test.ds | 12 +++++ test/std/math/hex_encode_test.ds | 6 +++ 9 files changed, 208 insertions(+) create mode 100644 duckscript_sdk/src/sdk/std/math/hex_decode/help.md create mode 100644 duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs create mode 100644 duckscript_sdk/src/sdk/std/math/hex_encode/help.md create mode 100644 duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs create mode 100644 test/std/math/hex_decode_test.ds create mode 100644 test/std/math/hex_encode_test.ds diff --git a/duckscript_sdk/src/sdk/std/math/hex_decode/help.md b/duckscript_sdk/src/sdk/std/math/hex_decode/help.md new file mode 100644 index 0000000..dfb83c7 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_decode/help.md @@ -0,0 +1,24 @@ +```sh +num = hex_decode str +``` + +Decode a hexadecimal string to the corresponding integer number.
+Not support negative numbers. + +#### Parameters + +A hexadecimal string. + +#### Return Value + +The corresponding integer number. + +#### Examples + +```sh +hex_num = set "0xff" +num = hex_decode ${hex_num} +res = calc ${num} + 1 + +assert_eq ${res} "256" +``` diff --git a/duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs b/duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs new file mode 100644 index 0000000..f7976c5 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs @@ -0,0 +1,46 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +#[derive(Clone)] +pub(crate) struct CommandImpl { + package: String, +} + +impl Command for CommandImpl { + fn name(&self) -> String { + pckg::concat(&self.package, "HexDecode") + } + + fn aliases(&self) -> Vec { + vec!["hex_decode".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn clone_and_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn run(&self, arguments: Vec) -> CommandResult { + if arguments.is_empty() { + CommandResult::Error("Value not provided.".to_string()) + } else { + match u64::from_str_radix(arguments[0].trim_start_matches("0x"), 16) { + Ok(num) => CommandResult::Continue(Some(num.to_string())), + Err(error) => CommandResult::Error(error.to_string()) + } + } + } +} + +pub(crate) fn create(package: &str) -> Box { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs b/duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs new file mode 100644 index 0000000..b40aa2b --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs @@ -0,0 +1,26 @@ +use super::*; +use crate::sdk::std::math::calc; +use crate::test; +use crate::test::CommandValidation; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_no_args() { + test::run_script_and_error(vec![create("")], "out = hex_decode", "out"); +} + +#[test] +fn run_valid() { + test::run_script_and_validate( + vec![create(""), calc::create("")], + r#" +num = hex_decode 0xFF +out = calc ${num} + 1 +"#, + CommandValidation::Match("out".to_string(), "256".to_string()), + ); +} diff --git a/duckscript_sdk/src/sdk/std/math/hex_encode/help.md b/duckscript_sdk/src/sdk/std/math/hex_encode/help.md new file mode 100644 index 0000000..6a01caa --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_encode/help.md @@ -0,0 +1,22 @@ +```sh +str = hex_encode num +``` + +Converts an integer number to the corresponding hexadecimal string.
+Not support negative numbers. + +#### Parameters + +An integer number. + +#### Return Value + +The corresponding hexadecimal string. + +#### Examples + +```sh +str = hex_encode 255 + +assert_eq ${str} "0xff" +``` diff --git a/duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs b/duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs new file mode 100644 index 0000000..73195db --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs @@ -0,0 +1,46 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +#[derive(Clone)] +pub(crate) struct CommandImpl { + package: String, +} + +impl Command for CommandImpl { + fn name(&self) -> String { + pckg::concat(&self.package, "HexEncode") + } + + fn aliases(&self) -> Vec { + vec!["hex_encode".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn clone_and_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn run(&self, arguments: Vec) -> CommandResult { + if arguments.is_empty() { + CommandResult::Error("Value not provided.".to_string()) + } else { + match &arguments[0].parse::() { + Ok(num) => CommandResult::Continue(Some(format!("{:#x}", num))), + Err(error) => CommandResult::Error(error.to_string()) + } + } + } +} + +pub(crate) fn create(package: &str) -> Box { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs b/duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs new file mode 100644 index 0000000..c63c8bf --- /dev/null +++ b/duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs @@ -0,0 +1,22 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_no_args() { + test::run_script_and_error(vec![create("")], "out = hex_encode", "out"); +} + +#[test] +fn run_valid() { + test::run_script_and_validate( + vec![create("")], + "out = hex_encode 255", + CommandValidation::Match("out".to_string(), "0xff".to_string()), + ); +} diff --git a/duckscript_sdk/src/sdk/std/math/mod.rs b/duckscript_sdk/src/sdk/std/math/mod.rs index d2a66e5..fa9ab2f 100755 --- a/duckscript_sdk/src/sdk/std/math/mod.rs +++ b/duckscript_sdk/src/sdk/std/math/mod.rs @@ -1,5 +1,7 @@ mod calc; mod greater_than; +mod hex_decode; +mod hex_encode; mod less_than; use crate::utils::pckg; @@ -13,6 +15,8 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(calc::create(&package))?; commands.set(greater_than::create(&package))?; + commands.set(hex_encode::create(&package))?; + commands.set(hex_decode::create(&package))?; commands.set(less_than::create(&package))?; Ok(()) diff --git a/test/std/math/hex_decode_test.ds b/test/std/math/hex_decode_test.ds new file mode 100644 index 0000000..14409d3 --- /dev/null +++ b/test/std/math/hex_decode_test.ds @@ -0,0 +1,12 @@ + +fn test_hex_decode + result = hex_decode 0xff + + assert_eq ${result} 255 +end + +fn test_hex_decode_no_prefix + result = hex_decode ff + + assert_eq ${result} 255 +end diff --git a/test/std/math/hex_encode_test.ds b/test/std/math/hex_encode_test.ds new file mode 100644 index 0000000..e2fa6e8 --- /dev/null +++ b/test/std/math/hex_encode_test.ds @@ -0,0 +1,6 @@ + +fn test_hex_encode + result = hex_encode 255 + + assert_eq ${result} 0xff +end