Giter Site home page Giter Site logo

Comments (1)

benjamin-hodgson avatar benjamin-hodgson commented on May 12, 2024

I don't think there's a combinator which'll do this for-break type of loop out of the box. You can code it up yourself using recursion:

Parser<byte, IEnumerable<byte>> NullTerminated()
    => NullTerminated(64);

Parser<byte, IEnumerable<byte>> NullTerminated(int count)
{
    if (count == 0)
    {
        return Return(new[] { });
    }
    return Byte.Then(b =>
        b == 0
            ? Return(new[] { })
            : NullTerminated(count - 1).Select(bs => new[]{ b }.Concat(bs))
    );
}

ofc that won't be very efficient cos you're building parsers at runtime and storing the output bytes in a linked list shaped structure. (Could perhaps tweak this by eg putting the bytes in a List or caching the recursive NullTerminated calls but that's messy.)


One way to design an API for this might be some sort of "bounded Many", which runs a parser in a loop until it fails without consuming input or some maximum number of repetitions is reached. Then your code would look like this:

Parser<byte, IEnumerable<byte>> NullTerminated
    => Token(b => b != 0).ManyUpTo(64);  // name TBD

I'd be happy to accept a PR implementing this ManyUpTo combinator.

from pidgin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.