| 1 |
<?php |
<?php |
| 2 |
function str_length(string $str) : string |
function str_length(string $str, bool $skip_ctrl_seq = false) : int |
| 3 |
{ |
{ |
| 4 |
$len = strlen($str); |
$len = strlen($str); |
| 5 |
$ret = 0; |
$ret = 0; |
| 8 |
{ |
{ |
| 9 |
$c = $str[$i]; |
$c = $str[$i]; |
| 10 |
|
|
| 11 |
|
if ($c == "\r" || $c == "\7") // skip |
| 12 |
|
{ |
| 13 |
|
continue; |
| 14 |
|
} |
| 15 |
|
|
| 16 |
|
if ($skip_ctrl_seq && $c == "\033" && isset($str[$i + 1]) && $str[$i + 1] == "[") // Skip control sequence |
| 17 |
|
{ |
| 18 |
|
for ($i = $i + 2; |
| 19 |
|
isset($str[$i]) && (ctype_digit($str[$i]) || $str[$i] == ';' || $str[$i] == '?'); |
| 20 |
|
$i++) |
| 21 |
|
; |
| 22 |
|
|
| 23 |
|
if (isset($str[$i]) && $str[$i] == 'm') // valid |
| 24 |
|
{ |
| 25 |
|
// skip |
| 26 |
|
} |
| 27 |
|
else if (isset($str[$i]) && ctype_alpha($str[$i])) |
| 28 |
|
{ |
| 29 |
|
// unsupported ANSI CSI command |
| 30 |
|
} |
| 31 |
|
else |
| 32 |
|
{ |
| 33 |
|
$i--; |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
continue; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
// Process UTF-8 Chinese characters |
// Process UTF-8 Chinese characters |
| 40 |
$v1 = ord($c); |
$v1 = ord($c); |
| 41 |
if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character |
if ($v1 & 0x80) //head of multi-byte character |
| 42 |
{ |
{ |
| 43 |
$v2 = ($v1 & 0b01111000) << 1; |
$v2 = ($v1 & 0x70) << 1; |
| 44 |
while ($v2 & 0b10000000) |
while ($v2 & 0x80) |
| 45 |
{ |
{ |
| 46 |
$i++; |
$i++; |
| 47 |
$v3 = $str[$i]; |
$c .= $str[$i]; |
| 48 |
$c .= $v3; |
$v2 = ($v2 & 0x7f) << 1; |
|
$v2 = ($v2 & 0b01111111 ) << 1; |
|
| 49 |
} |
} |
| 50 |
|
|
| 51 |
$ret += 2; |
$ret += 2; |
| 59 |
return $ret; |
return $ret; |
| 60 |
} |
} |
| 61 |
|
|
| 62 |
function split_line(string $str, string $prefix = "", int $width = 76, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string |
function split_line(string $str, string $prefix = "", int $width = PHP_INT_MAX, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string |
| 63 |
{ |
{ |
| 64 |
if ($width <= 0) |
if ($width <= 0) |
| 65 |
{ |
{ |
| 66 |
return $str; |
$width = PHP_INT_MAX; |
| 67 |
} |
} |
| 68 |
|
|
| 69 |
$result = ""; |
$result = ""; |
| 100 |
|
|
| 101 |
// Process UTF-8 Chinese characters |
// Process UTF-8 Chinese characters |
| 102 |
$v1 = ord($c); |
$v1 = ord($c); |
| 103 |
if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character |
if ($v1 & 0x80) //head of multi-byte character |
| 104 |
{ |
{ |
| 105 |
$v2 = ($v1 & 0b01111000) << 1; |
$v2 = ($v1 & 0x70) << 1; |
| 106 |
while ($v2 & 0b10000000) |
while ($v2 & 0x80) |
| 107 |
{ |
{ |
| 108 |
$i++; |
$i++; |
| 109 |
$v3 = $str[$i]; |
$c .= $str[$i]; |
| 110 |
$c .= $v3; |
$v2 = ($v2 & 0x7f) << 1; |
|
$v2 = ($v2 & 0b01111111 ) << 1; |
|
| 111 |
} |
} |
| 112 |
|
|
| 113 |
// Each UTF-8 CJK character should use two character length for display |
// Each UTF-8 CJK character should use two character length for display |
| 153 |
|
|
| 154 |
return $result; |
return $result; |
| 155 |
} |
} |
|
?> |
|