Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about offset into some data? #337

Open
r4v3n6101 opened this issue Feb 11, 2022 · 3 comments · May be fixed by #342
Open

How about offset into some data? #337

r4v3n6101 opened this issue Feb 11, 2022 · 3 comments · May be fixed by #342

Comments

@r4v3n6101
Copy link

r4v3n6101 commented Feb 11, 2022

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
Copy link
Owner

Marwes commented Feb 11, 2022

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
Copy link
Author

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
Copy link
Owner

Marwes commented Feb 13, 2022

Sure, that seems generally useful.

@r4v3n6101 r4v3n6101 linked a pull request Mar 8, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants