Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow $ and 0X (as well as 0x) for hex values #164

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`, `$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` | `()` |
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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` | `()` |
Expand Down Expand Up @@ -566,7 +566,7 @@
//! ## License
//!
//! This crate is primarily distributed under the terms of the MIT license.
//! See [LICENSE](LICENSE) for details.

Check warning on line 569 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

unresolved link to `LICENSE`

Check warning on line 569 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

unresolved link to `LICENSE`

Check warning on line 569 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

unresolved link to `LICENSE`
//!

#![deny(missing_docs)]
Expand Down
6 changes: 5 additions & 1 deletion src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult<Vec<Token>> {
}

fn parse_dec_or_hex(literal: &str) -> Result<IntType, std::num::ParseIntError> {
if let Some(literal) = literal.strip_prefix("0x") {
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)
Expand Down
3 changes: 3 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading