-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sam/record/data/field/value/integer: Support integer values up to (2^…
…32) - 1 Values between 2^31 and (2^32) - 1 are now parsed as unsigned 32-bit integers (`u32`), which are used as such in BAM and CRAM. This allows `hts-specs/test/sam/passed/aux.pass-i.sam` to pass, which requires support for parsing 4294967295.
- Loading branch information
Showing
4 changed files
with
72 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,66 @@ | ||
use std::io; | ||
|
||
use lexical_core::FromLexical; | ||
|
||
use crate::alignment::record::data::field::Value; | ||
|
||
pub(super) fn parse_int32_value<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> { | ||
let (n, i) = lexical_core::parse_partial(src) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
pub(super) fn parse_integer_value<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> { | ||
parse_int(src) | ||
.map(Value::Int32) | ||
.or_else(|e| match e { | ||
lexical_core::Error::Overflow(_) => parse_int(src).map(Value::UInt32), | ||
_ => Err(e), | ||
}) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) | ||
} | ||
|
||
fn parse_int<N>(src: &mut &[u8]) -> lexical_core::Result<N> | ||
where | ||
N: FromLexical, | ||
{ | ||
let (n, i) = lexical_core::parse_partial(src)?; | ||
*src = &src[i..]; | ||
Ok(n) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_integer_value() -> io::Result<()> { | ||
let mut src = &b"-2147483649"[..]; // -(1 << 31) - 1 | ||
assert!(matches!( | ||
parse_integer_value(&mut src), | ||
Err(e) if e.kind() == io::ErrorKind::InvalidData | ||
)); | ||
|
||
let mut src = &b"-2147483648"[..]; | ||
let actual = parse_integer_value(&mut src)?; | ||
assert!(matches!(actual, Value::Int32(i32::MIN))); | ||
|
||
let mut src = &b"0"[..]; | ||
let actual = parse_integer_value(&mut src)?; | ||
assert!(matches!(actual, Value::Int32(0))); | ||
|
||
let mut src = &b"2147483647"[..]; | ||
let actual = parse_integer_value(&mut src)?; | ||
assert!(matches!(actual, Value::Int32(i32::MAX))); | ||
|
||
let mut src = &b"2147483648"[..]; | ||
let actual = parse_integer_value(&mut src)?; | ||
assert!(matches!(actual, Value::UInt32(n) if n == 1 << 31)); | ||
|
||
let mut src = &b"4294967295"[..]; | ||
let actual = parse_integer_value(&mut src)?; | ||
assert!(matches!(actual, Value::UInt32(u32::MAX))); | ||
|
||
let mut src = &b"4294967296"[..]; // 1 << 32 | ||
assert!(matches!( | ||
parse_integer_value(&mut src), | ||
Err(e) if e.kind() == io::ErrorKind::InvalidData | ||
)); | ||
|
||
Ok(Value::Int32(n)) | ||
Ok(()) | ||
} | ||
} |