MCPcopy Index your code
hub / github.com/microsoft/SandDance / bidirectionalIndexOf

Function bidirectionalIndexOf

docs/app/js/sanddance-app.js:113293–113334  ·  view source on GitHub ↗
(buffer, val, byteOffset, encoding, dir)

Source from the content-addressed store, hash-verified

113291// - encoding - an optional encoding, relevant is val is a string
113292// - dir - true for indexOf, false for lastIndexOf
113293function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
113294 // Empty buffer means no match
113295 if (buffer.length === 0) return -1;
113296 // Normalize byteOffset
113297 if (typeof byteOffset === "string") {
113298 encoding = byteOffset;
113299 byteOffset = 0;
113300 } else if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff;
113301 else if (byteOffset < -2147483648) byteOffset = -2147483648;
113302 byteOffset = +byteOffset // Coerce to Number.
113303 ;
113304 if (numberIsNaN(byteOffset)) // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
113305 byteOffset = dir ? 0 : buffer.length - 1;
113306 // Normalize byteOffset: negative offsets start from the end of the buffer
113307 if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
113308 if (byteOffset >= buffer.length) {
113309 if (dir) return -1;
113310 else byteOffset = buffer.length - 1;
113311 } else if (byteOffset < 0) {
113312 if (dir) byteOffset = 0;
113313 else return -1;
113314 }
113315 // Normalize val
113316 if (typeof val === "string") val = Buffer.from(val, encoding);
113317 // Finally, search either indexOf (if dir is true) or lastIndexOf
113318 if (Buffer.isBuffer(val)) {
113319 // Special case: looking for empty string/buffer always fails
113320 if (val.length === 0) return -1;
113321 return arrayIndexOf(buffer, val, byteOffset, encoding, dir);
113322 } else if (typeof val === "number") {
113323 val = val & 0xFF // Search for a byte value [0-255]
113324 ;
113325 if (typeof Uint8Array.prototype.indexOf === "function") {
113326 if (dir) return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
113327 else return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
113328 }
113329 return arrayIndexOf(buffer, [
113330 val
113331 ], byteOffset, encoding, dir);
113332 }
113333 throw new TypeError("val must be string, number or Buffer");
113334}
113335function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
113336 var indexSize = 1;
113337 var arrLength = arr.length;

Callers 1

sanddance-app.jsFile · 0.70

Calls 3

numberIsNaNFunction · 0.70
arrayIndexOfFunction · 0.70
fromMethod · 0.45

Tested by

no test coverage detected