GeezerVison is showing github.com — buffer: add end parameter · nodejs/node@e2eb88b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/buffer.md +21-6Lines changed: 21 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2085,19 +2085,24 @@ console.log(buf.fill('zz', 'hex')); | 2085 | 2085 | // Throws an exception. | 2086 | 2086 | ``` | 2087 | 2087 |
| 2088 | | -### `buf.includes(value[, byteOffset][, encoding])` | | 2088 | +### `buf.includes(value[, start[, end]][, encoding])` | 2089 | 2089 |
| 2090 | 2090 | <!-- YAML | 2091 | 2091 | added: v5.3.0 | 2092 | 2092 | changes: | | 2093 | + - version: REPLACEME | | 2094 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2095 | + description: Added the `end` parameter. | 2093 | 2096 | - version: v24.13.1 | 2094 | 2097 | pr-url: https://github.com/nodejs/node/pull/56578 | 2095 | 2098 | description: supports Uint8Array as `this` value. | 2096 | 2099 | --> | 2097 | 2100 |
| 2098 | 2101 | * `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 | 2100 | 2103 | offset is calculated from the end of `buf`. **Default:** `0`. | | 2104 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2105 | + `buf.length`. | 2101 | 2106 | * `encoding` {string} If `value` is a string, this is its encoding. | 2102 | 2107 | **Default:** `'utf8'`. | 2103 | 2108 | * Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise. | @@ -2146,11 +2151,14 @@ console.log(buf.includes('this', 4)); | 2146 | 2151 | // Prints: false | 2147 | 2152 | ``` | 2148 | 2153 |
| 2149 | | -### `buf.indexOf(value[, byteOffset][, encoding])` | | 2154 | +### `buf.indexOf(value[, start[, end]][, encoding])` | 2150 | 2155 |
| 2151 | 2156 | <!-- YAML | 2152 | 2157 | added: v1.5.0 | 2153 | 2158 | changes: | | 2159 | + - version: REPLACEME | | 2160 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2161 | + description: Added the `end` parameter. | 2154 | 2162 | - version: v8.0.0 | 2155 | 2163 | pr-url: https://github.com/nodejs/node/pull/10236 | 2156 | 2164 | description: The `value` can now be a `Uint8Array`. | @@ -2163,8 +2171,10 @@ changes: | 2163 | 2171 | --> | 2164 | 2172 |
| 2165 | 2173 | * `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 | 2167 | 2175 | offset is calculated from the end of `buf`. **Default:** `0`. | | 2176 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2177 | + `buf.length`. | 2168 | 2178 | * `encoding` {string} If `value` is a string, this is the encoding used to | 2169 | 2179 | determine the binary representation of the string that will be searched for in | 2170 | 2180 | `buf`. **Default:** `'utf8'`. | @@ -2324,20 +2334,25 @@ for (const key of buf.keys()) { | 2324 | 2334 | // 5 | 2325 | 2335 | ``` | 2326 | 2336 |
| 2327 | | -### `buf.lastIndexOf(value[, byteOffset][, encoding])` | | 2337 | +### `buf.lastIndexOf(value[, start[, end]][, encoding])` | 2328 | 2338 |
| 2329 | 2339 | <!-- YAML | 2330 | 2340 | added: v6.0.0 | 2331 | 2341 | changes: | | 2342 | + - version: REPLACEME | | 2343 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2344 | + description: Added the `end` parameter. | 2332 | 2345 | - version: v8.0.0 | 2333 | 2346 | pr-url: https://github.com/nodejs/node/pull/10236 | 2334 | 2347 | description: The `value` can now be a `Uint8Array`. | 2335 | 2348 | --> | 2336 | 2349 |
| 2337 | 2350 | * `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 | 2339 | 2352 | offset is calculated from the end of `buf`. **Default:** | 2340 | 2353 | `buf.length - 1`. | | 2354 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2355 | + `buf.length`. | 2341 | 2356 | * `encoding` {string} If `value` is a string, this is the encoding used to | 2342 | 2357 | determine the binary representation of the string that will be searched for in | 2343 | 2358 | `buf`. **Default:** `'utf8'`. | |
Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +53-26Lines changed: 53 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -714,83 +714,91 @@ const encodingOps = { | 714 | 714 | byteLength: byteLengthUtf8, | 715 | 715 | write: utf8Write, | 716 | 716 | 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), | 719 | 719 | }, | 720 | 720 | ucs2: { | 721 | 721 | encoding: 'ucs2', | 722 | 722 | encodingVal: encodingsMap.utf16le, | 723 | 723 | byteLength: (string) => string.length * 2, | 724 | 724 | write: ucs2Write, | 725 | 725 | 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), | 728 | 728 | }, | 729 | 729 | utf16le: { | 730 | 730 | encoding: 'utf16le', | 731 | 731 | encodingVal: encodingsMap.utf16le, | 732 | 732 | byteLength: (string) => string.length * 2, | 733 | 733 | write: ucs2Write, | 734 | 734 | 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), | 737 | 737 | }, | 738 | 738 | latin1: { | 739 | 739 | encoding: 'latin1', | 740 | 740 | encodingVal: encodingsMap.latin1, | 741 | 741 | byteLength: (string) => string.length, | 742 | 742 | write: latin1Write, | 743 | 743 | 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), | 746 | 746 | }, | 747 | 747 | ascii: { | 748 | 748 | encoding: 'ascii', | 749 | 749 | encodingVal: encodingsMap.ascii, | 750 | 750 | byteLength: (string) => string.length, | 751 | 751 | write: asciiWrite, | 752 | 752 | 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), | 755 | 760 | }, | 756 | 761 | base64: { | 757 | 762 | encoding: 'base64', | 758 | 763 | encodingVal: encodingsMap.base64, | 759 | 764 | byteLength: (string) => base64ByteLength(string, string.length), | 760 | 765 | write: base64Write, | 761 | 766 | slice: base64Slice, | 762 | | - indexOf: (buf, val, byteOffset, dir) => | | 767 | + indexOf: (buf, val, byteOffset, dir, end) => | 763 | 768 | indexOfBuffer(buf, | 764 | 769 | fromStringFast(val, encodingOps.base64), | 765 | 770 | byteOffset, | 766 | 771 | encodingsMap.base64, | 767 | | - dir), | | 772 | + dir, | | 773 | + end), | 768 | 774 | }, | 769 | 775 | base64url: { | 770 | 776 | encoding: 'base64url', | 771 | 777 | encodingVal: encodingsMap.base64url, | 772 | 778 | byteLength: (string) => base64ByteLength(string, string.length), | 773 | 779 | write: base64urlWrite, | 774 | 780 | slice: base64urlSlice, | 775 | | - indexOf: (buf, val, byteOffset, dir) => | | 781 | + indexOf: (buf, val, byteOffset, dir, end) => | 776 | 782 | indexOfBuffer(buf, | 777 | 783 | fromStringFast(val, encodingOps.base64url), | 778 | 784 | byteOffset, | 779 | 785 | encodingsMap.base64url, | 780 | | - dir), | | 786 | + dir, | | 787 | + end), | 781 | 788 | }, | 782 | 789 | hex: { | 783 | 790 | encoding: 'hex', | 784 | 791 | encodingVal: encodingsMap.hex, | 785 | 792 | byteLength: (string) => string.length >>> 1, | 786 | 793 | write: hexWrite, | 787 | 794 | slice: hexSlice, | 788 | | - indexOf: (buf, val, byteOffset, dir) => | | 795 | + indexOf: (buf, val, byteOffset, dir, end) => | 789 | 796 | indexOfBuffer(buf, | 790 | 797 | fromStringFast(val, encodingOps.hex), | 791 | 798 | byteOffset, | 792 | 799 | encodingsMap.hex, | 793 | | - dir), | | 800 | + dir, | | 801 | + end), | 794 | 802 | }, | 795 | 803 | }; | 796 | 804 | function getEncodingOps(encoding) { | @@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target, | 1044 | 1052 | // - buffer - a Buffer to search | 1045 | 1053 | // - val - a string, Buffer, or number | 1046 | 1054 | // - byteOffset - an index into `buffer`; will be clamped to an int32 | | 1055 | +// - end - absolute exclusive end of the search range | 1047 | 1056 | // - encoding - an optional encoding, relevant if val is a string | 1048 | 1057 | // - dir - true for indexOf, false for lastIndexOf | 1049 | | -function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | | 1058 | +function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) { | 1050 | 1059 | validateBuffer(buffer); | 1051 | 1060 |
| 1052 | 1061 | if (typeof byteOffset === 'string') { | @@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | 1066 | 1075 | dir = !!dir; // Cast to bool. | 1067 | 1076 |
| 1068 | 1077 | if (typeof val === 'number') | 1069 | | - return indexOfNumber(buffer, val >>> 0, byteOffset, dir); | | 1078 | + return indexOfNumber(buffer, val >>> 0, byteOffset, dir, end); | 1070 | 1079 |
| 1071 | 1080 | let ops; | 1072 | 1081 | if (encoding === undefined) | @@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | 1077 | 1086 | if (typeof val === 'string') { | 1078 | 1087 | if (ops === undefined) | 1079 | 1088 | throw new ERR_UNKNOWN_ENCODING(encoding); | 1080 | | - return ops.indexOf(buffer, val, byteOffset, dir); | | 1089 | + return ops.indexOf(buffer, val, byteOffset, dir, end); | 1081 | 1090 | } | 1082 | 1091 |
| 1083 | 1092 | if (isUint8Array(val)) { | 1084 | 1093 | const encodingVal = | 1085 | 1094 | (ops === undefined ? encodingsMap.utf8 : ops.encodingVal); | 1086 | | - return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir); | | 1095 | + return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir, end); | 1087 | 1096 | } | 1088 | 1097 |
| 1089 | 1098 | throw new ERR_INVALID_ARG_TYPE( | 1090 | 1099 | 'value', ['number', 'string', 'Buffer', 'Uint8Array'], val, | 1091 | 1100 | ); | 1092 | 1101 | } | 1093 | 1102 |
| 1094 | | -Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) { | 1095 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, true); | | 1103 | +Buffer.prototype.indexOf = function indexOf(val, offset, end, encoding) { | | 1104 | + if (typeof end === 'string') { | | 1105 | + encoding = end; | | 1106 | + end = this.length; | | 1107 | + } else if (end === undefined) { | | 1108 | + end = this.length; | | 1109 | + } | | 1110 | + return bidirectionalIndexOf(this, val, offset, end, encoding, true); | 1096 | 1111 | }; | 1097 | 1112 |
| 1098 | | -Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { | 1099 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, false); | | 1113 | +Buffer.prototype.lastIndexOf = function lastIndexOf(val, offset, end, encoding) { | | 1114 | + if (typeof end === 'string') { | | 1115 | + encoding = end; | | 1116 | + end = this.length; | | 1117 | + } else if (end === undefined) { | | 1118 | + end = this.length; | | 1119 | + } | | 1120 | + return bidirectionalIndexOf(this, val, offset, end, encoding, false); | 1100 | 1121 | }; | 1101 | 1122 |
| 1102 | | -Buffer.prototype.includes = function includes(val, byteOffset, encoding) { | 1103 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, true) !== -1; | | 1123 | +Buffer.prototype.includes = function includes(val, offset, end, encoding) { | | 1124 | + if (typeof end === 'string') { | | 1125 | + encoding = end; | | 1126 | + end = this.length; | | 1127 | + } else if (end === undefined) { | | 1128 | + end = this.length; | | 1129 | + } | | 1130 | + return bidirectionalIndexOf(this, val, offset, end, encoding, true) !== -1; | 1104 | 1131 | }; | 1105 | 1132 |
| 1106 | 1133 | // Usage: | |
|