* Copies `bytes` up to the byte length needed for `length` bits, then clears * unused least-significant bits in the final byte. * @param {number} length * @param {ArrayBuffer|ArrayBufferView} bytes * @returns {Uint8Array}
(length, bytes)
| 568 | * @returns {Uint8Array} |
| 569 | */ |
| 570 | function truncateToBitLength(length, bytes) { |
| 571 | const lengthBytes = numBitsToBytes(length); |
| 572 | const isView = ArrayBufferIsView(bytes); |
| 573 | const byteView = isView ? |
| 574 | new Uint8Array( |
| 575 | getDataViewOrTypedArrayBuffer(bytes), |
| 576 | getDataViewOrTypedArrayByteOffset(bytes), |
| 577 | getDataViewOrTypedArrayByteLength(bytes), |
| 578 | ) : |
| 579 | new Uint8Array(bytes, 0, ArrayBufferPrototypeGetByteLength(bytes)); |
| 580 | const result = TypedArrayPrototypeSlice( |
| 581 | byteView, |
| 582 | 0, |
| 583 | lengthBytes, |
| 584 | ); |
| 585 | |
| 586 | const remainder = length % 8; |
| 587 | if (remainder !== 0) |
| 588 | result[lengthBytes - 1] &= (0xff << (8 - remainder)) & 0xff; |
| 589 | |
| 590 | return result; |
| 591 | } |
| 592 | |
| 593 | let webidl; |
| 594 |
no test coverage detected
searching dependent graphs…