Skip to content

Commit

Permalink
Fixed: 编译期运算将单个负号当做数字并解析失败
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 1, 2024
1 parent d8ec478 commit 8dcdcd1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.14.1"
version = "0.14.2"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion tools/var_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions tools/var_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AsVarType for str {
}
fn as_number(s: &str) -> Option<f64> {
static NUM_REGEX: &Lazy<Regex> = regex!(
r"^-?(?:\d+(?:e[+\-]?\d+|\.\d*)?|\d*(?:\.\d+)?)$"
r"^-?(?:\d+(?:e[+\-]?\d+|\.\d*)?|\d*\.\d+)$"
);
static HEX_REGEX: &Lazy<Regex> = regex!(
r"^0x-?[0-9A-Fa-f]+$"
Expand All @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions tools/var_utils/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn test() {
}
false {
"+",
"-",
"0xg",
"0x_2",
"0b3",
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 8dcdcd1

Please sign in to comment.