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

.scan to search for a buffer #21

Open
SamyPesse opened this issue Apr 17, 2016 · 1 comment
Open

.scan to search for a buffer #21

SamyPesse opened this issue Apr 17, 2016 · 1 comment

Comments

@SamyPesse
Copy link

node-binary has a method .scan:

var b = binary()
    .scan('line', new Buffer('\r\n'))
    .tap(function (vars) {
        console.log(vars.line)
    });

I'll be happy to propose a PR to add this if @deoxxa thinks it's a good idea.

@SamyPesse SamyPesse changed the title Implement .scan .scan to search for a buffer Apr 17, 2016
@SamyPesse
Copy link
Author

SamyPesse commented Apr 17, 2016

Here is a basic implementation:

function scan(name, search) {
    search = new Buffer(search, 'utf8');
    var result = new Buffer('');

    return this.loop(function(end) {
        this.buffer('tmpSearch', 1)
        .tap(function() {
            var c = this.vars.tmpSearch;
            result = Buffer.concat([result, c]);

            var toSearch = result.slice(-search.length);

            if (toSearch.compare(search) === 0) {
                delete this.vars.tmpSearch;
                this.vars[name] = result.slice(0, -search.length);
                end();
            }
        });
    });
}

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

1 participant