(buffer, val, byteOffset, encoding, dir)
| 700 | // - encoding - an optional encoding, relevant is val is a string |
| 701 | // - dir - true for indexOf, false for lastIndexOf |
| 702 | function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { |
| 703 | // Empty buffer means no match |
| 704 | if (buffer.length === 0) return -1 |
| 705 | |
| 706 | // Normalize byteOffset |
| 707 | if (typeof byteOffset === 'string') { |
| 708 | encoding = byteOffset |
| 709 | byteOffset = 0 |
| 710 | } else if (byteOffset > 0x7fffffff) { |
| 711 | byteOffset = 0x7fffffff |
| 712 | } else if (byteOffset < -0x80000000) { |
| 713 | byteOffset = -0x80000000 |
| 714 | } |
| 715 | byteOffset = +byteOffset // Coerce to Number. |
| 716 | if (numberIsNaN(byteOffset)) { |
| 717 | // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer |
| 718 | byteOffset = dir ? 0 : (buffer.length - 1) |
| 719 | } |
| 720 | |
| 721 | // Normalize byteOffset: negative offsets start from the end of the buffer |
| 722 | if (byteOffset < 0) byteOffset = buffer.length + byteOffset |
| 723 | if (byteOffset >= buffer.length) { |
| 724 | if (dir) return -1 |
| 725 | else byteOffset = buffer.length - 1 |
| 726 | } else if (byteOffset < 0) { |
| 727 | if (dir) byteOffset = 0 |
| 728 | else return -1 |
| 729 | } |
| 730 | |
| 731 | // Normalize val |
| 732 | if (typeof val === 'string') { |
| 733 | val = Buffer.from(val, encoding) |
| 734 | } |
| 735 | |
| 736 | // Finally, search either indexOf (if dir is true) or lastIndexOf |
| 737 | if (Buffer.isBuffer(val)) { |
| 738 | // Special case: looking for empty string/buffer always fails |
| 739 | if (val.length === 0) { |
| 740 | return -1 |
| 741 | } |
| 742 | return arrayIndexOf(buffer, val, byteOffset, encoding, dir) |
| 743 | } else if (typeof val === 'number') { |
| 744 | val = val & 0xFF // Search for a byte value [0-255] |
| 745 | if (typeof Uint8Array.prototype.indexOf === 'function') { |
| 746 | if (dir) { |
| 747 | return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) |
| 748 | } else { |
| 749 | return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) |
| 750 | } |
| 751 | } |
| 752 | return arrayIndexOf(buffer, [val], byteOffset, encoding, dir) |
| 753 | } |
| 754 | |
| 755 | throw new TypeError('val must be string, number or Buffer') |
| 756 | } |
| 757 | |
| 758 | function arrayIndexOf (arr, val, byteOffset, encoding, dir) { |
| 759 | let indexSize = 1 |
no test coverage detected
searching dependent graphs…