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
24 * Fallback functions for PHP installed without mbstring support
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.
40 * @param string $count
44 public static function mb_substr( $str, $start, $count = 'end' ) {
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 );
60 * @param int $splitPos
63 public static function mb_substr_split_unicode( $str, $splitPos ) {
64 if ( $splitPos == 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.
76 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
79 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
85 while ( $charPos++
< $splitPos ) {
87 // Move past any tail bytes
88 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
93 $splitPosX = $splitPos +
1;
94 $charPos = 0; // relative to end of string; we don't care about the actual char position here
96 while ( $bytePos > 0 && $charPos-- >= $splitPosX ) {
98 // Move past any tail bytes
99 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
109 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
111 * @param string $enc Optional encoding; ignored
114 public static function mb_strlen( $str, $enc = '' ) {
115 $counts = count_chars( $str );
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];
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
138 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
139 $needle = preg_quote( $needle, '/' );
142 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
144 if ( isset( $ar[0][1] ) ) {
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
159 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
160 $needle = preg_quote( $needle, '/' );
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];