4 use InvalidArgumentException;
7 * Some of these functions are adapted from places in MediaWiki.
8 * Should probably merge them for consistency.
10 * Copyright © 2004 Brion Vibber <brion@pobox.com>
11 * https://www.mediawiki.org/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
34 * Return UTF-8 sequence for a given Unicode code point.
36 * @param $codepoint Integer:
38 * @throws InvalidArgumentException if fed out of range data.
40 public static function codepointToUtf8( $codepoint ) {
41 if ( $codepoint < 0x80 ) {
42 return chr( $codepoint );
45 if ( $codepoint < 0x800 ) {
46 return chr( $codepoint >> 6 & 0x3f | 0xc0 ) .
47 chr( $codepoint & 0x3f | 0x80 );
50 if ( $codepoint < 0x10000 ) {
51 return chr( $codepoint >> 12 & 0x0f | 0xe0 ) .
52 chr( $codepoint >> 6 & 0x3f | 0x80 ) .
53 chr( $codepoint & 0x3f | 0x80 );
56 if ( $codepoint < 0x110000 ) {
57 return chr( $codepoint >> 18 & 0x07 | 0xf0 ) .
58 chr( $codepoint >> 12 & 0x3f | 0x80 ) .
59 chr( $codepoint >> 6 & 0x3f | 0x80 ) .
60 chr( $codepoint & 0x3f | 0x80 );
63 throw new InvalidArgumentException( "Asked for code outside of range ($codepoint)" );
67 * Take a series of space-separated hexadecimal numbers representing
68 * Unicode code points and return a UTF-8 string composed of those
69 * characters. Used by UTF-8 data generation and testing routines.
71 * @param $sequence String
73 * @throws InvalidArgumentException if fed out of range data.
74 * @private Used in tests and data table generation
76 public static function hexSequenceToUtf8( $sequence ) {
78 foreach ( explode( ' ', $sequence ) as $hex ) {
80 $utf .= self::codepointToUtf8( $n );
87 * Take a UTF-8 string and return a space-separated series of hex
88 * numbers representing Unicode code points. For debugging.
90 * @param string $str UTF-8 string.
94 private static function utf8ToHexSequence( $str ) {
96 foreach ( preg_split( '//u', $str, -1, PREG_SPLIT_NO_EMPTY ) as $cp ) {
97 $buf .= sprintf( '%04x ', self::utf8ToCodepoint( $cp ) );
100 return rtrim( $buf );
104 * Determine the Unicode codepoint of a single-character UTF-8 sequence.
105 * Does not check for invalid input data.
107 * @param $char String
110 public static function utf8ToCodepoint( $char ) {
112 $z = ord( $char[0] );
115 while ( $z & 0x80 ) {
123 if ( $length != strlen( $char ) ) {
127 if ( $length == 1 ) {
131 # Mask off the length-determining bits and shift back to the original location
135 # Add in the free bits from subsequent bytes
136 for ( $i = 1; $i < $length; $i++ ) {
138 $z |= ord( $char[$i] ) & 0x3f;
145 * Escape a string for inclusion in a PHP single-quoted string literal.
147 * @param string $string string to be escaped.
148 * @return String: escaped string.
150 public static function escapeSingleString( $string ) {
151 return strtr( $string,