C 某些库函数中字符串处理函数的具体实现

其中包括:strcpy(char *, char *),strncpy();strcat();strncat();strcmp();strncmp();strchr();strrchr();strlen();

strdup();strspn();strpbrk();strtok(); memset();memcpy();memmove();memcmp();memscan();strstr();

_strupr_();_strlen_();_strsplit_();_hex2dec_();_str2hex_();_str2dec_();

 

/*=========================================================================
  工程名称:C语言
  组成文件:string_lib.c
  功能描述:常用C库函数的C实现
  程序分析:
  维护记录:
=========================================================================*/

char * ___strtok = NULL;


char * strcpy(char * dest,const char *src)
{
	char *tmp = dest;

	while ((*dest++ = *src++) != '\0')
		/* nothing */;
	return tmp;
}



char * strncpy(char * dest,const char *src,size_t count)
{
	char *tmp = dest;

	while (count-- && (*dest++ = *src++) != '\0')
		/* nothing */;

	return tmp;
}



char * strcat(char * dest, const char * src)
{
	char *tmp = dest;

	while (*dest)
		dest++;
	while ((*dest++ = *src++) != '\0')
		;
	return tmp;
}



char * strncat(char *dest, const char *src, size_t count)
{
	char *tmp = dest;

	if (count) 
	{
		while (*dest)
			dest++;
		while ((*dest++ = *src++)) 
		{
			if (--count == 0) 
			{
				*dest = '\0';
				break;
			}
		}
	}
	return tmp;
}


int strcmp(char *s1, char *s2)
{
	while(*s1 && *s2)
	{
		if(*s1 - *s2)
			return (*s1 - *s2);
		s1++;
		s2++;
	}
	if(*s1 != 0)
		return 1;
	else
		return -1;
	return 0;
}


int strncmp(char *s1, char *s2, int len)
{
	while(len--)
	{
		if(*s1 && *s2)
		{
			if(*s1 - *s2)
				return (*s1 - *s2);
			s1++;
			s2++;
		}
		else
		{
			if(*s1 || *s2)
				return(*s1? *s1: *s2);
		}
	}
	return 0;
}


char * strchr(const char * s, int c)
{
	for(; *s != (char) c; ++s)
		if (*s == '\0')
			return NULL;
	return (char *) s;
}


char * strrchr(const char * s, int c)
{
       const char *p = s + strlen(s);
       do 
       {
	   			if (*p == (char)c)
	       	return (char *)p;
       } 
       while (--p >= s);
       return NULL;
}


size_t strlen(const char * s)
{
	const char *sc;

	for (sc = s; *sc != '\0'; ++sc)
		/* nothing */;
	return sc - s;
}


char * strdup(const char *s)
{
	char *new;

	if ( (s == NULL) || ((new = malloc (strlen(s) + 1)) == NULL) )
	 {
		return NULL;
	}

	strcpy (new, s);
	return new;
}


size_t strspn(const char *s, const char *accept)
{
	const char *p;
	const char *a;
	size_t count = 0;

	for (p = s; *p != '\0'; ++p) 
	{
		for (a = accept; *a != '\0'; ++a) 
		{
			if (*p == *a)
				break;
		}
		if (*a == '\0')
			return count;
			
		++count;
	}

	return count;
}


char * strpbrk(const char * cs,const char * ct)
{
	const char *sc1,*sc2;

	for( sc1 = cs; *sc1 != '\0'; ++sc1) 
	{
		for( sc2 = ct; *sc2 != '\0'; ++sc2) 
		{
			if (*sc1 == *sc2)
				return (char *) sc1;
		}
	}
	return NULL;
}


char * strtok(char * s,const char * ct)
{
	char *sbegin, *send;

	sbegin  = s ? s : ___strtok;
	if (!sbegin) 
	{
		return NULL;
	}
	sbegin += strspn(sbegin,ct);
	if (*sbegin == '\0') 
	{
		___strtok = NULL;
		return( NULL );
	}
	send = strpbrk( sbegin, ct);
	if (send && *send != '\0')
		*send++ = '\0';
	___strtok = send;
	return (sbegin);
}


void * memset(void * s,char c,size_t count)
{
	char *xs = (char *) s;

	while (count--)
		*xs++ = c;

	return s;
}


void * memcpy(void * dest,const void *src,size_t count)
{
	char *tmp = (char *) dest, *s = (char *) src;

	while (count--)
		*tmp++ = *s++;

	return dest;
}


void * memmove(void * dest,const void *src,size_t count)
{
	char *tmp, *s;

	if (dest <= src) 
	{
		tmp = (char *) dest;
		s = (char *) src;
		while (count--)
			*tmp++ = *s++;
	}
	else 
	{
		tmp = (char *) dest + count;
		s = (char *) src + count;
		while (count--)
			*--tmp = *--s;
	}

	return dest;
}


int memcmp(const void * cs,const void * ct,size_t count)
{
	const unsigned char *su1, *su2;
	signed char res = 0;

	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
		if ((res = *su1 - *su2) != 0)
			break;
	return res;
}


/*
 * find the first occurrence of byte 'c', or 1 past the area if none
 */
void * memscan(void * addr, int c, size_t size)
{
	unsigned char * p = (unsigned char *) addr;

	while (size) {
		if (*p == c)
			return (void *) p;
		p++;
		size--;
	}
	return (void *) p;
}


char * strstr(const char * s1,const char * s2)
{
	int l1, l2;

	l2 = strlen(s2);
	if (!l2)
		return (char *) s1;
	l1 = strlen(s1);
	while (l1 >= l2) 
	{
		l1--;
		if (!memcmp(s1,s2,l2))
			return (char *) s1;
		s1++;
	}
	return NULL;
}

//=============================================================
// 语法格式:	char *_strupr_(char *Str)
// 实现功能:	将字符串转换为全大写
// 参数:		Str	-	输入字符串首地址
// 返回值:		输出字符串首地址
// 注意:		该程序将直接修改原始字符串
//=============================================================
char *_strupr_(char *Str)
{
	char *cp;
	cp = Str;
	while (*cp != 0)
	{
		if (*cp >= 'a' && *cp <= 'z' )
		{
			*cp -=  'a' - 'A';
		}
		cp++;
	}
	return Str;
}

//=============================================================
// 语法格式:	int _strlen_(char *s)
// 实现功能:	求取字符串长度
// 参数:		s	-	输入字符串首地址
// 返回值:		字符串长度
//=============================================================
int _strlen_(char *s)
{
	int i = 0;
	while(*s)
	{
		i++;
		s++;
	}
	return i;
}


//=============================================================
// 语法格式:	int _strsplit_(char *sour, char *splstr, char *dest[])
// 实现功能:	字符串分割
// 参数:		sour	-	原始字符串首地址
//				splstr	-	分割字符串
//				dest[]	-	分割之后的每个字符串的首地址
// 返回值:		分割之后的字符串数量
//=============================================================
int _strsplit_(char *sour, char *splstr, char *dest[])
{
	int i = 0;
	int len;
	char *cp;
	if(*splstr == 0)
		return 0;

	cp = sour;

	dest[i] = cp;
	len = _strlen_(splstr);
	while(*sour)
	{
		if(*sour == *splstr)
		{
			if(_strncmp_(sour, splstr, _strlen_(splstr)) == 0)
			{
				i++;
				sour += len - 1;
				*cp++ = 0;
				dest[i] = cp;
			}
		}
		else cp++;
	   sour++;
	}
	return (i + 1);
}

//=============================================================
// 语法格式:	unsigned int _hex2dec_(unsigned int Binary)
// 实现功能:	二进制转BCD码(仅转换输入数据的低16bit)
// 参数:		Binary	-	输入数据
// 返回值:		BCD码
//=============================================================
unsigned int _hex2dec_(unsigned int Binary)
{
	unsigned int ulRet = 0;
	Binary &= 0xffff;
	while(Binary >= 10000)
	{
		ulRet += 10000;
		Binary -= 10000;
	}
	while(Binary >= 1000)
	{
		ulRet += 0x1000;
		Binary -= 1000;
	}
	while(Binary >= 100)
	{
		ulRet += 0x0100;
		Binary -= 100;
	}
	while(Binary >= 10)
	{
		ulRet += 0x0010;
		Binary -= 10;
	}
	ulRet += Binary;
	return (ulRet);
}

//=============================================================
// 语法格式:	int _str2hex_(char *s)
// 实现功能:	将一个字符串转换为十六进制数据
// 参数:		s	-	输入字符串
// 返回值:		字符串代表的十六进制数据
//=============================================================
int _str2hex_(char *s)
{
	int Ret = 0;
	s = _strupr_(s);
	if((s[0] != '0') || (s[1] != 'X'))
		return 0;
	s += 2;
	while(*s)
	{
		Ret <<= 4;
		if((*s >= '0') && (*s <= '9'))
			Ret += ((int)*s - '0');
		else if((*s >= 'A') && (*s <= 'Z'))
			Ret += ((int)*s - 'A' + 0x0a);
		else
			return 0;
		s++;
	}
	return Ret;
}

//=============================================================
// 语法格式:	int _str2dec_(char *s)
// 实现功能:	将一个字符串转换为十进制数据
// 参数:		s	-	输入字符串
// 返回值:		字符串代表的十进制数据
//=============================================================
int _str2dec_(char *s)
{
	int Ret, Flag = 0;
	Ret = 0;
	if(*s == '-')
	{
		Flag = 1;
		s++;
	}
	while(*s)
	{
		if((*s < '0') || (*s > '9'))
			return 0;
		Ret = Ret * 10 + (int)(*s - '0');
		s++;
	}
	if(Flag)
		Ret *= -1;
	return Ret;
}


 

你可能感兴趣的:(C 某些库函数中字符串处理函数的具体实现)