Chinaunix

标题: 中文字符处理函数集 [打印本页]

作者: FH    时间: 2004-10-21 13:55
标题: 中文字符处理函数集
这是最底层函数,基于GB18030,判断首字符的字节数。
  1. /******************************************************************************
  2. Function: GetCharSize
  3.    Description:
  4.      This function return the size of lead character of specified data string.
  5.    Input:
  6.      1. string to get character size
  7.      2. string size
  8.    Return:
  9.      -1. invalid character
  10.       0. null string
  11.      >;0. character size
  12. ******************************************************************************/
  13. int        GetCharSize( const char *Data, int Size )
  14. {
  15.         const unsigned char        *p = (unsigned char *) Data;

  16.         // check arguments
  17.         if ( p == NULL || Size <= 0 )
  18.                 return 0;
  19.         // Chinese 1st byte 0x81-0xFE
  20.         if ( p[0] < 0x81 || p[0] >; 0xFE )
  21.                 return 1;
  22.         // Chinese code size = 2, 4
  23.         if ( Size < 2 )
  24.                 return -1;
  25.         // Chinese 2nd byte 0x30-0x39, 0x40-0x7E, 0x80-0xFE
  26.         if ( p[1] < 0x30 || p[1] >; 0x39 && p[1] < 0x40 || p[1] == 0x7F
  27.                         || p[1] >; 0xFE )
  28.                 return -1;
  29.         // 2 bytes Chinese code
  30.         if ( p[1] >;= 0x40 )
  31.                 return 2;
  32.         // Chinese code size = 4
  33.         if ( Size < 4 )
  34.                 return -1;
  35.         // Chinese 3rd byte 0x81-0xFE
  36.         if ( p[2] < 0x81 || p[2] >; 0xFE )
  37.                 return -1;
  38.         // Chinese 4th byte 0x30-0x39
  39.         if ( p[3] < 0x30 || p[3] >; 0x39 )
  40.                 return -1;
  41.         return 4;
  42. }
复制代码

作者: superdoctor    时间: 2004-10-21 14:13
标题: 中文字符处理函数集
FH,那能不能这么说,如果返回-1的话这个字串就不是中文字符开头的?
作者: FH    时间: 2004-10-22 08:59
标题: 中文字符处理函数集
返回<2的都不是
作者: FH    时间: 2004-10-22 10:30
标题: 中文字符处理函数集
判断是否汉字:
  1. #define        IsChinese(x,y)        ( GetCharSize( x, y ) >; 1 )
复制代码

作者: FH    时间: 2004-10-22 10:41
标题: 中文字符处理函数集
查找字符串中的无效字符(不完整汉字),返回指向该字符的指针或空。
  1. /******************************************************************************
  2. Function: FindInvalidChar
  3.    Description:
  4.      This function return the pointer to the first invalid character of
  5.      specified data string or NULL.
  6.    Input:
  7.      1. string to check invalid character
  8.      2. string size
  9.    Return:
  10.      NULL. not found
  11.      Other. pointer to invalid character
  12. ******************************************************************************/
  13. const char        *FindInvalidChar( const char *Data, int Size )
  14. {
  15.         const char        *p = Data;
  16.         int        r, s = Size;

  17.         while ( ( r = GetCharSize( p, s ) ) >; 0 ) {
  18.                 p += r;
  19.                 s -= r;
  20.         }

  21.         if ( r == 0 )
  22.                 return NULL;
  23.         return p;
  24. }
复制代码

作者: FH    时间: 2004-10-22 10:48
标题: 中文字符处理函数集
计算字符串中的字符数:
  1. /******************************************************************************
  2. Function: GetCharCount
  3.    Description:
  4.      This function count the characters in specified data string.
  5.    Input:
  6.      1. string to count characters
  7.      2. string size
  8.    Return:
  9.      -1. find invalid character
  10.      >;0. count of characters
  11. ******************************************************************************/
  12. int        GetCharCount( const char *Data, int Size )
  13. {
  14.         const char        *p = Data;
  15.         int        r, c = 0, s = Size;

  16.         while ( ( r = GetCharSize( p, s ) ) >; 0 ) {
  17.                 c ++;
  18.                 p += r;
  19.                 s -= r;
  20.         }

  21.         if ( r == 0 )
  22.                 return c;
  23.         return -1;
  24. }
复制代码

作者: FH    时间: 2004-10-22 14:44
标题: 中文字符处理函数集
提取首字符并移动指针、更新长度:
  1. /******************************************************************************
  2. Function: PickFirstChar
  3.    Description:
  4.      This function pick the first character in specified data string then
  5.      update its pointer and size.
  6.    Input:
  7.      1. pointer of string to pick character
  8.      2. pointer of string size
  9.    Output:
  10.      1. updated pointer of string
  11.      2. pointer of undated string size
  12.    Return:
  13.      -1. fail
  14.      Other. the first character
  15. ******************************************************************************/
  16. unsigned long        PickFirstChar( const char **pData, int *pSize )
  17. {
  18.         int        i, r;
  19.         unsigned long        c = 0;

  20.         if ( ( r = GetCharSize( *pData, *pSize ) ) <= 0 )
  21.                 return -1;

  22.         for ( i = 0; i < r; i ++ ) {
  23.                 c = ( c << 8 ) + (unsigned char) (*pData)[i];
  24.         }

  25.         *pData += r;
  26.         *pSize -= r;
  27.         return c;
  28. }
复制代码

作者: aspbiz    时间: 2004-10-22 14:58
标题: 中文字符处理函数集
先收藏。
作者: 遗忘在黄昏    时间: 2004-10-26 16:56
标题: 中文字符处理函数集
好东西,对我很有用.
收藏!~!
作者: FH    时间: 2005-11-01 13:52
顶顶旧帖,争取俺也混个精品或保留。
这么好的帖子咋就没人识货呢?

作者: mq110    时间: 2005-11-01 13:56
都是乱码可如何是好。
作者: yintao_1982    时间: 2005-11-01 14:22
应该很有用的,先收藏
作者: aaaaal    时间: 2005-11-01 15:00
可惜代码现在里面很多乱码了啊
作者: 自然平衡    时间: 2005-11-01 22:59
唉,换论坛程序后,以前的代码可读性几乎为零了。
作者: lenovo    时间: 2005-11-01 23:24
原帖由 FH 于 2005-11-1 13:52 发表
顶顶旧帖,争取俺也混个精品或保留。
这么好的帖子咋就没人识货呢?


不好意思,刚刚看到。
这下满意了吧?
作者: FH    时间: 2005-11-02 08:40
原帖由 lenovo 于 2005-11-1 23:24 发表


不好意思,刚刚看到。
这下满意了吧?


谢谢版主
作者: linternt    时间: 2005-11-02 10:53
收藏一下,以后可能用得到,谢谢
作者: 帅绝人寰    时间: 2005-11-03 22:26
遗憾,好程序无法欣赏。
希望尽快改正乱码问题。
作者: er    时间: 2005-11-04 09:11
希望快快更正乱码问题
作者: oyjcq    时间: 2005-11-04 09:11
改版后代码格式好乱啊
看着晕了
作者: lynhoo    时间: 2005-11-04 09:27
是啊,乱码严重啊,费力费时阿
作者: shappen    时间: 2005-11-09 12:55
FH大哥,能不能把这些代码加到一个文件 中再挂附件传上来?
作者: FH    时间: 2005-11-09 16:37
原帖由 shappen 于 2005-11-9 12:55 发表
FH大哥,能不能把这些代码加到一个文件 中再挂附件传上来?


没办法了
下载以后用个编辑器批量替换一下吧
作者: ywchen2000    时间: 2006-10-19 10:24
好东西
作者: cuicp    时间: 2006-10-20 15:33
有个问题要请教楼主:
有个扩展ASCII字符集,是八个字节的,
那么在中文系统中,怎么判断字符是扩展ASCII字符还是中文字符呢?
难道中文系统中不用扩展ASCII字符集吗?

谢谢!!
作者: billzhou    时间: 2006-10-20 16:38
好东西 要顶




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2