-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
574 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use arrow_schema::ArrowError; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct BufCursor<'a> { | ||
buf: &'a [u8], | ||
start_len: usize, | ||
} | ||
|
||
impl<'a> BufCursor<'a> { | ||
pub(crate) fn new(buf: &'a [u8]) -> Self { | ||
Self { | ||
buf, | ||
start_len: buf.len(), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn offset(&self) -> usize { | ||
self.start_len - self.buf.len() | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_u8(&mut self) -> Result<u8, ArrowError> { | ||
match self.buf.first().copied() { | ||
Some(x) => { | ||
self.buf = &self.buf[1..]; | ||
Ok(x) | ||
} | ||
None => Err(ArrowError::ParseError("Unexpected EOF".to_string())), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_bool(&mut self) -> Result<bool, ArrowError> { | ||
Ok(self.get_u8()? != 0) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_int(&mut self) -> Result<i32, ArrowError> { | ||
let mut in_progress = 0; | ||
let mut shift = 0; | ||
|
||
while let Some(byte) = self.buf.first().copied() { | ||
self.buf = &self.buf[1..]; | ||
in_progress |= ((byte & 0x7F) as u32) << shift; | ||
shift += 7; | ||
if byte & 0x80 == 0 { | ||
let val = in_progress; | ||
in_progress = 0; | ||
shift = 0; | ||
return Ok((val >> 1) as i32 ^ -((val & 1) as i32)); | ||
} | ||
} | ||
Err(ArrowError::ParseError( | ||
"Unexpected EOF reading int".to_string(), | ||
)) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_long(&mut self) -> Result<i64, ArrowError> { | ||
let mut in_progress = 0; | ||
let mut shift = 0; | ||
|
||
while let Some(byte) = self.buf.first().copied() { | ||
self.buf = &self.buf[1..]; | ||
in_progress |= ((byte & 0x7F) as u64) << shift; | ||
shift += 7; | ||
if byte & 0x80 == 0 { | ||
let val = in_progress; | ||
in_progress = 0; | ||
shift = 0; | ||
return Ok((val >> 1) as i64 ^ -((val & 1) as i64)); | ||
} | ||
} | ||
Err(ArrowError::ParseError( | ||
"Unexpected EOF reading long".to_string(), | ||
)) | ||
} | ||
|
||
pub(crate) fn get_bytes(&mut self) -> Result<&'a [u8], ArrowError> { | ||
let len: usize = self.get_long()?.try_into().map_err(|_| { | ||
ArrowError::ParseError("offset overflow reading avro bytes".to_string()) | ||
})?; | ||
|
||
if (self.buf.len() < len) { | ||
return Err(ArrowError::ParseError( | ||
"Unexpected EOF reading bytes".to_string(), | ||
)); | ||
} | ||
let ret = &self.buf[..len]; | ||
self.buf = &self.buf[len..]; | ||
Ok(ret) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_float(&mut self) -> Result<f32, ArrowError> { | ||
if (self.buf.len() < 4) { | ||
return Err(ArrowError::ParseError( | ||
"Unexpected EOF reading float".to_string(), | ||
)); | ||
} | ||
let ret = f32::from_le_bytes(self.buf[..4].try_into().unwrap()); | ||
self.buf = &self.buf[4..]; | ||
Ok(ret) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_double(&mut self) -> Result<f64, ArrowError> { | ||
if (self.buf.len() < 8) { | ||
return Err(ArrowError::ParseError( | ||
"Unexpected EOF reading float".to_string(), | ||
)); | ||
} | ||
let ret = f64::from_le_bytes(self.buf[..8].try_into().unwrap()); | ||
self.buf = &self.buf[8..]; | ||
Ok(ret) | ||
} | ||
} |
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
Oops, something went wrong.