MCPcopy Index your code
hub / github.com/nodejs/node / bidirectionalIndexOf

Function bidirectionalIndexOf

lib/buffer.js:1041–1084  ·  view source on GitHub ↗
(buffer, val, byteOffset, end, encoding, dir)

Source from the content-addressed store, hash-verified

1039// - encoding - an optional encoding, relevant if val is a string
1040// - dir - true for indexOf, false for lastIndexOf
1041function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) {
1042 validateBuffer(buffer);
1043
1044 if (typeof byteOffset === 'string') {
1045 encoding = byteOffset;
1046 byteOffset = undefined;
1047 } else if (byteOffset > 0x7fffffff) {
1048 byteOffset = 0x7fffffff;
1049 } else if (byteOffset < -0x80000000) {
1050 byteOffset = -0x80000000;
1051 }
1052 // Coerce to Number. Values like null and [] become 0.
1053 byteOffset = +byteOffset;
1054 // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
1055 if (NumberIsNaN(byteOffset)) {
1056 byteOffset = dir ? 0 : (buffer.length || buffer.byteLength);
1057 }
1058 dir = !!dir; // Cast to bool.
1059
1060 if (typeof val === 'number')
1061 return indexOfNumber(buffer, val >>> 0, byteOffset, dir, end);
1062
1063 let ops;
1064 if (encoding === undefined)
1065 ops = encodingOps.utf8;
1066 else
1067 ops = getEncodingOps(encoding);
1068
1069 if (typeof val === 'string') {
1070 if (ops === undefined)
1071 throw new ERR_UNKNOWN_ENCODING(encoding);
1072 return ops.indexOf(buffer, val, byteOffset, dir, end);
1073 }
1074
1075 if (isUint8Array(val)) {
1076 const encodingVal =
1077 (ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
1078 return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir, end);
1079 }
1080
1081 throw new ERR_INVALID_ARG_TYPE(
1082 'value', ['number', 'string', 'Buffer', 'Uint8Array'], val,
1083 );
1084}
1085
1086Buffer.prototype.indexOf = function indexOf(val, offset, end, encoding) {
1087 if (typeof end === 'string') {

Callers 1

buffer.jsFile · 0.85

Calls 3

getEncodingOpsFunction · 0.85
isUint8ArrayFunction · 0.85
indexOfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…