免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 14031 | 回复: 25
打印 上一主题 下一主题

[函数] 中文字符处理函数集 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-10-21 14:13 |只看该作者

中文字符处理函数集

FH,那能不能这么说,如果返回-1的话这个字串就不是中文字符开头的?

论坛徽章:
0
3 [报告]
发表于 2004-10-22 08:59 |只看该作者

中文字符处理函数集

返回<2的都不是

论坛徽章:
0
4 [报告]
发表于 2004-10-22 10:30 |只看该作者

中文字符处理函数集

判断是否汉字:
  1. #define        IsChinese(x,y)        ( GetCharSize( x, y ) >; 1 )
复制代码

论坛徽章:
0
5 [报告]
发表于 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. }
复制代码

论坛徽章:
0
6 [报告]
发表于 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. }
复制代码

论坛徽章:
0
7 [报告]
发表于 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. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2004-10-22 14:58 |只看该作者

中文字符处理函数集

先收藏。

论坛徽章:
0
9 [报告]
发表于 2004-10-26 16:56 |只看该作者

中文字符处理函数集

好东西,对我很有用.
收藏!~!

论坛徽章:
0
10 [报告]
发表于 2005-11-01 13:52 |只看该作者
顶顶旧帖,争取俺也混个精品或保留。
这么好的帖子咋就没人识货呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP