| 1 |
<?php |
| 2 |
class LP_Compress_String { |
| 3 |
public static function decompressString( $compressed ) { |
| 4 |
$compressed = explode( ',', $compressed ); |
| 5 |
$dictSize = 256; |
| 6 |
$dictionary = array(); |
| 7 |
for ( $i = 1; $i < 256; $i ++ ) { |
| 8 |
$dictionary[ $i ] = chr( $i ); |
| 9 |
} |
| 10 |
$w = chr( $compressed[0] ); |
| 11 |
$result = $w; |
| 12 |
for ( $i = 1; $i < count( $compressed ); $i ++ ) { |
| 13 |
$entry = ''; |
| 14 |
$k = $compressed[ $i ]; |
| 15 |
if ( isset( $dictionary[ $k ] ) ) { |
| 16 |
$entry = $dictionary[ $k ]; |
| 17 |
} elseif ( $k == $dictSize ) { |
| 18 |
$entry = $w . self::charAt( $w, 0 ); |
| 19 |
} else { |
| 20 |
return null; |
| 21 |
} |
| 22 |
$result .= $entry; |
| 23 |
$dictionary[ $dictSize ++ ] = $w . self::charAt( $entry, 0 ); |
| 24 |
$w = $entry; |
| 25 |
} |
| 26 |
|
| 27 |
return $result; |
| 28 |
} |
| 29 |
|
| 30 |
public static function charAt( $string, $index ) { |
| 31 |
if ( $index < mb_strlen( $string ) ) { |
| 32 |
return mb_substr( $string, $index, 1 ); |
| 33 |
} else { |
| 34 |
return - 1; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public static function chr( $u ) { |
| 39 |
return mb_convert_encoding( '&#' . intval( $u ) . ';', 'UTF-8', 'HTML-ENTITIES' ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function toCharCodes( $c ) { |
| 43 |
$x = 'charCodeAt'; |
| 44 |
$b = ''; |
| 45 |
$e = array(); |
| 46 |
$f = str_split( $c ); |
| 47 |
$d = []; |
| 48 |
$a = $f[0]; |
| 49 |
$g = 256; |
| 50 |
for ( $b = 1; $b < sizeof( $f ); $b ++ ) { |
| 51 |
$c = $f[ $b ]; |
| 52 |
if ( null != $e[ $a . $c ] ) { |
| 53 |
$a .= $c; |
| 54 |
} else { |
| 55 |
//$d[] = 1 < sizeof( $a ) ? $e[ $a ] : $a[ $x ]( 0 ); |
| 56 |
$d[] = 1 < mb_strlen( $a ) ? $e[ $a ] : self::utf8_char_code_at( $a, 0 ); |
| 57 |
$e[ $a . $c ] = $g; |
| 58 |
$g ++; |
| 59 |
$a = $c; |
| 60 |
} |
| 61 |
} |
| 62 |
$d[] = 1 < mb_strlen( $a ) ? $e[ $a ] : self::utf8_char_code_at( $a, 0 ); |
| 63 |
|
| 64 |
return $d; |
| 65 |
} |
| 66 |
|
| 67 |
public static function compressString( $c ) { |
| 68 |
|
| 69 |
$d = self::toCharCodes( $c ); |
| 70 |
|
| 71 |
for ( $b = 0; $b < sizeof( $d ); $b ++ ) { |
| 72 |
$d[ $b ] = self::chr( $d[ $b ] ); |
| 73 |
} |
| 74 |
|
| 75 |
return join( $d, '' ); |
| 76 |
|
| 77 |
// $dictSize = 256; |
| 78 |
// $dictionary = array(); |
| 79 |
// for ( $i = 0; $i < 256; $i ++ ) { |
| 80 |
// $dictionary[ chr( $i ) ] = $i; |
| 81 |
// } |
| 82 |
// $w = ""; |
| 83 |
// $result = ""; |
| 84 |
// for ( $i = 0; $i < strlen( $uncompressed ); $i ++ ) { |
| 85 |
// $c = self::charAt( $uncompressed, $i ); |
| 86 |
// $wc = $w . $c; |
| 87 |
// if ( isset( $dictionary[ $wc ] ) ) { |
| 88 |
// $w = $wc; |
| 89 |
// } else { |
| 90 |
// if ( $result != "" ) { |
| 91 |
// $result .= "," . $dictionary[ $w ]; |
| 92 |
// } else { |
| 93 |
// $result .= $dictionary[ $w ]; |
| 94 |
// } |
| 95 |
// $dictionary[ $wc ] = $dictSize ++; |
| 96 |
// $w = "" . $c; |
| 97 |
// } |
| 98 |
// } |
| 99 |
// if ( $w != "" ) { |
| 100 |
// if ( $result != "" ) { |
| 101 |
// $result .= "," . $dictionary[ $w ]; |
| 102 |
// } else { |
| 103 |
// $result .= $dictionary[ $w ]; |
| 104 |
// |
| 105 |
// } |
| 106 |
// |
| 107 |
// return $result; |
| 108 |
// } |
| 109 |
} |
| 110 |
|
| 111 |
public static function utf8_char_code_at( $str, $index ) { |
| 112 |
$char = ''; |
| 113 |
$str_index = 0; |
| 114 |
|
| 115 |
$str = self::utf8_scrub( $str ); |
| 116 |
$len = strlen( $str ); |
| 117 |
|
| 118 |
for ( $i = 0; $i < $len; $i += 1 ) { |
| 119 |
|
| 120 |
$char .= $str[ $i ]; |
| 121 |
|
| 122 |
if ( self::utf8_check_encoding( $char ) ) { |
| 123 |
|
| 124 |
if ( $str_index === $index ) { |
| 125 |
return self::utf8_ord( $char ); |
| 126 |
} |
| 127 |
|
| 128 |
$char = ''; |
| 129 |
$str_index += 1; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
public static function utf8_scrub( $str ) { |
| 137 |
return htmlspecialchars_decode( htmlspecialchars( $str, ENT_SUBSTITUTE, 'UTF-8' ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public static function utf8_check_encoding( $str ) { |
| 141 |
return $str === self::utf8_scrub( $str ); |
| 142 |
} |
| 143 |
|
| 144 |
public static function utf8_ord( $char ) { |
| 145 |
$lead = ord( $char[0] ); |
| 146 |
|
| 147 |
if ( $lead < 0x80 ) { |
| 148 |
return $lead; |
| 149 |
} elseif ( $lead < 0xE0 ) { |
| 150 |
return ( ( $lead & 0x1F ) << 6 ) |
| 151 |
| ( ord( $char[1] ) & 0x3F ); |
| 152 |
} elseif ( $lead < 0xF0 ) { |
| 153 |
return ( ( $lead & 0xF ) << 12 ) |
| 154 |
| ( ( ord( $char[1] ) & 0x3F ) << 6 ) |
| 155 |
| ( ord( $char[2] ) & 0x3F ); |
| 156 |
} else { |
| 157 |
return ( ( $lead & 0x7 ) << 18 ) |
| 158 |
| ( ( ord( $char[1] ) & 0x3F ) << 12 ) |
| 159 |
| ( ( ord( $char[2] ) & 0x3F ) << 6 ) |
| 160 |
| ( ord( $char[3] ) & 0x3F ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|