-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
-2**63
(min sint64
value), add test cases of utmost ints
- Loading branch information
1 parent
fd7f308
commit e33828a
Showing
8 changed files
with
167 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
shared/src/main/scala/io/kaitai/struct/translators/MinSignedIntegers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.kaitai.struct.translators | ||
|
||
import io.kaitai.struct.exprlang.Ast | ||
|
||
/** | ||
* Special handling of the minimum values of 32-bit (`-2**31 = -0x8000_0000`) and 64-bit (`-2**63 = | ||
* -0x8000_0000_0000_0000`) signed integer types. Almost all languages have a unary minus (`-`) as | ||
* a normal unary operator; it takes its operand (usually a positive integer) and negates it. | ||
* However, there is no positive counterpart to | ||
* | ||
* 1. `-2**31` in signed 32-bit integers (the maximum `sint32` is `2**31 - 1`); | ||
* 2. `-2**64` in signed 64-bit integers (the maximum `sint64` is `2**63 - 1`). | ||
* | ||
* `2**31` and `2**63` generally require either an *un*signed {32,64}-bit integer, or a signed | ||
* integer of a larger size (e.g. `sint64` for `2**31`). But these types may not be available in | ||
* all languages/platforms and it doesn't make sense to rely on them just to be able to get the | ||
* minimum negative value. | ||
*/ | ||
trait MinSignedIntegers | ||
extends AbstractTranslator | ||
with CommonLiterals { | ||
override def doIntLiteral(n: BigInt): String = { | ||
if (n == Long.MinValue || n == Int.MinValue) { | ||
translate( | ||
Ast.expr.BinOp( | ||
Ast.expr.IntNum(n + 1), | ||
Ast.operator.Sub, | ||
Ast.expr.IntNum(1) | ||
) | ||
) | ||
} else { | ||
super.doIntLiteral(n) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters