]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/utfnormal/src/Util.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / utfnormal / src / Util.php
1 <?php
2 namespace UtfNormal;
3
4 use InvalidArgumentException;
5
6 /**
7  * Some of these functions are adapted from places in MediaWiki.
8  * Should probably merge them for consistency.
9  *
10  * Copyright © 2004 Brion Vibber <brion@pobox.com>
11  * https://www.mediawiki.org/
12  *
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.
17  *
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.
22  *
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
27  *
28  * @file
29  * @ingroup UtfNormal
30  */
31
32 class Utils {
33         /**
34          * Return UTF-8 sequence for a given Unicode code point.
35          *
36          * @param $codepoint Integer:
37          * @return String
38          * @throws InvalidArgumentException if fed out of range data.
39          */
40         public static function codepointToUtf8( $codepoint ) {
41                 if ( $codepoint < 0x80 ) {
42                         return chr( $codepoint );
43                 }
44
45                 if ( $codepoint < 0x800 ) {
46                         return chr( $codepoint >> 6 & 0x3f | 0xc0 ) .
47                         chr( $codepoint & 0x3f | 0x80 );
48                 }
49
50                 if ( $codepoint < 0x10000 ) {
51                         return chr( $codepoint >> 12 & 0x0f | 0xe0 ) .
52                         chr( $codepoint >> 6 & 0x3f | 0x80 ) .
53                         chr( $codepoint & 0x3f | 0x80 );
54                 }
55
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 );
61                 }
62
63                 throw new InvalidArgumentException( "Asked for code outside of range ($codepoint)" );
64         }
65
66         /**
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.
70          *
71          * @param $sequence String
72          * @return String
73          * @throws InvalidArgumentException if fed out of range data.
74          * @private Used in tests and data table generation
75          */
76         public static function hexSequenceToUtf8( $sequence ) {
77                 $utf = '';
78                 foreach ( explode( ' ', $sequence ) as $hex ) {
79                         $n = hexdec( $hex );
80                         $utf .= self::codepointToUtf8( $n );
81                 }
82
83                 return $utf;
84         }
85
86         /**
87          * Take a UTF-8 string and return a space-separated series of hex
88          * numbers representing Unicode code points. For debugging.
89          *
90          * @param string $str UTF-8 string.
91          * @return string
92          * @private
93          */
94         private static function utf8ToHexSequence( $str ) {
95                 $buf = '';
96                 foreach ( preg_split( '//u', $str, -1, PREG_SPLIT_NO_EMPTY ) as $cp ) {
97                         $buf .= sprintf( '%04x ', self::utf8ToCodepoint( $cp ) );
98                 }
99
100                 return rtrim( $buf );
101         }
102
103         /**
104          * Determine the Unicode codepoint of a single-character UTF-8 sequence.
105          * Does not check for invalid input data.
106          *
107          * @param $char String
108          * @return Integer
109          */
110         public static function utf8ToCodepoint( $char ) {
111                 # Find the length
112                 $z = ord( $char[0] );
113                 if ( $z & 0x80 ) {
114                         $length = 0;
115                         while ( $z & 0x80 ) {
116                                 $length++;
117                                 $z <<= 1;
118                         }
119                 } else {
120                         $length = 1;
121                 }
122
123                 if ( $length != strlen( $char ) ) {
124                         return false;
125                 }
126
127                 if ( $length == 1 ) {
128                         return ord( $char );
129                 }
130
131                 # Mask off the length-determining bits and shift back to the original location
132                 $z &= 0xff;
133                 $z >>= $length;
134
135                 # Add in the free bits from subsequent bytes
136                 for ( $i = 1; $i < $length; $i++ ) {
137                         $z <<= 6;
138                         $z |= ord( $char[$i] ) & 0x3f;
139                 }
140
141                 return $z;
142         }
143
144         /**
145          * Escape a string for inclusion in a PHP single-quoted string literal.
146          *
147          * @param string $string string to be escaped.
148          * @return String: escaped string.
149          */
150         public static function escapeSingleString( $string ) {
151                 return strtr( $string,
152                         array(
153                                 '\\' => '\\\\',
154                                 '\'' => '\\\''
155                         ) );
156         }
157 }