VS2010 字符串函数源码汇总


// strcpy(p, p1)		复制字符串 源码在		strcat.c	中
// strncpy(p, p1, n)	复制指定长度字符串		strncpy.c
// strcat(p, p1)		附加字符串				strcat.c
// strncat(p, p1, n)	附加指定长度字符串		strncat.c
// strlen(p)			取字符串长度			strlen.c
// strcmp(p, p1)		比较字符串				strcmp.c
// strncmp(p, p1, n)	比较指定长度字符串		strncmp.c
// strcasecmp			忽略大小写比较字符串	stricmp.c	#define strcasecmp stricmp
// strchr(p, c)			在字符串中查找指定字符  strchr.c
// strrchr(p, c)		在字符串中反向查找		strrchr.c
// strstr(p, p1)		查找字符串				strstr.c
// strpbrk(p, p1)		以目标字符串的所有字符作为集合,在当前字符串查找该集合的任一元素 
// strspn(p, p1)		以目标字符串的所有字符作为集合,在当前字符串查找不属于该集合的任一元素的偏移 
// strcspn(p, p1)		以目标字符串的所有字符作为集合,在当前字符串查找属于该集合的任一元素的偏移  
// * 具有指定长度的字符串处理函数在已处理的字符串之后填补零结尾符 

/***
李小乾 注:
	1,const char * 关于const的使用
	2,“while(*p++ );” 代码指向后 p 指向的是 \0 后面的字符 

char * __cdecl strcpy(char * dst, const char * src)
{
	char * cp = dst;

	while( *cp++ = *src++ )
		;               /* Copy src over dst */

	return( dst );
}

/***
*char *strncpy(dest, source, count) - copy at most n characters
*
*Purpose:
*       Copies count characters from the source string to the
*       destination.  If count is less than the length of source,
*       NO NULL CHARACTER is put onto the end of the copied string.
*       If count is greater than the length of sources, dest is padded
*       with null characters to length count.
*/
char * __cdecl strncpy (
	char * dest,
	const char * source,
	size_t count
	)
{
	char *start = dest;

	while (count && (*dest++ = *source++))    /* copy string */
		count--;

	if (count)                              /* pad out with zeroes */
		while (--count)
			*dest++ = '\0';

	return(start);
}

/***
*char *strcat(dst, src) - concatenate (append) one string to another
*
*Purpose:
*       Concatenates src onto the end of dest.  Assumes enough
*       space in dest.
*/
char * __cdecl strcat (	char * dst,	const char * src )
{
	char * cp = dst;

	while( *cp )
		cp++;                   /* find end of dst */

	while( *cp++ = *src++ ) ;       /* Copy src to end of dst */

	return( dst );                  /* return dst */

}

/***
*char *strncat(front, back, count) - append count chars of back onto front
*
*Purpose:
*       Appends at most count characters of the string back onto the
*       end of front, and ALWAYS terminates with a null character.
*       If count is greater than the length of back, the length of back
*       is used instead.  (Unlike strncpy, this routine does not pad out
*       to count characters).
*/
char * __cdecl strncat (char * front,const char * back,	size_t count)
{
	char *start = front;

	while (*front++)
		;
	front--;

	while (count--)
		if (!(*front++ = *back++))
			return(start);

	*front = '\0';
	return(start);
}

size_t __cdecl strlen (	const char * str )
{
	const char *eos = str;

	while( *eos++ ) ; //eos 会指向 \0 后面的字符 故下面有 -1 操作

	return( eos - str - 1 );
}

/***
*Purpose:
*       Comparison is done byte by byte on an UNSIGNED basis, which is to
*       say that Null (0) is less than any other character (1-255).
*李小乾 注:
*       while语句 乃 关键代码 while退出循环 有 两个相与 条件
*       条件1,! (ret = *(unsigned char *)src - *(unsigned char *)dst) 
*			满足条件:两个字符串已经开始不相等了
*       条件2,*dst 满足条件:dst还没比较到 \0
*		
*		两个字符串 进行 比较,有下面三大类,六小类 情况								
*		1,strlen(str)<strlen(dst)
*			①str未遍历完 已分胜负
*			②str遍历完,即到\0便分胜负(因为\0不等于任何非\0字符,下同)
*		2,strlen(str)==strlen(dst)
*			③str,dst未遍历完 已分胜负
*			④str,dst遍历完,一起到\0便分胜负
*		3,strlen(str)>strlen(dst)
*			⑤dst未遍历完 已分胜负
*			⑥dst遍历完,即到\0便分胜负
*				
*		条件1,2 对应 上面六种情况
*		
*		while关键段可改为 
*		while( *str==*dst && *str),第二个条件*str也可以*dst
*		要是 == 用来比较 的话,就不用加(unsigned char *)了
*       结果可以用*(unsigned char *)str-*(unsigned char *)dst即可,更直观
*/
int __cdecl strcmp (const char * src,const char * dst )
{
	int ret = 0 ;

	while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
		++src, ++dst;

	if ( ret < 0 )
		ret = -1 ;
	else if ( ret > 0 )
		ret = 1 ;

	return( ret );
}

/***
*Purpose:
*   Compares two strings for lexical order.  The comparison stops after:
*   (1) a difference between the strings is found 
*	(2) the end of the strings is reached
*   (3) count characters have been compared
*/
int __cdecl strncmp (
	const char * first,
	const char * last,
	size_t count
	)
{
	size_t n = 0;

	if (!count)
		return(0);

	if( count >= 4 )
	{
		/* unroll by four */
		//李小乾 注:这里的功能当然就是为了for循环少点啦,
		//这里for循环 比较 n 后面四个 字符 
		for (; n < count-4; n += 4)
		{
			first += 4;
			last += 4;

			if (*(first - 4) == 0 || *(first - 4) != *(last - 4))
			{
				return (*(unsigned char *)(first - 4) - *(unsigned char *)(last - 4));
			}

			if (*(first - 3) == 0 || *(first - 3) != *(last - 3))
			{
				return (*(unsigned char *)(first - 3) - *(unsigned char *)(last - 3));
			}

			if (*(first - 2) == 0 || *(first - 2) != *(last - 2))
			{
				return (*(unsigned char *)(first - 2) - *(unsigned char *)(last - 2));
			}

			if (*(first - 1) == 0 || *(first - 1) != *(last - 1))
			{
				return (*(unsigned char *)(first - 1) - *(unsigned char *)(last - 1));
			}
		}
	}

	/* residual loop */
	for (; n < count; ++n)
	{
		if (*first == 0 || *first != *last)
		{
			return (*(unsigned char *)first - *(unsigned char *)last);
		}
		++first;
		++last;
	}

	return 0;
}
/***
*李小乾 注
*	strcasecmp 实现之一
*/
extern "C" int __cdecl __ascii_stricmp (
	const char * dst,
	const char * src
	)
{
	int f, l;

	do
	{
		if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') )
			f -= 'A' - 'a';
		if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') )
			l -= 'A' - 'a';
	}
	while ( f && (f == l) );

	return(f - l);
}

char * __cdecl strchr (	const char * string,int ch	)
{
	while (*string && *string != (char)ch)
		string++;

	if (*string == (char)ch)
		return((char *)string);
	return(NULL);
}

/***
*李小乾 注:
*	while (*string++) ; 后 string指向 \0后面的字符,
*	加一行 string--; 代码岂不更好
*/
char * __cdecl strrchr ( const char * string,int ch	)
{
	char *start = (char *)string;

	while (*string++)                       /* find end of string */
		;
	/* search towards front */
	while (--string != start && *string != (char)ch)
		;

	if (*string == (char)ch)                /* char found ? */
		return( (char *)string );

	return(NULL);
}

char * __cdecl strstr (	const char * str1,const char * str2	)
{
	char *cp = (char *) str1;
	char *s1, *s2;

	if ( !*str2 )
		return((char *)str1);

	while (*cp)
	{
		s1 = cp;
		s2 = (char *) str2;

		while ( *s1 && *s2 && !(*s1-*s2) )
			s1++, s2++;

		if (!*s2)
			return(cp);

		cp++;
	}

	return(NULL);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值