GeezerVison is showing github.com — buffer: add end parameter · nodejs/node@34a6454
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 |
|---|
@@ -2069,11 +2069,14 @@ console.log(buf.fill('zz', 'hex')); | 2069 | 2069 | // Throws an exception. | 2070 | 2070 | ``` | 2071 | 2071 |
| 2072 | | -### `buf.includes(value[, byteOffset][, encoding])` | | 2072 | +### `buf.includes(value[, start[, end]][, encoding])` | 2073 | 2073 |
| 2074 | 2074 | <!-- YAML | 2075 | 2075 | added: v5.3.0 | 2076 | 2076 | changes: | | 2077 | + - version: REPLACEME | | 2078 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2079 | + description: Added the `end` parameter. | 2077 | 2080 | - version: | 2078 | 2081 | - v25.5.0 | 2079 | 2082 | - v24.13.1 | @@ -2082,8 +2085,10 @@ changes: | 2082 | 2085 | --> | 2083 | 2086 |
| 2084 | 2087 | * `value` {string|Buffer|Uint8Array|integer} What to search for. | 2085 | | -* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then | | 2088 | +* `start` {integer} Where to begin searching in `buf`. If negative, then | 2086 | 2089 | offset is calculated from the end of `buf`. **Default:** `0`. | | 2090 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2091 | + `buf.length`. | 2087 | 2092 | * `encoding` {string} If `value` is a string, this is its encoding. | 2088 | 2093 | **Default:** `'utf8'`. | 2089 | 2094 | * Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise. | @@ -2132,11 +2137,14 @@ console.log(buf.includes('this', 4)); | 2132 | 2137 | // Prints: false | 2133 | 2138 | ``` | 2134 | 2139 |
| 2135 | | -### `buf.indexOf(value[, byteOffset][, encoding])` | | 2140 | +### `buf.indexOf(value[, start[, end]][, encoding])` | 2136 | 2141 |
| 2137 | 2142 | <!-- YAML | 2138 | 2143 | added: v1.5.0 | 2139 | 2144 | changes: | | 2145 | + - version: REPLACEME | | 2146 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2147 | + description: Added the `end` parameter. | 2140 | 2148 | - version: v8.0.0 | 2141 | 2149 | pr-url: https://github.com/nodejs/node/pull/10236 | 2142 | 2150 | description: The `value` can now be a `Uint8Array`. | @@ -2149,8 +2157,10 @@ changes: | 2149 | 2157 | --> | 2150 | 2158 |
| 2151 | 2159 | * `value` {string|Buffer|Uint8Array|integer} What to search for. | 2152 | | -* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then | | 2160 | +* `start` {integer} Where to begin searching in `buf`. If negative, then | 2153 | 2161 | offset is calculated from the end of `buf`. **Default:** `0`. | | 2162 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2163 | + `buf.length`. | 2154 | 2164 | * `encoding` {string} If `value` is a string, this is the encoding used to | 2155 | 2165 | determine the binary representation of the string that will be searched for in | 2156 | 2166 | `buf`. **Default:** `'utf8'`. | @@ -2310,20 +2320,25 @@ for (const key of buf.keys()) { | 2310 | 2320 | // 5 | 2311 | 2321 | ``` | 2312 | 2322 |
| 2313 | | -### `buf.lastIndexOf(value[, byteOffset][, encoding])` | | 2323 | +### `buf.lastIndexOf(value[, start[, end]][, encoding])` | 2314 | 2324 |
| 2315 | 2325 | <!-- YAML | 2316 | 2326 | added: v6.0.0 | 2317 | 2327 | changes: | | 2328 | + - version: REPLACEME | | 2329 | + pr-url: https://github.com/nodejs/node/pull/62390 | | 2330 | + description: Added the `end` parameter. | 2318 | 2331 | - version: v8.0.0 | 2319 | 2332 | pr-url: https://github.com/nodejs/node/pull/10236 | 2320 | 2333 | description: The `value` can now be a `Uint8Array`. | 2321 | 2334 | --> | 2322 | 2335 |
| 2323 | 2336 | * `value` {string|Buffer|Uint8Array|integer} What to search for. | 2324 | | -* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then | | 2337 | +* `start` {integer} Where to begin searching in `buf`. If negative, then | 2325 | 2338 | offset is calculated from the end of `buf`. **Default:** | 2326 | 2339 | `buf.length - 1`. | | 2340 | +* `end` {integer} Where to stop searching in `buf` (exclusive). **Default:** | | 2341 | + `buf.length`. | 2327 | 2342 | * `encoding` {string} If `value` is a string, this is the encoding used to | 2328 | 2343 | determine the binary representation of the string that will be searched for in | 2329 | 2344 | `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 |
|---|
@@ -700,83 +700,91 @@ const encodingOps = { | 700 | 700 | byteLength: byteLengthUtf8, | 701 | 701 | write: utf8Write, | 702 | 702 | slice: utf8Slice, | 703 | | - indexOf: (buf, val, byteOffset, dir) => | 704 | | - indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir), | | 703 | + indexOf: (buf, val, byteOffset, dir, end) => | | 704 | + indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir, end), | 705 | 705 | }, | 706 | 706 | ucs2: { | 707 | 707 | encoding: 'ucs2', | 708 | 708 | encodingVal: encodingsMap.utf16le, | 709 | 709 | byteLength: (string) => string.length * 2, | 710 | 710 | write: ucs2Write, | 711 | 711 | slice: ucs2Slice, | 712 | | - indexOf: (buf, val, byteOffset, dir) => | 713 | | - indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | | 712 | + indexOf: (buf, val, byteOffset, dir, end) => | | 713 | + indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir, end), | 714 | 714 | }, | 715 | 715 | utf16le: { | 716 | 716 | encoding: 'utf16le', | 717 | 717 | encodingVal: encodingsMap.utf16le, | 718 | 718 | byteLength: (string) => string.length * 2, | 719 | 719 | write: ucs2Write, | 720 | 720 | slice: ucs2Slice, | 721 | | - indexOf: (buf, val, byteOffset, dir) => | 722 | | - indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | | 721 | + indexOf: (buf, val, byteOffset, dir, end) => | | 722 | + indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir, end), | 723 | 723 | }, | 724 | 724 | latin1: { | 725 | 725 | encoding: 'latin1', | 726 | 726 | encodingVal: encodingsMap.latin1, | 727 | 727 | byteLength: (string) => string.length, | 728 | 728 | write: latin1Write, | 729 | 729 | slice: latin1Slice, | 730 | | - indexOf: (buf, val, byteOffset, dir) => | 731 | | - indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir), | | 730 | + indexOf: (buf, val, byteOffset, dir, end) => | | 731 | + indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir, end), | 732 | 732 | }, | 733 | 733 | ascii: { | 734 | 734 | encoding: 'ascii', | 735 | 735 | encodingVal: encodingsMap.ascii, | 736 | 736 | byteLength: (string) => string.length, | 737 | 737 | write: asciiWrite, | 738 | 738 | slice: asciiSlice, | 739 | | - indexOf: (buf, val, byteOffset, dir) => | 740 | | - indexOfString(buf, val, byteOffset, encodingsMap.ascii, dir), | | 739 | + indexOf: (buf, val, byteOffset, dir, end) => | | 740 | + indexOfBuffer(buf, | | 741 | + fromStringFast(val, encodingOps.ascii), | | 742 | + byteOffset, | | 743 | + encodingsMap.ascii, | | 744 | + dir, | | 745 | + end), | 741 | 746 | }, | 742 | 747 | base64: { | 743 | 748 | encoding: 'base64', | 744 | 749 | encodingVal: encodingsMap.base64, | 745 | 750 | byteLength: (string) => base64ByteLength(string, string.length), | 746 | 751 | write: base64Write, | 747 | 752 | slice: base64Slice, | 748 | | - indexOf: (buf, val, byteOffset, dir) => | | 753 | + indexOf: (buf, val, byteOffset, dir, end) => | 749 | 754 | indexOfBuffer(buf, | 750 | 755 | fromStringFast(val, encodingOps.base64), | 751 | 756 | byteOffset, | 752 | 757 | encodingsMap.base64, | 753 | | - dir), | | 758 | + dir, | | 759 | + end), | 754 | 760 | }, | 755 | 761 | base64url: { | 756 | 762 | encoding: 'base64url', | 757 | 763 | encodingVal: encodingsMap.base64url, | 758 | 764 | byteLength: (string) => base64ByteLength(string, string.length), | 759 | 765 | write: base64urlWrite, | 760 | 766 | slice: base64urlSlice, | 761 | | - indexOf: (buf, val, byteOffset, dir) => | | 767 | + indexOf: (buf, val, byteOffset, dir, end) => | 762 | 768 | indexOfBuffer(buf, | 763 | 769 | fromStringFast(val, encodingOps.base64url), | 764 | 770 | byteOffset, | 765 | 771 | encodingsMap.base64url, | 766 | | - dir), | | 772 | + dir, | | 773 | + end), | 767 | 774 | }, | 768 | 775 | hex: { | 769 | 776 | encoding: 'hex', | 770 | 777 | encodingVal: encodingsMap.hex, | 771 | 778 | byteLength: (string) => string.length >>> 1, | 772 | 779 | write: hexWrite, | 773 | 780 | slice: hexSlice, | 774 | | - indexOf: (buf, val, byteOffset, dir) => | | 781 | + indexOf: (buf, val, byteOffset, dir, end) => | 775 | 782 | indexOfBuffer(buf, | 776 | 783 | fromStringFast(val, encodingOps.hex), | 777 | 784 | byteOffset, | 778 | 785 | encodingsMap.hex, | 779 | | - dir), | | 786 | + dir, | | 787 | + end), | 780 | 788 | }, | 781 | 789 | }; | 782 | 790 | function getEncodingOps(encoding) { | @@ -1030,9 +1038,10 @@ Buffer.prototype.compare = function compare(target, | 1030 | 1038 | // - buffer - a Buffer to search | 1031 | 1039 | // - val - a string, Buffer, or number | 1032 | 1040 | // - byteOffset - an index into `buffer`; will be clamped to an int32 | | 1041 | +// - end - absolute exclusive end of the search range | 1033 | 1042 | // - encoding - an optional encoding, relevant if val is a string | 1034 | 1043 | // - dir - true for indexOf, false for lastIndexOf | 1035 | | -function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | | 1044 | +function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) { | 1036 | 1045 | validateBuffer(buffer); | 1037 | 1046 |
| 1038 | 1047 | if (typeof byteOffset === 'string') { | @@ -1052,7 +1061,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | 1052 | 1061 | dir = !!dir; // Cast to bool. | 1053 | 1062 |
| 1054 | 1063 | if (typeof val === 'number') | 1055 | | - return indexOfNumber(buffer, val >>> 0, byteOffset, dir); | | 1064 | + return indexOfNumber(buffer, val >>> 0, byteOffset, dir, end); | 1056 | 1065 |
| 1057 | 1066 | let ops; | 1058 | 1067 | if (encoding === undefined) | @@ -1063,30 +1072,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | 1063 | 1072 | if (typeof val === 'string') { | 1064 | 1073 | if (ops === undefined) | 1065 | 1074 | throw new ERR_UNKNOWN_ENCODING(encoding); | 1066 | | - return ops.indexOf(buffer, val, byteOffset, dir); | | 1075 | + return ops.indexOf(buffer, val, byteOffset, dir, end); | 1067 | 1076 | } | 1068 | 1077 |
| 1069 | 1078 | if (isUint8Array(val)) { | 1070 | 1079 | const encodingVal = | 1071 | 1080 | (ops === undefined ? encodingsMap.utf8 : ops.encodingVal); | 1072 | | - return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir); | | 1081 | + return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir, end); | 1073 | 1082 | } | 1074 | 1083 |
| 1075 | 1084 | throw new ERR_INVALID_ARG_TYPE( | 1076 | 1085 | 'value', ['number', 'string', 'Buffer', 'Uint8Array'], val, | 1077 | 1086 | ); | 1078 | 1087 | } | 1079 | 1088 |
| 1080 | | -Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) { | 1081 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, true); | | 1089 | +Buffer.prototype.indexOf = function indexOf(val, offset, end, encoding) { | | 1090 | + if (typeof end === 'string') { | | 1091 | + encoding = end; | | 1092 | + end = this.length; | | 1093 | + } else if (end === undefined) { | | 1094 | + end = this.length; | | 1095 | + } | | 1096 | + return bidirectionalIndexOf(this, val, offset, end, encoding, true); | 1082 | 1097 | }; | 1083 | 1098 |
| 1084 | | -Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { | 1085 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, false); | | 1099 | +Buffer.prototype.lastIndexOf = function lastIndexOf(val, offset, end, encoding) { | | 1100 | + if (typeof end === 'string') { | | 1101 | + encoding = end; | | 1102 | + end = this.length; | | 1103 | + } else if (end === undefined) { | | 1104 | + end = this.length; | | 1105 | + } | | 1106 | + return bidirectionalIndexOf(this, val, offset, end, encoding, false); | 1086 | 1107 | }; | 1087 | 1108 |
| 1088 | | -Buffer.prototype.includes = function includes(val, byteOffset, encoding) { | 1089 | | - return bidirectionalIndexOf(this, val, byteOffset, encoding, true) !== -1; | | 1109 | +Buffer.prototype.includes = function includes(val, offset, end, encoding) { | | 1110 | + if (typeof end === 'string') { | | 1111 | + encoding = end; | | 1112 | + end = this.length; | | 1113 | + } else if (end === undefined) { | | 1114 | + end = this.length; | | 1115 | + } | | 1116 | + return bidirectionalIndexOf(this, val, offset, end, encoding, true) !== -1; | 1090 | 1117 | }; | 1091 | 1118 |
| 1092 | 1119 | // Usage: | |
|