2004-08-29 10:30:23 +00:00
|
|
|
<?php
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
|
|
|
|
* Some of these functions are adapted from places in MediaWiki.
|
|
|
|
|
* Should probably merge them for consistency.
|
|
|
|
|
*
|
2010-08-15 07:47:23 +00:00
|
|
|
* Copyright © 2004 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* http://www.mediawiki.org/
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup UtfNormal
|
2004-09-03 23:00:01 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
|
2004-10-28 02:56:13 +00:00
|
|
|
/**
|
|
|
|
|
* Return UTF-8 sequence for a given Unicode code point.
|
|
|
|
|
* May die if fed out of range data.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $codepoint Integer:
|
|
|
|
|
* @return String
|
|
|
|
|
* @public
|
2004-10-28 02:56:13 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function codepointToUtf8( $codepoint ) {
|
|
|
|
|
if($codepoint < 0x80) return chr($codepoint);
|
|
|
|
|
if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
|
|
|
|
|
chr($codepoint & 0x3f | 0x80);
|
|
|
|
|
if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
|
|
|
|
|
chr($codepoint >> 6 & 0x3f | 0x80) .
|
|
|
|
|
chr($codepoint & 0x3f | 0x80);
|
|
|
|
|
if($codepoint < 0x110000) return chr($codepoint >> 18 & 0x07 | 0xf0) .
|
|
|
|
|
chr($codepoint >> 12 & 0x3f | 0x80) .
|
|
|
|
|
chr($codepoint >> 6 & 0x3f | 0x80) .
|
|
|
|
|
chr($codepoint & 0x3f | 0x80);
|
|
|
|
|
|
2006-01-14 02:49:43 +00:00
|
|
|
echo "Asked for code outside of range ($codepoint)\n";
|
|
|
|
|
die( -1 );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
2004-10-28 02:56:13 +00:00
|
|
|
/**
|
|
|
|
|
* Take a series of space-separated hexadecimal numbers representing
|
|
|
|
|
* Unicode code points and return a UTF-8 string composed of those
|
|
|
|
|
* characters. Used by UTF-8 data generation and testing routines.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $sequence String
|
|
|
|
|
* @return String
|
|
|
|
|
* @private
|
2004-10-28 02:56:13 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function hexSequenceToUtf8( $sequence ) {
|
|
|
|
|
$utf = '';
|
|
|
|
|
foreach( explode( ' ', $sequence ) as $hex ) {
|
|
|
|
|
$n = hexdec( $hex );
|
|
|
|
|
$utf .= codepointToUtf8( $n );
|
|
|
|
|
}
|
|
|
|
|
return $utf;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-15 00:58:36 +00:00
|
|
|
/**
|
|
|
|
|
* Take a UTF-8 string and return a space-separated series of hex
|
|
|
|
|
* numbers representing Unicode code points. For debugging.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $str String: UTF-8 string.
|
2004-11-15 00:58:36 +00:00
|
|
|
* @return string
|
2006-04-19 15:46:24 +00:00
|
|
|
* @private
|
2004-11-15 00:58:36 +00:00
|
|
|
*/
|
|
|
|
|
function utf8ToHexSequence( $str ) {
|
|
|
|
|
return rtrim( preg_replace( '/(.)/uSe',
|
|
|
|
|
'sprintf("%04x ", utf8ToCodepoint("$1"))',
|
|
|
|
|
$str ) );
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-28 02:56:13 +00:00
|
|
|
/**
|
|
|
|
|
* Determine the Unicode codepoint of a single-character UTF-8 sequence.
|
|
|
|
|
* Does not check for invalid input data.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $char String
|
|
|
|
|
* @return Integer
|
|
|
|
|
* @public
|
2004-10-28 02:56:13 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function utf8ToCodepoint( $char ) {
|
|
|
|
|
# Find the length
|
2011-04-17 07:59:58 +00:00
|
|
|
$z = ord( $char[0] );
|
2004-08-29 10:30:23 +00:00
|
|
|
if ( $z & 0x80 ) {
|
|
|
|
|
$length = 0;
|
|
|
|
|
while ( $z & 0x80 ) {
|
|
|
|
|
$length++;
|
|
|
|
|
$z <<= 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$length = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $length != strlen( $char ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( $length == 1 ) {
|
|
|
|
|
return ord( $char );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Mask off the length-determining bits and shift back to the original location
|
|
|
|
|
$z &= 0xff;
|
|
|
|
|
$z >>= $length;
|
|
|
|
|
|
|
|
|
|
# Add in the free bits from subsequent bytes
|
|
|
|
|
for ( $i=1; $i<$length; $i++ ) {
|
|
|
|
|
$z <<= 6;
|
2011-04-17 07:59:58 +00:00
|
|
|
$z |= ord( $char[$i] ) & 0x3f;
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $z;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-28 02:56:13 +00:00
|
|
|
/**
|
|
|
|
|
* Escape a string for inclusion in a PHP single-quoted string literal.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $string String: string to be escaped.
|
|
|
|
|
* @return String: escaped string.
|
|
|
|
|
* @public
|
2004-10-28 02:56:13 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function escapeSingleString( $string ) {
|
|
|
|
|
return strtr( $string,
|
|
|
|
|
array(
|
|
|
|
|
'\\' => '\\\\',
|
|
|
|
|
'\'' => '\\\''
|
|
|
|
|
));
|
|
|
|
|
}
|