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

New combinator "fixed :: Int -> Parser a -> Parser a" #48

Closed
merijn opened this issue Jul 27, 2013 · 1 comment
Closed

New combinator "fixed :: Int -> Parser a -> Parser a" #48

merijn opened this issue Jul 27, 2013 · 1 comment

Comments

@merijn
Copy link

merijn commented Jul 27, 2013

I frequently find myself wanting/needing to parse complex fields of a fixed size, which is currently rather tricky using attoparsec. For example a 20 byte field consisting of a zero padded ASCII string. This field can be easily described as manyTill anyWord8 (word8 0) <* many (word8 0), but taking care of the size limiting is non-trivial.

I would like to have a combinator as the one proposed in the issue name, with the semantics that it tries to match it's argument parser completely against the next N bytes. An example implementation (although probably rather inefficient) would be:

fixed :: Int -> Parser a -> Parser a
fixed i p = do
    intermediate <- take i
    case parseOnly (p <* endOfInput) intermediate of
        Left _ -> empty
        Right x -> return x
@bos
Copy link
Collaborator

bos commented Feb 15, 2014

Your implementation seems reasonable, but I haven't seen any other requests for this, so I'm not inclined to add it in response to a once-off request. I'll reconsider if someone else shows up. Thanks.

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

No branches or pull requests

2 participants