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

Add Buffer.prototype.lastIndexOf() #4604

Closed
felixfbecker opened this issue Jan 10, 2016 · 18 comments
Closed

Add Buffer.prototype.lastIndexOf() #4604

felixfbecker opened this issue Jan 10, 2016 · 18 comments
Labels
buffer Issues and PRs related to the buffer subsystem. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.

Comments

@felixfbecker
Copy link
Contributor

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]
@cjihrig
Copy link
Contributor

cjihrig commented Jan 10, 2016

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

@felixfbecker
Copy link
Contributor Author

> Uint8Array.prototype.indexOf
[Function: indexOf]
> Uint8Array.prototype.lastIndexOf
[Function: lastIndexOf]

@felixfbecker
Copy link
Contributor Author

@mscdex mscdex added buffer Issues and PRs related to the buffer subsystem. doc Issues and PRs related to the documentations. labels Jan 10, 2016
@mscdex
Copy link
Contributor

mscdex commented Jan 10, 2016

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

@felixfbecker
Copy link
Contributor Author

But Why?

@mscdex
Copy link
Contributor

mscdex commented Jan 10, 2016

@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.

@felixfbecker
Copy link
Contributor Author

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

@mscdex
Copy link
Contributor

mscdex commented Jan 10, 2016

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

@felixfbecker felixfbecker changed the title Document Buffer.prototype.lastIndexOf() Add Buffer.prototype.lastIndexOf() Jan 10, 2016
@felixfbecker
Copy link
Contributor Author

@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.

@feross
Copy link
Contributor

feross commented Jan 11, 2016

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.

@Fishrock123 Fishrock123 added the good first issue Issues that are suitable for first-time contributors. label Jan 11, 2016
@emars
Copy link

emars commented Jan 15, 2016

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?

@mscdex
Copy link
Contributor

mscdex commented Jan 15, 2016

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

@zeusdeux
Copy link
Contributor

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

Edit: What do these byteOffset checks signify?

@zeusdeux
Copy link
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

@dcposch
Copy link
Contributor

dcposch commented Jan 24, 2016

@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 :)

@dcposch
Copy link
Contributor

dcposch commented Jan 24, 2016

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

@dcposch
Copy link
Contributor

dcposch commented Jan 28, 2016

PR now done, I think. Reviewers appreciated!

#4846

@felixfbecker @trevnorris @mikeal

@silverwind
Copy link
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
Labels
buffer Issues and PRs related to the buffer subsystem. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests

9 participants