Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit e2eb88b

Browse files
ronagaduh95
authored andcommitted
buffer: add end parameter
To limit how far into the buffer we search without allocating an unnecessary subarray. Signed-off-by: Robert Nagy <ronay@icloud.com> PR-URL: #62390 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f4a5c6 commit e2eb88b

5 files changed

Lines changed: 195 additions & 58 deletions

File tree

β€Ždoc/api/buffer.mdβ€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex'));
20852085
// Throws an exception.
20862086
```
20872087

2088-
### `buf.includes(value[, byteOffset][, encoding])`
2088+
### `buf.includes(value[, start[, end]][, encoding])`
20892089

20902090
<!-- YAML
20912091
added: v5.3.0
20922092
changes:
2093+
- version: REPLACEME
2094+
pr-url: https://github.com/nodejs/node/pull/62390
2095+
description: Added the `end` parameter.
20932096
- version: v24.13.1
20942097
pr-url: https://github.com/nodejs/node/pull/56578
20952098
description: supports Uint8Array as `this` value.
20962099
-->
20972100

20982101
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2099-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2102+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21002103
offset is calculated from the end of `buf`. **Default:**`0`.
2104+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2105+
`buf.length`.
21012106
*`encoding` {string} If `value` is a string, this is its encoding.
21022107
**Default:**`'utf8'`.
21032108
* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
@@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4));
21462151
// Prints: false
21472152
```
21482153

2149-
### `buf.indexOf(value[, byteOffset][, encoding])`
2154+
### `buf.indexOf(value[, start[, end]][, encoding])`
21502155

21512156
<!-- YAML
21522157
added: v1.5.0
21532158
changes:
2159+
- version: REPLACEME
2160+
pr-url: https://github.com/nodejs/node/pull/62390
2161+
description: Added the `end` parameter.
21542162
- version: v8.0.0
21552163
pr-url: https://github.com/nodejs/node/pull/10236
21562164
description: The `value` can now be a `Uint8Array`.
@@ -2163,8 +2171,10 @@ changes:
21632171
-->
21642172

21652173
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2166-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2174+
*`start` {integer} Where to begin searching in `buf`. If negative, then
21672175
offset is calculated from the end of `buf`. **Default:**`0`.
2176+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2177+
`buf.length`.
21682178
*`encoding` {string} If `value` is a string, this is the encoding used to
21692179
determine the binary representation of the string that will be searched for in
21702180
`buf`. **Default:**`'utf8'`.
@@ -2324,20 +2334,25 @@ for (const key of buf.keys()) {
23242334
// 5
23252335
```
23262336

2327-
### `buf.lastIndexOf(value[, byteOffset][, encoding])`
2337+
### `buf.lastIndexOf(value[, start[, end]][, encoding])`
23282338

23292339
<!-- YAML
23302340
added: v6.0.0
23312341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs/node/pull/62390
2344+
description: Added the `end` parameter.
23322345
- version: v8.0.0
23332346
pr-url: https://github.com/nodejs/node/pull/10236
23342347
description: The `value` can now be a `Uint8Array`.
23352348
-->
23362349

23372350
*`value` {string|Buffer|Uint8Array|integer} What to search for.
2338-
*`byteOffset` {integer} Where to begin searching in `buf`. If negative, then
2351+
*`start` {integer} Where to begin searching in `buf`. If negative, then
23392352
offset is calculated from the end of `buf`. **Default:**
23402353
`buf.length - 1`.
2354+
*`end` {integer} Where to stop searching in `buf` (exclusive). **Default:**
2355+
`buf.length`.
23412356
*`encoding` {string} If `value` is a string, this is the encoding used to
23422357
determine the binary representation of the string that will be searched for in
23432358
`buf`. **Default:**`'utf8'`.

β€Žlib/buffer.jsβ€Ž

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,83 +714,91 @@ const encodingOps = {
714714
byteLength: byteLengthUtf8,
715715
write: utf8Write,
716716
slice: utf8Slice,
717-
indexOf: (buf,val,byteOffset,dir)=>
718-
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir),
717+
indexOf: (buf,val,byteOffset,dir,end)=>
718+
indexOfString(buf,val,byteOffset,encodingsMap.utf8,dir,end),
719719
},
720720
ucs2: {
721721
encoding: 'ucs2',
722722
encodingVal: encodingsMap.utf16le,
723723
byteLength: (string)=>string.length*2,
724724
write: ucs2Write,
725725
slice: ucs2Slice,
726-
indexOf: (buf,val,byteOffset,dir)=>
727-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
726+
indexOf: (buf,val,byteOffset,dir,end)=>
727+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
728728
},
729729
utf16le: {
730730
encoding: 'utf16le',
731731
encodingVal: encodingsMap.utf16le,
732732
byteLength: (string)=>string.length*2,
733733
write: ucs2Write,
734734
slice: ucs2Slice,
735-
indexOf: (buf,val,byteOffset,dir)=>
736-
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir),
735+
indexOf: (buf,val,byteOffset,dir,end)=>
736+
indexOfString(buf,val,byteOffset,encodingsMap.utf16le,dir,end),
737737
},
738738
latin1: {
739739
encoding: 'latin1',
740740
encodingVal: encodingsMap.latin1,
741741
byteLength: (string)=>string.length,
742742
write: latin1Write,
743743
slice: latin1Slice,
744-
indexOf: (buf,val,byteOffset,dir)=>
745-
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir),
744+
indexOf: (buf,val,byteOffset,dir,end)=>
745+
indexOfString(buf,val,byteOffset,encodingsMap.latin1,dir,end),
746746
},
747747
ascii: {
748748
encoding: 'ascii',
749749
encodingVal: encodingsMap.ascii,
750750
byteLength: (string)=>string.length,
751751
write: asciiWrite,
752752
slice: asciiSlice,
753-
indexOf: (buf,val,byteOffset,dir)=>
754-
indexOfString(buf,val,byteOffset,encodingsMap.ascii,dir),
753+
indexOf: (buf,val,byteOffset,dir,end)=>
754+
indexOfBuffer(buf,
755+
fromStringFast(val,encodingOps.ascii),
756+
byteOffset,
757+
encodingsMap.ascii,
758+
dir,
759+
end),
755760
},
756761
base64: {
757762
encoding: 'base64',
758763
encodingVal: encodingsMap.base64,
759764
byteLength: (string)=>base64ByteLength(string,string.length),
760765
write: base64Write,
761766
slice: base64Slice,
762-
indexOf: (buf,val,byteOffset,dir)=>
767+
indexOf: (buf,val,byteOffset,dir,end)=>
763768
indexOfBuffer(buf,
764769
fromStringFast(val,encodingOps.base64),
765770
byteOffset,
766771
encodingsMap.base64,
767-
dir),
772+
dir,
773+
end),
768774
},
769775
base64url: {
770776
encoding: 'base64url',
771777
encodingVal: encodingsMap.base64url,
772778
byteLength: (string)=>base64ByteLength(string,string.length),
773779
write: base64urlWrite,
774780
slice: base64urlSlice,
775-
indexOf: (buf,val,byteOffset,dir)=>
781+
indexOf: (buf,val,byteOffset,dir,end)=>
776782
indexOfBuffer(buf,
777783
fromStringFast(val,encodingOps.base64url),
778784
byteOffset,
779785
encodingsMap.base64url,
780-
dir),
786+
dir,
787+
end),
781788
},
782789
hex: {
783790
encoding: 'hex',
784791
encodingVal: encodingsMap.hex,
785792
byteLength: (string)=>string.length>>>1,
786793
write: hexWrite,
787794
slice: hexSlice,
788-
indexOf: (buf,val,byteOffset,dir)=>
795+
indexOf: (buf,val,byteOffset,dir,end)=>
789796
indexOfBuffer(buf,
790797
fromStringFast(val,encodingOps.hex),
791798
byteOffset,
792799
encodingsMap.hex,
793-
dir),
800+
dir,
801+
end),
794802
},
795803
};
796804
functiongetEncodingOps(encoding){
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052
// - buffer - a Buffer to search
10451053
// - val - a string, Buffer, or number
10461054
// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+
// - end - absolute exclusive end of the search range
10471056
// - encoding - an optional encoding, relevant if val is a string
10481057
// - dir - true for indexOf, false for lastIndexOf
1049-
functionbidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){
1058+
functionbidirectionalIndexOf(buffer,val,byteOffset,end,encoding,dir){
10501059
validateBuffer(buffer);
10511060

10521061
if(typeofbyteOffset==='string'){
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075
dir=!!dir;// Cast to bool.
10671076

10681077
if(typeofval==='number')
1069-
returnindexOfNumber(buffer,val>>>0,byteOffset,dir);
1078+
returnindexOfNumber(buffer,val>>>0,byteOffset,dir,end);
10701079

10711080
letops;
10721081
if(encoding===undefined)
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086
if(typeofval==='string'){
10781087
if(ops===undefined)
10791088
thrownewERR_UNKNOWN_ENCODING(encoding);
1080-
returnops.indexOf(buffer,val,byteOffset,dir);
1089+
returnops.indexOf(buffer,val,byteOffset,dir,end);
10811090
}
10821091

10831092
if(isUint8Array(val)){
10841093
constencodingVal=
10851094
(ops===undefined ? encodingsMap.utf8 : ops.encodingVal);
1086-
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir);
1095+
returnindexOfBuffer(buffer,val,byteOffset,encodingVal,dir,end);
10871096
}
10881097

10891098
thrownewERR_INVALID_ARG_TYPE(
10901099
'value',['number','string','Buffer','Uint8Array'],val,
10911100
);
10921101
}
10931102

1094-
Buffer.prototype.indexOf=functionindexOf(val,byteOffset,encoding){
1095-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true);
1103+
Buffer.prototype.indexOf=functionindexOf(val,offset,end,encoding){
1104+
if(typeofend==='string'){
1105+
encoding=end;
1106+
end=this.length;
1107+
}elseif(end===undefined){
1108+
end=this.length;
1109+
}
1110+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true);
10961111
};
10971112

1098-
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,byteOffset,encoding){
1099-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,false);
1113+
Buffer.prototype.lastIndexOf=functionlastIndexOf(val,offset,end,encoding){
1114+
if(typeofend==='string'){
1115+
encoding=end;
1116+
end=this.length;
1117+
}elseif(end===undefined){
1118+
end=this.length;
1119+
}
1120+
returnbidirectionalIndexOf(this,val,offset,end,encoding,false);
11001121
};
11011122

1102-
Buffer.prototype.includes=functionincludes(val,byteOffset,encoding){
1103-
returnbidirectionalIndexOf(this,val,byteOffset,encoding,true)!==-1;
1123+
Buffer.prototype.includes=functionincludes(val,offset,end,encoding){
1124+
if(typeofend==='string'){
1125+
encoding=end;
1126+
end=this.length;
1127+
}elseif(end===undefined){
1128+
end=this.length;
1129+
}
1130+
returnbidirectionalIndexOf(this,val,offset,end,encoding,true)!==-1;
11041131
};
11051132

11061133
// Usage:

0 commit comments

Comments
Β (0)