MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / arrayIndexOf

Function arrayIndexOf

lib/models/dav.js:2573–2627  ·  view source on GitHub ↗
(arr, val, byteOffset, encoding, dir)

Source from the content-addressed store, hash-verified

2571}
2572
2573function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
2574 var indexSize = 1;
2575 var arrLength = arr.length;
2576 var valLength = val.length;
2577
2578 if (encoding !== undefined) {
2579 encoding = String(encoding).toLowerCase();
2580 if (encoding === 'ucs2' || encoding === 'ucs-2' ||
2581 encoding === 'utf16le' || encoding === 'utf-16le') {
2582 if (arr.length < 2 || val.length < 2) {
2583 return -1
2584 }
2585 indexSize = 2;
2586 arrLength /= 2;
2587 valLength /= 2;
2588 byteOffset /= 2;
2589 }
2590 }
2591
2592 function read (buf, i) {
2593 if (indexSize === 1) {
2594 return buf[i]
2595 } else {
2596 return buf.readUInt16BE(i * indexSize)
2597 }
2598 }
2599
2600 var i;
2601 if (dir) {
2602 var foundIndex = -1;
2603 for (i = byteOffset; i < arrLength; i++) {
2604 if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
2605 if (foundIndex === -1) foundIndex = i;
2606 if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
2607 } else {
2608 if (foundIndex !== -1) i -= i - foundIndex;
2609 foundIndex = -1;
2610 }
2611 }
2612 } else {
2613 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
2614 for (i = byteOffset; i >= 0; i--) {
2615 var found = true;
2616 for (var j = 0; j < valLength; j++) {
2617 if (read(arr, i + j) !== read(val, j)) {
2618 found = false;
2619 break
2620 }
2621 }
2622 if (found) return i
2623 }
2624 }
2625
2626 return -1
2627}
2628
2629Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
2630 return this.indexOf(val, byteOffset, encoding) !== -1

Callers 1

bidirectionalIndexOfFunction · 0.85

Calls 1

readFunction · 0.85

Tested by

no test coverage detected