Skip to content

Add Buffer.prototype.lastIndexOf() #4604

Description

@felixfbecker

The docs only mention indexOf, but it's there, I would like to use it and don't feel good using undocumented API

$ node
> Buffer.prototype.lastIndexOf
[Function: lastIndexOf]

Activity

  1. cjihrig commented on Jan 10, 2016

    @cjihrig
    Contributor

    I believe that comes from the Uint8Array prototype, not Buffer itself like indexOf() does.

    EDIT:

    > Buffer.prototype.lastIndexOf === Uint8Array.prototype.lastIndexOf;
    true
    > Buffer.prototype.indexOf === Uint8Array.prototype.indexOf;
    false
    
  2. felixfbecker commented on Jan 10, 2016

    @felixfbecker
    ContributorAuthor
    > Uint8Array.prototype.indexOf
    [Function: indexOf]
    > Uint8Array.prototype.lastIndexOf
    [Function: lastIndexOf]
    
  3. felixfbecker commented on Jan 10, 2016

    @felixfbecker
    ContributorAuthor
  4. added
    bufferIssues and PRs related to the buffer subsystem.
    docIssues and PRs related to Node.js documentation.
    on Jan 10, 2016
  5. mscdex commented on Jan 10, 2016

    @mscdex
    Contributor

    @felixfbecker I think Buffer's indexOf() is a custom implementation though, it doesn't use Uint8Array?

  6. felixfbecker commented on Jan 10, 2016

    @felixfbecker
    ContributorAuthor

    But Why?

  7. mscdex commented on Jan 10, 2016

    @mscdex
    Contributor

    @felixfbecker Presumably because implementations for uint8Arrray.indexOf() aren't necessarily fast? buffer.indexOf() uses Boyer Moore, I'm not sure what v8 uses for their uint8Array.indexOf() implementation.

  8. felixfbecker commented on Jan 10, 2016

    @felixfbecker
    ContributorAuthor

    I'm not familiar with the different algorithms but why has indexOf then a special implementation and lastIndexOf doesn't?

  9. mscdex commented on Jan 10, 2016

    @mscdex
    Contributor

    @felixfbecker Probably because nobody has created a PR to add lastIndexOf() yet.

  10. changed the title [-]Document Buffer.prototype.lastIndexOf()[/-] [+]Add Buffer.prototype.lastIndexOf()[/+] on Jan 10, 2016
  11. felixfbecker commented on Jan 10, 2016

    @felixfbecker
    ContributorAuthor

    @mscdex thanks for the explanation. I would say this isnt a high priority, but it's confusing when browsing the API docs to see the indexOf method documented like it was special to Buffer even though it has the exact same API as the Uint8Array, just different implementation.

  12. feross commented on Jan 11, 2016

    @feross
    Contributor

    The implementation of buffer.indexOf is different from unit8array.indexOf.

    Uint8Array.prototype.indexOf can only search for individual elements (i.e. numbers).

    Buffer.prototype.indexOf can search for string, Buffer or numbers, so it's more similar to String.prototype.indexOf than Uint8Array.prototype.indexOf.

  13. emars commented on Jan 15, 2016

    @emars

    Does documentation for this belong in the buffer API file? (https://github.com/emars/node/blob/master/doc/api/buffer.markdown). I found that the method does not work to search for Sub-Buffers or Strings(returns -1 in both cases), only numbers. Seems kinda buggy to put in the core docs. Any thoughts?

  14. mscdex commented on Jan 15, 2016

    @mscdex
    Contributor

    @emars That's probably worth creating a new issue for.

  15. zeusdeux commented on Jan 22, 2016

    @zeusdeux
    Contributor

    I would like to take this up if no one else is already on it.

    Edit: What do these byteOffset checks signify?

  16. zeusdeux commented on Jan 22, 2016

    @zeusdeux
    Contributor

    So I have two ways of looking at the implementation for this:

    1. Call indexOfString, etc with a reversed copy of array pointed to by this and include adjusted offset (dunno what the adjustment is as yet)
    2. Implement indexOfNumberFromEnd, etc which use something like memrchr and then build lastIndexOf in terms of 'em

    Option one obviously has a big perf hit and two involves either implementing new functions or passing flags to existing ones to tell which end to search from.

    Which sounds better? Is there another approach that anyone has in mind?

    /cc @mscdex @cjihrig

  17. dcposch commented on Jan 24, 2016

    @dcposch
    Contributor

    @zeusdeux sorry, just saw your posts. just posted a PR with a patch. i started working on it on a few days ago on an island in the middle of nowhere :)

  18. dcposch commented on Jan 24, 2016

    @dcposch
    Contributor

    the PR is a work in progress: interface and tests, boyer moore not done. in short, it works but it's slow

  19. dcposch commented on Jan 28, 2016

    @dcposch
    Contributor

    PR now done, I think. Reviewers appreciated!

    #4846

    @felixfbecker @trevnorris @mikeal

  20. silverwind commented on May 4, 2016

    @silverwind
    Contributor

    Oh hey, this was implemented in 6c1e5ad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions