]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/normal/UtfNormalUtil.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / normal / UtfNormalUtil.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21  * Some of these functions are adapted from places in MediaWiki.
22  * Should probably merge them for consistency.
23  *
24  * @ingroup UtfNormal
25  * @public
26  */
27
28 /** */
29 require_once dirname(__FILE__).'/UtfNormalDefines.php';
30
31 /**
32  * Return UTF-8 sequence for a given Unicode code point.
33  * May die if fed out of range data.
34  *
35  * @param $codepoint Integer:
36  * @return String
37  * @public
38  */
39 function codepointToUtf8( $codepoint ) {
40         if($codepoint <         0x80) return chr($codepoint);
41         if($codepoint <    0x800) return chr($codepoint >>      6 & 0x3f | 0xc0) .
42                                                                          chr($codepoint           & 0x3f | 0x80);
43         if($codepoint <  0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
44                                                                          chr($codepoint >>      6 & 0x3f | 0x80) .
45                                                                          chr($codepoint           & 0x3f | 0x80);
46         if($codepoint < 0x110000) return chr($codepoint >> 18 & 0x07 | 0xf0) .
47                                                                          chr($codepoint >> 12 & 0x3f | 0x80) .
48                                                                          chr($codepoint >>      6 & 0x3f | 0x80) .
49                                                                          chr($codepoint           & 0x3f | 0x80);
50
51         echo "Asked for code outside of range ($codepoint)\n";
52         die( -1 );
53 }
54
55 /**
56  * Take a series of space-separated hexadecimal numbers representing
57  * Unicode code points and return a UTF-8 string composed of those
58  * characters. Used by UTF-8 data generation and testing routines.
59  *
60  * @param $sequence String
61  * @return String
62  * @private
63  */
64 function hexSequenceToUtf8( $sequence ) {
65         $utf = '';
66         foreach( explode( ' ', $sequence ) as $hex ) {
67                 $n = hexdec( $hex );
68                 $utf .= codepointToUtf8( $n );
69         }
70         return $utf;
71 }
72
73 /**
74  * Take a UTF-8 string and return a space-separated series of hex
75  * numbers representing Unicode code points. For debugging.
76  *
77  * @param $str String: UTF-8 string.
78  * @return string
79  * @private
80  */
81 function utf8ToHexSequence( $str ) {
82         return rtrim( preg_replace( '/(.)/uSe',
83                                     'sprintf("%04x ", utf8ToCodepoint("$1"))',
84                                     $str ) );
85 }
86
87 /**
88  * Determine the Unicode codepoint of a single-character UTF-8 sequence.
89  * Does not check for invalid input data.
90  *
91  * @param $char String
92  * @return Integer
93  * @public
94  */
95 function utf8ToCodepoint( $char ) {
96         # Find the length
97         $z = ord( $char{0} );
98         if ( $z & 0x80 ) {
99                 $length = 0;
100                 while ( $z & 0x80 ) {
101                         $length++;
102                         $z <<= 1;
103                 }
104         } else {
105                 $length = 1;
106         }
107
108         if ( $length != strlen( $char ) ) {
109                 return false;
110         }
111         if ( $length == 1 ) {
112                 return ord( $char );
113         }
114
115         # Mask off the length-determining bits and shift back to the original location
116         $z &= 0xff;
117         $z >>= $length;
118
119         # Add in the free bits from subsequent bytes
120         for ( $i=1; $i<$length; $i++ ) {
121                 $z <<= 6;
122                 $z |= ord( $char{$i} ) & 0x3f;
123         }
124
125         return $z;
126 }
127
128 /**
129  * Escape a string for inclusion in a PHP single-quoted string literal.
130  *
131  * @param $string String: string to be escaped.
132  * @return String: escaped string.
133  * @public
134  */
135 function escapeSingleString( $string ) {
136         return strtr( $string,
137                 array(
138                         '\\' => '\\\\',
139                         '\'' => '\\\''
140                 ));
141 }