MCPcopy Index your code
hub / github.com/feross/buffer / arrayIndexOf

Function arrayIndexOf

index.js:758–812  ·  view source on GitHub ↗
(arr, val, byteOffset, encoding, dir)

Source from the content-addressed store, hash-verified

756}
757
758function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
759 let indexSize = 1
760 let arrLength = arr.length
761 let valLength = val.length
762
763 if (encoding !== undefined) {
764 encoding = String(encoding).toLowerCase()
765 if (encoding === 'ucs2' || encoding === 'ucs-2' ||
766 encoding === 'utf16le' || encoding === 'utf-16le') {
767 if (arr.length < 2 || val.length < 2) {
768 return -1
769 }
770 indexSize = 2
771 arrLength /= 2
772 valLength /= 2
773 byteOffset /= 2
774 }
775 }
776
777 function read (buf, i) {
778 if (indexSize === 1) {
779 return buf[i]
780 } else {
781 return buf.readUInt16BE(i * indexSize)
782 }
783 }
784
785 let i
786 if (dir) {
787 let foundIndex = -1
788 for (i = byteOffset; i < arrLength; i++) {
789 if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
790 if (foundIndex === -1) foundIndex = i
791 if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
792 } else {
793 if (foundIndex !== -1) i -= i - foundIndex
794 foundIndex = -1
795 }
796 }
797 } else {
798 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
799 for (i = byteOffset; i >= 0; i--) {
800 let found = true
801 for (let j = 0; j < valLength; j++) {
802 if (read(arr, i + j) !== read(val, j)) {
803 found = false
804 break
805 }
806 }
807 if (found) return i
808 }
809 }
810
811 return -1
812}
813
814Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
815 return this.indexOf(val, byteOffset, encoding) !== -1

Callers 1

bidirectionalIndexOfFunction · 0.85

Calls 1

readFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…