/[LeafOK_CVS]/lbbs/src/str_process.c
ViewVC logotype

Diff of /lbbs/src/str_process.c

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

Revision 1.18 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.20 by sysadm, Wed Jul 2 04:17:33 2025 UTC
# Line 20  Line 20 
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22    
23    int str_length(const char *str, int skip_ctrl_seq)
24    {
25            int i;
26            char c;
27            int ret = 0;
28    
29            for (i = 0; str[i] != '\0'; i++)
30            {
31                    c = str[i];
32    
33                    if (c == '\r' || c == '\7') // skip
34                    {
35                            continue;
36                    }
37    
38                    if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence
39                    {
40                            i += 2;
41                            while (str[i] != '\0' && str[i] != 'm')
42                            {
43                                    i++;
44                            }
45                            continue;
46                    }
47    
48                    // Process UTF-8 Chinese characters
49                    if (c & 0b10000000) // head of multi-byte character
50                    {
51                            c = (c & 0b01110000) << 1;
52                            while (c & 0b10000000)
53                            {
54                                    i++;
55                                    c = (c & 0b01111111) << 1;
56                            }
57    
58                            ret += 2;
59                    }
60                    else
61                    {
62                            ret++;
63                    }
64            }
65    
66            return ret;
67    }
68    
69  int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)  int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)
70  {  {
71          int i;          int i;
# Line 46  int split_line(const char *buffer, int m Line 92  int split_line(const char *buffer, int m
92                          continue;                          continue;
93                  }                  }
94    
95                  if (c < 0 || c > 127) // GBK chinese character                  if (c & 0b10000000) // head of multi-byte character
96                  {                  {
97                          if (*p_display_len + 2 > max_display_len)                          if (*p_display_len + 2 > max_display_len)
98                          {                          {
99                                  break;                                  break;
100                          }                          }
101                          i++;  
102                            c = (c & 0b01110000) << 1;
103                            while (c & 0b10000000)
104                            {
105                                    i++;
106                                    c = (c & 0b01111111) << 1;
107                            }
108    
109                          (*p_display_len) += 2;                          (*p_display_len) += 2;
110                  }                  }
111                  else                  else
# Line 76  int split_line(const char *buffer, int m Line 129  int split_line(const char *buffer, int m
129          return i;          return i;
130  }  }
131    
132  long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count, int skip_ctrl_seq)  long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count,
133                                              int skip_ctrl_seq, int *p_line_widths)
134  {  {
135          int line_cnt = 0;          int line_cnt = 0;
136          int len;          int len;
# Line 89  long split_data_lines(const char *p_buf, Line 143  long split_data_lines(const char *p_buf,
143          {          {
144                  len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq);                  len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq);
145    
146                    if (p_line_widths)
147                    {
148                            p_line_widths[line_cnt] = display_len;
149                    }
150    
151                  // Exceed max_line_cnt                  // Exceed max_line_cnt
152                  if (line_cnt + 1 >= line_offsets_count)                  if (line_cnt + 1 >= line_offsets_count)
153                  {                  {


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

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