00001 <?php
00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00029 if( php_sapi_name() != 'cli' ) {
00030 die( "Run me from the command line please.\n" );
00031 }
00032
00034 require_once( 'UtfNormal.php' );
00035 require_once( '../DifferenceEngine.php' );
00036
00037 dl('php_utfnormal.so' );
00038
00039 # mt_srand( 99999 );
00040
00041 function randomString( $length, $nullOk, $ascii = false ) {
00042 $out = '';
00043 for( $i = 0; $i < $length; $i++ )
00044 $out .= chr( mt_rand( $nullOk ? 0 : 1, $ascii ? 127 : 255 ) );
00045 return $out;
00046 }
00047
00048
00049 function donorm( $str ) {
00050 # We exclude a few chars that ICU would not.
00051 $str = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str );
00052 $str = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $str );
00053 $str = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $str );
00054
00055 # UnicodeString constructor fails if the string ends with a head byte.
00056 # Add a junk char at the end, we'll strip it off
00057 return rtrim( utf8_normalize( $str . "\x01", UNORM_NFC ), "\x01" );
00058 }
00059
00060 function wfMsg($x) {
00061 return $x;
00062 }
00063
00064 function showDiffs( $a, $b ) {
00065 $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
00066 $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
00067
00068 $diffs = new Diff( $ota, $nta );
00069 $formatter = new TableDiffFormatter();
00070 $funky = $formatter->format( $diffs );
00071 $matches = array();
00072 preg_match_all( '/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches );
00073 foreach( $matches[1] as $bit ) {
00074 $hex = bin2hex( $bit );
00075 echo "\t$hex\n";
00076 }
00077 }
00078
00079 $size = 16;
00080 $n = 0;
00081 while( true ) {
00082 $n++;
00083 echo "$n\n";
00084
00085 $str = randomString( $size, true);
00086 $clean = UtfNormal::cleanUp( $str );
00087 $norm = donorm( $str );
00088
00089 echo strlen( $clean ) . ", " . strlen( $norm );
00090 if( $clean == $norm ) {
00091 echo " (match)\n";
00092 } else {
00093 echo " (FAIL)\n";
00094 echo "\traw: " . bin2hex( $str ) . "\n" .
00095 "\tphp: " . bin2hex( $clean ) . "\n" .
00096 "\ticu: " . bin2hex( $norm ) . "\n";
00097 echo "\n\tdiffs:\n";
00098 showDiffs( $clean, $norm );
00099 die();
00100 }
00101
00102
00103 $str = '';
00104 $clean = '';
00105 $norm = '';
00106 }