Merge "Improving some import-related error messages"
[mediawiki.git] / includes / Fallback.php
blob8e7f4b7e27c11c04f1bd04f9955d07cd81822f1c
1 <?php
2 /**
3 * Fallback functions for PHP installed without mbstring support.
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.
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.
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
20 * @file
23 /**
24 * Fallback functions for PHP installed without mbstring support
26 class Fallback {
28 /**
29 * Fallback implementation for mb_substr, hardcoded to UTF-8.
30 * Attempts to be at least _moderately_ efficient; best optimized
31 * for relatively small offset and count values -- about 5x slower
32 * than native mb_string in my testing.
34 * Larger offsets are still fairly efficient for Latin text, but
35 * can be up to 100x slower than native if the text is heavily
36 * multibyte and we have to slog through a few hundred kb.
38 * @param string $str
39 * @param int $start
40 * @param string $count
42 * @return string
44 public static function mb_substr( $str, $start, $count = 'end' ) {
45 if ( $start != 0 ) {
46 $split = self::mb_substr_split_unicode( $str, intval( $start ) );
47 $str = substr( $str, $split );
50 if ( $count !== 'end' ) {
51 $split = self::mb_substr_split_unicode( $str, intval( $count ) );
52 $str = substr( $str, 0, $split );
55 return $str;
58 /**
59 * @param string $str
60 * @param int $splitPos
61 * @return int
63 public static function mb_substr_split_unicode( $str, $splitPos ) {
64 if ( $splitPos == 0 ) {
65 return 0;
68 $byteLen = strlen( $str );
70 if ( $splitPos > 0 ) {
71 if ( $splitPos > 256 ) {
72 // Optimize large string offsets by skipping ahead N bytes.
73 // This will cut out most of our slow time on Latin-based text,
74 // and 1/2 to 1/3 on East European and Asian scripts.
75 $bytePos = $splitPos;
76 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
77 ++$bytePos;
79 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
80 } else {
81 $charPos = 0;
82 $bytePos = 0;
85 while ( $charPos++ < $splitPos ) {
86 ++$bytePos;
87 // Move past any tail bytes
88 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
89 ++$bytePos;
92 } else {
93 $splitPosX = $splitPos + 1;
94 $charPos = 0; // relative to end of string; we don't care about the actual char position here
95 $bytePos = $byteLen;
96 while ( $bytePos > 0 && $charPos-- >= $splitPosX ) {
97 --$bytePos;
98 // Move past any tail bytes
99 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
100 --$bytePos;
105 return $bytePos;
109 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
110 * @param string $str
111 * @param string $enc Optional encoding; ignored
112 * @return int
114 public static function mb_strlen( $str, $enc = '' ) {
115 $counts = count_chars( $str );
116 $total = 0;
118 // Count ASCII bytes
119 for ( $i = 0; $i < 0x80; $i++ ) {
120 $total += $counts[$i];
123 // Count multibyte sequence heads
124 for ( $i = 0xc0; $i < 0xff; $i++ ) {
125 $total += $counts[$i];
127 return $total;
131 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
132 * @param string $haystack
133 * @param string $needle
134 * @param string $offset Optional start position
135 * @param string $encoding Optional encoding; ignored
136 * @return int
138 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
139 $needle = preg_quote( $needle, '/' );
141 $ar = array();
142 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
144 if ( isset( $ar[0][1] ) ) {
145 return $ar[0][1];
146 } else {
147 return false;
152 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
153 * @param string $haystack
154 * @param string $needle
155 * @param string $offset Optional start position
156 * @param string $encoding Optional encoding; ignored
157 * @return int
159 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
160 $needle = preg_quote( $needle, '/' );
162 $ar = array();
163 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
165 if ( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
166 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
167 return $ar[0][count( $ar[0] ) - 1][1];
168 } else {
169 return false;