CodeLobe.com/tools/ascii-compress


ASCII Fold - Plain and simple text compression


 

Usage

Use the ↑ Load ASCII File ↑ button or paste some plain ASCII text into the text box. Use the ► Compress Text ◄ button to reduce size of repeated parts of text. Use the ◄ Decompress Text ► button to expand the escape codes back into text. ↕ Select All Text ↔ then copy decompressed text, or hit the ↓ Save ASCII File ↓ button to download the box contents.

Since the output is ASCII you can experiment with it. Try editing compressed text and see what comes out when decompressed.

About ASCII Fold

ASCII Fold is a program I initially wrote to compress lengthy JSON data & WebGL shaders. This simple program is small & light-weight, but is very limited in that it only works on text compatible with 7 bit ASCII. However, it's just handy enough that I've found myself reusing it to crunch various repetitive text data.

This compressor escapes non-printable characters using printable ASCII. This means the output is plain ASCII text and can be placed in a <script> tag or elsewhere that character encodings may be limited. Text output also lends itself to educational use since it's easy to demonstrate how the compressor operates and manually modify the (base 96) compression escape codes.

The Javascript decompression function is just 286 chars so it can be included with compressed output without significantly impacting the compression savings.

Even after source code minification ASCII Fold can usually still compress program source code. For example, ASCII Fold makes the minimized version of JQuery about 50% smaller. The compression is not without cost, as it does take a bit of processing to decompress, but the algorithm is very simple and fast.

My goal was not to replace existing general purpose compression tools, but to solve a particular use case with a domain specific compression / decompression function. Most web servers use compression during transport and some servers can store pre-compressed files. Those solutions should be used first if you're trying to save bandwidth.

However, there are still many cases where it's nice to have a text compressor handy. For example, today's browsers have localStorage and indexDB object cache. Client side compression can lead to faster initial page load times when handling a lot of local state; Especially if pulling data from storage is the bottleneck. This is often the case with today's browser localStorage implementations since the entire database is loaded even if you just request a single value.

Decompression Code

If you want to use the ASCII Folded text in your project here's the decompression function:

function U(i){var c,f,l,r=0,o='';function g(){c=i.charCodeAt(r++)}function p(x){o+=String.fromCharCode(x)}while(r<i.length){g();if(c==96){g();if(c==96)p(c);else if(c>65){c-=c>96?62:61;l=c;g();f=c-28;g();f+=94*(c-33);o+=o.substr(o.length-f,l)}else if(c>32)p(c-34&127)}else p(c)}return o}

To use the function: decompressedTxt = U( compressedTxt );