/[LeafOK_CVS]/fenglin/lib/str_process.inc.php
ViewVC logotype

Diff of /fenglin/lib/str_process.inc.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.12 by sysadm, Wed Mar 26 07:09:08 2025 UTC Revision 1.24 by sysadm, Fri Oct 31 10:31:55 2025 UTC
# Line 1  Line 1 
1  <?  <?php
2  function str_length($str)  function str_length(string $str, bool $skip_ctrl_seq = false) : int
3  {  {
4          $len = strlen($str);          $len = strlen($str);
5          $ret = 0;          $ret = 0;
# Line 8  function str_length($str) Line 8  function str_length($str)
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" && $i + 1 < $len && $str[$i + 1] == "[") // Skip control sequence
17                    {
18                            for ($i = $i + 2;
19                                    $i < $len && (ctype_digit($str[$i]) || $str[$i] == ';' || $str[$i] == '?');
20                                    $i++)
21                                    ;
22    
23                            if ($i < $len && $str[$i] == 'm') // valid
24                            {
25                                    // skip
26                            }
27                            else if ($i < $len && 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;
# Line 32  function str_length($str) Line 59  function str_length($str)
59          return $ret;          return $ret;
60  }  }
61    
62  function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)  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                  return $str;          {
66                            $width = PHP_INT_MAX;
67            }
68    
69          $result = "";          $result = "";
70          $len = strlen($str);          $len = strlen($str);
71          $prefix_len = str_length($prefix);          $prefix_len = str_length($prefix);
# Line 54  function split_line($str, $prefix = "", Line 83  function split_line($str, $prefix = "",
83                  {                  {
84                          continue;                          continue;
85                  }                  }
86                                    
87                  if ($c == "\n")                  if ($c == "\n")
88                  {                  {
89                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
90                            {
91                                    break;
92                            }
93    
94                            $result .= ($line . $end_of_line);
95                          $lines_count++;                          $lines_count++;
96                          $line = $prefix;                          $line = $prefix;
97                          $line_len = $prefix_len;                          $line_len = $prefix_len;
# Line 66  function split_line($str, $prefix = "", Line 100  function split_line($str, $prefix = "",
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
# Line 85  function split_line($str, $prefix = "", Line 118  function split_line($str, $prefix = "",
118                                          break;                                          break;
119                                  }                                  }
120    
121                                  $result .= ($line . "\n");                                  $result .= ($line . $end_of_line);
122                                  $lines_count++;                                  $lines_count++;
123                                  $line = $prefix;                                  $line = $prefix;
124                                  $line_len = $prefix_len;                                  $line_len = $prefix_len;
# Line 97  function split_line($str, $prefix = "", Line 130  function split_line($str, $prefix = "",
130                          $line_len++;                          $line_len++;
131                  }                  }
132    
133                  $line .= $c;                  if ($line_len > $width)
   
                 if ($line_len >= $width)  
134                  {                  {
135                          if ($lines_count + 1 >= $lines_limit)                          if ($lines_count + 1 >= $lines_limit)
136                          {                          {
137                                  break;                                  break;
138                          }                          }
139    
140                          $result .= ($line . "\n");                          $result .= ($line . $end_of_line);
141                          $lines_count++;                          $lines_count++;
142                          $line = $prefix;                          $line = $prefix;
143                          $line_len = $prefix_len;                          $line_len = $prefix_len + 1;
144                  }                  }
145    
146                    $line .= $c;
147          }          }
148    
149          if ($lines_count < $lines_limit)          if ($lines_count < $lines_limit)
# Line 120  function split_line($str, $prefix = "", Line 153  function split_line($str, $prefix = "",
153    
154          return $result;          return $result;
155  }  }
 ?>  


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1