#include <stddef.h> /* size_t */
int memcmp(const void* s1, const void* s2,size_t n)
{
const unsigned char *p1 = s1, *p2 = s2;
while(n--)
if( *p1 != *p2 )
return *p1 - *p2;
else
*p1++,*p2++;
return 0;
}
#include <stddef.h> /* size_t */
void *memset(void *s, int c, size_t n)
{
unsigned char* p=s;
while(n--)
*p++ = (unsigned char)c;
return s;
}
#include <stddef.h> /* size_t */
void *memcpy(void *dest, const void *src, size_t n)
{
char *dp = dest;
const char *sp = src;
while (n--)
*dp++ = *sp++;
return dest;
}
int strcmp(const char* s1, const char* s2)
{
while(*s1 && (*s1==*s2))
s1++,s2++;
return *(const unsigned char*)s1-*(const unsigned char*)s2;
}
#ifdef _NC_RESTRICT
char *strcpy(char *restrict dest, const char *restrict src)
#else
char *strcpy(char *dest, const char* src)
#endif
{
char *ret = dest;
while (*dest++ = *src++)
;
return ret;
}
#include <stddef.h> /* for size_t */
size_t strlen(const char *s) {
size_t i;
for (i = 0; s[i] != '/0'; i++) ;
return i;
}
char *strcat(char *dest, const char *src)
{
char *ret = dest;
while (*dest)
dest++;
while (*dest++ = *src++)
;
return ret;
}
#include <string.h> /* strspn() strcspn() */
char *strtok(char * str, const char * delim)
{
static char* p=0;
if(str)
p=str;
else if(!p)
return 0;
str=p+strspn(p,delim);
p=str+strcspn(str,delim);
if(p==str)
return p=0;
p = *p ? *p=0,p+1 : 0;
return str;
}
#include <string.h> /* size_t strchr() */
size_t strspn(const char *s1, const char *s2)
{
size_t ret=0;
while(*s1 && strchr(s2,*s1++))
ret++;
return ret;
}
char *strchr(const char *s, int c)
{
while (*s != (char)c)
if (!*s++)
return 0;
return (char *)s;
}
常用C函数实现 C Standard Library
最新推荐文章于 2020-12-15 11:35:59 发布