C字符串处理函数的实现
C字符串处理函数的实现(Linux)
#include
#include
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)
break;
}
}
return tmp;
}
int strcmp(const char * cs,const char * ct)
{
register signed char __res;
while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++) /*is !*cs++ necessary? incase more cmp*/
break;
}
return __res;
}
int strncmp(const char * cs,const char * ct,size_t count)
{
register signed char __res = 0;
while (count) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
count--;
}
return __res;
}
char * strchr(const char * s,char c)
{
for(; *s != c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
size_t strlen(const char * s)
{
const char *sc;
for (sc = s; *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
size_t strnlen(const char * s, size_t count)
{
const char *sc;
for (sc = s; *sc != '\0' && count--; ++sc)
/* nothing */;
return sc - s;
}
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;
}
char * bcopy(const char * src, char * dest, int count)
{
char *tmp = dest;
while (count--)
*tmp++ = *src++;
return dest;
}
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; /*12345678*/
} /*12345678*/
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, unsigned char c, size_t size)
{
unsigned char * p = (unsigned char *) addr;
while (size) {
if (*p == c)
return (void *) p;
p++;
size--;
}
return (void *) p;
}
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)
break;
}
}
return tmp;
}
int strcmp(const char * cs,const char * ct)
{
register signed char __res;
while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++) /*is !*cs++ necessary? incase more cmp*/
break;
}
return __res;
}
int strncmp(const char * cs,const char * ct,size_t count)
{
register signed char __res = 0;
while (count) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
count--;
}
return __res;
}
char * strchr(const char * s,char c)
{
for(; *s != c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
size_t strlen(const char * s)
{
const char *sc;
for (sc = s; *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
size_t strnlen(const char * s, size_t count)
{
const char *sc;
for (sc = s; *sc != '\0' && count--; ++sc)
/* nothing */;
return sc - s;
}
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;
}
char * bcopy(const char * src, char * dest, int count)
{
char *tmp = dest;
while (count--)
*tmp++ = *src++;
return dest;
}
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; /*12345678*/
} /*12345678*/
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, unsigned char c, size_t size)
{
unsigned char * p = (unsigned char *) addr;
while (size) {
if (*p == c)
return (void *) p;
p++;
size--;
}
return (void *) p;
}
void main(void)
{
return;
}
补充strstr
char *
strstr (const char *haystack, const char *needle)
{
char *result = (char *)NULL;
if ((haystack != (char *)NULL) && (needle != (char *)NULL))
{
register int i;
int hl = strlen (haystack);
int nl = strlen (needle);
for (i = 0; i < (hl - nl); i++)
if (strncmp (haystack + i, needle, nl) == 0)
{
result = haystack + i;
break;
}
}
return (result);
}还有 加州大学的:char *strstr(const char *string, const char *substring){ const char *a, *b; b = substring; if (*b == 0) { return (char*)string; } for( ; *string != 0; string += 1) { if (*string != *b) { continue; } a = string; while ((*a++ == *b++) && (*b != 0) ); if (*b == 0) { return (char*) string; } b = substring; }}最后是AT&T:extern char*
strstr(register const char* s1, register const char* s2)
{
register int c1;
register int c2;
register const char* t1;
register const char* t2;
if (s2)
{
if (!*s2)
return (char*)s1;
c2 = *s2++;
while (c1 = *s1++)
if (c1 == c2)
{
t1 = s1;
t2 = s2;
do
{
if (!*t2)
return (char*)s1 - 1;
} while (*t1++ == *t2++);
}
}
return 0;
}--------------------------------------------------------------------------
http://blog.csdn.net/wzbob/archive/2006/11/10/1377600.aspx
c和汇编实现 一些基本字符串处理函数
1. 将一个字符串(src)拷贝到另一个字符串(dest),直到遇到NULL 字符后停止。
char *strcpy (char *dest, const char *src)
{
_asm
{
pushf
mov esi,src
mov edi,dest
cld
l1: lodsb
stosb
test al,al
jne l1
popf
}
return dest;
}
{
return;
}
补充strstr
char *
strstr (const char *haystack, const char *needle)
{
char *result = (char *)NULL;
if ((haystack != (char *)NULL) && (needle != (char *)NULL))
{
register int i;
int hl = strlen (haystack);
int nl = strlen (needle);
for (i = 0; i < (hl - nl); i++)
if (strncmp (haystack + i, needle, nl) == 0)
{
result = haystack + i;
break;
}
}
return (result);
}还有 加州大学的:char *strstr(const char *string, const char *substring){ const char *a, *b; b = substring; if (*b == 0) { return (char*)string; } for( ; *string != 0; string += 1) { if (*string != *b) { continue; } a = string; while ((*a++ == *b++) && (*b != 0) ); if (*b == 0) { return (char*) string; } b = substring; }}最后是AT&T:extern char*
strstr(register const char* s1, register const char* s2)
{
register int c1;
register int c2;
register const char* t1;
register const char* t2;
if (s2)
{
if (!*s2)
return (char*)s1;
c2 = *s2++;
while (c1 = *s1++)
if (c1 == c2)
{
t1 = s1;
t2 = s2;
do
{
if (!*t2)
return (char*)s1 - 1;
} while (*t1++ == *t2++);
}
}
return 0;
}--------------------------------------------------------------------------
http://blog.csdn.net/wzbob/archive/2006/11/10/1377600.aspx
c和汇编实现 一些基本字符串处理函数
1. 将一个字符串(src)拷贝到另一个字符串(dest),直到遇到NULL 字符后停止。
char *strcpy (char *dest, const char *src)
{
_asm
{
pushf
mov esi,src
mov edi,dest
cld
l1: lodsb
stosb
test al,al
jne l1
popf
}
return dest;
}
2.拷贝源字符串count 个字节到目的字符串。
char *strncpy (char *dest, const char *src, int count)
{
_asm{
pushf
mov esi,src
mov edi,dest
mov ecx,count
cld
l1: dec ecx
js l2
lodsb
stosb
test al,al
jne l1
rep stosb
l2: popf
}
return dest;
}
3.将源字符串拷贝到目的字符串的末尾处。
char *strcat (char *dest, const char *src)
{
_asm {
pushf
mov esi,src
mov edi,dest
xor al,al
mov ecx,0xffffffff
cld // 清方向位。
repne scasb
dec edi
l1: lodsb
stosb
test al,al
jne l1
popf
}
return dest;
}
char *strcat (char *dest, const char *src)
{
_asm {
pushf
mov esi,src
mov edi,dest
xor al,al
mov ecx,0xffffffff
cld // 清方向位。
repne scasb
dec edi
l1: lodsb
stosb
test al,al
jne l1
popf
}
return dest;
}
4.将源字符串的count 个字节复制到目的字符串的末尾处,最后添一空字符。
char *strncat (char *dest, const char *src, int count)
{
_asm {
pushf
mov esi,src
mov edi,dest
xor al,al
mov ecx,0xffffffff
cld
repne scasb
dec edi
mov ecx,count
l1: dec ecx
js l2
lodsb
stosb
test al,al
jne l1
l2: xor al,al
stosb
popf
}
return dest;
}
char *strncat (char *dest, const char *src, int count)
{
_asm {
pushf
mov esi,src
mov edi,dest
xor al,al
mov ecx,0xffffffff
cld
repne scasb
dec edi
mov ecx,count
l1: dec ecx
js l2
lodsb
stosb
test al,al
jne l1
l2: xor al,al
stosb
popf
}
return dest;
}
5. 将一个字符串与另一个字符串进行比较。
int strcmp (const char *csrc, const char *ct)
{
_asm{
pushf
mov edi,csrc
mov esi,ct
cld
l1: lodsb
scasb
jne l2
test al,al
jne l1
xor eax,eax
jmp l3
l2: mov eax,1
jl l3
neg eax
l3: popf
}
}
------------------------------------------------------------------------------------
1. strlen(),计算字符串长度
int strlen(const char string)
{
int i=0;
while(string[i]) i++;
return i;
}
2. strcpy(), 字符串拷贝.
char *strcpy(char *destination, const char *source)
{
while(*destinaton++=*source++);
return (destination-1);
}
3. strcat(), 字符串的连接.
char *strcat(char *target,const char *source)
{
char *original=target;
while(*target) target++; // Find the end of the string
while(*target++=*source++);
return(original);
}
4. streql(), 判断两个字符串是否相等.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(), 在字符串中查找某个字符.
char *strchr(const char *string,int letter)
{
while((*string!=letter)&(*string))
string++;
return (string);
}
6. chrcnt(), 计算某个字符在字符串中出现的次数.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
if(*string==letter)count++;
return count;
}
7. strcmp(), 判断两个字符串是否相等.
int strcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return o;
else if((*str1)&&(!*str2)) //Same but str1 longer
return -1;
else if((*str2)&&(!*str1)) //Same but str2 longer
else
return((*str1>*str2)?-1:1);
}
int strcmp (const char *csrc, const char *ct)
{
_asm{
pushf
mov edi,csrc
mov esi,ct
cld
l1: lodsb
scasb
jne l2
test al,al
jne l1
xor eax,eax
jmp l3
l2: mov eax,1
jl l3
neg eax
l3: popf
}
}
------------------------------------------------------------------------------------
1. strlen(),计算字符串长度
int strlen(const char string)
{
int i=0;
while(string[i]) i++;
return i;
}
2. strcpy(), 字符串拷贝.
char *strcpy(char *destination, const char *source)
{
while(*destinaton++=*source++);
return (destination-1);
}
3. strcat(), 字符串的连接.
char *strcat(char *target,const char *source)
{
char *original=target;
while(*target) target++; // Find the end of the string
while(*target++=*source++);
return(original);
}
4. streql(), 判断两个字符串是否相等.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(), 在字符串中查找某个字符.
char *strchr(const char *string,int letter)
{
while((*string!=letter)&(*string))
string++;
return (string);
}
6. chrcnt(), 计算某个字符在字符串中出现的次数.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
if(*string==letter)count++;
return count;
}
7. strcmp(), 判断两个字符串是否相等.
int strcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return o;
else if((*str1)&&(!*str2)) //Same but str1 longer
return -1;
else if((*str2)&&(!*str1)) //Same but str2 longer
else
return((*str1>*str2)?-1:1);
}