mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-23 07:29:54 +00:00
producer can seek
This commit is contained in:
parent
59de09ad36
commit
8a94b6a318
@ -32,8 +32,9 @@ use self::ProducerState::*;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::num::Int;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::io::{Read,Seek,SeekFrom};
|
||||
|
||||
/// Holds the data producer's current state
|
||||
///
|
||||
@ -55,7 +56,9 @@ pub enum ProducerState<O> {
|
||||
|
||||
/// A producer implements the produce method, currently working with u8 arrays
|
||||
pub trait Producer {
|
||||
fn produce(&mut self) -> ProducerState<&[u8]>;
|
||||
fn produce(&mut self) -> ProducerState<&[u8]>;
|
||||
fn seek_to(&mut self, position:u64) -> Option<u64>;
|
||||
fn seek_offset(&mut self, offset:i64) -> Option<u64>;
|
||||
}
|
||||
|
||||
/// Can produce data from a file
|
||||
@ -100,6 +103,14 @@ impl Producer for FileProducer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn seek_to(&mut self, position: u64) -> Option<u64> {
|
||||
self.file.seek(SeekFrom::Start(position)).ok()
|
||||
}
|
||||
|
||||
fn seek_offset(&mut self, offset: i64) -> Option<u64> {
|
||||
self.file.seek(SeekFrom::Current(offset)).ok()
|
||||
}
|
||||
}
|
||||
|
||||
/// Can parse data from an already in memory byte array
|
||||
@ -146,6 +157,35 @@ impl<'x> Producer for MemProducer<'x> {
|
||||
ProducerError(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn seek_to(&mut self, position: u64) -> Option<u64> {
|
||||
if position as usize > self.length {
|
||||
self.index = self.length
|
||||
} else {
|
||||
self.index = position as usize
|
||||
}
|
||||
Some(self.index as u64)
|
||||
}
|
||||
|
||||
fn seek_offset(&mut self, offset: i64) -> Option<u64> {
|
||||
let next = if offset >= 0 {
|
||||
(self.index as u64).checked_add(offset as u64)
|
||||
} else {
|
||||
(self.index as u64).checked_sub(-offset as u64)
|
||||
};
|
||||
|
||||
match next {
|
||||
None => None,
|
||||
Some(u) => {
|
||||
if u as usize > self.length {
|
||||
self.index = self.length
|
||||
} else {
|
||||
self.index = u as usize
|
||||
}
|
||||
Some(self.index as u64)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepares a parser function for a push pipeline
|
||||
|
Loading…
Reference in New Issue
Block a user