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

buffer: Added Buffer#includes() to keep parity with TypedArray. #3567

Closed
wants to merge 1 commit into from
Closed

buffer: Added Buffer#includes() to keep parity with TypedArray. #3567

wants to merge 1 commit into from

Conversation

umamialex
Copy link
Contributor

Simply adds Buffer.prototype.includes by wrapping an indexOf and
performing a strict equals check to -1.

The includes method takes the search value, byteOffset, and
encoding as arguments.

The test is a modified version of the indexOf test.

Addresses #3552.

@Fishrock123 Fishrock123 added the buffer Issues and PRs related to the buffer subsystem. label Oct 28, 2015
@Fishrock123
Copy link
Contributor

cc @trevnorris

@Fishrock123 Fishrock123 added the semver-minor PRs that contain new features and should be released in the next minor version. label Oct 28, 2015
@@ -0,0 +1,256 @@
'use strict';
var common = require('../common');
Copy link
Contributor

Choose a reason for hiding this comment

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

const instead of var please

@umamialex
Copy link
Contributor Author

Switched var to const where possible.

@bnoordhuis
Copy link
Member

Isn't this a little premature? TypedArray#includes() is still behind a flag in the V8 we ship, isn't it?

@trevnorris
Copy link
Contributor

@bnoordhuis v8's implementation of this and other methods like fill(), indexOf(), etc. are all restrictive and/or slow, and we've already replaced them with our own implementation. So figured it would be fine to implement this now using our faster implementation since the change was relatively minimal.

@trevnorris
Copy link
Contributor

@suitupalex Excellent job on all the tests. LGTM if CI is happy.

CI: https://ci.nodejs.org/job/node-test-pull-request/640/

@evanlucas
Copy link
Contributor

Mind adding docs for it?

Simply adds Buffer.prototype.includes by wrapping an indexOf and
performing a strict equals check to -1.

The includes method takes the search value, byteOffset, and
encoding as arguments.

The test is a modified version of the indexOf test.

Addresses #3552.
@umamialex
Copy link
Contributor Author

@trevnorris thanks! Glad to be able to contribute.

@evanlucas absolutely. Just reupped with documentation.

@evanlucas
Copy link
Contributor

CI looks good. Those failures look unrelated. It looks like the PPC machines are having some trouble though? /cc @nodejs/build

@evanlucas
Copy link
Contributor

LGTM

@rvagg
Copy link
Member

rvagg commented Oct 28, 2015

Even though we're bypassing V8 on this I'd be in favour of waiting till TypedArray#includes() has landed stable upstream. There's a chance we could be overlooking something and it would be good to be able to test our implementation alongside theirs so we get maximum coverage of the requirements. The fact that they still have it in staging suggests (to me, without any actual knowledge of why) that it's non-trivial.

@rvagg
Copy link
Member

rvagg commented Oct 28, 2015

^ the main reason for waiting would be to ensure that we don't ship something we have to break in a later version because we've overlooked something.

@trevnorris
Copy link
Contributor

@rvagg I'm curious what you could mean? Our implementations all break ECMA standards (b/c honestly they suck in this case), so I'm not sure what we should be worried about.

@domenic
Copy link
Contributor

domenic commented Oct 30, 2015

includes was specifically designed not to suck so I'd at least like to see some benchmarks before creating an overriden version...

@trevnorris
Copy link
Contributor

@domenic but you can still only pass a number, right? Buffer#includes() should allow multi character strings or Buffers to be passed in like similar existing APIs in core. In this case it's not only about performance.

@domenic
Copy link
Contributor

domenic commented Oct 30, 2015

Why? That isn't what "includes" means in ES.

@targos
Copy link
Member

targos commented Oct 30, 2015

It would be similar to String#includes:

new Buffer('abcd').includes('bc') <=> 'abcd'.includes('bc')

@Fishrock123
Copy link
Contributor

@domenic Keep in mind this isn't TypedArray.includes -- this is for buffer, which acts more like a string in some cases.

@Fishrock123
Copy link
Contributor

Is anything blocking this?

@trevnorris
Copy link
Contributor

Nothing from me.

@Fishrock123
Copy link
Contributor

LGTM

@jasnell
Copy link
Member

jasnell commented Nov 5, 2015

Perhaps the approach to take on this is to get it landed in master but hold off just a bit before landing it in v5.x? The change itself LGTM

@trevnorris
Copy link
Contributor

@Fishrock123 Follow up, can this be merged?

@Fishrock123
Copy link
Contributor

@trevnorris is the agreement to ship in master but not land it in a stable until TypedArray adds it?

@trevnorris
Copy link
Contributor

@Fishrock123 Not sure. Since this disregards the Typed Array spec I was under the impression that it wouldn't matter. Let's land this on master and discuss back porting after.

trevnorris pushed a commit that referenced this pull request Dec 10, 2015
Add Buffer#includes() by wrapping an indexOf and performing a strict
equals check to -1.

The includes method takes the search value, byteOffset, and encoding as
arguments.

The test is a modified version of the indexOf test.

Fixes: #3552
PR-URL: #3567
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@trevnorris
Copy link
Contributor

Thanks much! Landed in 67e1819.

@trevnorris trevnorris closed this Dec 10, 2015
cjihrig pushed a commit that referenced this pull request Dec 16, 2015
Add Buffer#includes() by wrapping an indexOf and performing a strict
equals check to -1.

The includes method takes the search value, byteOffset, and encoding as
arguments.

The test is a modified version of the indexOf test.

Fixes: #3552
PR-URL: #3567
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
cjihrig added a commit that referenced this pull request Dec 16, 2015
Notable changes:

* buffer:
  - Buffer.prototype.includes() has been added to keep parity
    with TypedArrays. (Alexander Martin) #3567.
* domains:
  - Fix handling of uncaught exceptions.
    (Julien Gilli) #3654.
* https:
  - Added support for disabling session caching.
    (Fedor Indutny) #4252.
* repl:
  - Allow third party modules to be imported using
    require(). This corrects a regression from 5.2.0.
    (Ben Noordhuis) #4215.
* deps:
  - Upgrade libuv to 1.8.0.
    (Saúl Ibarra Corretgé) #4276.

PR-URL: #4281
cjihrig added a commit that referenced this pull request Dec 16, 2015
Notable changes:

* buffer:
  - Buffer.prototype.includes() has been added to keep parity
    with TypedArrays. (Alexander Martin) #3567.
* domains:
  - Fix handling of uncaught exceptions.
    (Julien Gilli) #3654.
* https:
  - Added support for disabling session caching.
    (Fedor Indutny) #4252.
* repl:
  - Allow third party modules to be imported using
    require(). This corrects a regression from 5.2.0.
    (Ben Noordhuis) #4215.
* deps:
  - Upgrade libuv to 1.8.0.
    (Saúl Ibarra Corretgé) #4276.

PR-URL: #4281

Conflicts:
	src/node_version.h
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
Add Buffer#includes() by wrapping an indexOf and performing a strict
equals check to -1.

The includes method takes the search value, byteOffset, and encoding as
arguments.

The test is a modified version of the indexOf test.

Fixes: nodejs#3552
PR-URL: nodejs#3567
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
Notable changes:

* buffer:
  - Buffer.prototype.includes() has been added to keep parity
    with TypedArrays. (Alexander Martin) nodejs#3567.
* domains:
  - Fix handling of uncaught exceptions.
    (Julien Gilli) nodejs#3654.
* https:
  - Added support for disabling session caching.
    (Fedor Indutny) nodejs#4252.
* repl:
  - Allow third party modules to be imported using
    require(). This corrects a regression from 5.2.0.
    (Ben Noordhuis) nodejs#4215.
* deps:
  - Upgrade libuv to 1.8.0.
    (Saúl Ibarra Corretgé) nodejs#4276.

PR-URL: nodejs#4281

Conflicts:
	src/node_version.h
aqrln added a commit to aqrln/node that referenced this pull request Mar 26, 2017
Some of the tests for `buffer.includes()` functionality introduced in
nodejs#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

Refs: nodejs#3567
jasnell pushed a commit that referenced this pull request Mar 27, 2017
Some of the tests for `buffer.includes()` functionality introduced in
#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

PR-URL: #12040
Ref: #3567
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Mar 28, 2017
Some of the tests for `buffer.includes()` functionality introduced in
#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

PR-URL: #12040
Ref: #3567
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 18, 2017
Some of the tests for `buffer.includes()` functionality introduced in
#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

PR-URL: #12040
Ref: #3567
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Some of the tests for `buffer.includes()` functionality introduced in
#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

PR-URL: #12040
Ref: #3567
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
Some of the tests for `buffer.includes()` functionality introduced in
nodejs/node#3567 have been broken in a way that
caused them to always pass regardless of the result of the tested
method.

This behavior was caused by two reasons:

 * These tests were written as though `buffer.includes()` was supposed
   to return the same value that `buffer.indexOf()` does, i.e., used
   indices or -1 as expected return values instead of true and false.
 * `assert()` was used as the assertion function to do that instead of
   `assert.strictEqual()`.

Thus `assert()` was called with a non-zero number as the first argument
effectively causing these tests to pass.

This commit changes the tests to use `assert.ok()` and removes redundant
indices.

PR-URL: nodejs/node#12040
Ref: nodejs/node#3567
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
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. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants