Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[lhc/web/wiklou.git] / includes / Fallback.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 */
20
21 /**
22 * Fallback functions for PHP installed without mbstring support
23 */
24 class Fallback {
25
26 /**
27 * @param $from
28 * @param $to
29 * @param $string
30 * @return string
31 */
32 public static function iconv( $from, $to, $string ) {
33 if ( substr( $to, -8 ) == '//IGNORE' ) {
34 $to = substr( $to, 0, strlen( $to ) - 8 );
35 }
36 if( strcasecmp( $from, $to ) == 0 ) {
37 return $string;
38 }
39 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
40 return utf8_decode( $string );
41 }
42 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
43 return utf8_encode( $string );
44 }
45 return $string;
46 }
47
48 /**
49 * Fallback implementation for mb_substr, hardcoded to UTF-8.
50 * Attempts to be at least _moderately_ efficient; best optimized
51 * for relatively small offset and count values -- about 5x slower
52 * than native mb_string in my testing.
53 *
54 * Larger offsets are still fairly efficient for Latin text, but
55 * can be up to 100x slower than native if the text is heavily
56 * multibyte and we have to slog through a few hundred kb.
57 *
58 * @param $str
59 * @param $start
60 * @param $count string
61 *
62 * @return string
63 */
64 public static function mb_substr( $str, $start, $count = 'end' ) {
65 if( $start != 0 ) {
66 $split = self::mb_substr_split_unicode( $str, intval( $start ) );
67 $str = substr( $str, $split );
68 }
69
70 if( $count !== 'end' ) {
71 $split = self::mb_substr_split_unicode( $str, intval( $count ) );
72 $str = substr( $str, 0, $split );
73 }
74
75 return $str;
76 }
77
78 /**
79 * @param $str
80 * @param $splitPos
81 * @return int
82 */
83 public static function mb_substr_split_unicode( $str, $splitPos ) {
84 if( $splitPos == 0 ) {
85 return 0;
86 }
87
88 $byteLen = strlen( $str );
89
90 if( $splitPos > 0 ) {
91 if( $splitPos > 256 ) {
92 // Optimize large string offsets by skipping ahead N bytes.
93 // This will cut out most of our slow time on Latin-based text,
94 // and 1/2 to 1/3 on East European and Asian scripts.
95 $bytePos = $splitPos;
96 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
97 ++$bytePos;
98 }
99 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
100 } else {
101 $charPos = 0;
102 $bytePos = 0;
103 }
104
105 while( $charPos++ < $splitPos ) {
106 ++$bytePos;
107 // Move past any tail bytes
108 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
109 ++$bytePos;
110 }
111 }
112 } else {
113 $splitPosX = $splitPos + 1;
114 $charPos = 0; // relative to end of string; we don't care about the actual char position here
115 $bytePos = $byteLen;
116 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
117 --$bytePos;
118 // Move past any tail bytes
119 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
120 --$bytePos;
121 }
122 }
123 }
124
125 return $bytePos;
126 }
127
128 /**
129 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
130 * @param string $str
131 * @param string $enc optional encoding; ignored
132 * @return int
133 */
134 public static function mb_strlen( $str, $enc = '' ) {
135 $counts = count_chars( $str );
136 $total = 0;
137
138 // Count ASCII bytes
139 for( $i = 0; $i < 0x80; $i++ ) {
140 $total += $counts[$i];
141 }
142
143 // Count multibyte sequence heads
144 for( $i = 0xc0; $i < 0xff; $i++ ) {
145 $total += $counts[$i];
146 }
147 return $total;
148 }
149
150
151 /**
152 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
153 * @param $haystack String
154 * @param $needle String
155 * @param $offset String: optional start position
156 * @param $encoding String: optional encoding; ignored
157 * @return int
158 */
159 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
160 $needle = preg_quote( $needle, '/' );
161
162 $ar = array();
163 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
164
165 if( isset( $ar[0][1] ) ) {
166 return $ar[0][1];
167 } else {
168 return false;
169 }
170 }
171
172 /**
173 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
174 * @param $haystack String
175 * @param $needle String
176 * @param $offset String: optional start position
177 * @param $encoding String: optional encoding; ignored
178 * @return int
179 */
180 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
181 $needle = preg_quote( $needle, '/' );
182
183 $ar = array();
184 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
185
186 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
187 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
188 return $ar[0][count( $ar[0] ) - 1][1];
189 } else {
190 return false;
191 }
192 }
193
194 /**
195 * Fallback implementation of stream_resolve_include_path()
196 * Native stream_resolve_include_path is available for PHP 5 >= 5.3.2
197 * @param $filename String
198 * @return String
199 */
200 public static function stream_resolve_include_path( $filename ) {
201 $pathArray = explode( PATH_SEPARATOR, get_include_path() );
202 foreach ( $pathArray as $path ) {
203 $fullFilename = $path . DIRECTORY_SEPARATOR . $filename;
204 if ( file_exists( $fullFilename ) ) {
205 return $fullFilename;
206 }
207 }
208 return false;
209 }
210
211 }