Fixing file upload params ($_FILES) normalization. Closes #75
[akelos.git] / vendor / phputf8 / utils / position.php
blobdb8ba3b4a0700fd9a10bbf2566fd217b16982608
1 <?php
2 /**
3 * Locate a byte index given a UTF-8 character index
4 * @version $Id: position.php,v 1.1 2006/10/01 00:01:31 harryf Exp $
5 * @package utf8
6 * @subpackage position
7 */
9 //--------------------------------------------------------------------
10 /**
11 * Given a string and a character index in the string, in
12 * terms of the UTF-8 character position, returns the byte
13 * index of that character. Can be useful when you want to
14 * PHP's native string functions but we warned, locating
15 * the byte can be expensive
16 * Takes variable number of parameters - first must be
17 * the search string then 1 to n UTF-8 character positions
18 * to obtain byte indexes for - it is more efficient to search
19 * the string for multiple characters at once, than make
20 * repeated calls to this function
22 * @author Chris Smith<chris@jalakai.co.uk>
23 * @param string string to locate index in
24 * @param int (n times)
25 * @return mixed - int if only one input int, array if more
26 * @return boolean TRUE if it's all ASCII
27 * @package utf8
28 * @subpackage position
30 function utf8_byte_position() {
32 $args = func_get_args();
33 $str =& array_shift($args);
34 if (!is_string($str)) return false;
36 $result = array();
38 // trivial byte index, character offset pair
39 $prev = array(0,0);
41 // use a short piece of str to estimate bytes per character
42 // $i (& $j) -> byte indexes into $str
43 $i = utf8_locate_next_chr($str, 300);
45 // $c -> character offset into $str
46 $c = strlen(utf8_decode(substr($str,0,$i)));
48 // deal with arguments from lowest to highest
49 sort($args);
51 foreach ($args as $offset) {
52 // sanity checks FIXME
54 // 0 is an easy check
55 if ($offset == 0) { $result[] = 0; continue; }
57 // ensure no endless looping
58 $safety_valve = 50;
60 do {
62 if ( ($c - $prev[1]) == 0 ) {
63 // Hack: gone past end of string
64 $error = 0;
65 $i = strlen($str);
66 break;
69 $j = $i + (int)(($offset-$c) * ($i - $prev[0]) / ($c - $prev[1]));
71 // correct to utf8 character boundary
72 $j = utf8_locate_next_chr($str, $j);
74 // save the index, offset for use next iteration
75 $prev = array($i,$c);
77 if ($j > $i) {
78 // determine new character offset
79 $c += strlen(utf8_decode(substr($str,$i,$j-$i)));
80 } else {
81 // ditto
82 $c -= strlen(utf8_decode(substr($str,$j,$i-$j)));
85 $error = abs($c-$offset);
87 // ready for next time around
88 $i = $j;
90 // from 7 it is faster to iterate over the string
91 } while ( ($error > 7) && --$safety_valve) ;
93 if ($error && $error <= 7) {
95 if ($c < $offset) {
96 // move up
97 while ($error--) { $i = utf8_locate_next_chr($str,++$i); }
98 } else {
99 // move down
100 while ($error--) { $i = utf8_locate_current_chr($str,--$i); }
103 // ready for next arg
104 $c = $offset;
106 $result[] = $i;
109 if ( count($result) == 1 ) {
110 return $result[0];
113 return $result;
116 //--------------------------------------------------------------------
118 * Given a string and any byte index, returns the byte index
119 * of the start of the current UTF-8 character, relative to supplied
120 * position. If the current character begins at the same place as the
121 * supplied byte index, that byte index will be returned. Otherwise
122 * this function will step backwards, looking for the index where
123 * curent UTF-8 character begins
124 * @author Chris Smith<chris@jalakai.co.uk>
125 * @param string
126 * @param int byte index in the string
127 * @return int byte index of start of next UTF-8 character
128 * @package utf8
129 * @subpackage position
131 function utf8_locate_current_chr( &$str, $idx ) {
133 if ($idx <= 0) return 0;
135 $limit = strlen($str);
136 if ($idx >= $limit) return $limit;
138 // Binary value for any byte after the first in a multi-byte UTF-8 character
139 // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
140 // of byte - assuming well formed UTF-8
141 while ($idx && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx--;
143 return $idx;
146 //--------------------------------------------------------------------
148 * Given a string and any byte index, returns the byte index
149 * of the start of the next UTF-8 character, relative to supplied
150 * position. If the next character begins at the same place as the
151 * supplied byte index, that byte index will be returned.
152 * @author Chris Smith<chris@jalakai.co.uk>
153 * @param string
154 * @param int byte index in the string
155 * @return int byte index of start of next UTF-8 character
156 * @package utf8
157 * @subpackage position
159 function utf8_locate_next_chr( &$str, $idx ) {
161 if ($idx <= 0) return 0;
163 $limit = strlen($str);
164 if ($idx >= $limit) return $limit;
166 // Binary value for any byte after the first in a multi-byte UTF-8 character
167 // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
168 // of byte - assuming well formed UTF-8
169 while (($idx < $limit) && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx++;
171 return $idx;