Merge branch 'maint/7.0'
[ninja.git] / system / core / utf8 / ord.php
blob29723b6bb0e708b6a81e143811efd9230cee0fcc
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * utf8::ord
5 * @package Core
6 * @author Kohana Team
7 * @copyright (c) 2007 Kohana Team
8 * @copyright (c) 2005 Harry Fuecks
9 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
11 function _ord($chr)
13 $ord0 = ord($chr);
15 if ($ord0 >= 0 AND $ord0 <= 127)
17 return $ord0;
20 if ( ! isset($chr[1]))
22 trigger_error('Short sequence - at least 2 bytes expected, only 1 seen', E_USER_WARNING);
23 return FALSE;
26 $ord1 = ord($chr[1]);
28 if ($ord0 >= 192 AND $ord0 <= 223)
30 return ($ord0 - 192) * 64 + ($ord1 - 128);
33 if ( ! isset($chr[2]))
35 trigger_error('Short sequence - at least 3 bytes expected, only 2 seen', E_USER_WARNING);
36 return FALSE;
39 $ord2 = ord($chr[2]);
41 if ($ord0 >= 224 AND $ord0 <= 239)
43 return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
46 if ( ! isset($chr[3]))
48 trigger_error('Short sequence - at least 4 bytes expected, only 3 seen', E_USER_WARNING);
49 return FALSE;
52 $ord3 = ord($chr[3]);
54 if ($ord0 >= 240 AND $ord0 <= 247)
56 return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
59 if ( ! isset($chr[4]))
61 trigger_error('Short sequence - at least 5 bytes expected, only 4 seen', E_USER_WARNING);
62 return FALSE;
65 $ord4 = ord($chr[4]);
67 if ($ord0 >= 248 AND $ord0 <= 251)
69 return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
72 if ( ! isset($chr[5]))
74 trigger_error('Short sequence - at least 6 bytes expected, only 5 seen', E_USER_WARNING);
75 return FALSE;
78 if ($ord0 >= 252 AND $ord0 <= 253)
80 return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($c[5]) - 128);
83 if ($ord0 >= 254 AND $ord0 <= 255)
85 trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0, E_USER_WARNING);
86 return FALSE;