From 8dcdcd100def523c5c65a82b2fe56e47b8cd125c Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Mon, 1 Jan 2024 08:15:19 +0800 Subject: [PATCH] =?UTF-8?q?Fixed:=20=E7=BC=96=E8=AF=91=E6=9C=9F=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E5=B0=86=E5=8D=95=E4=B8=AA=E8=B4=9F=E5=8F=B7=E5=BD=93?= =?UTF-8?q?=E5=81=9A=E6=95=B0=E5=AD=97=E5=B9=B6=E8=A7=A3=E6=9E=90=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- tools/var_utils/Cargo.toml | 2 +- tools/var_utils/src/lib.rs | 8 ++++++-- tools/var_utils/src/tests.rs | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 864555c..1831eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,7 +292,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.14.1" +version = "0.14.2" dependencies = [ "display_source", "parser", @@ -585,7 +585,7 @@ version = "0.1.2" [[package]] name = "var_utils" -version = "0.5.0" +version = "0.5.1" dependencies = [ "lazy-regex", ] diff --git a/Cargo.toml b/Cargo.toml index e025a29..9b8eb82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.14.1" +version = "0.14.2" edition = "2021" authors = ["A4-Tacks "] diff --git a/tools/var_utils/Cargo.toml b/tools/var_utils/Cargo.toml index 8e79f12..18f39c1 100644 --- a/tools/var_utils/Cargo.toml +++ b/tools/var_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "var_utils" -version = "0.5.0" +version = "0.5.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/var_utils/src/lib.rs b/tools/var_utils/src/lib.rs index e9ac4eb..9b34685 100644 --- a/tools/var_utils/src/lib.rs +++ b/tools/var_utils/src/lib.rs @@ -105,7 +105,7 @@ impl AsVarType for str { } fn as_number(s: &str) -> Option { static NUM_REGEX: &Lazy = regex!( - r"^-?(?:\d+(?:e[+\-]?\d+|\.\d*)?|\d*(?:\.\d+)?)$" + r"^-?(?:\d+(?:e[+\-]?\d+|\.\d*)?|\d*\.\d+)$" ); static HEX_REGEX: &Lazy = regex!( r"^0x-?[0-9A-Fa-f]+$" @@ -127,7 +127,11 @@ impl AsVarType for str { } } if NUM_REGEX.is_match(s) { - Some(s.parse().unwrap()) + let res = match s.parse() { + Ok(n) => n, + Err(e) => panic!("{}, ({:?})", e, s), + }; + Some(res) } else if HEX_REGEX.is_match(s) { parse_radix(&s[2..], 16) } else if BIN_REGEX.is_match(s) { diff --git a/tools/var_utils/src/tests.rs b/tools/var_utils/src/tests.rs index be91fa4..431d421 100644 --- a/tools/var_utils/src/tests.rs +++ b/tools/var_utils/src/tests.rs @@ -50,6 +50,7 @@ fn test() { } false { "+", + "-", "0xg", "0x_2", "0b3", @@ -105,6 +106,19 @@ fn float_parser_test() { let r#type = src.as_var_type(); assert!(matches!(r#type, VarType::Number(_)), "{:?}", r#type); } + + let bad_src = [ + "-", + "-.", + ".", + "1.2e3", + "e3", + "-e3", + ]; + for src in bad_src { + let r#type = src.as_var_type(); + assert!(!matches!(r#type, VarType::Number(_)), "{:?}", r#type); + } } #[test]