Skip to content
Merged
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 @@ -437,7 +437,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`, `0b101`, `0o17` |
| `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 @@ -417,7 +417,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`, `0b101`, `0o17` |
//! | `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
15 changes: 10 additions & 5 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn partial_tokens_to_tokens<NumericTypes: EvalexprNumericTypes>(
},
PartialToken::Literal(literal) => {
cutoff = 1;
if let Ok(number) = parse_dec_or_hex::<NumericTypes>(&literal) {
if let Ok(number) = parse_integer::<NumericTypes>(&literal) {
Some(Token::Int(number))
} else if let Ok(number) = literal.parse::<NumericTypes::Float>() {
Some(Token::Float(number))
Expand Down Expand Up @@ -495,11 +495,16 @@ pub(crate) fn tokenize<NumericTypes: EvalexprNumericTypes>(
partial_tokens_to_tokens(&str_to_partial_tokens(string)?)
}

fn parse_dec_or_hex<NumericTypes: EvalexprNumericTypes>(
/// Can parse decimal (base 10), hexadecimal (base 16), binary (base 2), or octal (base 8).
fn parse_integer<NumericTypes: EvalexprNumericTypes>(
literal: &str,
) -> Result<NumericTypes::Int, ()> {
if let Some(literal) = literal.strip_prefix("0x") {
NumericTypes::Int::from_hex_str(literal)
} else if let Some(literal) = literal.strip_prefix("0b") {
NumericTypes::Int::from_binary_str(literal)
} else if let Some(literal) = literal.strip_prefix("0o") {
NumericTypes::Int::from_octal_str(literal)
} else {
NumericTypes::Int::from_str(literal).map_err(|_| ())
}
Expand Down Expand Up @@ -546,11 +551,11 @@ mod tests {
let token_string =
"+ - * / % ^ == != > < >= <= && || ! ( ) = += -= *= /= %= ^= &&= ||= , ; ";

let token_string_with_comments = r"+ - * / % ^ == != >
< >= <= && /* inline comment */ || ! ( )
let token_string_with_comments = r"+ - * / % ^ == != >
< >= <= && /* inline comment */ || ! ( )
= += -= *= /= %= ^=
// line comment
&&= ||= , ;
&&= ||= , ;
";

let tokens = tokenize::<DefaultNumericTypes>(token_string_with_comments).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions src/value/numeric_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ pub trait EvalexprInt<NumericTypes: EvalexprNumericTypes<Int = Self>>:
#[expect(clippy::result_unit_err)]
fn from_hex_str(literal: &str) -> Result<Self, ()>;

/// Parse `Self` from a binary string.
#[expect(clippy::result_unit_err)]
fn from_binary_str(literal: &str) -> Result<Self, ()>;

/// Parse `Self` from an octal string.
#[expect(clippy::result_unit_err)]
fn from_octal_str(literal: &str) -> Result<Self, ()>;

/// Perform an addition operation, returning an error on overflow.
fn checked_add(&self, rhs: &Self) -> EvalexprResult<Self, NumericTypes>;

Expand Down
8 changes: 8 additions & 0 deletions src/value/numeric_types/default_numeric_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ impl<NumericTypes: EvalexprNumericTypes<Int = Self>> EvalexprInt<NumericTypes> f
Self::from_str_radix(literal, 16).map_err(|_| ())
}

fn from_binary_str(literal: &str) -> Result<Self, ()> {
Self::from_str_radix(literal, 2).map_err(|_| ())
}

fn from_octal_str(literal: &str) -> Result<Self, ()> {
Self::from_str_radix(literal, 8).map_err(|_| ())
}

fn checked_add(&self, rhs: &Self) -> EvalexprResult<Self, NumericTypes> {
let result = (*self).checked_add(*rhs);
if let Some(result) = result {
Expand Down
26 changes: 26 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,32 @@ fn test_hex() {
);
}

#[test]
fn test_binary() {
assert_eq!(eval("0b11"), Ok(Value::Int(3)));
assert_eq!(eval("0b0101"), Ok(Value::Int(5)));
assert_eq!(eval("0b11111111"), Ok(Value::Int(255)));
assert_eq!(eval("-0b11111111"), Ok(Value::Int(-255)));
assert_eq!(
eval("0b2"),
// See `test_hex`.
Err(EvalexprError::VariableIdentifierNotFound("0b2".into()))
);
}

#[test]
fn test_octal() {
assert_eq!(eval("0o12"), Ok(Value::Int(10)));
assert_eq!(eval("0o3"), Ok(Value::Int(3)));
assert_eq!(eval("0o377"), Ok(Value::Int(255)));
assert_eq!(eval("-0o377"), Ok(Value::Int(-255)));
assert_eq!(
eval("0o8"),
// See `test_hex`.
Err(EvalexprError::VariableIdentifierNotFound("0o8".into()))
);
}

#[test]
fn test_broken_string() {
assert_eq!(
Expand Down