Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * Hoa
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2007-2017, Hoa community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
 
37
namespace Psy\Readline\Hoa;
38
 
39
/**
40
 * This class represents a UTF-8 string.
41
 * Please, see:
42
 *   * http://www.ietf.org/rfc/rfc3454.txt,
43
 *   * http://unicode.org/reports/tr9/,
44
 *   * http://www.unicode.org/Public/6.0.0/ucd/UnicodeData.txt.
45
 */
46
class Ustring
47
{
48
    /**
49
     * Check if ext/mbstring is available.
50
     */
51
    public static function checkMbString(): bool
52
    {
53
        return \function_exists('mb_substr');
54
    }
55
 
56
    /**
57
     * Get the number of column positions of a wide-character.
58
     *
59
     * This is a PHP implementation of wcwidth() and wcswidth() (defined in IEEE
60
     * Std 1002.1-2001) for Unicode, by Markus Kuhn. Please, see
61
     * http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c.
62
     *
63
     * The wcwidth(wc) function shall either return 0 (if wc is a null
64
     * wide-character code), or return the number of column positions to be
65
     * occupied by the wide-character code wc, or return -1 (if wc does not
66
     * correspond to a printable wide-character code).
67
     */
68
    public static function getCharWidth(string $char): int
69
    {
70
        $char = (string) $char;
71
        $c = static::toCode($char);
72
 
73
        // Test for 8-bit control characters.
74
        if (0x0 === $c) {
75
            return 0;
76
        }
77
 
78
        if (0x20 > $c || (0x7F <= $c && $c < 0xA0)) {
79
            return -1;
80
        }
81
 
82
        // Non-spacing characters.
83
        if (0xAD !== $c &&
84
 
85
            return 0;
86
        }
87
 
88
        // If we arrive here, $c is not a combining C0/C1 control character.
89
        return 1 +
90
            (0x1100 <= $c &&
91
                (0x115F >= $c ||                        // Hangul Jamo init. consonants
92
                 0x2329 === $c || 0x232A === $c ||
93
                     (0x2E80 <= $c && 0xA4CF >= $c &&
94
                      0x303F !== $c) ||                // CJK…Yi
95
                     (0xAC00 <= $c && 0xD7A3 >= $c) || // Hangul Syllables
96
                     (0xF900 <= $c && 0xFAFF >= $c) || // CJK Compatibility Ideographs
97
                     (0xFE10 <= $c && 0xFE19 >= $c) || // Vertical forms
98
                     (0xFE30 <= $c && 0xFE6F >= $c) || // CJK Compatibility Forms
99
                     (0xFF00 <= $c && 0xFF60 >= $c) || // Fullwidth Forms
100
                     (0xFFE0 <= $c && 0xFFE6 >= $c) ||
101
                     (0x20000 <= $c && 0x2FFFD >= $c) ||
102
                     (0x30000 <= $c && 0x3FFFD >= $c)));
103
    }
104
 
105
    /**
106
     * Check whether the character is printable or not.
107
     */
108
    public static function isCharPrintable(string $char): bool
109
    {
110
        return 1 <= static::getCharWidth($char);
111
    }
112
 
113
    /**
114
     * Get a decimal code representation of a specific character.
115
     */
116
    public static function toCode(string $char): int
117
    {
118
        $char = (string) $char;
119
        $code = \ord($char[0]);
120
        $bytes = 1;
121
 
122
        if (!($code & 0x80)) { // 0xxxxxxx
123
            return $code;
124
        }
125
 
126
        if (($code & 0xE0) === 0xC0) { // 110xxxxx
127
            $bytes = 2;
128
            $code = $code & ~0xC0;
129
        } elseif (($code & 0xF0) === 0xE0) { // 1110xxxx
130
            $bytes = 3;
131
            $code = $code & ~0xE0;
132
        } elseif (($code & 0xF8) === 0xF0) { // 11110xxx
133
            $bytes = 4;
134
            $code = $code & ~0xF0;
135
        }
136
 
137
        for ($i = 2; $i <= $bytes; $i++) { // 10xxxxxx
138
            $code = ($code << 6) + (\ord($char[$i - 1]) & ~0x80);
139
        }
140
 
141
        return $code;
142
    }
143
}