From cf93a5ea0ff443ff380b18acc141e5cc55ed2133 Mon Sep 17 00:00:00 2001 From: paul moore Date: Wed, 3 Jan 2024 17:59:22 -0800 Subject: [PATCH 1/4] allow $ and 0X (as well as 0x) for hex values --- src/token/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/token/mod.rs b/src/token/mod.rs index ea22890..9876d30 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -480,7 +480,10 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult> { } fn parse_dec_or_hex(literal: &str) -> Result { - if let Some(literal) = literal.strip_prefix("0x") { + if let Some(literal) = literal + .strip_prefix("0x") + .or(literal.strip_prefix("0X").or(literal.strip_prefix("$"))) + { IntType::from_str_radix(literal, 16) } else { IntType::from_str_radix(literal, 10) From 4343e8d07d853adfdaae93a5b6b1af153cc3e7c0 Mon Sep 17 00:00:00 2001 From: paul moore Date: Wed, 3 Jan 2024 18:51:25 -0800 Subject: [PATCH 2/4] added doc and $hex tests --- README.md | 2 +- tests/integration.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 363e55a..6a5f8f1 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ Values are denoted as displayed in the following table. |------------|---------| | `Value::String` | `"abc"`, `""`, `"a\"b\\c"` | | `Value::Boolean` | `true`, `false` | -| `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0x1e` | +| `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0x1e`, `$f4` | | `Value::Float` | `3.`, `.35`, `1.00`, `0.5`, `123.554`, `23e4`, `-2e-3`, `3.54e+2` | | `Value::Tuple` | `(3, 55.0, false, ())`, `(1, 2)` | | `Value::Empty` | `()` | diff --git a/tests/integration.rs b/tests/integration.rs index 695056b..5cd34a7 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -2316,6 +2316,9 @@ fn test_hex() { assert_eq!(eval("0x3"), Ok(Value::Int(3))); assert_eq!(eval("0xFF"), Ok(Value::Int(255))); assert_eq!(eval("-0xFF"), Ok(Value::Int(-255))); + assert_eq!(eval("0XFF"), Ok(Value::Int(255))); + assert_eq!(eval("$fe"), Ok(Value::Int(254))); + assert!(eval("$xy").is_err()); assert_eq!( eval("0x"), // The "VariableIdentifierNotFound" error is what evalexpr currently returns, From a6dab9d9b4d19b8c7ec5476d341588cc476c27bf Mon Sep 17 00:00:00 2001 From: paul moore Date: Thu, 4 Jan 2024 10:12:15 -0800 Subject: [PATCH 3/4] clean up README sync and clippy complaint --- README.md | 2 +- src/lib.rs | 2 +- src/token/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6a5f8f1..566cf17 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ Values are denoted as displayed in the following table. |------------|---------| | `Value::String` | `"abc"`, `""`, `"a\"b\\c"` | | `Value::Boolean` | `true`, `false` | -| `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0x1e`, `$f4` | +| `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0X1e`, `$fe` | | `Value::Float` | `3.`, `.35`, `1.00`, `0.5`, `123.554`, `23e4`, `-2e-3`, `3.54e+2` | | `Value::Tuple` | `(3, 55.0, false, ())`, `(1, 2)` | | `Value::Empty` | `()` | diff --git a/src/lib.rs b/src/lib.rs index 763e94b..e80f395 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -413,7 +413,7 @@ //! |------------|---------| //! | `Value::String` | `"abc"`, `""`, `"a\"b\\c"` | //! | `Value::Boolean` | `true`, `false` | -//! | `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0x1e` | +//! | `Value::Int` | `3`, `-9`, `0`, `135412`, `0xfe02`, `-0X1e`, `$fe` | //! | `Value::Float` | `3.`, `.35`, `1.00`, `0.5`, `123.554`, `23e4`, `-2e-3`, `3.54e+2` | //! | `Value::Tuple` | `(3, 55.0, false, ())`, `(1, 2)` | //! | `Value::Empty` | `()` | diff --git a/src/token/mod.rs b/src/token/mod.rs index 9876d30..40e7ce9 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -482,7 +482,7 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult> { fn parse_dec_or_hex(literal: &str) -> Result { if let Some(literal) = literal .strip_prefix("0x") - .or(literal.strip_prefix("0X").or(literal.strip_prefix("$"))) + .or(literal.strip_prefix("0X").or(literal.strip_prefix('$'))) { IntType::from_str_radix(literal, 16) } else { From d96e0496ecd85b229e46bd23530ca7e9e05726f6 Mon Sep 17 00:00:00 2001 From: paul moore Date: Thu, 4 Jan 2024 13:50:42 -0800 Subject: [PATCH 4/4] change or to or_else --- src/token/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/token/mod.rs b/src/token/mod.rs index 40e7ce9..92e7521 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -480,10 +480,11 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult> { } fn parse_dec_or_hex(literal: &str) -> Result { - if let Some(literal) = literal - .strip_prefix("0x") - .or(literal.strip_prefix("0X").or(literal.strip_prefix('$'))) - { + if let Some(literal) = literal.strip_prefix("0x").or_else(|| { + literal + .strip_prefix("0X") + .or_else(|| literal.strip_prefix('$')) + }) { IntType::from_str_radix(literal, 16) } else { IntType::from_str_radix(literal, 10)