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 trevnorris · Pull Request #561 · nodejs/node

Conversation

@trevnorris

@trevnorris trevnorris commented Jan 22, 2015

Copy link
Copy Markdown
Contributor

Add Buffer#indexOf(). Support strings, numbers and other Buffers. This
is more support than String#indexOf() gives, but the increased
versatility should be helpful.

Special thanks to Sam Rijs for first proposing this
change.

R=@bnoordhuis

This is a re-hash of #160. Done this way so future support can include regexp's and arrays.

Sorry, something went wrong.

Uh oh!

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

All reactions
@trevnorris trevnorris added buffer Issues and PRs related to the buffer subsystem. enhancement labels Jan 22, 2015
@trevnorris
trevnorris force-pushed the buf-indexof-enhancement branch from 52ebeaf to ce9e4b2 Compare January 22, 2015 23:51
@trevnorris

trevnorris commented Jan 22, 2015

Copy link
Copy Markdown
Contributor Author

Ref to original PR that brought this about: #160

All reactions

Sorry, something went wrong.

Uh oh!

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

rvagg
rvagg reviewed Jan 23, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@rvagg rvagg Jan 23, 2015

Copy link
Copy Markdown
Member

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.

maybe else if, does that have any optimisation benefit here?

Sorry, something went wrong.

Uh oh!

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

All reactions

@trevnorris trevnorris Jan 23, 2015

Copy link
Copy Markdown
Contributor 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.

Good point. Will do.

Sorry, something went wrong.

Uh oh!

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

All reactions
@trevnorris
trevnorris force-pushed the buf-indexof-enhancement branch from ce9e4b2 to e950613 Compare January 23, 2015 00:30
chrisdickinson
chrisdickinson reviewed Jan 23, 2015
View reviewed changes
Comment thread src/node_buffer.cc Outdated

@chrisdickinson chrisdickinson Jan 23, 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.

We silently truncate values >255?

Sorry, something went wrong.

Uh oh!

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

All reactions

@trevnorris trevnorris Jan 23, 2015

Copy link
Copy Markdown
Contributor 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.

Am I supposed to throw?

Sorry, something went wrong.

Uh oh!

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

All reactions

@feross feross Jan 26, 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.

We already silently truncate values >255 when initializing a buffer:

new Buffer([256]) // <Buffer 00>

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisdickinson chrisdickinson Jan 26, 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.

Mostly curious if we'd want to treat >0xff values as "search for byte sequence," though I suppose that could start to get into endianness issues.

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisdickinson chrisdickinson Jan 26, 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.

(alternatively, yes, I would consider throwing for the sake of proper input validation.)

Sorry, something went wrong.

Uh oh!

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

All reactions

@trevnorris trevnorris Jan 26, 2015

Copy link
Copy Markdown
Contributor 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.

We're not going to throw. I was also considering the search for byte sequence. e.g. b.indexOf(0xbada55), and think it is a good solution. Though users will need to know that byte sequences over 0x20000000000000 loose precision. Also, I'm not worried about endianness. The input data is endiannes independent, and it's up to the user to know what the data looks like that they're searching for.

@bnoordhuis What would be the fastest way to convert a number to a byte sequence that can be searched for?

Sorry, something went wrong.

Uh oh!

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

All reactions

@bnoordhuis bnoordhuis Jan 26, 2015

Copy link
Copy Markdown
Member

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.

You mean like this?

uint32_t needle = args[1]->Uint32Value();
void* ptr = memmem(haystack, haystack_len, &needle, sizeof(needle));

Sorry, something went wrong.

Uh oh!

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

All reactions
@trevnorris
trevnorris force-pushed the buf-indexof-enhancement branch from e950613 to f28b793 Compare January 23, 2015 00:44
mscdex
mscdex reviewed Jan 23, 2015
View reviewed changes
Comment thread src/node_buffer.cc Outdated

@mscdex mscdex Jan 23, 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.

Missing ASSERT(args[2]->IsNumber()); ?

Sorry, something went wrong.

Uh oh!

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

All reactions

@trevnorris trevnorris Jan 23, 2015

Copy link
Copy Markdown
Contributor 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.

Hm. Guess I could put it in. The JS simply does coercion for sanity, but the Uint32Value() will still generally do the correct thing. Guess I'll throw it in.

Sorry, something went wrong.

Uh oh!

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

All reactions

@mscdex mscdex Jan 23, 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.

Ah ok.

Sorry, something went wrong.

Uh oh!

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

All reactions
@trevnorris
trevnorris force-pushed the buf-indexof-enhancement branch from f28b793 to 0d50c86 Compare January 23, 2015 00:49
@mscdex

mscdex commented Jan 23, 2015

Copy link
Copy Markdown
Contributor

How would regexp functionality be implemented/supported? The built-in C regex library, v8's regex library, pcre, or?

All reactions

Sorry, something went wrong.

Uh oh!

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

@trevnorris

trevnorris commented Jan 23, 2015

Copy link
Copy Markdown
Contributor Author

@mscdex I'm working on that now. Can hack around it by having a pre-allocated external array class and reassign it on the fly, then convert the regex into 1 byte characters (for utf8 safety). So V8 would still do the heavy lifting but all the resources would still live outside the heap.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rvagg

rvagg commented Jan 23, 2015

Copy link
Copy Markdown
Member

see also #161 for lastIndexOf() (listing here for reference, not necessarily recommendation)

All reactions

Sorry, something went wrong.

Uh oh!

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

@rvagg rvagg added the semver-minor PRs that contain new features and should be released in the next minor version. label Jan 23, 2015
bnoordhuis
bnoordhuis reviewed Jan 23, 2015
View reviewed changes
Comment thread src/node_buffer.cc Outdated

@bnoordhuis bnoordhuis Jan 23, 2015

Copy link
Copy Markdown
Member

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.

Suggestion: let's switch to std::min() and std::max() in a follow-up PR.

Sorry, something went wrong.

Uh oh!

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

All reactions
@feross

feross commented Jan 26, 2015

Copy link
Copy Markdown
Contributor

@trevnorris Great work!

While considering how to add support in buffer, I realized I don't like how Buffer.prototype.indexOf behaves with a negative byteOffet value.

If byteOffset < 0, we currently search the whole buffer (same as passing 0). This is how String.prototype.indexOf behaves.

I think it makes more sense to treat a byteOffset < 0 as the offset from the end of the buffer. This is how Array.prototype.indexOf and TypedArray.prototype.indexOf do it. In general, I'd prefer to copy the way TypedArray works unless we have a good reason to do otherwise. Here's the spec.

All reactions

Sorry, something went wrong.

Uh oh!

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

feross
feross reviewed Jan 26, 2015
View reviewed changes
Comment thread test/parallel/test-buffer.js Outdated

@feross feross Jan 26, 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.

If we changed to Array.prototype.indexOf/TypedArray.prototype.indexOf semantics, this would change to:

assert.equal(b.indexOf('a', -1), -1);

And I'd add a few more tests for good measure:

assert.equal(b.indexOf('a', -4), -1);
assert.equal(b.indexOf('a', -5), 0);

Sorry, something went wrong.

Uh oh!

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

All reactions

@trevnorris trevnorris Mar 2, 2015

Copy link
Copy Markdown
Contributor 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.

thanks.

Sorry, something went wrong.

Uh oh!

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

All reactions
This was referenced Dec 1, 2023
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#368
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#374
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#376
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#383
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#386
Open
[Snyk] Security upgrade xo from 0.11.2 to 0.42.0 leonardoadame/node#390
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#392
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#398
Open
[Snyk] Security upgrade xo from 0.24.0 to 0.42.0 leonardoadame/node#400
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#407
Open
[Snyk] Security upgrade xo from 0.35.0 to 0.42.0 leonardoadame/node#409
Open
[Snyk] Security upgrade xo from 0.16.0 to 0.42.0 leonardoadame/node#412
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#414
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#430
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#434
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#436
Closed
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#439
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#449
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#454
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#456
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#461
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#469
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#476
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#487
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#494
Open
[Snyk] Security upgrade xo from 0.23.0 to 0.42.0 leonardoadame/node#509
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#524
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#537
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#544
Open
[Snyk] Fix for 1 vulnerabilities leonardoadame/node#546
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

buffer Issues and PRs related to the buffer subsystem. 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.

11 participants

@trevnorris @mscdex @rvagg @feross @Fishrock123 @mikeal @julianduque @bnoordhuis @chrisdickinson @ralphtheninja @brendanashworth
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.