(in_p)
return out
A literal port of the PICO-8 C compression routine.

    TODO: The original algorithm uses a brute force search for blocks
    (_find_repeatable_block()), which makes the overall algorithm O(n^2).
    I had a previous implementation that was faster but did not produce
    the same compressed result. It should be possible to optimize the
    working implementation using Python features without changing its
    result. (A quick attempt at memoization did not result in a speed
    increase.)

    Args:
        in_p: The code to compress, as a bytestring.

    Returns:
        The compressed code, as a bytearray. The compressed result is
        returned even if it is longer than in_p. The caller is responsible
        for comparing it to the original and acting accordingly.
A literal port of the PICO-8 C compression routine.
A literal port of the PICO - 8 C compression routine .
def compress_code(in_p):
    """A literal port of the PICO-8 C compression routine.

    TODO: The original algorithm uses a brute force search for blocks
    (_find_repeatable_block()), which makes the overall algorithm O(n^2).
    I had a previous implementation that was faster but did not produce
    the same compressed result. It should be possible to optimize the
    working implementation using Python features without changing its
    result. (A quick attempt at memoization did not result in a speed
    increase.)

    Args:
        in_p: The code to compress, as a bytestring.

    Returns:
        The compressed code, as a bytearray. The compressed result is
        returned even if it is longer than in_p. The caller is responsible
        for comparing it to the original and acting accordingly.
    """
    PICO8_CODE_ALLOC_SIZE = (0x10000 + 1)
    pos = 0

    literal_index = [0] * 256
    for i in range(1, len(COMPRESSED_LUA_CHAR_TABLE)):
        literal_index[COMPRESSED_LUA_CHAR_TABLE[i]] = i

    if b'_update60' in in_p and len(in_p) < PICO8_CODE_ALLOC_SIZE - (
            len(PICO8_FUTURE_CODE2) + 1):
        if in_p[-1] != b' '[0] and in_p[-1] != b'\n'[0]:
            in_p += b'\n'
        in_p += PICO8_FUTURE_CODE2

    out = bytearray()

    # The PICO-8 C code adds the preamble here, but we do it in
    # get_bytes_from_code().
    # out += b':c:\x00'
    # out.append(len(in_p) // 256)
    # out.append(len(in_p) % 256)
    # out += b'\x00\x00'

    while pos < len(in_p):
        block_len, block_offset = _find_repeatable_block(in_p, pos)

        if block_len >= 3:
            out.append(
                (block_offset // 16) + len(COMPRESSED_LUA_CHAR_TABLE))
            out.append((block_offset % 16) + (block_len - 2) * 16)
            pos += block_len
        else:
            out.append(literal_index[in_p[pos]])
            if literal_index[in_p[pos]] == 0:
                out.append(in_p[pos])
            pos += 1

    return out
def compress_code ( in_p ) : PICO8_CODE_ALLOC_SIZE = ( 0x10000 + 1 ) pos = 0 literal_index = [ 0 ] * 256 for i in range ( 1 , len ( COMPRESSED_LUA_CHAR_TABLE ) ) : literal_index [ COMPRESSED_LUA_CHAR_TABLE [ i ] ] = i if b'_update60' in in_p and len ( in_p ) < PICO8_CODE_ALLOC_SIZE - ( len ( PICO8_FUTURE_CODE2 ) + 1 ) : if in_p [ - 1 ] != b' ' [ 0 ] and in_p [ - 1 ] != b'\n' [ 0 ] : in_p += b'\n' in_p += PICO8_FUTURE_CODE2 out = bytearray ( ) # The PICO-8 C code adds the preamble here, but we do it in # get_bytes_from_code(). # out += b':c:\x00' # out.append(len(in_p) // 256) # out.append(len(in_p) % 256) # out += b'\x00\x00' while pos < len ( in_p ) : block_len , block_offset = _find_repeatable_block ( in_p , pos ) if block_len >= 3 : out . append ( ( block_offset // 16 ) + len ( COMPRESSED_LUA_CHAR_TABLE ) ) out . append ( ( block_offset % 16 ) + ( block_len - 2 ) * 16 ) pos += block_len else : out . append ( literal_index [ in_p [ pos ] ] ) if literal_index [ in_p [ pos ] ] == 0 : out . append ( in_p [ pos ] ) pos += 1 return out