* s~ +$~~
[lhc/web/wiklou.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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @package UtfNormal
25 * @access public
26 */
27
28 /** */
29
30 /**
31 * Return UTF-8 sequence for a given Unicode code point.
32 * May die if fed out of range data.
33 *
34 * @param int $codepoint
35 * @return string
36 * @access public
37 */
38 function codepointToUtf8( $codepoint ) {
39 if($codepoint < 0x80) return chr($codepoint);
40 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
41 chr($codepoint & 0x3f | 0x80);
42 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
43 chr($codepoint >> 6 & 0x3f | 0x80) .
44 chr($codepoint & 0x3f | 0x80);
45 if($codepoint < 0x110000) return chr($codepoint >> 18 & 0x07 | 0xf0) .
46 chr($codepoint >> 12 & 0x3f | 0x80) .
47 chr($codepoint >> 6 & 0x3f | 0x80) .
48 chr($codepoint & 0x3f | 0x80);
49
50 die("Asked for code outside of range ($codepoint)\n");
51 }
52
53 /**
54 * Take a series of space-separated hexadecimal numbers representing
55 * Unicode code points and return a UTF-8 string composed of those
56 * characters. Used by UTF-8 data generation and testing routines.
57 *
58 * @param string $sequence
59 * @return string
60 * @access private
61 */
62 function hexSequenceToUtf8( $sequence ) {
63 $utf = '';
64 foreach( explode( ' ', $sequence ) as $hex ) {
65 $n = hexdec( $hex );
66 $utf .= codepointToUtf8( $n );
67 }
68 return $utf;
69 }
70
71 /**
72 * Take a UTF-8 string and return a space-separated series of hex
73 * numbers representing Unicode code points. For debugging.
74 *
75 * @param string $str
76 * @return string
77 * @access private
78 */
79 function utf8ToHexSequence( $str ) {
80 return rtrim( preg_replace( '/(.)/uSe',
81 'sprintf("%04x ", utf8ToCodepoint("$1"))',
82 $str ) );
83 }
84
85 /**
86 * Determine the Unicode codepoint of a single-character UTF-8 sequence.
87 * Does not check for invalid input data.
88 *
89 * @param string $char
90 * @return int
91 * @access public
92 */
93 function utf8ToCodepoint( $char ) {
94 # Find the length
95 $z = ord( $char{0} );
96 if ( $z & 0x80 ) {
97 $length = 0;
98 while ( $z & 0x80 ) {
99 $length++;
100 $z <<= 1;
101 }
102 } else {
103 $length = 1;
104 }
105
106 if ( $length != strlen( $char ) ) {
107 return false;
108 }
109 if ( $length == 1 ) {
110 return ord( $char );
111 }
112
113 # Mask off the length-determining bits and shift back to the original location
114 $z &= 0xff;
115 $z >>= $length;
116
117 # Add in the free bits from subsequent bytes
118 for ( $i=1; $i<$length; $i++ ) {
119 $z <<= 6;
120 $z |= ord( $char{$i} ) & 0x3f;
121 }
122
123 return $z;
124 }
125
126 /**
127 * Escape a string for inclusion in a PHP single-quoted string literal.
128 *
129 * @param string $string
130 * @return string
131 * @access public
132 */
133 function escapeSingleString( $string ) {
134 return strtr( $string,
135 array(
136 '\\' => '\\\\',
137 '\'' => '\\\''
138 ));
139 }
140
141 ?>