New records could not hold multiple habtm associates using ::set(). Fixes #160.
[akelos.git] / vendor / phputf8 / ord.php
blob79f13dcb505895cba25ff77fa5742de542c9f0ea
1 <?php
2 /**
3 * @version $Id: ord.php,v 1.4 2006/09/11 15:22:54 harryf Exp $
4 * @package utf8
5 * @subpackage strings
6 */
8 //---------------------------------------------------------------
9 /**
10 * UTF-8 aware alternative to ord
11 * Returns the unicode ordinal for a character
12 * @param string UTF-8 encoded character
13 * @return int unicode ordinal for the character
14 * @see http://www.php.net/ord
15 * @see http://www.php.net/manual/en/function.ord.php#46267
17 function utf8_ord($chr) {
19 $ord0 = ord($chr);
21 if ( $ord0 >= 0 && $ord0 <= 127 ) {
22 return $ord0;
25 if ( !isset($chr{1}) ) {
26 trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
27 return FALSE;
30 $ord1 = ord($chr{1});
31 if ( $ord0 >= 192 && $ord0 <= 223 ) {
32 return ( $ord0 - 192 ) * 64
33 + ( $ord1 - 128 );
36 if ( !isset($chr{2}) ) {
37 trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
38 return FALSE;
40 $ord2 = ord($chr{2});
41 if ( $ord0 >= 224 && $ord0 <= 239 ) {
42 return ($ord0-224)*4096
43 + ($ord1-128)*64
44 + ($ord2-128);
47 if ( !isset($chr{3}) ) {
48 trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
49 return FALSE;
51 $ord3 = ord($chr{3});
52 if ($ord0>=240 && $ord0<=247) {
53 return ($ord0-240)*262144
54 + ($ord1-128)*4096
55 + ($ord2-128)*64
56 + ($ord3-128);
60 if ( !isset($chr{4}) ) {
61 trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
62 return FALSE;
64 $ord4 = ord($chr{4});
65 if ($ord0>=248 && $ord0<=251) {
66 return ($ord0-248)*16777216
67 + ($ord1-128)*262144
68 + ($ord2-128)*4096
69 + ($ord3-128)*64
70 + ($ord4-128);
73 if ( !isset($chr{5}) ) {
74 trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
75 return FALSE;
77 if ($ord0>=252 && $ord0<=253) {
78 return ($ord0-252) * 1073741824
79 + ($ord1-128)*16777216
80 + ($ord2-128)*262144
81 + ($ord3-128)*4096
82 + ($ord4-128)*64
83 + (ord($c{5})-128);
86 if ( $ord0 >= 254 && $ord0 <= 255 ) {
87 trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
88 return FALSE;