📄 string.c
字号:
/* MShowTec - www.mshowtec.com
** msLinux string.c ver1.0
** 20051221 lmjx create limiao@mshowtec.com
**
*/
#define MSLINUX_STRING_C
__inline char *strcpy (char *dest, const char *src)
{
char *p = dest;
while(*src){
*dest++ = *src++;
}
*dest = 0;
return p;
}
__inline char *strncpy (char *dest, const char *src, int count)
{
char *p = dest;
while((*src)&&(count>0)){
*dest++ = *src++;
count--;
}
while(count>0){
*dest++ = 0;
count--;
}
*dest = 0;
return p;
}
__inline char *strcat (char *dest, const char *src)
{
char *tmp = dest;
while (*dest)
dest++;
while ((*dest++ = *src++) != '\0')
;
return tmp;
}
__inline char *strncat (char *dest, const char *src, int count)
{
char *tmp = dest;
if (count) {
while (*dest)
dest++;
while ((*dest++ = *src++)) {
if (--count == 0) {
*dest = '\0';
break;
}
}
}
return tmp;
}
__inline int strcmp (const char *csrc, const char *ct)
{
while( 1 )
{
unsigned char s1 = *csrc++;
unsigned char s2 = *ct++;
if( s1 > s2 )
return 1;
else if( s1 < s2 )
return -1;
else if( s1 == 0 && s2 == 0 )
break;
}
return 0;
}
__inline int strncmp(const char * cs,const char * ct,int count)
{
while( count-- )
{
unsigned char s1 = *cs++;
unsigned char s2 = *ct++;
if( s1 > s2 )
return 1;
else if( s1 < s2 )
return -1;
else if( s1 == 0 && s2 == 0 )
break;
}
return 0;
}
/**
* strchr - Find the first occurrence of a character in a string
* @s: The string to be searched
* @c: The character to search for
*/
__inline char * strchr(const char * s, int c)
{
for(; *s != (char) c; ++s)
if (*s == '\0')
return 0;
return (char *) s;
}
/**
* strrchr - Find the last occurrence of a character in a string
* @s: The string to be searched
* @c: The character to search for
*/
__inline 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 0;
}
__inline int strlen (const char *s)
{
int ret = 0;
while(*s++){
ret++;
}
return ret;
}
/**
* strspn - Calculate the length of the initial substring of @s which only
* contain letters in @accept
* @s: The string to be searched
* @accept: The string to search for
*/
__inline unsigned int strspn(const char *s, const char *accept)
{
const char *p;
const char *a;
unsigned int 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;
}
__inline void *memcpy (void *dest, const void *src, int n)
{
void *p = dest;
while(n>0){
*((unsigned char*)dest)++ = *((unsigned char*)src)++;
n--;
}
return p;
}
/**
* memmove - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* Unlike memcpy(), memmove() copes with overlapping areas.
*/
__inline void * memmove(void * dest,const void *src,int 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;
}
/**
* memcmp - Compare two areas of memory
* @cs: One area of memory
* @ct: Another area of memory
* @count: The size of the area.
*/
__inline int memcmp(const void * cs,const void * ct,int count)
{
const unsigned char *su1, *su2;
int res = 0;
for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0)
break;
return res;
}
/**
* memscan - Find a character in an area of memory.
* @addr: The memory area
* @c: The byte to search for
* @size: The size of the area.
*
* returns the address of the first occurrence of @c, or 1 byte past
* the area if @c is not found
*/
__inline void * memscan(void * addr, int c, int size)
{
unsigned char * p = (unsigned char *) addr;
while (size) {
if (*p == c)
return (void *) p;
p++;
size--;
}
return (void *) p;
}
/**
* strstr - Find the first substring in a %NUL terminated string
* @s1: The string to be searched
* @s2: The string to search for
*/
__inline 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 0;
}
/**
* memchr - Find a character in an area of memory.
* @s: The memory area
* @c: The byte to search for
* @n: The size of the area.
*
* returns the address of the first occurrence of @c, or %NULL
* if @c is not found
*/
void *memchr(const void *s, int c, int n)
{
const unsigned char *p = s;
while (n-- != 0) {
if ((unsigned char)c == *p++) {
return (void *)(p-1);
}
}
return 0;
}
__inline void *memset (void *s, char c, int count)
{
void *p = s;
while(count>0){
*((unsigned char*)s)++ = c;
}
return p;
}
__inline void itoa(int value, char *string, int radix)
{
int i;
int count = 0;
unsigned long temp;
unsigned long res;
char ch[20];
if(radix==10 && value<0){ch[0] = '-';count++;temp = 0-value;}
else{temp = value;}
while(1)
{
res = temp%radix;
temp=temp/radix;
if(res<10) ch[count++]=res+'0';
else ch[count++]=res-10+'a';
if(temp==0)
break;
}
ch[count]='\0';
string[0]='\0';
for(i=0;i<count;i++)
string[count-i-1]=ch[i];
string[count]='\0';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -