首页
社区
课程
招聘
[原创]字符串_常用操作函数与数值之间相关函数_重写练习之CPP源码!!!
发表于: 2021-11-29 21:31 17210

[原创]字符串_常用操作函数与数值之间相关函数_重写练习之CPP源码!!!

2021-11-29 21:31
17210

早期学习C的时候写的练手代码,
但是那会写的都是中文变量名称,
现在要练习写英文的,拿他复习
顺便练手改英文名称
有些想去职业圈玩了,一个人玩太无聊了.

头文件:

实现源码如下:

 
typedef long long llong;
class demo_hys
{
public:
    static size_t str_len(char* str);
    static size_t str_len(wchar_t* str);
    static char* str_reverse(char* str);
    static wchar_t* str_reverse(wchar_t* str);
    static int ctoi(char* c);
    static int ctoi(wchar_t* c);
    static wchar_t itoc(size_t num);
    static intptr_t strtohex(char* str);
    static intptr_t strtohex(wchar_t* str);
    static intptr_t hextoint(char* str);
    static intptr_t hextoint(wchar_t* str);
    static llong hextolong(char* str);
    static llong hextolong(wchar_t* str);
    static char* inttohex(intptr_t num, char* phex);
    static wchar_t* inttohex(intptr_t num, wchar_t* phex);
    static char* longtohex(llong num, char* phex);
    static wchar_t* longtohex(llong num, wchar_t* phex);
    static size_t str_getnumlenth(char* str);
    static size_t str_getnumlenth(wchar_t* str);
    static size_t inttostr(intptr_t num, char* pstr);
    static size_t inttostr(intptr_t num, wchar_t* pstr);
    static size_t longtostr(llong num, char* pstr);
    static size_t longtostr(llong num, wchar_t* pstr);
    static intptr_t strtoint(char* str);
    static intptr_t strtoint(wchar_t* str);
    static llong strtolong(char* str);
    static llong strtolong(wchar_t* str);
    static double strtodouble(char* str);
    static double strtodouble(wchar_t* str);
    static char* str_delspace(char* str);
    static wchar_t* str_delspace(wchar_t* str);
    static char* str_toupper(char* 文本);
    static wchar_t* str_toupper(wchar_t* 文本);
    static char* str_tolower(char* 文本);
    static wchar_t* str_tolower(wchar_t* 文本);
    static void str_sub(char* str, size_t start, size_t len, char* outstr, bool endzero = false);
    static void str_sub(wchar_t* str, size_t start, size_t len, wchar_t* outstr, bool endzero = false);
    static bool str_cmp(char* str1, char* str2);
    static bool str_cmp(wchar_t* str1, wchar_t* str2);
    static size_t str_replace(char* str, char* find, char* rep, char* outstr, intptr_t count = -1);
    static size_t str_replace(wchar_t* str, wchar_t* find, wchar_t* rep, wchar_t* outstr, intptr_t count = -1);
    static intptr_t str_find(char* str, char* find);
    static intptr_t str_find(wchar_t* str, wchar_t* find);
    static intptr_t str_rfind(char* str, char* rfind);
    static intptr_t str_rfind(wchar_t* str, wchar_t* rfind);
    static char* str_copy(char* str, char* copy, bool endzero = false);
    static wchar_t* str_copy(wchar_t* str, wchar_t* copy, bool endzero = false);
    static bool hexcmp(char* hex1, char* hex2, size_t count);
    static intptr_t hexfind(char* hex, size_t hexlen, char* find, size_t findlen);
    static intptr_t hexrfind(char* hex, size_t hexlen, char* find, size_t findlen);
 
};
typedef long long llong;
class demo_hys
{
public:
    static size_t str_len(char* str);
    static size_t str_len(wchar_t* str);
    static char* str_reverse(char* str);
    static wchar_t* str_reverse(wchar_t* str);
    static int ctoi(char* c);
    static int ctoi(wchar_t* c);
    static wchar_t itoc(size_t num);
    static intptr_t strtohex(char* str);
    static intptr_t strtohex(wchar_t* str);
    static intptr_t hextoint(char* str);
    static intptr_t hextoint(wchar_t* str);
    static llong hextolong(char* str);
    static llong hextolong(wchar_t* str);
    static char* inttohex(intptr_t num, char* phex);
    static wchar_t* inttohex(intptr_t num, wchar_t* phex);
    static char* longtohex(llong num, char* phex);
    static wchar_t* longtohex(llong num, wchar_t* phex);
    static size_t str_getnumlenth(char* str);
    static size_t str_getnumlenth(wchar_t* str);
    static size_t inttostr(intptr_t num, char* pstr);
    static size_t inttostr(intptr_t num, wchar_t* pstr);
    static size_t longtostr(llong num, char* pstr);
    static size_t longtostr(llong num, wchar_t* pstr);
    static intptr_t strtoint(char* str);
    static intptr_t strtoint(wchar_t* str);
    static llong strtolong(char* str);
    static llong strtolong(wchar_t* str);
    static double strtodouble(char* str);
    static double strtodouble(wchar_t* str);
    static char* str_delspace(char* str);
    static wchar_t* str_delspace(wchar_t* str);
    static char* str_toupper(char* 文本);
    static wchar_t* str_toupper(wchar_t* 文本);
    static char* str_tolower(char* 文本);
    static wchar_t* str_tolower(wchar_t* 文本);
    static void str_sub(char* str, size_t start, size_t len, char* outstr, bool endzero = false);
    static void str_sub(wchar_t* str, size_t start, size_t len, wchar_t* outstr, bool endzero = false);
    static bool str_cmp(char* str1, char* str2);
    static bool str_cmp(wchar_t* str1, wchar_t* str2);
    static size_t str_replace(char* str, char* find, char* rep, char* outstr, intptr_t count = -1);
    static size_t str_replace(wchar_t* str, wchar_t* find, wchar_t* rep, wchar_t* outstr, intptr_t count = -1);
    static intptr_t str_find(char* str, char* find);
    static intptr_t str_find(wchar_t* str, wchar_t* find);
    static intptr_t str_rfind(char* str, char* rfind);
    static intptr_t str_rfind(wchar_t* str, wchar_t* rfind);
    static char* str_copy(char* str, char* copy, bool endzero = false);
    static wchar_t* str_copy(wchar_t* str, wchar_t* copy, bool endzero = false);
    static bool hexcmp(char* hex1, char* hex2, size_t count);
    static intptr_t hexfind(char* hex, size_t hexlen, char* find, size_t findlen);
    static intptr_t hexrfind(char* hex, size_t hexlen, char* find, size_t findlen);
 
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define NULL 0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
size_t demo_hys::str_len(char* str)
{
    char* p = str;
    while (*p) { p++; }
    return p - str;
}
//---------------------------------------------------------------------------------------
size_t demo_hys::str_len(wchar_t* str)
{
    wchar_t* p = str;
    while (*p) { p++; }
    return p - str;
}
//---------------------------------------------------------------------------------------
char* demo_hys::str_reverse(char* str)
{
    size_t slen = str_len(str);
    char* p = str + slen;
    char tmp = NULL;
    while (--p > str)
    {
        tmp = *str;
        *str++ = *p;
        *p = tmp;
    }
    return str - slen / 2;
}
//---------------------------------------------------------------------------------------
wchar_t* demo_hys::str_reverse(wchar_t* str)
{
    size_t slen = str_len(str);
    wchar_t* p = str + slen; wchar_t tmp = NULL;
    while (--p > str)
    {
        tmp = *str;
        *str++ = *p;
        *p = tmp;
    }
    return str - slen / 2;
}
//---------------------------------------------------------------------------------------
//把单个字符【0-9】和【a-f】转为数值_失败返回-1
int demo_hys::ctoi(char* c)
{
    //判断是否0-9的字符
    if (*c <= '9')
    {
        if (*c >= '0')
        {
            return *c - '0';
        }
    }
    //判断是否A-F的字符
    else if (*c <= 'F')
    {
        if (*c >= 'A')
        {
            return *c - 'A' + 10;
        }
    }
    //判断是否a-f的字符
    else if (*c <= 'f')
    {
        if (*c >= 'a')
        {
            return *c - 'a' + 10;
        }
    }
    //失败返回【-1
    return -1;
}
//---------------------------------------------------------------------------------------
//把单个字符【0-9】和【a-f】转为数值_失败返回-1
int demo_hys::ctoi(wchar_t* c)
{
    //判断是否0-9的字符
    if (*c <= L'9')
    {
        if (*c >= L'0')
        {
            return *c - L'0';
        }
    }
    //判断是否A-F的字符
    else if (*c <= L'F')
    {
        if (*c >= L'A')
        {
            return *c - L'A' + 10;
        }
    }
    //判断是否a-f的字符
    else if (*c <= L'f')
    {
        if (*c >= L'a')
        {
            return *c - L'a' + 10;
        }
    }
    //失败返回【-1
    return -1;
}
//---------------------------------------------------------------------------------------
//失败返回值【S】,成功返回一个字符【1-9】【A-F】
wchar_t demo_hys::itoc(size_t num)
{
    if (num <= 9)
    {
        return (wchar_t)((num + '0') & 0xff);//数字
    }
    else if (num <= 15)
    {
        return (wchar_t)((num - 10 + 'A') & 0xff); //字符
    }
    else
    {
        return L'S';//错误
    }
}
//---------------------------------------------------------------------------------------
//成功返回十六进制数值,失败返回【-1
intptr_t demo_hys::strtohex(char* str)
{
    int tens = NULL, ones = NULL;   
    tens = ctoi(str++);    //十位数
    ones = ctoi(str);//个位数
    if (ones == -1 || tens == -1)
    {
        return -1;
    }   
    return ones * 16 + tens;
}
//---------------------------------------------------------------------------------------
//成功返回十六进制数值,失败返回【-1
intptr_t demo_hys::strtohex(wchar_t* 文本)
{
    int tens = NULL, ones = NULL;   
    tens = ctoi(文本++);//十位数   
    ones = ctoi(文本);//个位数
    if (ones == -1 || tens == -1)
    {
        return -1;
    }   
    return ones * 16 + tens;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::hextoint(char* str)
{
    char pint[10] = { 0 };
    intptr_t ilen = sizeof(intptr_t);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < ilen)
    {
        ilen = hexlen;
    }
    while (ilen--)
    {
        pint[ilen] = (char)strtohex(str);
        str += 2;
    }
    return *(intptr_t*)pint;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::hextoint(wchar_t* str)
{
    char pint[10] = { 0 };
    intptr_t ilen = sizeof(intptr_t);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < ilen)
    {
        ilen = hexlen;
    }
    while (ilen--)
    {
        pint[ilen] = (char)strtohex(str);
        str += 2;
    }
    return *(intptr_t*)pint;
}
//---------------------------------------------------------------------------------------
llong demo_hys::hextolong(char* str)
{
    char pint[22] = { 0 };
    intptr_t longlen = sizeof(llong);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < longlen)
    {
        longlen = hexlen;
    }
    while (longlen--)
    {
        pint[longlen] = (char)strtohex(str);
        str += 2;
    }
    return *(llong*)pint;
}
//---------------------------------------------------------------------------------------
llong demo_hys::hextolong(wchar_t* str)
{
    char pint[22] = { 0 };
    intptr_t longlen = sizeof(llong);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < longlen)
    {
        longlen = hexlen;
    }
    while (longlen--)
    {
        pint[longlen] = (char)strtohex(str);
        str += 2;
    }
    return *(llong*)pint;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
char* demo_hys::inttohex(intptr_t num, char* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(intptr_t);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
wchar_t* demo_hys::inttohex(intptr_t num, wchar_t* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(intptr_t);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
char* demo_hys::longtohex(llong num, char* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(llong);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
wchar_t* demo_hys::longtohex(llong num, wchar_t* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(llong);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//只取连续的【0-9】数字的长度
size_t demo_hys::str_getnumlenth(char* str)
{
    char* p = str;
    while (*p <= '9' && *p >= '0')
    {
        p++;
    }
    return p - str;
}
//---------------------------------------------------------------------------------------
//只取连续的【0-9】数字的长度
size_t demo_hys::str_getnumlenth(wchar_t* str)
{
    wchar_t* p = str;
    while (*p <= L'9' && *p >= L'0')
    {
        p++;
    }
    return p - str;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::inttostr(intptr_t num, char* pstr)
{
    char* p = pstr;
    size_t flag = NULL;//负数标志位
    if (!num)
    {
        *pstr++ = '0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = '-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = (char)itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::inttostr(intptr_t num, wchar_t* pstr)
{
    wchar_t* p = pstr;
    size_t flag = NULL;//负数
    if (!num)
    {
        *pstr++ = L'0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = L'-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::longtostr(llong num, char* pstr)
{
    char* p = pstr;
    size_t flag = NULL;//负数
    if (!num)
    {
        *pstr++ = '0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = '-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = (char)itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::longtostr(llong num, wchar_t* pstr)
{
    wchar_t* p = pstr;
    size_t flag = NULL;
    if (!num)
    {
        *pstr++ = L'0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = L'-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = itoc(num % 10);
        num /= 10;
    }
    *p = NULL;   
    str_reverse(pstr);   
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::strtoint(char* str)
{
    intptr_t num = NULL;
    intptr_t bit = 1;
    intptr_t flag = 1;//正负数标志位
    int i = NULL;
    if (*str == '-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }   
    return num * flag;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::strtoint(wchar_t* str)
{
    intptr_t num = NULL;
    intptr_t bit = 1;
    intptr_t flag = 1;//正负数标志位
    int i = NULL;
    if (*str == L'-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }
    return num * flag;
}
//---------------------------------------------------------------------------------------
llong demo_hys::strtolong(char* str)
{
    llong num = NULL;
    llong bit = 1;
    llong flag = 1;//正负数标志位
    int i = NULL;
    if (*str == '-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }
    return num * flag;
}
//---------------------------------------------------------------------------------------
llong demo_hys::strtolong(wchar_t* str)
{
    llong num = NULL;
    llong bit = 1;
    llong flag = 1;//正负数标志位
    int i = NULL;
    if (*str == L'-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }
    return num * flag;
}
//---------------------------------------------------------------------------------------
double demo_hys::strtodouble(char* str)
{
    double num = NULL;
    double bit = 1;
    double flag = 1;//正负数标志
    int i = NULL;
    if (*str == '-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    char* p = str;
    //处理整数部分
    while (len--)
    {
        i = ctoi(str--);   
        if (i == -1) { break; }
        num += i * bit;   
        bit *= 10;
    }
    //处理小数部分
    if (*++p == '.')
    {
        len = str_getnumlenth(++p);
        bit = 10;
        while (len--) {
            i = ctoi(p++);
            if (i == -1) { break; }
            num += i / bit;
            bit *= 10;
        }
    }   
    return num * flag;
}
//---------------------------------------------------------------------------------------
double demo_hys::strtodouble(wchar_t* str)
{
    double num = NULL;
    double bit = 1;
    double flag = 1;//正负数标志
    int i = NULL;
    if (*str == L'-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    wchar_t* p = str;
    //处理整数部分
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }
    //处理小数部分
    if (*++p == '.')
    {
        len = str_getnumlenth(++p);
        bit = 10;
        while (len--) {
            i = ctoi(p++);
            if (i == -1) { break; }
            num += i / bit;
            bit *= 10;
        }
    }
    return num * flag;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本空格已经被过滤了
char* demo_hys::str_delspace(char* str)
{
    char* p = str;
    char* ret = str;
    //字符串结尾为0,非0就继续
    while (*p)
    {
        //空格0x20 全角空格0xa10xffffffa1】  制表符0x09
        if (*p == ' ' || *p == (char)0xffffffa1 || *p == '\t')
        {
            p++;
        }
        else
        {
            *str++ = *p++;
        }
    }
    *str = 0;
    return ret;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本空格已经被过滤了
wchar_t* demo_hys::str_delspace(wchar_t* str)
{
    wchar_t* p = str, * ret = str;
    //字符串结尾为0,非0就继续
    while (*p)
    {
        //空格0x20 全角空格0x3000  制表符0x09
        if (*p == L' ' || *p == 0x3000 || *p == L'\t')
        {
            p++;
        }
        else
        {
            *str++ = *p++;
        }
    }
    *str = 0;
    return ret;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本字母已经转为大写
char* demo_hys::str_toupper(char* str)
{
    char* p = str;
    char con = 'a' - 'A';
    //字符串结尾为0,非0就继续
    while (*p)
    {
        if (*p <= 'z' && *p >= 'a')
        {
            *p++ = *p - con;
        }   
        p++;
    }
    return str;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本字母已经转为大写
wchar_t* demo_hys::str_toupper(wchar_t* str)
{
    wchar_t* p = str;
    wchar_t con = 'a' - 'A';
    //字符串结尾为0,非0就继续
    while (*p)
    {
        if (*p <= L'z' && *p >= L'a')
        {
            *p++ = *p - con;
        }
        p++;
    }
    return str;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本字母已经转为小写
char* demo_hys::str_tolower(char* str)
{
    char* p = str;
    char con = 'a' - 'A';
    //字符串结尾为0,非0就继续
    while (*p)
    {
        if (*p <= 'Z' && *p >= 'A')
        {
            *p++ = *p + con;
        }
        p++;
    }
    return str;
}
//---------------------------------------------------------------------------------------
//返回值是文本的指针,没有特殊使用无视返回值即可,传进来的文本字母已经转为小写
wchar_t* demo_hys::str_tolower(wchar_t* str)
{
    wchar_t* p = str;
    wchar_t con = 'a' - 'A';
    //字符串结尾为0,非0就继续
    while (*p)
    {
        if (*p <= L'Z' && *p >= L'A')
        {
            *p++ = *p + con;
        }
        p++;
    }
    return str;
}
//---------------------------------------------------------------------------------------
//尾部清零是为了处理某没有初始化的变量,或者是保存文本_本身有数据之类的情况
void demo_hys::str_sub(char* str, size_t start, size_t len, char* outstr, bool endzero)
{
    str += start;
    for (size_t i = 0; i < len; i++)
    {
        *outstr++ = *str;
    }   
    if (endzero)
    {
        *outstr = NULL;
    }
}
//---------------------------------------------------------------------------------------
//【尾部清零】是为了处理某没有初始化的变量,或者是保存文本_本身有数据之类的情况
void demo_hys::str_sub(wchar_t* str, size_t start, size_t len, wchar_t* outstr, bool endzero)
{
    str += start;
    for (size_t i = 0; i < len; i++)
    {
        *outstr++ = *str;
    }
    if (endzero)
    {
        *outstr = NULL;
    }
}
//---------------------------------------------------------------------------------------
//一样返回真,不一样返回假
bool demo_hys::str_cmp(char* str1, char* str2)
{
    while (*str1 && *str2)
    {
        if (*str1++ != *str2++)
        {
            return false;
        }
    }   
    return true;
}
//---------------------------------------------------------------------------------------
//一样返回真,不一样返回假
bool demo_hys::str_cmp(wchar_t* str1, wchar_t* str2)
{
    while (*str1 && *str2)
    {
        if (*str1++ != *str2++)
        {
            return false;
        }
    }
    return true;
}
//---------------------------------------------------------------------------------------
//返回文本长度_替换次数默认无限
size_t demo_hys::str_replace(char* str, char* find, char* rep, char* outstr, intptr_t count)
{
    size_t len = NULL;
    size_t findlen = str_len(find);
    while (*str)
    {
        //比较文本是否一样,真就代表有需要查找的文本,所以我们需要替换
        if (!count && str_cmp(str, find))
        {
            while (*rep) { *outstr++ = *rep++;    len++; }
            if (!count) { count--; }    str += findlen;
        }
        //如果不一样就继续下一个
        else { *outstr++ = *str++; len++; }
    }
    return len;
}
//---------------------------------------------------------------------------------------
//返回文本长度_替换次数默认无限
size_t demo_hys::str_replace(wchar_t* str, wchar_t* find, wchar_t* rep, wchar_t* outstr, intptr_t count)
{
    size_t len = NULL;
    size_t findlen = str_len(find);
    while (*str)
    {
        //比较文本是否一样,真就代表有需要查找的文本,所以我们需要替换
        if (!count && str_cmp(str, find))
        {
            while (*rep)
            {
                *outstr++ = *rep++;   
                len++;
            }
            if (!count)
            {
                count--;
            }
            str += findlen;
        }
        //如果不一样就继续下一个
        else
        {
            *outstr++ = *str++;
            len++;
        }
    }
    return len;
}
//---------------------------------------------------------------------------------------
//返回要查找的文本第一个字符所在位置_失败返回-1
intptr_t demo_hys::str_find(char* str, char* find)
{
    char* p = str;   
    while (*p)
    {
        if (str_cmp(p++, find))
        {
            return --p - str;
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//返回要查找的文本第一个字符所在位置_失败返回-1
intptr_t demo_hys::str_find(wchar_t* str, wchar_t* find)
{
    wchar_t* p = str;
    while (*p)
    {
        if (str_cmp(p++, find))
        {
            return --p - str;
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//返回要查找的文本第一个字符所在位置_失败返回-1
intptr_t demo_hys::str_rfind(char* str, char* rfind)
{
    size_t len = str_len(str);
    size_t rfindlen = str_len(rfind);
    char* p = str + len - rfindlen;
    while (*p)
    {
        if (str_cmp(p--, rfind))
        {
            return ++p - str;
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//返回要查找的文本第一个字符所在位置_失败返回-1
intptr_t demo_hys::str_rfind(wchar_t* str, wchar_t* rfind)
{
    size_t len = str_len(str);
    size_t rfindlen = str_len(rfind);
    wchar_t* p = str + len - rfindlen;
    while (*p)
    {
        if (str_cmp(p--, rfind))
        {
            return ++p - str;
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//源文本为要写入拷贝文本的指针地址_返回源文本指针
char* demo_hys::str_copy(char* str, char* copy, bool endzero)
{
    char* p = str;
    while (*copy)
    {
        *str++ = *copy++;
    }
    if (endzero)
    {
        *str = NULL;
    }
    return p;
}
//---------------------------------------------------------------------------------------
//源文本为要写入拷贝的指针地址_返回源指针
wchar_t* demo_hys::str_copy(wchar_t* str, wchar_t* copy, bool endzero)
{
    wchar_t* p = str;
    while (*copy)
    {
        *str++ = *copy++;
    }
    if (endzero)
    {
        *str = NULL;
    }
    return p;
}
//---------------------------------------------------------------------------------------
//对比字节集,一样返回真,失败返回假_对比长度如果只有一个字节,就不要用这个对比了
bool demo_hys::hexcmp(char* hex1, char* hex2, size_t count)
{
    size_t loop = 1;
    //对比第一个和最后一个字节是否一致
    if (*hex1 != *hex2 ||
        *(hex1++ + --count) != *(hex2++ + count--))
    {
        return false;
    }
    //先双数后单数循环对比
match:
    for (size_t i = loop; i < count; i += 2)
    {
        if (*(hex1 + i) != *(hex2 + i))
        {
            return false;
        }
    }
    if (loop)
    {
        loop = 0;
        goto match;
    }
    return true;
}
//---------------------------------------------------------------------------------------
//成功返回查找字节集第一个字节的位置,失败返回-1
intptr_t demo_hys::hexfind(char* hex, size_t hexlen, char* find, size_t findlen)
{
    char* p = hex;
    if (findlen == 1)
    {
        while (hexlen--)
        {
            if (*p++ == *find)
            {
                return --p - hex;
            }
        }
    }
    else
    {
        while (hexlen-- >= findlen)
        {
            if (hexcmp(p++, find, findlen))
            {
                return --p - hex;
            }
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//成功返回查找字节集第一个字节的位置,失败返回-1
intptr_t demo_hys::hexrfind(char* hex, size_t hexlen, char* find, size_t findlen)
{
    char* p = hex + hexlen - findlen;
    while (hexlen-- >= findlen)
    {
        if (hexcmp(p--, find, findlen))
        {
            return ++p - hex;
        }
    }
    return -1;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define NULL 0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
size_t demo_hys::str_len(char* str)
{
    char* p = str;
    while (*p) { p++; }
    return p - str;
}
//---------------------------------------------------------------------------------------
size_t demo_hys::str_len(wchar_t* str)
{
    wchar_t* p = str;
    while (*p) { p++; }
    return p - str;
}
//---------------------------------------------------------------------------------------
char* demo_hys::str_reverse(char* str)
{
    size_t slen = str_len(str);
    char* p = str + slen;
    char tmp = NULL;
    while (--p > str)
    {
        tmp = *str;
        *str++ = *p;
        *p = tmp;
    }
    return str - slen / 2;
}
//---------------------------------------------------------------------------------------
wchar_t* demo_hys::str_reverse(wchar_t* str)
{
    size_t slen = str_len(str);
    wchar_t* p = str + slen; wchar_t tmp = NULL;
    while (--p > str)
    {
        tmp = *str;
        *str++ = *p;
        *p = tmp;
    }
    return str - slen / 2;
}
//---------------------------------------------------------------------------------------
//把单个字符【0-9】和【a-f】转为数值_失败返回-1
int demo_hys::ctoi(char* c)
{
    //判断是否0-9的字符
    if (*c <= '9')
    {
        if (*c >= '0')
        {
            return *c - '0';
        }
    }
    //判断是否A-F的字符
    else if (*c <= 'F')
    {
        if (*c >= 'A')
        {
            return *c - 'A' + 10;
        }
    }
    //判断是否a-f的字符
    else if (*c <= 'f')
    {
        if (*c >= 'a')
        {
            return *c - 'a' + 10;
        }
    }
    //失败返回【-1
    return -1;
}
//---------------------------------------------------------------------------------------
//把单个字符【0-9】和【a-f】转为数值_失败返回-1
int demo_hys::ctoi(wchar_t* c)
{
    //判断是否0-9的字符
    if (*c <= L'9')
    {
        if (*c >= L'0')
        {
            return *c - L'0';
        }
    }
    //判断是否A-F的字符
    else if (*c <= L'F')
    {
        if (*c >= L'A')
        {
            return *c - L'A' + 10;
        }
    }
    //判断是否a-f的字符
    else if (*c <= L'f')
    {
        if (*c >= L'a')
        {
            return *c - L'a' + 10;
        }
    }
    //失败返回【-1
    return -1;
}
//---------------------------------------------------------------------------------------
//失败返回值【S】,成功返回一个字符【1-9】【A-F】
wchar_t demo_hys::itoc(size_t num)
{
    if (num <= 9)
    {
        return (wchar_t)((num + '0') & 0xff);//数字
    }
    else if (num <= 15)
    {
        return (wchar_t)((num - 10 + 'A') & 0xff); //字符
    }
    else
    {
        return L'S';//错误
    }
}
//---------------------------------------------------------------------------------------
//成功返回十六进制数值,失败返回【-1
intptr_t demo_hys::strtohex(char* str)
{
    int tens = NULL, ones = NULL;   
    tens = ctoi(str++);    //十位数
    ones = ctoi(str);//个位数
    if (ones == -1 || tens == -1)
    {
        return -1;
    }   
    return ones * 16 + tens;
}
//---------------------------------------------------------------------------------------
//成功返回十六进制数值,失败返回【-1
intptr_t demo_hys::strtohex(wchar_t* 文本)
{
    int tens = NULL, ones = NULL;   
    tens = ctoi(文本++);//十位数   
    ones = ctoi(文本);//个位数
    if (ones == -1 || tens == -1)
    {
        return -1;
    }   
    return ones * 16 + tens;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::hextoint(char* str)
{
    char pint[10] = { 0 };
    intptr_t ilen = sizeof(intptr_t);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < ilen)
    {
        ilen = hexlen;
    }
    while (ilen--)
    {
        pint[ilen] = (char)strtohex(str);
        str += 2;
    }
    return *(intptr_t*)pint;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::hextoint(wchar_t* str)
{
    char pint[10] = { 0 };
    intptr_t ilen = sizeof(intptr_t);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < ilen)
    {
        ilen = hexlen;
    }
    while (ilen--)
    {
        pint[ilen] = (char)strtohex(str);
        str += 2;
    }
    return *(intptr_t*)pint;
}
//---------------------------------------------------------------------------------------
llong demo_hys::hextolong(char* str)
{
    char pint[22] = { 0 };
    intptr_t longlen = sizeof(llong);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < longlen)
    {
        longlen = hexlen;
    }
    while (longlen--)
    {
        pint[longlen] = (char)strtohex(str);
        str += 2;
    }
    return *(llong*)pint;
}
//---------------------------------------------------------------------------------------
llong demo_hys::hextolong(wchar_t* str)
{
    char pint[22] = { 0 };
    intptr_t longlen = sizeof(llong);
    intptr_t hexlen = str_len(str) / 2;
    if (hexlen < longlen)
    {
        longlen = hexlen;
    }
    while (longlen--)
    {
        pint[longlen] = (char)strtohex(str);
        str += 2;
    }
    return *(llong*)pint;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
char* demo_hys::inttohex(intptr_t num, char* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(intptr_t);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
wchar_t* demo_hys::inttohex(intptr_t num, wchar_t* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(intptr_t);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
char* demo_hys::longtohex(llong num, char* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(llong);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//返回文本指针_可以忽略返回值_直接使用传进来的结果文本
wchar_t* demo_hys::longtohex(llong num, wchar_t* phex)
{
    char* p = (char*)&num;
    size_t ilen = sizeof(llong);
    phex += ilen * 2;
    char ichar[10] = { 0 };
    *phex = NULL;
    while (ilen--)
    {
        ichar[0] = *p++;
        *--phex = (char)itoc(*(size_t*)ichar % 16);
        *--phex = (char)itoc(*(size_t*)ichar / 16);
    }
    return phex;
}
//---------------------------------------------------------------------------------------
//只取连续的【0-9】数字的长度
size_t demo_hys::str_getnumlenth(char* str)
{
    char* p = str;
    while (*p <= '9' && *p >= '0')
    {
        p++;
    }
    return p - str;
}
//---------------------------------------------------------------------------------------
//只取连续的【0-9】数字的长度
size_t demo_hys::str_getnumlenth(wchar_t* str)
{
    wchar_t* p = str;
    while (*p <= L'9' && *p >= L'0')
    {
        p++;
    }
    return p - str;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::inttostr(intptr_t num, char* pstr)
{
    char* p = pstr;
    size_t flag = NULL;//负数标志位
    if (!num)
    {
        *pstr++ = '0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = '-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = (char)itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::inttostr(intptr_t num, wchar_t* pstr)
{
    wchar_t* p = pstr;
    size_t flag = NULL;//负数
    if (!num)
    {
        *pstr++ = L'0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = L'-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::longtostr(llong num, char* pstr)
{
    char* p = pstr;
    size_t flag = NULL;//负数
    if (!num)
    {
        *pstr++ = '0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = '-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = (char)itoc(num % 10);
        num /= 10;
    }
    *p = NULL;
    str_reverse(pstr);
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
//返回文本长度
size_t demo_hys::longtostr(llong num, wchar_t* pstr)
{
    wchar_t* p = pstr;
    size_t flag = NULL;
    if (!num)
    {
        *pstr++ = L'0';
        *pstr = NULL;
        return 1;
    }
    if (num < 0)
    {
        *p++ = L'-';
        num *= -1;
        pstr++;
        flag = 1;
    }
    while (num > 0)
    {
        *p++ = itoc(num % 10);
        num /= 10;
    }
    *p = NULL;   
    str_reverse(pstr);   
    return p - pstr + flag;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::strtoint(char* str)
{
    intptr_t num = NULL;
    intptr_t bit = 1;
    intptr_t flag = 1;//正负数标志位
    int i = NULL;
    if (*str == '-')
    {
        flag = -1;
        str++;
    }
    size_t len = str_getnumlenth(str);
    --str += len;
    while (len--)
    {
        i = ctoi(str--);
        if (i == -1) { break; }
        num += i * bit;
        bit *= 10;
    }   
    return num * flag;
}
//---------------------------------------------------------------------------------------
intptr_t demo_hys::strtoint(wchar_t* str)
{
    intptr_t num = NULL;
    intptr_t bit = 1;
    intptr_t flag = 1;//正负数标志位
    int i = NULL;
    if (*str == L'-')

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 2
支持
分享
最新回复 (1)
雪    币: 5288
活跃值: (4036)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
注释再完整点的话都快赶上易语言精易模块的文本处理集了。给力!
2021-11-30 00:22
0
游客
登录 | 注册 方可回帖
返回
//