Marwes / combine

A parser combinator library for Rust
https://docs.rs/combine/*/combine/
MIT License
1.29k stars 93 forks source link

How about offset into some data? #337

Open r4v3n6101 opened 2 years ago

r4v3n6101 commented 2 years ago

Hello. First of all, my thanks for this crate. So, I'm working on binary data parser, but I have some problems about offsets. Structure looks like this:

begin
header (16 bytes)
offsets point to data from beginning (count of N i32 which is acquired from header) 
...
data somewhere in block of input 
...
more data
...
and so on
...
end

How can I solve the problem of offset access using combine crate? Header and data by offsets should be stored in custom struct. Thanks in advance!

Marwes commented 2 years ago

I think you would use something like thenhttps://docs.rs/combine/4.6.3/combine/trait.Parser.html#method.then .

header()
   .then(|header| {
        parse(|input| {
             // Do a separate parse at the offset(s)
             let (output, _input) = my_parser().parse_state(&input[header.offset..])?;
            // Then return the original input to continue parsing there (or return the input where you want to continue parsing)
             Ok((output, input))
        })
   })
r4v3n6101 commented 2 years ago

That's exact functionality that I need. Thanks! Let me ask a question, what do you think about new in-crate parser called offset. It's kind of look_ahead + skip: we go to our offset as the current base index, but when we're out of this parser it uses previous index. I can provide test implementation of my idea, just let me know what you think about it.

Marwes commented 2 years ago

Sure, that seems generally useful.