Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / psy / psysh / src / Util / Str.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
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      * @return string
61      */
62     public static function unvis($input)
63     {
64         $output = preg_replace_callback(self::UNVIS_RX, 'self::unvisReplace', $input);
65         // other escapes & octal are handled by stripcslashes
66         return stripcslashes($output);
67     }
68
69     /**
70      * Callback for Str::unvis.
71      *
72      * @param array $match The matches passed by preg_replace_callback
73      *
74      * @return string
75      */
76     protected static function unvisReplace($match)
77     {
78         // \040, \s
79         if (!empty($match[1])) {
80             return "\x20";
81         }
82         // \240
83         if (!empty($match[2])) {
84             return "\xa0";
85         }
86         // \M-(.)
87         if (isset($match[3]) && $match[3] !== '') {
88             $chr = $match[3];
89             // unvis S_META1
90             $cp = 0200;
91             $cp |= ord($chr);
92
93             return chr($cp);
94         }
95         // \M^(.)
96         if (isset($match[4]) && $match[4] !== '') {
97             $chr = $match[4];
98             // unvis S_META | S_CTRL
99             $cp = 0200;
100             $cp |= ($chr === '?') ? 0177 : ord($chr) & 037;
101
102             return chr($cp);
103         }
104         // \^(.)
105         if (isset($match[5]) && $match[5] !== '') {
106             $chr = $match[5];
107             // unvis S_CTRL
108             $cp = 0;
109             $cp |= ($chr === '?') ? 0177 : ord($chr) & 037;
110
111             return chr($cp);
112         }
113     }
114 }