判断字符串const char*是 多字节 还是 宽字符(UTF8)

unsigned utf8decode(const char* p, const char* end, int* len)

{

  unsigned char c = *(unsigned char*)p;

  if (c < 0x80) {

    *len = 1;

    return c;

  } else if (c < 0xc2) {

    goto FAIL;

  }

  if (p+1 >= end || (p[1]&0xc0) != 0x80) goto FAIL;

  if (c < 0xe0) {

    *len = 2;

    return

      ((p[0] & 0x1f) << 6) +

      ((p[1] & 0x3f));

  } else if (c == 0xe0) {

    if (((unsigned char*)p)[1] < 0xa0) goto FAIL;

    goto UTF8_3;

  }

#if STRICT_RFC3629

  else if (c == 0xed) {

    // RFC 3629 says surrogate chars are illegal.

    if (((unsigned char*)p)[1] >= 0xa0) goto FAIL;

    goto UTF8_3;

  } else if (c == 0xef) {

    // 0xfffe and 0xffff are also illegal characters

    if (((unsigned char*)p)[1]==0xbf &&

    ((unsigned char*)p)[2]>=0xbe) goto FAIL;

    goto UTF8_3;

  }

#endif

  else if (c < 0xf0) {

  UTF8_3:

    if (p+2 >= end || (p[2]&0xc0) != 0x80) goto FAIL;

    *len = 3;

    return

      ((p[0] & 0x0f) << 12) +

      ((p[1] & 0x3f) << 6) +

      ((p[2] & 0x3f));

  } else if (c == 0xf0) {

    if (((unsigned char*)p)[1] < 0x90) goto FAIL;

    goto UTF8_4;

  } else if (c < 0xf4) {

  UTF8_4:

    if (p+3 >= end || (p[2]&0xc0) != 0x80 || (p[3]&0xc0) != 0x80) goto FAIL;

    *len = 4;

#if STRICT_RFC3629

    // RFC 3629 says all codes ending in fffe or ffff are illegal:

    if ((p[1]&0xf)==0xf &&

    ((unsigned char*)p)[2] == 0xbf &&

    ((unsigned char*)p)[3] >= 0xbe) goto FAIL;

#endif

    return

      ((p[0] & 0x07) << 18) +

      ((p[1] & 0x3f) << 12) +

      ((p[2] & 0x3f) << 6) +

      ((p[3] & 0x3f));

  } else if (c == 0xf4) {

    if (((unsigned char*)p)[1] > 0x8f) goto FAIL; // after 0x10ffff

    goto UTF8_4;

  } else {

  FAIL:

    *len = 1;

    return 0xfffd; // Unicode REPLACEMENT CHARACTER

  }

}

 

bool Is_wchar(const char* src )

{

    int  srclen= (int)strlen(src );

    int ret = 1;
    const char* p = src ;
    const char* e = src + srclen;
    while( p < e )
    {
        if( *p == 0 )
            return false;
        if( *p & 0x80 )
        {
            int len = 0;
            utf8decode(p, e, &len);
            if( len < 2 ) return 0;
            if( len > ret ) ret = len;
            p += len;
        }

        else

            p++;
   }
    return ret != 0 ? true : false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值