Crushed Stax

From Esolang
Jump to navigation Jump to search

Crushed Stax

In short, Crushed Stax is 7-bit version of PackedStax, to remove the reductant 1 in most significant bit in each byte.

First, the code should be turned to a binary stream, then grouped into 7-bit groups, then a 1 will appended in start of each group and then interpreted as PackedStax.

Since in most times, the amount of bits in 7-bit ASCII is not divisible by 8, the interpreter should automatically detect remainder bits in end and ignore them.

Implementation

Here is a simple encoder and decoder for crushed stax in JavaScript:

PackedStax to Crushed Stax

function packToCrush(packed_str) {
    let packed_to_bin = [...packed_str].map(c =>
        c.charCodeAt().toString(2).padStart(8,'0').slice(1)
    ).join();
   
    let bin_to_packed = packed_to_bin.match(/.{1,8}/g).map(b =>
        String.fromCharCode(parseInt(b.padEnd(8,'0'),2))
    ).join();
   
    return bin_to_packed;
}

Crushed Stax to PackedStax

function crushToPack(crushed_str) {
    let crushed_to_bin = [...crushed_str].map(c =>
        c.charCodeAt().toString(2).padStart(8,'0')
    ).join();
    
    let total_bits = crushed_to_bin.length;
    let keep_bits = total_bits - ((total_bits % 7) - 1);
    crushed_to_bin = crushed_to_bin.slice(0,keep_bits);
    
    let bin_to_crushed = crushed_to_bin.match(/.{7}/g).map(b =>
        String.fromCharCode(parseInt('1'+b,2))
    ).join();
   
    return bin_to_crushed;
}