留下自己的学习笔记
// stddef.h
截取一下stddef 头文件里面的一个类型定义,不然到后面有像我一样的菜菜不知道什么size_t是个什么类型的东东
typedef int ptrdiff_t;
typedef unsigned int size_t;
strlen (const char *s)
{
__SIZE_TYPE__ i;//
i = 0;
while (s[i] != 0)
i++;
return i;
}
char *strcpy (char *d, const char *s)
{
char *r = d;
#if defined __OPTIMIZE__ && !defined __OPTIMIZE_SIZE__
if (inside_main)
abort ();
#endif
while ((*d++ = *s++));
return r;
}
int strcmp (const char *s1, const char *s2)
{
#ifdef __OPTIMIZE__
if (inside_main)
abort ();
#endif
while (*s1 != 0 && *s1 == *s2)
s1++, s2++;
if (*s1 == 0 || *s2 == 0)
return (unsigned char) *s1 - (unsigned char) *s2;
return *s1 - *s2;
}
char * strncpy(char *s1, const char *s2, size_t n)
{
char *dest = s1;
#ifdef __OPTIMIZE__
if (inside_main)
abort();
#endif
for (; *s2 && n; n--)
*s1++ = *s2++;
while (n--)
*s1++ = 0;
return dest;
}
int
strncmp(const char *s1, const char *s2, register size_t n)
{
register unsigned char u1, u2;
while (n-- > 0)
{
u1 = (unsigned char) *s1++;
u2 = (unsigned char) *s2++;
if (u1 != u2)
return u1 - u2;
if (u1 == '\0')
return 0;
}
return 0;
}
char *
strncat (char *s1, const char *s2, size_t n)
{
char *dest = s1;
char c = '\0';
#ifdef __OPTIMIZE__
if (inside_main)
abort();
#endif
while (*s1) s1++;
c = '\0';
while (n > 0)
{
c = *s2++;
*s1++ = c;
if (c == '\0')
return dest;
n--;
}
if (c != '\0')
*s1 = '\0';
return dest;
}
char *
strrchr (const char *s, int c)
{
__SIZE_TYPE__ i;
#ifdef __OPTIMIZE__
if (inside_main)
abort ();
#endif
i = 0;
while (s[i] != 0)
i++;
do
if (s[i] == c)
return (char *) s + i;
while (i-- != 0);
return 0;
}
char *
strpbrk(const char *s1, const char *s2)
{
const char *p;
#ifdef __OPTIMIZE__
if (inside_main)
abort ();
#endif
while (*s1)
{
for (p = s2; *p; p++)
if (*s1 == *p)
return (char *)s1;
s1++;
}
return 0;
}
strcspn (const char *s1, const char *s2)
{
const char *p, *q;
#ifdef __OPTIMIZE__
if (inside_main)
abort();
#endif
for (p = s1; *p; p++)
{
for (q = s2; *q; q++)
if (*p == *q)
goto proceed;
break;
proceed:;
}
return p - s1;
}
void bcopy (const void*, void*, size_t);
PTR
memcpy (PTR out, const PTR in, size_t length)
{
bcopy(in, out, length);
return out;
}
void
bcopy (const void *src, void *dest, size_t len)
{
if (dest < src)
{
const char *firsts = src;
char *firstd = dest;
while (len--)
*firstd++ = *firsts++;
}
else
{
const char *lasts = (const char *)src + (len-1);
char *lastd = (char *)dest + (len-1);
while (len--)
*lastd-- = *lasts--;
}
}
int
memcmp (const void *s1, const void *s2, __SIZE_TYPE__ len)
{
const unsigned char *sp1, *sp2;
#ifdef __OPTIMIZE__
if (inside_main)
abort ();
#endif
sp1 = s1;
sp2 = s2;
while (len != 0 && *sp1 == *sp2)
sp1++, sp2++, len--;
if (len == 0)
return 0;
return *sp1 - *sp2;
}
int
memcmp (const PTR str1, const PTR str2, size_t count)
{
register const unsigned char *s1 = (const unsigned char*)str1;
register const unsigned char *s2 = (const unsigned char*)str2;
while (count-- > 0)
{
if (*s1++ != *s2++)
return s1[-1] < s2[-1] ? -1 : 1;
}
return 0;
}
int
memcmp (const void *str1, const void *str2, size_t count)
{
const unsigned char *s1 = str1;
const unsigned char *s2 = str2;
while (count-- > 0)
{
if (*s1++ != *s2++)
return s1[-1] < s2[-1] ? -1 : 1;
}
return 0;
}
PTR
memchr (register const PTR src_void, int c, size_t length)
{
const unsigned char *src = (const unsigned char *)src_void;
while (length-- > 0)
{
if (*src == c)
return (PTR)src;
src++;
}
return NULL;
}
void *
memset (void *dst, int c, __SIZE_TYPE__ n)
{
/* Single-byte memsets should be done inline when optimisation
is enabled. */
#ifdef __OPTIMIZE__
if (inside_main && n < 2)
abort ();
#endif
while (n-- != 0)
n[(char *) dst] = c;
return 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 );
}