|
1 | 1 | import { constants } from 'buffer'; |
2 | 2 |
|
3 | 3 | import { DynamicBufferIterator } from './iterator'; |
4 | | -import { rangeCheck } from './utils'; |
| 4 | +import { rangeCheck, swap } from './utils'; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * The character encoding that is supported by Node.js, copy from Node.js Buffer module. |
@@ -737,6 +737,106 @@ export class DynamicBuffer { |
737 | 737 | return this.buffer.copy(target, targetStart, sourceStart, sourceEnd); |
738 | 738 | } |
739 | 739 |
|
| 740 | + /** |
| 741 | + * Interprets buf as an array of unsigned 16-bit integers and swaps the byte order in-place. |
| 742 | + * |
| 743 | + * ```ts |
| 744 | + * const buf = new DynamicBuffer('ABCDEFGH'); |
| 745 | + * buf.swap16(); |
| 746 | + * console.log(buf.toString()); |
| 747 | + * // BADCFEHG |
| 748 | + * ``` |
| 749 | + * |
| 750 | + * @returns A reference to this buffer. |
| 751 | + */ |
| 752 | + swap16(): DynamicBuffer { |
| 753 | + if (this.length % 2 !== 0) { |
| 754 | + throw new RangeError('Buffer size must be a multiple of 2'); |
| 755 | + } |
| 756 | + |
| 757 | + if (this.length === 0 || !this.buffer) { |
| 758 | + return this; |
| 759 | + } |
| 760 | + |
| 761 | + if (this.length < 128 || typeof this.buffer.swap16 !== 'function') { |
| 762 | + for (let i = 0; i < this.length; i += 2) { |
| 763 | + swap(this.buffer, i, i + 1); |
| 764 | + } |
| 765 | + } else { |
| 766 | + this.buffer.subarray(0, this.length).swap16(); |
| 767 | + } |
| 768 | + |
| 769 | + return this; |
| 770 | + } |
| 771 | + |
| 772 | + /** |
| 773 | + * Interprets buf as an array of unsigned 32-bit integers and swaps the byte order in-place. |
| 774 | + * |
| 775 | + * ```ts |
| 776 | + * const buf = new DynamicBuffer('ABCDEFGH'); |
| 777 | + * buf.swap32(); |
| 778 | + * console.log(buf.toString()); |
| 779 | + * // DCBAHGFE |
| 780 | + * ``` |
| 781 | + * |
| 782 | + * @returns A reference to this buffer. |
| 783 | + */ |
| 784 | + swap32(): DynamicBuffer { |
| 785 | + if (this.length % 4 !== 0) { |
| 786 | + throw new RangeError('Buffer size must be a multiple of 4'); |
| 787 | + } |
| 788 | + |
| 789 | + if (this.length === 0 || !this.buffer) { |
| 790 | + return this; |
| 791 | + } |
| 792 | + |
| 793 | + if (this.length < 192 || typeof this.buffer.swap32 !== 'function') { |
| 794 | + for (let i = 0; i < this.length; i += 4) { |
| 795 | + swap(this.buffer, i, i + 3); |
| 796 | + swap(this.buffer, i + 1, i + 2); |
| 797 | + } |
| 798 | + } else { |
| 799 | + this.buffer.subarray(0, this.length).swap32(); |
| 800 | + } |
| 801 | + |
| 802 | + return this; |
| 803 | + } |
| 804 | + |
| 805 | + /** |
| 806 | + * Interprets buf as an array of unsigned 64-bit integers and swaps the byte order in-place. |
| 807 | + * |
| 808 | + * ```ts |
| 809 | + * const buf = new DynamicBuffer('ABCDEFGH'); |
| 810 | + * buf.swap64(); |
| 811 | + * console.log(buf.toString()); |
| 812 | + * // HGFEDCBA |
| 813 | + * ``` |
| 814 | + * |
| 815 | + * @returns A reference to this buffer. |
| 816 | + */ |
| 817 | + swap64(): DynamicBuffer { |
| 818 | + if (this.length % 8 !== 0) { |
| 819 | + throw new RangeError('Buffer size must be a multiple of 8'); |
| 820 | + } |
| 821 | + |
| 822 | + if (this.length === 0 || !this.buffer) { |
| 823 | + return this; |
| 824 | + } |
| 825 | + |
| 826 | + if (this.length < 192 || typeof this.buffer.swap32 !== 'function') { |
| 827 | + for (let i = 0; i < this.length; i += 8) { |
| 828 | + swap(this.buffer, i, i + 7); |
| 829 | + swap(this.buffer, i + 1, i + 6); |
| 830 | + swap(this.buffer, i + 2, i + 5); |
| 831 | + swap(this.buffer, i + 3, i + 4); |
| 832 | + } |
| 833 | + } else { |
| 834 | + this.buffer.subarray(0, this.length).swap64(); |
| 835 | + } |
| 836 | + |
| 837 | + return this; |
| 838 | + } |
| 839 | + |
740 | 840 | /** |
741 | 841 | * Write data into internal buffer with the specified offset. |
742 | 842 | * |
|
0 commit comments