mirror of
https://github.com/google/brotli.git
synced 2026-07-08 17:56:58 +00:00
How to compress utf-16 string and decompress? #462
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ivanmem on GitHub (May 31, 2023).
Would love to have an example in the documentation for getting a compressed string and getting the original UTF-16 string (string in javascript).
@ivanmem commented on GitHub (May 31, 2023):
Here is an example that I got, but I doubt its correctness. It often throws an error
Uncaught RangeError: byte length of Uint16Array should be a multiple of 2 at new Uint16Array. So don't use it.@eustas commented on GitHub (Jun 20, 2023):
"brotli-compress" is a third party module, so I can only guess.
Brotli compressor is byte-oriented, i.e. it takes
(U)Int8Arrayfor input and produces(U)Int8Arrayas output.Indeed, internally JS stores strings as UTF-16, but there is no reason to compress UTF-16 data. Brotli does much better work in UTF-8.
Your code snippet looks well. Could you provide a sample that causes the error?
@ivanmem commented on GitHub (Jun 20, 2023):
@eustas The error occurs if you run the following code:
@eustas commented on GitHub (Jul 4, 2023):
uInt8ArrayToStrat the end ofcompressStrshould not be used. Result of compression is byte array. It can be even, it can be odd length. Moreover, JS is allowed to replace characters it does not understand with placeholders -> compressed data will be corrupted.Fixed code is here: https://stackblitz.com/edit/node-5zrzir?file=index.js