[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/ -> Fallback.php (source)

   1  <?php
   2  /**
   3   * Fallback functions for PHP installed without mbstring support.
   4   *
   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.
   9   *
  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.
  14   *
  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
  19   *
  20   * @file
  21   */
  22  
  23  /**
  24   * Fallback functions for PHP installed without mbstring support
  25   */
  26  class Fallback {
  27  
  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.
  33       *
  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.
  37       *
  38       * @param string $str
  39       * @param int $start
  40       * @param string $count
  41       *
  42       * @return string
  43       */
  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 );
  48          }
  49  
  50          if ( $count !== 'end' ) {
  51              $split = self::mb_substr_split_unicode( $str, intval( $count ) );
  52              $str = substr( $str, 0, $split );
  53          }
  54  
  55          return $str;
  56      }
  57  
  58      /**
  59       * @param string $str
  60       * @param int $splitPos
  61       * @return int
  62       */
  63  	public static function mb_substr_split_unicode( $str, $splitPos ) {
  64          if ( $splitPos == 0 ) {
  65              return 0;
  66          }
  67  
  68          $byteLen = strlen( $str );
  69  
  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;
  78                  }
  79                  $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
  80              } else {
  81                  $charPos = 0;
  82                  $bytePos = 0;
  83              }
  84  
  85              while ( $charPos++ < $splitPos ) {
  86                  ++$bytePos;
  87                  // Move past any tail bytes
  88                  while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
  89                      ++$bytePos;
  90                  }
  91              }
  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;
 101                  }
 102              }
 103          }
 104  
 105          return $bytePos;
 106      }
 107  
 108      /**
 109       * Fallback implementation of mb_strlen, hardcoded to UTF-8.
 110       * @param string $str
 111       * @param string $enc Optional encoding; ignored
 112       * @return int
 113       */
 114  	public static function mb_strlen( $str, $enc = '' ) {
 115          $counts = count_chars( $str );
 116          $total = 0;
 117  
 118          // Count ASCII bytes
 119          for ( $i = 0; $i < 0x80; $i++ ) {
 120              $total += $counts[$i];
 121          }
 122  
 123          // Count multibyte sequence heads
 124          for ( $i = 0xc0; $i < 0xff; $i++ ) {
 125              $total += $counts[$i];
 126          }
 127          return $total;
 128      }
 129  
 130      /**
 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
 137       */
 138  	public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
 139          $needle = preg_quote( $needle, '/' );
 140  
 141          $ar = array();
 142          preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
 143  
 144          if ( isset( $ar[0][1] ) ) {
 145              return $ar[0][1];
 146          } else {
 147              return false;
 148          }
 149      }
 150  
 151      /**
 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
 158       */
 159  	public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
 160          $needle = preg_quote( $needle, '/' );
 161  
 162          $ar = array();
 163          preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
 164  
 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;
 170          }
 171      }
 172  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1