PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phputf8/utils/position.php

https://github.com/dianaprajescu/joomla-framework
PHP | 168 lines | 59 code | 35 blank | 74 comment | 27 complexity | 7b6a37784ce41f853eaeae586faefc1d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Locate a byte index given a UTF-8 character index
  4. * @package utf8
  5. */
  6. //--------------------------------------------------------------------
  7. /**
  8. * Given a string and a character index in the string, in
  9. * terms of the UTF-8 character position, returns the byte
  10. * index of that character. Can be useful when you want to
  11. * PHP's native string functions but we warned, locating
  12. * the byte can be expensive
  13. * Takes variable number of parameters - first must be
  14. * the search string then 1 to n UTF-8 character positions
  15. * to obtain byte indexes for - it is more efficient to search
  16. * the string for multiple characters at once, than make
  17. * repeated calls to this function
  18. *
  19. * @author Chris Smith<chris@jalakai.co.uk>
  20. * @param string string to locate index in
  21. * @param int (n times)
  22. * @return mixed - int if only one input int, array if more
  23. * @return boolean TRUE if it's all ASCII
  24. * @package utf8
  25. */
  26. function utf8_byte_position() {
  27. $args = func_get_args();
  28. $str =& array_shift($args);
  29. if (!is_string($str)) return false;
  30. $result = array();
  31. // trivial byte index, character offset pair
  32. $prev = array(0,0);
  33. // use a short piece of str to estimate bytes per character
  34. // $i (& $j) -> byte indexes into $str
  35. $i = utf8_locate_next_chr($str, 300);
  36. // $c -> character offset into $str
  37. $c = strlen(utf8_decode(substr($str,0,$i)));
  38. // deal with arguments from lowest to highest
  39. sort($args);
  40. foreach ($args as $offset) {
  41. // sanity checks FIXME
  42. // 0 is an easy check
  43. if ($offset == 0) { $result[] = 0; continue; }
  44. // ensure no endless looping
  45. $safety_valve = 50;
  46. do {
  47. if ( ($c - $prev[1]) == 0 ) {
  48. // Hack: gone past end of string
  49. $error = 0;
  50. $i = strlen($str);
  51. break;
  52. }
  53. $j = $i + (int)(($offset-$c) * ($i - $prev[0]) / ($c - $prev[1]));
  54. // correct to utf8 character boundary
  55. $j = utf8_locate_next_chr($str, $j);
  56. // save the index, offset for use next iteration
  57. $prev = array($i,$c);
  58. if ($j > $i) {
  59. // determine new character offset
  60. $c += strlen(utf8_decode(substr($str,$i,$j-$i)));
  61. } else {
  62. // ditto
  63. $c -= strlen(utf8_decode(substr($str,$j,$i-$j)));
  64. }
  65. $error = abs($c-$offset);
  66. // ready for next time around
  67. $i = $j;
  68. // from 7 it is faster to iterate over the string
  69. } while ( ($error > 7) && --$safety_valve) ;
  70. if ($error && $error <= 7) {
  71. if ($c < $offset) {
  72. // move up
  73. while ($error--) { $i = utf8_locate_next_chr($str,++$i); }
  74. } else {
  75. // move down
  76. while ($error--) { $i = utf8_locate_current_chr($str,--$i); }
  77. }
  78. // ready for next arg
  79. $c = $offset;
  80. }
  81. $result[] = $i;
  82. }
  83. if ( count($result) == 1 ) {
  84. return $result[0];
  85. }
  86. return $result;
  87. }
  88. //--------------------------------------------------------------------
  89. /**
  90. * Given a string and any byte index, returns the byte index
  91. * of the start of the current UTF-8 character, relative to supplied
  92. * position. If the current character begins at the same place as the
  93. * supplied byte index, that byte index will be returned. Otherwise
  94. * this function will step backwards, looking for the index where
  95. * curent UTF-8 character begins
  96. * @author Chris Smith<chris@jalakai.co.uk>
  97. * @param string
  98. * @param int byte index in the string
  99. * @return int byte index of start of next UTF-8 character
  100. * @package utf8
  101. */
  102. function utf8_locate_current_chr( &$str, $idx ) {
  103. if ($idx <= 0) return 0;
  104. $limit = strlen($str);
  105. if ($idx >= $limit) return $limit;
  106. // Binary value for any byte after the first in a multi-byte UTF-8 character
  107. // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
  108. // of byte - assuming well formed UTF-8
  109. while ($idx && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx--;
  110. return $idx;
  111. }
  112. //--------------------------------------------------------------------
  113. /**
  114. * Given a string and any byte index, returns the byte index
  115. * of the start of the next UTF-8 character, relative to supplied
  116. * position. If the next character begins at the same place as the
  117. * supplied byte index, that byte index will be returned.
  118. * @author Chris Smith<chris@jalakai.co.uk>
  119. * @param string
  120. * @param int byte index in the string
  121. * @return int byte index of start of next UTF-8 character
  122. * @package utf8
  123. */
  124. function utf8_locate_next_chr( &$str, $idx ) {
  125. if ($idx <= 0) return 0;
  126. $limit = strlen($str);
  127. if ($idx >= $limit) return $limit;
  128. // Binary value for any byte after the first in a multi-byte UTF-8 character
  129. // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
  130. // of byte - assuming well formed UTF-8
  131. while (($idx < $limit) && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx++;
  132. return $idx;
  133. }