GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: add indexOf method by srijs · Pull Request #160 · nodejs/node

Conversation

@srijs

@srijs srijs commented Dec 14, 2014

Copy link
Copy Markdown

Adds a .indexOf method to Buffer, which borrows semantics from
both Array.prototype.indexOf and String.prototype.indexOf.

Buffer.prototype.indexOf can be invoked with a Buffer, a string
or a number as the needle. If the needle a Buffer or string, it will
find the first occurrence of this sequence of bytes. If the needle is
a number, it will find the first occurrence of this byte.

Fixes #95.

If you're unsure about or not happy with the semantics, let's discuss :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@piscisaureus piscisaureus added the unknown label Dec 14, 2014
@srijs
srijs force-pushed the feature/buffer-indexof branch from 3d12cad to 6d4cdc7 Compare December 14, 2014 02:38
@srijs
srijs force-pushed the feature/buffer-indexof branch from 6d4cdc7 to cc46164 Compare December 14, 2014 02:46
@srijs
buffer: add indexOf method
984a21f
Adds a `.indexOf` method to `Buffer`, which borrows semantics from
both `Array.prototype.indexOf` and `String.prototype.indexOf`.

`Buffer.prototype.indexOf` can be invoked with a Buffer, a string
or a number as the needle.  If the needle a Buffer or string, it will
find the first occurrence of this sequence of bytes.  If the needle is
a number, it will find the first occurrence of this byte.

Reviewed-by: Sam Rijs <[email protected]>
Fixes: nodejs#95
PR-URL: nodejs#160
@srijs
srijs force-pushed the feature/buffer-indexof branch from cc46164 to 984a21f Compare December 14, 2014 03:01
@srijs srijs mentioned this pull request Dec 14, 2014
feature: Buffer.prototype.indexOf #95
Closed
@vkurchatkin

vkurchatkin commented Dec 14, 2014

Copy link
Copy Markdown
Contributor

Though this function might be useful as it is, I think indexOf is a bad name, because it doesn't work the same way as %TypedArray%.prototype.indexOf

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vkurchatkin
vkurchatkin reviewed Dec 14, 2014
View reviewed changes
Comment thread lib/buffer.js

@vkurchatkin vkurchatkin Dec 14, 2014

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allocating a buffer for a single byte seems too much. Also it's unclear that only one byte numbers are accepted. What if I want to search for 16 byte number? How to specify endianness?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@srijs srijs Dec 14, 2014

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intentionally follows the semantics of Array.prototype.indexOf.

It can thus only search for an element of the array. If you wanted to search for a sequence of elements (bytes), you can supply a Buffer as the needle.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisdickinson chrisdickinson Dec 15, 2014

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vkurchatkin As I understand it, the backing slow buffer will have already been allocated, so this shouldn't be too bad of an allocation.

We could amortize the cost of repeated calls by having a statically available single byte buffer into which we swap the value, which might be a nice optimization.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vkurchatkin vkurchatkin Dec 15, 2014

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about special functions on internal for each case

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@srijs srijs Dec 16, 2014

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with a hybrid interface, I also think different specialised functions would be a cleaner approach.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@srijs

srijs commented Dec 14, 2014

Copy link
Copy Markdown
Author

That depends on how you look at it.

The situation is that there is String.prototype.indexOf as well as Array.prototype.indexOf (which is semantically equivalent to %TypedArray%.prototype.indexOf). The method implemented by me is polymorphic wrt the needle you can supply, and can work perfectly fine for "find the index of byte 0xFF in this buffer" (Array.prototype.indexOf) and "find the index of the substring 'abc' (or Buffer<61,62,63>) in this buffer".

It might be that I am missing something, in that case don't hesitate to point it out :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vkurchatkin

vkurchatkin commented Dec 14, 2014

Copy link
Copy Markdown
Contributor

I think that function that finds a buffer inside of another buffer is generally more useful than function that finds a byte inside a buffer. But the spec says that Typed Arrays should behave just like arrays of numbers. And IMO Buffer should behave just like Uint8Array. Two reasons for that:

  • there were some talks about making Buffer inherit from Uint8Array when it's possible; in that case overriding methods is unwanted;
  • portability; as an example:
obj.on('data', function (data) {
// what is data?
// in node it's a `Buffer`
// in browser or ES6-compatible environment it's a `Uint8Array`
// but they are compatible (to some extent), so we don't really care
});

It is maybe too late for full compatibility, but we shouldn't make things worse for no good reason.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex

mscdex commented Dec 15, 2014

Copy link
Copy Markdown
Contributor

What about using something more efficient for multi-byte needles? I'm thinking of boyer-moore or a similar algorithm. streamsearch (used in dicer and other modules) uses a pure-javascript implementation of the boyer-moore-horspool algorithm and it is significantly faster than the typical naive search algorithm. Perhaps such an algorithm could be automatically used on haystacks whose lengths exceed some constant threshold.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aredridel

aredridel commented Dec 15, 2014

Copy link
Copy Markdown
Contributor

boyer-moore-horspool ++

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bnoordhuis

bnoordhuis commented Dec 15, 2014

Copy link
Copy Markdown
Member

However, Boyer-Moore-Horspool's worst case is O(n*m) whereas Boyer-Moore runs in linear time.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srijs

srijs commented Dec 16, 2014

Copy link
Copy Markdown
Author

While I would be in favour of using a more sophisticated approach than naive searching, for the case that we go with String.indexOf, I'd be interested to hear a few other opinions on the interface:

Go with the current one (hybrid), or choose only one of Array.indexOf or String.indexOf? And if yes, which one?

I think @vkurchatkin has some valid points, I'd be interested to hear what some other people think of it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@piscisaureus piscisaureus removed the unknown label Dec 16, 2014
@mikeal

mikeal commented Dec 16, 2014

Copy link
Copy Markdown
Contributor

This will make writing parsers so much better ;)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@piscisaureus piscisaureus closed this Dec 30, 2014
@piscisaureus piscisaureus reopened this Dec 30, 2014
@sonewman sonewman mentioned this pull request Jan 6, 2015
Streams pain points #89
Closed
@chrisdickinson

chrisdickinson commented Jan 14, 2015

Copy link
Copy Markdown
Contributor

Checking in.

@vkurchatkin I think the ship has sailed re: divergence from Uint8Array. We have already greatly diverged from the TypedArray spec in terms of public methods, and buffer-browserify shows how Buffers can be implemented on top of typed arrays for in-browser use.

@srijs There seems to be a preference for implementing this function in terms of Boyer-Moore, would you be interested in continuing this PR in that direction?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srijs

srijs commented Jan 14, 2015

Copy link
Copy Markdown
Author

@chrisdickinson Yes, I'd be interested in going that direction. I will start with the implementation in the next couple of days.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@feross

feross commented Jan 20, 2015

Copy link
Copy Markdown
Contributor

@srijs Ping. Any progress?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trevnorris
trevnorris reviewed Jan 22, 2015
View reviewed changes
Comment thread doc/api/buffer.markdown

@trevnorris trevnorris Jan 22, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly something like byteOffset would be better than fromIndex. The latter makes it sound like the character placement, but that's incorrect for multi-byte strings.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@trevnorris

trevnorris commented Jan 22, 2015

Copy link
Copy Markdown
Contributor

@srijs Thanks for doing this work. There are several optimizations that could be implemented, but it'd take me a while to explain how to do them. Mind if I implement this myself, and I could throw in regex support as well. :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex

mscdex commented Jan 22, 2015

Copy link
Copy Markdown
Contributor

+100

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris trevnorris mentioned this pull request Jan 22, 2015
buffer: add indexOf() method #561
Closed
@dannybrown13 dannybrown13 mentioned this pull request Nov 30, 2023
[Snyk] Security upgrade make-fetch-happen from 9.1.0 to 11.0.0 dannybrown13/node#50
Open
@137717unity 137717unity mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities 137717unity/node#110
Open
@dannybrown13 dannybrown13 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities dannybrown13/node#53
Open
@ryan-ally ryan-ally mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities ryan-ally/node#90
Open
@BryanLaura BryanLaura mentioned this pull request Nov 30, 2023
[Snyk] Security upgrade make-fetch-happen from 9.1.0 to 11.0.0 BryanLaura/node#52
Open
@bumplzz69 bumplzz69 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities bumplzz69/node#57
Open
@baby636 baby636 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities baby636/node#113
Open
@molllyn1 molllyn1 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities molllyn1/node#91
Open
@pratapadi pratapadi mentioned this pull request Nov 30, 2023
[Snyk] Security upgrade make-fetch-happen from 9.1.0 to 11.0.0 pratapadi/node#82
Open
@awaisab172 awaisab172 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities awaisab172/node#50
Open
@dewaynesmith3711 dewaynesmith3711 mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities dewaynesmith3711/node#49
Open
@xphillyx xphillyx mentioned this pull request Nov 30, 2023
[Snyk] Fix for 1 vulnerabilities xphillyx/node#27
Open
@AshdonG AshdonG mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities AshdonG/node#46
Open
@sirinartk sirinartk mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities sirinartk/node#49
Open
@BryanLaura BryanLaura mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities BryanLaura/node#66
Open
@terrorizer1980 terrorizer1980 mentioned this pull request Dec 1, 2023
[Snyk] Fix for 3 vulnerabilities terrorizer1980/node-1#15
Open
@Verdinjoshua26 Verdinjoshua26 mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Verdinjoshua26/node#54
Open
@Bhanditz Bhanditz mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Bhanditz/node#36
Open
@adamlaska adamlaska mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities adamlaska/node#129
Open
@Redwoodtj Redwoodtj mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Redwoodtj/node#11
Open
@wedataintelligence wedataintelligence mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities wedataintelligence/node#93
Open
@adamlaska adamlaska mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities adamlaska/node#131
Open
@enterstudio enterstudio mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities enterstudio/node#33
Open
@kaocher82 kaocher82 mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Xtuden-com/node#39
Open
@terrorizer1980 terrorizer1980 mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Exhorder6/node#24
Open
@Supermajestcd Supermajestcd mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities Supermajestcd/node#18
Open
@ajesse11x ajesse11x mentioned this pull request Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities ajesse11x/node#46
Open
This was referenced Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities MaxMood96/node#39
Open
[Snyk] Fix for 1 vulnerabilities Mement-Mori/node#34
Open
@snyk-io snyk-io Bot mentioned this pull request Oct 21, 2024
[Snyk] Fix for 26 vulnerabilities Graybar-codespace/node#49
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

12 participants

@srijs @vkurchatkin @mscdex @aredridel @bnoordhuis @mikeal @chrisdickinson @feross @trevnorris @piscisaureus @rvagg @Fishrock123
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.