4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 * String utility methods.
21 const UNVIS_RX = <<<'EOS'
34 * Decodes a string encoded by libsd's strvis.
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:
42 * \040 Represents ASCII space.
44 * \240 Represents Meta-space (  in HTML).
46 * \M-C Represents character ‘C’ with the 8th bit set.
47 * Spans characters ‘\241’ through ‘\376’.
49 * \M^C Represents control character ‘C’ with the 8th bit set.
50 * Spans characters ‘\200’ through ‘\237’, and ‘\377’ (as ‘\M^?’).
52 * \^C Represents the control character ‘C’.
53 * Spans characters ‘\000’ through ‘\037’, and ‘\177’ (as ‘\^?’).
55 * The other formats are supported by PHP's stripcslashes,
56 * except for the \s sequence (ASCII space).
58 * @param string $input The string to decode
62 public static function unvis($input)
64 $output = preg_replace_callback(self::UNVIS_RX, 'self::unvisReplace', $input);
65 // other escapes & octal are handled by stripcslashes
66 return stripcslashes($output);
70 * Callback for Str::unvis.
72 * @param array $match The matches passed by preg_replace_callback
76 protected static function unvisReplace($match)
79 if (!empty($match[1])) {
83 if (!empty($match[2])) {
87 if (isset($match[3]) && $match[3] !== '') {
96 if (isset($match[4]) && $match[4] !== '') {
98 // unvis S_META | S_CTRL
100 $cp |= ($chr === '?') ? 0177 : ord($chr) & 037;
105 if (isset($match[5]) && $match[5] !== '') {
109 $cp |= ($chr === '?') ? 0177 : ord($chr) & 037;