Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Util;
13
 
14
/**
15
 * String utility methods.
16
 *
17
 * @author ju1ius
18
 */
19
class Str
20
{
21
    const UNVIS_RX = <<<'EOS'
22
/
23
    \\(?:
24
        ((?:040)|s)
25
        | (240)
26
        | (?: M-(.) )
27
        | (?: M\^(.) )
28
        | (?: \^(.) )
29
    )
30
/xS
31
EOS;
32
 
33
    /**
34
     * Decodes a string encoded by libsd's strvis.
35
     *
36
     * From `man 3 vis`:
37
     *
38
     * Use an ‘M’ to represent meta characters (characters with the 8th bit set),
39
     * and use a caret ‘^’ to represent control characters (see iscntrl(3)).
40
     * The following formats are used:
41
     *
42
     *      \040    Represents ASCII space.
43
     *
44
     *      \240    Represents Meta-space (&nbsp in HTML).
45
     *
46
     *      \M-C    Represents character ‘C’ with the 8th bit set.
47
     *              Spans characters ‘\241’ through ‘\376’.
48
     *
49
     *      \M^C    Represents control character ‘C’ with the 8th bit set.
50
     *              Spans characters ‘\200’ through ‘\237’, and ‘\377’ (as ‘\M^?’).
51
     *
52
     *      \^C     Represents the control character ‘C’.
53
     *              Spans characters ‘\000’ through ‘\037’, and ‘\177’ (as ‘\^?’).
54
     *
55
     * The other formats are supported by PHP's stripcslashes,
56
     * except for the \s sequence (ASCII space).
57
     *
58
     * @param string $input The string to decode
59
     */
60
    public static function unvis(string $input): string
61
    {
200 lars 62
        $output = \preg_replace_callback(self::UNVIS_RX, [self::class, 'unvisReplace'], $input);
148 lars 63
        // other escapes & octal are handled by stripcslashes
64
        return \stripcslashes($output);
65
    }
66
 
67
    /**
68
     * Callback for Str::unvis.
69
     *
70
     * @param array $match The matches passed by preg_replace_callback
71
     */
72
    protected static function unvisReplace(array $match): string
73
    {
74
        // \040, \s
75
        if (!empty($match[1])) {
76
            return "\x20";
77
        }
78
        // \240
79
        if (!empty($match[2])) {
80
            return "\xa0";
81
        }
82
        // \M-(.)
83
        if (isset($match[3]) && $match[3] !== '') {
84
            $chr = $match[3];
85
            // unvis S_META1
86
            $cp = 0200;
87
            $cp |= \ord($chr);
88
 
89
            return \chr($cp);
90
        }
91
        // \M^(.)
92
        if (isset($match[4]) && $match[4] !== '') {
93
            $chr = $match[4];
94
            // unvis S_META | S_CTRL
95
            $cp = 0200;
96
            $cp |= ($chr === '?') ? 0177 : \ord($chr) & 037;
97
 
98
            return \chr($cp);
99
        }
100
        // \^(.)
101
        if (isset($match[5]) && $match[5] !== '') {
102
            $chr = $match[5];
103
            // unvis S_CTRL
104
            $cp = 0;
105
            $cp |= ($chr === '?') ? 0177 : \ord($chr) & 037;
106
 
107
            return \chr($cp);
108
        }
109
    }
110
}