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
26 public static function iconv( $from, $to, $string ) {
27 if ( substr( $to, -8 ) == '//IGNORE' ) {
28 $to = substr( $to, 0, strlen( $to ) - 8 );
30 if( strcasecmp( $from, $to ) == 0 ) {
33 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
34 return utf8_decode( $string );
36 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
37 return utf8_encode( $string );
43 * Fallback implementation for mb_substr, hardcoded to UTF-8.
44 * Attempts to be at least _moderately_ efficient; best optimized
45 * for relatively small offset and count values -- about 5x slower
46 * than native mb_string in my testing.
48 * Larger offsets are still fairly efficient for Latin text, but
49 * can be up to 100x slower than native if the text is heavily
50 * multibyte and we have to slog through a few hundred kb.
54 * @param $count string
58 public static function mb_substr( $str, $start, $count = 'end' ) {
60 $split = self
::mb_substr_split_unicode( $str, intval( $start ) );
61 $str = substr( $str, $split );
64 if( $count !== 'end' ) {
65 $split = self
::mb_substr_split_unicode( $str, intval( $count ) );
66 $str = substr( $str, 0, $split );
72 public static function mb_substr_split_unicode( $str, $splitPos ) {
73 if( $splitPos == 0 ) {
77 $byteLen = strlen( $str );
80 if( $splitPos > 256 ) {
81 // Optimize large string offsets by skipping ahead N bytes.
82 // This will cut out most of our slow time on Latin-based text,
83 // and 1/2 to 1/3 on East European and Asian scripts.
85 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
88 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
94 while( $charPos++
< $splitPos ) {
96 // Move past any tail bytes
97 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
102 $splitPosX = $splitPos +
1;
103 $charPos = 0; // relative to end of string; we don't care about the actual char position here
105 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
107 // Move past any tail bytes
108 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
118 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
120 * @param string $enc optional encoding; ignored
123 public static function mb_strlen( $str, $enc = '' ) {
124 $counts = count_chars( $str );
128 for( $i = 0; $i < 0x80; $i++
) {
129 $total +
= $counts[$i];
132 // Count multibyte sequence heads
133 for( $i = 0xc0; $i < 0xff; $i++
) {
134 $total +
= $counts[$i];
141 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
142 * @param $haystack String
143 * @param $needle String
144 * @param $offset String: optional start position
145 * @param $encoding String: optional encoding; ignored
148 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
149 $needle = preg_quote( $needle, '/' );
152 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
154 if( isset( $ar[0][1] ) ) {
162 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
163 * @param $haystack String
164 * @param $needle String
165 * @param $offset String: optional start position
166 * @param $encoding String: optional encoding; ignored
169 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
170 $needle = preg_quote( $needle, '/' );
173 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
175 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
176 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
177 return $ar[0][count( $ar[0] ) - 1][1];
184 * Fallback implementation of stream_resolve_include_path()
185 * Native stream_resolve_include_path is available for PHP 5 >= 5.3.2
186 * @param $filename String
189 public static function stream_resolve_include_path( $filename ) {
190 $pathArray = explode( PATH_SEPARATOR
, get_include_path() );
191 foreach ( $pathArray as $path ) {
192 $fullFilename = $path . DIRECTORY_SEPARATOR
. $filename;
193 if ( file_exists( $fullFilename ) ) {
194 return $fullFilename;