數據庫Length函數實現

Length函數的三種實現方式

#include "iostream" 

unsigned char utf8_look_for_table[] = {
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
    4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
};

#define UTFLEN(first_byte)  utf8_look_for_table[(first_byte)]

#define UTF8_CHAR_LEN( first_byte ) ((( 0xE5000000 >> (( first_byte >> 3 ) & 0x1e )) & 3 ) + 1)

/********************************************************************
*
* SQL Length1函數實現
*   @str : 字符串的首地址
*   @srcLen: 字符串的長度
* 返回值:
*   表示字符串中包含多少個字符。
*
********************************************************************/

int Length1(const char *str, int srcLen) {
    int len = 0;
    int retLen = 0;
    int utfLen = 0;

    for (const char *ptr = str; len < srcLen; retLen++) {
        utfLen = UTF8_CHAR_LEN((unsigned char)*ptr);
        len += utfLen;
        ptr += utfLen;
    }

    return retLen;
}

/********************************************************************
*
* SQL Length2函數實現
*   @str : 字符串的首地址
*   @srcLen: 字符串的長度
* 返回值:
*   表示字符串中包含多少個字符。
*
********************************************************************/
int Length2(const char *str, int srcLen) {
    int len = 0;
    int retLen = 0;
    int utfLen = 0;
    unsigned char ch;
    for (const char *ptr = str; len < srcLen; retLen++) {
        ch = (unsigned char)*ptr;
        if (ch < 192) {
            utfLen = 1;
        } else if (ch >= 224 && ch < 240) {
            utfLen = 3;
        } else if (ch >= 192 && ch < 224) {
            utfLen = 2;
        } else if (ch >= 240 && ch < 248) {
            utfLen = 4;
        } else if (ch >= 248 && ch < 252) {
            utfLen = 5;
        } else if (ch >= 252 && ch < 254) {
            utfLen = 6;
        } else {
            utfLen = 1;
        }
        len += utfLen;
        ptr += utfLen;
    }
    return retLen;
}

/********************************************************************
*
* SQL Length3函數實現
*   @str : 字符串的首地址
*   @srcLen: 字符串的長度
* 返回值:
*   表示字符串中包含多少個字符。
*
********************************************************************/
int Length3(const char *str, int srcLen) {
    int len = 0;
    int retLen = 0;
    int utfLen = 0;
    for (const char *ptr = str; len < srcLen; retLen++) {
        utfLen = UTFLEN((unsigned char)*ptr);
        len += utfLen;
        ptr += utfLen;
    }
    return retLen;
}



int main() {
    //std::string str = "hello world 我們";
    //std::string str = "我們";
    std::string str = "a";
    int size1 = Length1(str.c_str(),str.length());
    std::cout << size1 << std::endl;
    int size2 = Length2(str.c_str(), str.length());
    std::cout << size2 << std::endl;
    int size3 = Length3(str.c_str(), str.length());
    std::cout << size3 << std::endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章