Yet-Another-PHP-Framework /system/utf8/ord.php

Language PHP Lines 89
MD5 Hash 78c52212e233c81ccc2b45d5e32a10fe Estimated Cost $4,829 (why?)
Repository https://github.com/avidenie/Yet-Another-PHP-Framework.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * This file is part of the Yet Another PHP Framework package.
 * (c) 2010 Adrian Videnie <{@link mailto:avidenie@gmail.com avidenie@gmail.com}>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package    Framework
 * @subpackage I18n
 * @author     Adrian Videnie <avidenie@gmail.com>
 * @copyright  2010 Adrian Videnie
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT license
 */

/**
 * Returns the unicode ordinal for a character.
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 *
 * @param  string  A character.
 * @return integer The unicode ordinal for a character.
 */
function _ord($chr)
{
    $ord0 = ord($chr);

    if (0 <= $ord0 && $ord0 <= 127) {
        return $ord0;
    }

    if (!isset($chr[1])) {
        trigger_error('Short sequence - at least 2 bytes expected, only 1 seen', E_USER_WARNING);
        return false;
    }

    $ord1 = ord($chr[1]);

    if (192 <= $ord0 && $ord0 <= 223) {
        return ($ord0 - 192) * 64 + ($ord1 - 128);
    }

    if (!isset($chr[2])) {
        trigger_error('Short sequence - at least 3 bytes expected, only 2 seen', E_USER_WARNING);
        return false;
    }

    $ord2 = ord($chr[2]);

    if (224 <= $ord0 && $ord0 <= 239) {
        return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
    }

    if (!isset($chr[3])) {
        trigger_error('Short sequence - at least 4 bytes expected, only 3 seen', E_USER_WARNING);
        return false;
    }

    $ord3 = ord($chr[3]);

    if (240 <= $ord0 && $ord0 <= 247) {
        return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
    }

    if (!isset($chr[4])) {
        trigger_error('Short sequence - at least 5 bytes expected, only 4 seen', E_USER_WARNING);
        return false;
    }

    $ord4 = ord($chr[4]);

    if (248 <= $ord0 && $ord0 <= 251) {
        return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
    }

    if (!isset($chr[5])) {
        trigger_error('Short sequence - at least 6 bytes expected, only 5 seen', E_USER_WARNING);
        return false;
    }

    if (252 <= $ord0 && $ord0 <= 253) {
        return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
    }
 
    if (254 <= $ord0 && $ord0 <= 255) {
        trigger_error('Invalid UTF-8 with surrogate ordinal ' . $ord0, E_USER_WARNING);
        return false;
    }
}
Back to Top