Git Product home page Git Product logo

Comments (4)

pieroxy avatar pieroxy commented on July 19, 2024

Can you tell me what column type you use to store the result of the compression? Also on what type of device does it fail?

from lz-string.

pinventado avatar pinventado commented on July 19, 2024

I think I solved the problem. It seems that the compressToUTF16 function sometimes generates \u2028 and \u2029 characters which are parsed as line terminators (see here for details).

I ended up replacing all occurrences of the 2 characters after compression and before storing to the db, then after retrieval and before decompression. Something like this:

before storage

var dbvalue = LZString.compressToUTF16(JSON.stringify(value));
dbvalue = dbvalue.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');

after retrieval

var utfdata = dbvalue.replace(/\\u2028/g,'\u2028').replace(/\\u2029/g,'\u2029');
var value = JSON.parse(LZString.decompressFromUTF16(utfdata));          

Of course, it would be better if these 2 characters can be escaped inside LZString instead.

from lz-string.

pieroxy avatar pieroxy commented on July 19, 2024

LZString itself is a hack about how to store bits in UTF-16 strings. The only guarantee you get here is that the produced string is a valid UTF-16 string. Now, if your particular storage mechanism doesn't support certain UTF characters, the best way is to replace them with others. But LZString cannot (and should not) accommodate all quirks of all storage mechanisms.

Now, about your code, the replace you've used is not safe. Nothing prevents LZString from outputting a string made of "\u2028" so your replace could conflict with this sequence.

You should rather replace both characters with other characters in the UTF range that are not used by LZString. LZString will use 15 bits, meaning char 0 to 32767 but shifted by 32 because chars below 32 aren't valid UTF. So the range used effectively is 32 to 32799. Just replace both your characters with 32800 and 32801 and you should be fine.

from lz-string.

pinventado avatar pinventado commented on July 19, 2024

I see your point. I guess it is better to have that part implemented outside of the library.

Thanks for the suggestion! I'll try working that into my code

from lz-string.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.