utf-8字符窜对应的辅助函数

逍遥云 posted @ 2010年12月28日 21:30 in c/c++ with tags utf-8 c/c++ 编码 , 2912 阅读

 

int
utf8_char_size(const unsigned char c)
{
    if(c < 0x80) return 1;
    if((c & 0xc0) == 0x80) return 0;

    int mask = 0x80;
    int num = 0;
    while(mask & c)
    {
        ++num;
        mask >>= 1;
    }

    return num;
}

int
utf8_len(const char *utf8)
{
    const unsigned char *input = (const unsigned char *)utf8;
    int count = 0;
    while(*input) {
        while((*input & 0xc0) == 0x80)
            ++input;
        count++;
        input++;
    };

    return count;
}

int
utf8_char_at(const char *utf8, int index)
{
    const unsigned char *input = (const unsigned char *)utf8;
    int count = 0;
    while(*input) {
        if(count == index) return input - (const unsigned char *)utf8;

        input += utf8_char_size(*input);
        count++;
    };

    return -1;
}
 

 

 

int utf8_char_size(const unsigned char c)  //取得utf-8字符的长度.

例子: char *str="中文";utf8_char_size(*str)=3;utf8_char_size(*++str)=0

int utf8_len(const char *utf8)  //取得字符窜的长度,类似wcslen(wchar_t)

例子:  utf8_len("hello你好,world") = 17

int utf8_char_at(const char *utf8, int index) // 取得utf-8字符窜真实地址偏移值

例子:utf8_char_at("hello你好,world", 7) = 11

Avatar_small
deep cleaning dubai 说:
2019年11月18日 20:03

Great execution of one's household washing and dusting jobs by professionals increase your ease and comfort levels. With these kinds of professionals available to manage your home-based chores, you don`t must worry above anything. Your comfort will probably be enhanced as well as the hygienic standards of your dwelling will you should your friends.

Avatar_small
babysitting services 说:
2021年1月28日 16:12

Maid agencies are booming divorce lawyers atlanta important spot in the world. A high level employer seeking that maid agency for you to trust and enable you to on your current maid demands, there's nothing at all preventing anyone from receiving one. They are all around the world, especially inside cyber entire world. With countless options all-around.

Avatar_small
painters in Dubai 说:
2021年6月29日 18:27

For this note, everyone has a common colors; on the other hand, when you happen to be choosing indoor paints, it usually is advantageous to view some in the popular hues among a lot of. The hues listed listed below are said a great impact for the mood of men and women and while using right mix, it could make for a terrific painting undertaking.

Avatar_small
home cleaning dubai 说:
2021年8月26日 16:12

Dubai is mostly a place while the business is normally booming for the whole family and pulls everything. In that busy time frame and moving life for Dubai, cleaning is mostly a hectic job for the whole family. Especially in which are employed or possibly having ones own businesses.

Avatar_small
House Cleaning Servi 说:
2021年9月18日 14:34

Should you have a puppy, then they may bring almost all of the dirt via outside, after they go out and about for participating in. The best answer is to set a doormat while you're watching outside entrance. In using this method, you will certainly protect your current floor quickly. If the thing is that some hard-to-remove stresses, you will use many of the home cures. Try employing shaving ointment, hydrogen peroxide, the baking soda and many others. Put lighting water for the strain, then implement shaving cream into it and preserve it for a long time. You will spot the influence. It won’t destruction the food fibre or spot it.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter