自己编写的string库函数
2009-06-24 09:23
225 查看
//都是自己编写的,仅供学习参考。
/*
date: 2009.6.24
author: Summon
function: functions for string.h
version: v1.0
right: all right opened
*/
////////////////////////////////////////////////////
char *MyStrcpy(char *pchDest, const char *pchSour)
{
if ((pchDest == NULL) || (pchSour == NULL))
{
return NULL;
}
char *ret = pchDest;
do
{
*pchDest = *pchSour;
pchDest++;
pchSour++;
}while (*pchSour != '/0');
*pchDest = '/0';
return ret;
}
///////////////////////////////////////////////////////////
char *MyStrncpy(char *pchDest, const char *pchSour, size_t count)
{
if ((pchDest == NULL) || (pchSour == NULL))
{
return NULL;
}
if (count == 0)
{
return pchDest;
}
char *ret = pchDest;
while (count--)
{
*pchDest = *pchSour;
pchDest++;
pchSour++;
if (*pchSour == '/0')
{
break;
}
}
*pchDest = '/0';
return ret;
}
////////////////////////////////////////////
int MyStrcmp(const char *pchStr1, const char *pchStr2)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
int ret = 0;
while (*pchStr1 == *pchStr2)
{
pchStr1++;
pchStr2++;
if (*pchStr1 == 0)
{
return ret;
}
}
ret = *(unsigned char *)pchStr1 - *(unsigned char *)pchStr2;
return ret;
}
//////////////////////////////////////////////////////
int MyStrlen(const char *pchDest
{
if (pchDest == NULL)
{
return -1;
}
int ret = 0;
while(*pchDest != 0)
{
ret++;
pchDest++;
}
return ret;
}
////////////////////////////////////////////////////////
int MyStrncmp(const char *pchStr1, const char *pchStr2, size_t count)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
if (count == 0)
{
return 0;
}
int ret = 0;
while (count--)
{
if (*pchStr1 == *pchStr2)
{
if (*pchStr1 == 0)
{
return ret;
}
pchStr1++;
pchStr2++;//必须放到上个if后面再++
continue;
}else
{
pchStr1++;//虽然if和else里都有++,但是不能放到外面用共同的代码
pchStr2++;
break;
}
}
ret = *(unsigned char *)--pchStr1 - *(unsigned char *)--pchStr2;
return ret;//必须--才能返回正确的值,否则会向后移一个字符才比较
}
/////////////////////////////////////////////////////
void *MyMoveMemery(void *pDest, const void *pSrc, size_t nSize)
{
if ((pSrc != NULL) && (pDest != NULL))
{
return NULL;
}
if (nSize == 0)
{
return NULL;
}//input detected
void *ret = pDest;//remain the old pointer
unsigned char *d = (unsigned char *)pDest;//change to the most small memory unit
unsigned char *s = (unsigned char *)pSrc;
if (d < s + nSize)//special memory overlap
{
s += nSize - sizeof(pDest);
d += nSize - sizeof(pDest);
while (s != ((unsigned char *)pSrc - 1))//不能写!=pSrc,否则最后一个赋值语句不能执行。
{
*d = *s;
d--;
s--;
}
}else
{
while (nSize--)
{
*d = *s;
d++;
s++;
}
}
return ret;
//return the old pointer
}
///////////////////////////////////////////////
char *MyStrcat(char *pchDestination, const char *pchSource)
{
if ((pchDestination == NULL) || (pchSource == NULL))
{
return NULL;
exit(-1);
}
char *ret = pchDestination;
while (*pchDestination != '/0')
{
pchDestination++;
}
while (*pchSource != '/0')
{
*pchDestination = *pchSource;
pchSource++;
pchDestination++;
}
*pchDestination = '/0';
return ret;
}
////////////////////////////////////////////
char *MyStrncat(char *pchDestination, const char *pchSource, size_t count)
{
if ((pchDestination == NULL) || (pchSource == NULL))
{
return NULL;
exit(-1);
}
if (count == 0)
{
return pchDestination;
}
char *ret = pchDestination;
while (*pchDestination != '/0')
{
pchDestination++;
}
while (count--)
{
if (*pchSource != '/0')
{
*pchDestination = *pchSource;
pchSource++;
pchDestination++;
}else
{
break;
}
}
*pchDestination = '/0';
return ret;
}
///////////////////////////////////////////////
char *MyStrchr(const char *pStr, int find)
{
if (pStr == NULL)
{
return NULL;
}
if (find == 0)
{
return NULL;
}
char *ret = (char *)pStr;
while (*ret != '/0')
{
if (*ret == find)
{
return ret;
}
ret++;
}
return NULL;
}
////////////////////////////////////////
void *MyMemchr(const void *pStr, int find, size_t count)
{
if ((count == 0) || (pStr == NULL))
{
return NULL;
}
const unsigned char *p = (const unsigned char *)pStr;//为什么要转换成这种类型呢?
while (count--)
{
if (*p != find)
{
p++;
}else
{
return (void *)p;
}
}
return NULL;
}
//////////////////////////////////////////
int MyMemcmp(const void *pchStr1, const void *pchStr2, size_t count)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
if (count == 0)
{
return 0;
}
int ret = 0;
const unsigned char *p1 = (const unsigned char *)pchStr1;//比较内存都要转换为最小存储单位
const unsigned char *p2 = (const unsigned char *)pchStr2;
while (count--)
{
if (*p1 == *p2)
{
p1++;
p2++;
}else
{
ret = (*p1 - *p2);
}
}
return ret;
}
/////////////////////////////////////////////////
void *MyMemset(void *pchDest, int set, size_t count)
{
if (pchDest == NULL)
{
return NULL;
}
if (count == 0)
{
return pchDest;
}
unsigned char *p = (unsigned char *)pchDest;
unsigned char s = (unsigned char)set;
while (count--)
{
*p = s;
p++;
}
return pchDest;
}
//////////////////////////////////////////////////////////////
/*
date: 2009.6.24
author: Summon
function: functions for string.h
version: v1.0
right: all right opened
*/
////////////////////////////////////////////////////
char *MyStrcpy(char *pchDest, const char *pchSour)
{
if ((pchDest == NULL) || (pchSour == NULL))
{
return NULL;
}
char *ret = pchDest;
do
{
*pchDest = *pchSour;
pchDest++;
pchSour++;
}while (*pchSour != '/0');
*pchDest = '/0';
return ret;
}
///////////////////////////////////////////////////////////
char *MyStrncpy(char *pchDest, const char *pchSour, size_t count)
{
if ((pchDest == NULL) || (pchSour == NULL))
{
return NULL;
}
if (count == 0)
{
return pchDest;
}
char *ret = pchDest;
while (count--)
{
*pchDest = *pchSour;
pchDest++;
pchSour++;
if (*pchSour == '/0')
{
break;
}
}
*pchDest = '/0';
return ret;
}
////////////////////////////////////////////
int MyStrcmp(const char *pchStr1, const char *pchStr2)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
int ret = 0;
while (*pchStr1 == *pchStr2)
{
pchStr1++;
pchStr2++;
if (*pchStr1 == 0)
{
return ret;
}
}
ret = *(unsigned char *)pchStr1 - *(unsigned char *)pchStr2;
return ret;
}
//////////////////////////////////////////////////////
int MyStrlen(const char *pchDest
{
if (pchDest == NULL)
{
return -1;
}
int ret = 0;
while(*pchDest != 0)
{
ret++;
pchDest++;
}
return ret;
}
////////////////////////////////////////////////////////
int MyStrncmp(const char *pchStr1, const char *pchStr2, size_t count)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
if (count == 0)
{
return 0;
}
int ret = 0;
while (count--)
{
if (*pchStr1 == *pchStr2)
{
if (*pchStr1 == 0)
{
return ret;
}
pchStr1++;
pchStr2++;//必须放到上个if后面再++
continue;
}else
{
pchStr1++;//虽然if和else里都有++,但是不能放到外面用共同的代码
pchStr2++;
break;
}
}
ret = *(unsigned char *)--pchStr1 - *(unsigned char *)--pchStr2;
return ret;//必须--才能返回正确的值,否则会向后移一个字符才比较
}
/////////////////////////////////////////////////////
void *MyMoveMemery(void *pDest, const void *pSrc, size_t nSize)
{
if ((pSrc != NULL) && (pDest != NULL))
{
return NULL;
}
if (nSize == 0)
{
return NULL;
}//input detected
void *ret = pDest;//remain the old pointer
unsigned char *d = (unsigned char *)pDest;//change to the most small memory unit
unsigned char *s = (unsigned char *)pSrc;
if (d < s + nSize)//special memory overlap
{
s += nSize - sizeof(pDest);
d += nSize - sizeof(pDest);
while (s != ((unsigned char *)pSrc - 1))//不能写!=pSrc,否则最后一个赋值语句不能执行。
{
*d = *s;
d--;
s--;
}
}else
{
while (nSize--)
{
*d = *s;
d++;
s++;
}
}
return ret;
//return the old pointer
}
///////////////////////////////////////////////
char *MyStrcat(char *pchDestination, const char *pchSource)
{
if ((pchDestination == NULL) || (pchSource == NULL))
{
return NULL;
exit(-1);
}
char *ret = pchDestination;
while (*pchDestination != '/0')
{
pchDestination++;
}
while (*pchSource != '/0')
{
*pchDestination = *pchSource;
pchSource++;
pchDestination++;
}
*pchDestination = '/0';
return ret;
}
////////////////////////////////////////////
char *MyStrncat(char *pchDestination, const char *pchSource, size_t count)
{
if ((pchDestination == NULL) || (pchSource == NULL))
{
return NULL;
exit(-1);
}
if (count == 0)
{
return pchDestination;
}
char *ret = pchDestination;
while (*pchDestination != '/0')
{
pchDestination++;
}
while (count--)
{
if (*pchSource != '/0')
{
*pchDestination = *pchSource;
pchSource++;
pchDestination++;
}else
{
break;
}
}
*pchDestination = '/0';
return ret;
}
///////////////////////////////////////////////
char *MyStrchr(const char *pStr, int find)
{
if (pStr == NULL)
{
return NULL;
}
if (find == 0)
{
return NULL;
}
char *ret = (char *)pStr;
while (*ret != '/0')
{
if (*ret == find)
{
return ret;
}
ret++;
}
return NULL;
}
////////////////////////////////////////
void *MyMemchr(const void *pStr, int find, size_t count)
{
if ((count == 0) || (pStr == NULL))
{
return NULL;
}
const unsigned char *p = (const unsigned char *)pStr;//为什么要转换成这种类型呢?
while (count--)
{
if (*p != find)
{
p++;
}else
{
return (void *)p;
}
}
return NULL;
}
//////////////////////////////////////////
int MyMemcmp(const void *pchStr1, const void *pchStr2, size_t count)
{
if ((pchStr1 == NULL) || (pchStr2 == NULL))
{
printf("StrCompare input error!/n");
exit(-1);
}
if (count == 0)
{
return 0;
}
int ret = 0;
const unsigned char *p1 = (const unsigned char *)pchStr1;//比较内存都要转换为最小存储单位
const unsigned char *p2 = (const unsigned char *)pchStr2;
while (count--)
{
if (*p1 == *p2)
{
p1++;
p2++;
}else
{
ret = (*p1 - *p2);
}
}
return ret;
}
/////////////////////////////////////////////////
void *MyMemset(void *pchDest, int set, size_t count)
{
if (pchDest == NULL)
{
return NULL;
}
if (count == 0)
{
return pchDest;
}
unsigned char *p = (unsigned char *)pchDest;
unsigned char s = (unsigned char)set;
while (count--)
{
*p = s;
p++;
}
return pchDest;
}
//////////////////////////////////////////////////////////////
相关文章推荐
- 自己编写taglib,调用Java自定义方法,程序员的个性化。
- Win8系统执行自己Vc编写的程序时回车闪退解决方法。
- 如何将自己编写的app放到真机上运行
- python import自己编写的模块:import json和simplejson
- Cocos2dx-OpenGL ES2.0教程:编写自己的shader(2)
- 如何自己编写Makefile
- 编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A、B、C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示。如:ABCABCABC…… 依次递归
- 从零开始编写自己的C#框架(12)——T4模板在逻辑层中的应用(一)(附源码)
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- jQuery自己编写插件()
- MFC 编写自己的特色启动窗体...........
- Eric6与pyqt5学习笔记 11【编写自己的密码加密管理器~ python3.5+PYQT5】
- 自己编写小程序开发调试函数
- 编写自己rpc框架——java rpc的实现
- 从零开始编写自己的C#框架(8)——后台管理系统功能设计
- 自己编写的基于VC++6.0的串口调试软件,并贡献源程序!
- qt-designer使用教程--编写自己的槽
- 看了网上的大多防SQL攻击程序,自己编写了一个函数,比较完美。大家一起测测~
- hadoop如何执行自己编写的MapReduce程序
- 自己编写jdbc框架