ls1x-master-v0.4/public/ls1x_string.c

483 lines
9.9 KiB
C
Raw Normal View History

2024-11-27 15:39:05 +08:00
#include "ls1x_string.h"
#include "ls1x_printf.h"
/***********************************************************************
function: cs和ct中字节
@param : cs
ct
count
@other :
@return : 0
1
@auther :
@time : 202049
***********************************************************************/
int memcmp(const void *cs, const void *ct, int count)
{
const unsigned char *su1, *su2;
for( su1 = cs, su2 = ct; count > 0; ++su1, ++su2, --count)
{
if (*su1 != *su2)
{
return 1;
}
}
return 0;
}
/***********************************************************************
function:
@param : s1
s2
n
@other :
@return :
@auther :
@time : 202049
***********************************************************************/
void *memcpy(void *s1, const void *s2, int n)
{
const unsigned char *psrc = s2;
volatile unsigned char *pdst = s1;
while (n-- > 0)
*pdst++ = *psrc++;
return s1;
}
/***********************************************************************
function:
@param : s
c
n
@other :
@return :
@auther :
@time : 202049
***********************************************************************/
void *memset(void *s, int c, int count)
{
unsigned char *xs = (unsigned char *) s;
while (count--)
*xs++ = c;
return s;
}
/***********************************************************************
function: sub_str是否是str的子串
@param : str
sub_str
num
@other :
@return : 0
1
@auther :
@time : 202049
***********************************************************************/
INT8U strstr(const INT8U *str, const INT8U *sub_str, INT8U num)
{
unsigned char cont = 0;
const unsigned char *p, *q;
while(*str != '\0')
{
cont = 0;
p = str;
q = sub_str;
if(*p == *q)
{
while(*p && *q && *p == *q)
{
p++;
q++;
cont += 1;
if(cont == num)
return 1;
}
}
str++;
}
return 0;
}
/***********************************************************************
function: needle you haystack的字符串
@param : haystack
needle
@other :
@return : 0
other
@auther :
@time : 202049
***********************************************************************/
char *pstrstr(const char *haystack, const char *needle)
{
char *cp = (char *)haystack;
char *s1, *s2;
if (!*needle)
return((char *)haystack);
while (*cp)
{
s1 = cp;
s2 = (char *)needle;
while(*s1 && *s2 && !(*s1 - *s2))
{
s1++;
s2++;
}
if(!*s2)
return(cp);
cp++;
}
return NULL;
}
char *strchr(const char *s, char c)
{
if (NULL == s) return NULL;
while ('0' != *s && c != *s) s++;
return ('0' == *s) ? NULL : (char *)s;
}
/***********************************************************************
:
@param:
pstr
@other:
@return:
0
1
***********************************************************************/
int atoi(char *pstr)
{
int Ret_Integer = 0;
int Integer_sign = 1;
if(pstr == NULL)
{
return 0;
}
if(*pstr == '-')
{
Integer_sign = -1;
}
if(*pstr == '-' || *pstr == '+')
{
pstr++;
}
while(*pstr >= '0' && *pstr <= '9')
{
Ret_Integer = Ret_Integer * 10 + *pstr - '0';
pstr++;
}
Ret_Integer = Integer_sign * Ret_Integer;
return Ret_Integer;
}
/***********************************************************************
:
@param:
chWord
pstr
@other:
@return:
***********************************************************************/
void itoa( char chWord[], int Num)
{
int i = 0, j;
char chTemp;
if(Num == 0)
{
chWord[i] = '0' + Num % 10;
i++;
}
while(Num != 0)
{
chWord[i] = '0' + Num % 10;
i++;
Num = Num / 10;
}
chWord[i] = '\0';
for(j = 0; j < i / 2; j++)
{
chTemp = chWord[j];
chWord[j] = chWord[i - 1 - j];
chWord[i - 1 - j] = chTemp;
}
return ;
}
/***********************************************************************
:
@param:
dst
src
@other:
@return:
***********************************************************************/
char *strcat(char *dst, const char *src)
{
char *d;
if (!dst || !src)
return (dst);
d = dst;
for (; *d; d++);
for (; *src; src++)
*d++ = *src;
*d = 0;
return (dst);
}
/***********************************************************************
:
@param:
dst
src
@other:
@return:
0
1
***********************************************************************/
int strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - * (const unsigned char *)--s2);
}
/***********************************************************************
:
@param:
dest
src
@other:
@return:
0
1
***********************************************************************/
char *strcpy(char *dest, const char *src)
{
char *r = dest;
while((*dest++ = *src++) != '\0');
return r;
}
/***********************************************************************
:
@param:
p
@other:
@return:
***********************************************************************/
int strlen(const char *p)
{
int len = 0;
while(*p++)
len++;
return len;
}
int isContained(const char *str, char c)
{
if (str == NULL)
{
return 0;
}
const char *p = str;
while (*p != '\0')
{
if (*p == c)
{
return 1;
}
p++;
}
return 0;
}
char *strtok_r(char *str, const char *sep, char **lasts)
{
char *str_return = NULL; //返回的字符串
const char *ctrl = sep; //分隔符
if (sep == NULL)
{
return NULL;
}
if (str == NULL && *lasts == NULL)
{
return NULL;
}
//将分隔符放入map中
char map[255] = {0};
int len = 0;
while (*ctrl != '\0')
{
if (isContained(map, *ctrl))
{
continue;
}
map[len] = *ctrl;
len++;
ctrl++;
}
if (str == NULL)
{
str_return = *lasts;
}
else
{
str_return = str;
}
//忽略掉字符串中起始部分的分隔符,找到第一个不是分隔符的字符指针
while(*str_return != '\0')
{
if (isContained(map, *str_return))
{
str_return++;
continue;
}
break;
}
str = str_return;
//查找第一个分隔符
while (*str_return)
{
if (isContained(map, *str_return))
{
*str_return++ = '\0'; //当找到时把匹配字符填为0并且把str指向下一位
break;
}
str_return++;
}
*lasts = str_return; // 把剩余字符串的指针保存到静态变量last
if (str == str_return)
{
*lasts = NULL;
return NULL;
}
else
{
return str;
}
}
/*
* 16
* @param
* dest
* src
* len
*/
void hex_to_str(INT8U *dest, INT8U *src, INT8S len)
{
INT32S i;
for (i = 0; i < len; i++)
{
sprintf((INT8S *)dest + i * 2, "%02x", *(src + i));
}
}
/*
* 16
* @param
* hex 16
* dsp
* len
*/
void str_to_hex(INT8U *hex, INT8U *dsp, INT8S len)
{
int i, j;
for (i = 0; i < len / 8; i++)
{
for (j = 0; j < 8; j++)
{
hex[i] |= dsp[i * 8 + j] << (8 - j);
}
}
}
/*
* 16ASCII整型数值
* @param
* str
* @return
* int ASCII整型数值
*/
int atohex(char *str)
{
int singleResult = 0;
int totalResult = 0;
INT8U i, j;
INT8U lenth = strlen(str);
for(i = 0; i < lenth; i++)
{
singleResult = str[i];
if ((singleResult >= '0') && (singleResult <= '9'))
{
singleResult = (str[i] - '0');
}
else
{
if ((singleResult >= 'a') && (singleResult <= 'f'))
{
singleResult = (str[i] - 'a') + 10;
}
else
{
if ((singleResult >= 'A') && (singleResult <= 'F'))
{
singleResult = (str[i] - 'A') + 10;
}
else
{
return 0;
}
}
}
for (j = 0; j < (lenth - 1 - i); j++)
{
singleResult = singleResult * 16;
}
totalResult += singleResult;
}
return totalResult;
}