Skip to content

Commit 9133674

Browse files
committed
Merge 'Parse hex integers 2' from Anton Harniakou
Continuation of #1329 Reviewed-by: Jussi Saurio <[email protected]> Closes #1347
2 parents 13a703d + 8c797a9 commit 9133674

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

core/translate/expr.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ pub fn translate_expr(
18501850
}
18511851
ast::Expr::Literal(lit) => match lit {
18521852
ast::Literal::Numeric(val) => {
1853-
if val.starts_with("0x") {
1853+
if val.starts_with("0x") || val.starts_with("0X") {
18541854
// must be a hex decimal
18551855
let int_value = i64::from_str_radix(&val[2..], 16)?;
18561856
program.emit_insn(Insn::Integer {
@@ -1941,14 +1941,22 @@ pub fn translate_expr(
19411941
// Special case: if we're negating "9223372036854775808", this is exactly MIN_INT64
19421942
// If we don't do this -1 * 9223372036854775808 will overflow and parse will fail
19431943
// and trigger conversion to Real.
1944-
if numeric_value == "9223372036854775808" {
1944+
if numeric_value == "9223372036854775808"
1945+
|| numeric_value.eq_ignore_ascii_case("0x7fffffffffffffff")
1946+
{
19451947
program.emit_insn(Insn::Integer {
19461948
value: i64::MIN,
19471949
dest: target_register,
19481950
});
19491951
} else {
1950-
let maybe_int = numeric_value.parse::<i64>();
1951-
if let Ok(value) = maybe_int {
1952+
if numeric_value.starts_with("0x") || numeric_value.starts_with("0X") {
1953+
// must be a hex decimal
1954+
let int_value = i64::from_str_radix(&numeric_value[2..], 16)?;
1955+
program.emit_insn(Insn::Integer {
1956+
value: -int_value,
1957+
dest: target_register,
1958+
});
1959+
} else if let Ok(value) = numeric_value.parse::<i64>() {
19521960
program.emit_insn(Insn::Integer {
19531961
value: value * -1,
19541962
dest: target_register,
@@ -1982,8 +1990,13 @@ pub fn translate_expr(
19821990
Ok(target_register)
19831991
}
19841992
(UnaryOperator::BitwiseNot, ast::Expr::Literal(ast::Literal::Numeric(num_val))) => {
1985-
let maybe_int = num_val.parse::<i64>();
1986-
if let Ok(val) = maybe_int {
1993+
if num_val.starts_with("0x") || num_val.starts_with("0X") {
1994+
let int_value = i64::from_str_radix(&num_val[2..], 16)?;
1995+
program.emit_insn(Insn::Integer {
1996+
value: !int_value,
1997+
dest: target_register,
1998+
});
1999+
} else if let Ok(val) = num_val.parse::<i64>() {
19872000
program.emit_insn(Insn::Integer {
19882001
value: !val,
19892002
dest: target_register,

testing/math.test

+5-1
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,14 @@ do_execsql_test bitwise-not-text-float {
624624
SELECT ~'823.34'
625625
} {-824}
626626

627-
do_execsql_test bitwise-not-text-int {
627+
do_execsql_test bitwise-not-text-int-1 {
628628
SELECT ~'1234'
629629
} {-1235}
630630

631+
do_execsql_test bitwise-not-text-int-2 {
632+
SELECT ~0xA
633+
} {-11}
634+
631635
do_execsql_test bitwise-not-scalar-float {
632636
SELECT ~abs(693.9)
633637
} {-694}

testing/select.test

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ do_execsql_test select-const-3 {
1515
SELECT 0xDEAF
1616
} {57007}
1717

18+
do_execsql_test select-const-4 {
19+
SELECT -0xA
20+
} {-10}
21+
1822
do_execsql_test select-true {
1923
SELECT true
2024
} {1}

0 commit comments

Comments
 (0)