/[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.24 by sysadm, Tue Nov 4 14:58:56 2025 UTC Revision 1.29 by sysadm, Mon Nov 10 12:53:16 2025 UTC
# Line 11  Line 11 
11  #include "str_process.h"  #include "str_process.h"
12  #include <ctype.h>  #include <ctype.h>
13  #include <stdio.h>  #include <stdio.h>
14    #include <stdlib.h>
15  #include <string.h>  #include <string.h>
16    #include <wchar.h>
17    
18    int UTF8_fixed_width = 1;
19    
20  int str_length(const char *str, int skip_ctrl_seq)  int str_length(const char *str, int skip_ctrl_seq)
21  {  {
22            int str_len;
23            char input_str[5];
24            wchar_t wcs[2];
25            int wc_len;
26          int i;          int i;
27          char c;          char c;
28          int ret = 0;          int ret = 0;
# Line 52  int str_length(const char *str, int skip Line 60  int str_length(const char *str, int skip
60                  // Process UTF-8 Chinese characters                  // Process UTF-8 Chinese characters
61                  if (c & 0x80) // head of multi-byte character                  if (c & 0x80) // head of multi-byte character
62                  {                  {
63                          c = (c & 0x70) << 1;                          str_len = 0;
64                            c = (char)(c & 0xf0);
65                          while (c & 0x80)                          while (c & 0x80)
66                          {                          {
67                                  i++;                                  input_str[str_len] = str[i + str_len];
68                                    str_len++;
69                                  c = (c & 0x7f) << 1;                                  c = (c & 0x7f) << 1;
70                          }                          }
71                            input_str[str_len] = '\0';
72    
73                          ret += 2;                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
74                            {
75    #ifdef _DEBUG
76                                    log_error("mbstowcs(%s) error\n", input_str);
77    #endif
78                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
79                            }
80                            else
81                            {
82                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
83                            }
84    
85                            i += (str_len - 1);
86                            ret += wc_len;
87                  }                  }
88                  else                  else
89                  {                  {
# Line 76  int split_line(const char *buffer, int m Line 100  int split_line(const char *buffer, int m
100          *p_eol = 0;          *p_eol = 0;
101          *p_display_len = 0;          *p_display_len = 0;
102          char c;          char c;
103            int str_len;
104            char input_str[5];
105            wchar_t wcs[2];
106            int wc_len;
107    
108          for (i = 0; buffer[i] != '\0'; i++)          for (i = 0; buffer[i] != '\0'; i++)
109          {          {
# Line 98  int split_line(const char *buffer, int m Line 126  int split_line(const char *buffer, int m
126    
127                  if (c & 0x80) // head of multi-byte character                  if (c & 0x80) // head of multi-byte character
128                  {                  {
129                          if (*p_display_len + 2 > max_display_len)                          str_len = 0;
130                            c = (char)(c & 0xf0);
131                            while (c & 0x80)
132                          {                          {
133                                  break;                                  input_str[str_len] = buffer[i + str_len];
134                                    str_len++;
135                                    c = (c & 0x7f) << 1;
136                          }                          }
137                            input_str[str_len] = '\0';
138    
139                          c = (c & 0x70) << 1;                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
                         while (c & 0x80)  
140                          {                          {
141                                  i++;  #ifdef _DEBUG
142                                  c = (c & 0x7f) << 1;                                  log_error("mbstowcs(%s) error\n", input_str);
143    #endif
144                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
145                            }
146                            else
147                            {
148                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
149                            }
150    
151                            if (*p_display_len + wc_len > max_display_len)
152                            {
153                                    break;
154                          }                          }
155    
156                          (*p_display_len) += 2;                          i += (str_len - 1);
157                            (*p_display_len) += wc_len;
158                  }                  }
159                  else                  else
160                  {                  {
# Line 162  long split_data_lines(const char *p_buf, Line 206  long split_data_lines(const char *p_buf,
206                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
207                  line_cnt++;                  line_cnt++;
208                  p_buf += len;                  p_buf += len;
209          } while (p_buf[0] != '\0');          } while (p_buf[0] != '\0' || end_of_line);
210    
211          return line_cnt;          return line_cnt;
212  }  }


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

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