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.
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.
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
22 * Fallback functions for PHP installed without mbstring support
32 public static function iconv( $from, $to, $string ) {
33 if ( substr( $to, -8 ) == '//IGNORE' ) {
34 $to = substr( $to, 0, strlen( $to ) - 8 );
36 if( strcasecmp( $from, $to ) == 0 ) {
39 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
40 return utf8_decode( $string );
42 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
43 return utf8_encode( $string );
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.
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.
60 * @param $count string
64 public static function mb_substr( $str, $start, $count = 'end' ) {
66 $split = self
::mb_substr_split_unicode( $str, intval( $start ) );
67 $str = substr( $str, $split );
70 if( $count !== 'end' ) {
71 $split = self
::mb_substr_split_unicode( $str, intval( $count ) );
72 $str = substr( $str, 0, $split );
83 public static function mb_substr_split_unicode( $str, $splitPos ) {
84 if( $splitPos == 0 ) {
88 $byteLen = strlen( $str );
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.
96 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
99 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
105 while( $charPos++
< $splitPos ) {
107 // Move past any tail bytes
108 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
113 $splitPosX = $splitPos +
1;
114 $charPos = 0; // relative to end of string; we don't care about the actual char position here
116 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
118 // Move past any tail bytes
119 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
129 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
131 * @param string $enc optional encoding; ignored
134 public static function mb_strlen( $str, $enc = '' ) {
135 $counts = count_chars( $str );
139 for( $i = 0; $i < 0x80; $i++
) {
140 $total +
= $counts[$i];
143 // Count multibyte sequence heads
144 for( $i = 0xc0; $i < 0xff; $i++
) {
145 $total +
= $counts[$i];
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
159 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
160 $needle = preg_quote( $needle, '/' );
163 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
165 if( isset( $ar[0][1] ) ) {
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
180 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
181 $needle = preg_quote( $needle, '/' );
184 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
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];
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
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;