Created
April 4, 2024 08:36
-
-
Save okayurisotto/890b734c767631c587e3d4e628aad915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function* separate( | |
data: Uint8Array, | |
bitLength: number | |
): Generator<number> { | |
let bufferBitLength = 0; | |
let buffer = 0; | |
for (const chunk of data) { | |
bufferBitLength += 8; | |
buffer = (buffer << 8) | chunk; | |
while (bufferBitLength > bitLength) { | |
yield buffer >> (bufferBitLength - bitLength); | |
bufferBitLength -= bitLength; | |
buffer = buffer & (2 ** bufferBitLength - 1); | |
} | |
} | |
yield buffer << (bitLength - bufferBitLength); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment