#!/usr/bin/perl -w # encode_ending.pl - Don Yang (uguu.org) # # 2014-10-16 use strict; use constant REPEAT_BASE => 93; use constant LITERAL_BASE => 60; # Output a single repeat or literal header sub OutputTagHeader($$) { my ($base, $count) = @_; my $chr = $base + $count; if( $chr < 32 || $chr >= 127 || $chr == ord(';') || $chr == ord('\"') || $chr == ord('\\') ) { die "base=$base, count=$count"; } print chr($chr); } # Load input open my $file, ") { chomp $line; $text .= $line . ";"; } close $file; # Consume input while( $text ne "" ) { # Try replacing prefix with repeated character my $first = substr($text, 0, 1); if( length($text) >= 3 && substr($text, 0, 3) eq ($first x 3) ) { # Found 3 repeating characters, see how far it goes my $i = 3; for(; $i < length($text) && substr($text, $i, 1) eq $first; $i++) {} OutputTagHeader(REPEAT_BASE, $i - 3); print $first; $text = substr($text, $i); next; } # Replace prefix with literal my $i = 1; for(; $i < length($text); $i++) { if( $i + 3 < length($text) && substr($text, $i, 1) eq substr($text, $i + 1, 1) && substr($text, $i + 1, 1) eq substr($text, $i + 2, 1) ) { last; } } OutputTagHeader(LITERAL_BASE, $i - 1); print substr($text, 0, $i); $text = substr($text, $i); }