(arr, val, byteOffset, encoding, dir)
| 669 | throw new TypeError('val must be string, number or Buffer'); |
| 670 | } |
| 671 | function arrayIndexOf(arr, val, byteOffset, encoding, dir) { |
| 672 | var indexSize = 1; |
| 673 | var arrLength = arr.length; |
| 674 | var valLength = val.length; |
| 675 | if (encoding !== undefined) { |
| 676 | encoding = String(encoding).toLowerCase(); |
| 677 | if (encoding === 'ucs2' || encoding === 'ucs-2' || encoding === 'utf16le' || encoding === 'utf-16le') { |
| 678 | if (arr.length < 2 || val.length < 2) { |
| 679 | return -1; |
| 680 | } |
| 681 | indexSize = 2; |
| 682 | arrLength /= 2; |
| 683 | valLength /= 2; |
| 684 | byteOffset /= 2; |
| 685 | } |
| 686 | } |
| 687 | function read(buf, i) { |
| 688 | if (indexSize === 1) { |
| 689 | return buf[i]; |
| 690 | } else { |
| 691 | return buf.readUInt16BE(i * indexSize); |
| 692 | } |
| 693 | } |
| 694 | var i; |
| 695 | if (dir) { |
| 696 | var foundIndex = -1; |
| 697 | for (i = byteOffset; i < arrLength; i++) { |
| 698 | if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { |
| 699 | if (foundIndex === -1) foundIndex = i; |
| 700 | if (i - foundIndex + 1 === valLength) return foundIndex * indexSize; |
| 701 | } else { |
| 702 | if (foundIndex !== -1) i -= i - foundIndex; |
| 703 | foundIndex = -1; |
| 704 | } |
| 705 | } |
| 706 | } else { |
| 707 | if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength; |
| 708 | for (i = byteOffset; i >= 0; i--) { |
| 709 | var found = true; |
| 710 | for (var j = 0; j < valLength; j++) { |
| 711 | if (read(arr, i + j) !== read(val, j)) { |
| 712 | found = false; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | if (found) return i; |
| 717 | } |
| 718 | } |
| 719 | return -1; |
| 720 | } |
| 721 | Buffer.prototype.includes = function includes(val, byteOffset, encoding) { |
| 722 | return this.indexOf(val, byteOffset, encoding) !== -1; |
| 723 | }; |
no test coverage detected
searching dependent graphs…