-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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: add indexOf() method #561
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,6 +303,24 @@ Buffer.prototype.compare = function compare(b) { | |
}; | ||
|
||
|
||
Buffer.prototype.indexOf = function indexOf(val, byteOffset) { | ||
if (byteOffset > 0x7fffffff) | ||
byteOffset = 0x7fffffff; | ||
else if (byteOffset < -0x80000000) | ||
byteOffset = -0x80000000; | ||
byteOffset >>= 0; | ||
|
||
if (typeof val === 'string') | ||
return binding.indexOfString(this, val, byteOffset); | ||
if (val instanceof Buffer) | ||
return binding.indexOfBuffer(this, val, byteOffset); | ||
if (typeof val === 'number') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably throw an exception if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or just truncate in some way on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it truncates automatically. |
||
return binding.indexOfNumber(this, val, byteOffset); | ||
|
||
throw new TypeError('val must be string, number or Buffer'); | ||
}; | ||
|
||
|
||
Buffer.prototype.fill = function fill(val, start, end) { | ||
start = start >> 0; | ||
end = (end === undefined) ? this.length : end >> 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
var Buffer = require('buffer').Buffer; | ||
|
||
var b = new Buffer('abcdef'); | ||
var buf_a = new Buffer('a'); | ||
var buf_bc = new Buffer('bc'); | ||
var buf_f = new Buffer('f'); | ||
var buf_z = new Buffer('z'); | ||
var buf_empty = new Buffer(''); | ||
|
||
assert.equal(b.indexOf('a'), 0); | ||
assert.equal(b.indexOf('a', 1), -1); | ||
assert.equal(b.indexOf('a', -1), -1); | ||
assert.equal(b.indexOf('a', -4), -1); | ||
assert.equal(b.indexOf('a', -b.length), 0); | ||
assert.equal(b.indexOf('a', NaN), 0); | ||
assert.equal(b.indexOf('a', -Infinity), 0); | ||
assert.equal(b.indexOf('a', Infinity), -1); | ||
assert.equal(b.indexOf('bc'), 1); | ||
assert.equal(b.indexOf('bc', 2), -1); | ||
assert.equal(b.indexOf('bc', -1), -1); | ||
assert.equal(b.indexOf('bc', -3), -1); | ||
assert.equal(b.indexOf('bc', -5), 1); | ||
assert.equal(b.indexOf('bc', NaN), 1); | ||
assert.equal(b.indexOf('bc', -Infinity), 1); | ||
assert.equal(b.indexOf('bc', Infinity), -1); | ||
assert.equal(b.indexOf('f'), b.length - 1); | ||
assert.equal(b.indexOf('z'), -1); | ||
assert.equal(b.indexOf(''), -1); | ||
assert.equal(b.indexOf('', 1), -1); | ||
assert.equal(b.indexOf('', b.length + 1), -1); | ||
assert.equal(b.indexOf('', Infinity), -1); | ||
assert.equal(b.indexOf(buf_a), 0); | ||
assert.equal(b.indexOf(buf_a, 1), -1); | ||
assert.equal(b.indexOf(buf_a, -1), -1); | ||
assert.equal(b.indexOf(buf_a, -4), -1); | ||
assert.equal(b.indexOf(buf_a, -b.length), 0); | ||
assert.equal(b.indexOf(buf_a, NaN), 0); | ||
assert.equal(b.indexOf(buf_a, -Infinity), 0); | ||
assert.equal(b.indexOf(buf_a, Infinity), -1); | ||
assert.equal(b.indexOf(buf_bc), 1); | ||
assert.equal(b.indexOf(buf_bc, 2), -1); | ||
assert.equal(b.indexOf(buf_bc, -1), -1); | ||
assert.equal(b.indexOf(buf_bc, -3), -1); | ||
assert.equal(b.indexOf(buf_bc, -5), 1); | ||
assert.equal(b.indexOf(buf_bc, NaN), 1); | ||
assert.equal(b.indexOf(buf_bc, -Infinity), 1); | ||
assert.equal(b.indexOf(buf_bc, Infinity), -1); | ||
assert.equal(b.indexOf(buf_f), b.length - 1); | ||
assert.equal(b.indexOf(buf_z), -1); | ||
assert.equal(b.indexOf(buf_empty), -1); | ||
assert.equal(b.indexOf(buf_empty, 1), -1); | ||
assert.equal(b.indexOf(buf_empty, b.length + 1), -1); | ||
assert.equal(b.indexOf(buf_empty, Infinity), -1); | ||
assert.equal(b.indexOf(0x61), 0); | ||
assert.equal(b.indexOf(0x61, 1), -1); | ||
assert.equal(b.indexOf(0x61, -1), -1); | ||
assert.equal(b.indexOf(0x61, -4), -1); | ||
assert.equal(b.indexOf(0x61, -b.length), 0); | ||
assert.equal(b.indexOf(0x61, NaN), 0); | ||
assert.equal(b.indexOf(0x61, -Infinity), 0); | ||
assert.equal(b.indexOf(0x61, Infinity), -1); | ||
assert.equal(b.indexOf(0x0), -1); | ||
|
||
assert.throws(function() { | ||
b.indexOf(function() { }); | ||
}); | ||
assert.throws(function() { | ||
b.indexOf({}); | ||
}); | ||
assert.throws(function() { | ||
b.indexOf([]); | ||
}); |
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.
should probably have been
shouldn't it?
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.
Unfortunately that removes the code formatting to let the user know that the text is a link. Technically the correct output would have been
to place the
<code></code>
around the<a></a>
, instead of the other way around, but markdown wouldn't be interpreted that way.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.
My point is that there is no code formatting on this text and it shoudl be added, or are you pointing to a deficiency in our styles that should be fixed?
grep '\[
' doc/api/*` says that we're using this pattern a lot already and I don't really see a problem.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.
that was supposed to be
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.
Yes there is an issue with the styling. If written as
then there is no visual queue that the text is a link. So I intentionally didn't write it that way.
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.
/cc @chrisdickinson this is a problem in our styling then