天天看点

c++ 头文件<cstring>(或者string.h)中的常见函数的实现!

下面是一些<cstring>中的常见函数的实现,本人已经测试过了,如有问题,望高手批评指正,函数封装于名字空间mystd中。(建议文件命名为"cstring.h",好记忆,而且与标准库并不冲突!)

下面贴代码!!!

// 此文件可以命名为"cstring.h" (这与标准库的<cstring>并不冲突)
// vs2012 调试通过
#pragma once 
#ifndef MYSTD_CSTRING_H
#define MYSTD_CSTRING_H
#include<cassert>  // assert 
#include<cstddef> // std::size_t

#define MYSTD_BEGIN namespace mystd{
#define MYSTD_END   }



#ifdef __cplusplus

 //封装于名字空间mystd中
MYSTD_BEGIN
	typedef std::size_t size_type;
	typedef unsigned char UCHAR;
	// C语言版本,void * memchr(const void *,int,size_type); 
	inline const void* memchr(const void *pointer,int val, size_type num)
	{
		assert(pointer != 0);
		UCHAR *ptr = (UCHAR*)pointer;
	    for(size_type i = 0; i < num; ++i)
		{
			if(*ptr == val)
				break;
			++ptr;
		}
		return ptr;
	}
    inline void* memchr(void *pointer, int val,size_type num) //c++重载
	{
		assert(pointer != 0);
		return (void*)mystd::memchr((const void*)pointer,val,num); // 转调
	}
	inline size_type strlen(const char *str)
	{
		assert(str != 0);
		size_type count = 0;
		while(*str++)
			++count;
		return count;
	}

	inline void* memmove(void *destination,const void *source, size_type num)
	{ // 对于memmove函数的实现,c++之父在《c++ 程序设计语言》(十周年中文纪念版第16章开篇)
	  //就说过,此函数无法由c++语言本身达到最优实现,实际应用时还是用标准库吧!
        assert(destination != 0 && source != 0);  
		if(destination == source || num == 0)
			return destination;
		UCHAR *des = (UCHAR*)destination;  
		const UCHAR *src = (UCHAR*)source;  
		if(des < src || des >= src + num)  
		{
				while(num--)
					*des++ = *src++;
				return destination;
		}
		des += num;
		src += num;
		while(num--) // 倒序复制
			*--des = *--src;
		return destination;
	}
	inline void* memcpy(void *destination,const void *source, size_type num)
	{
		assert(destination != 0 && source != 0);
		return mystd::memmove(destination,source,num);
	}
	inline int memcmp(const void *pointer_1,const void *pointer_2,size_type num)
	{
		assert(pointer_1 != 0 && pointer_2 != 0);
		const UCHAR *ptr_1 = (UCHAR*)pointer_1;
		const UCHAR *ptr_2 = (UCHAR*)pointer_2;
		while(num-- && *ptr_1 == *ptr_2)
			++ptr_1,++ptr_2;
		if(num == size_type(-1))
			return 0;
		else
			return *ptr_1 - *ptr_2;
	}
	inline void* memset(void *pointer,int val,size_type num)
	{
		assert(pointer != 0);
		UCHAR *ptr = (UCHAR*)pointer;
		while(num--)
			*ptr++ = val;
		return pointer;
	}
	inline char* strcat(char *destination,const char *source)
	{
		assert(destination != 0 && source != 0);
		char *ptr = destination + mystd::strlen(destination);
		while(*ptr++ = *source++);
		return destination;
	}
	inline char *strncat(char *destination,const char *source,size_type num)
	{
		assert(destination != 0 && source != 0);
		char *ptr = destination + mystd::strlen(destination);
		while(num-- && *source)
			*ptr++ = *source++;
		*ptr = 0;   // null-character 复制
		return destination;
	}
	inline char *strcpy(char *destination,const char *source)
	{
		assert(destination != 0 && source != 0);
		char *des = destination;
		while(*des++ = *source++); 
		return destination; // null-character被复制
	}
	inline char *strncpy(char *destination,const char *source,size_type num)
	{
		assert(destination != 0 && source != 0);
		char *des = destination;
		while(num--)
			*des++ = *source++; 
		return destination; // null-character可能没有被复制
	}
	inline int strcmp(const char *str1,const char *str2)
	{
      assert(str1 != 0 && str2 != 0);
	  while(*str1 && *str1 == *str2)
		  ++str1, ++str2;
	  return *str1 - *str2;
	}
	inline int strncmp(const char *str1,const char *str2,size_type num)
	{
		assert(str1 != 0 && str2 != 0);
		while(num-- && *str1 && *str1 == *str2)
			++str1, ++str2;
		if(num == size_type(-1))  // 包含了num == 0的情况
			return 0;
		else
			return *str1 - *str2;
	}
	//C语言只有一个版本 char* strchr(const char *, int); 
	inline const char* strchr(const char *str,int character)
	{
		assert(str != 0);
		// 语言标准规定character 为int,这里转换一下
		const char chr = *(char*)&character;
		while(*str && *str != chr) 
			++str;
		if(*str)
			return str;
		else
			return 0;
	}
    inline char* strchr(char *str,int character) //c++重载
	{
		assert(str != 0);
		return (char*)mystd::strchr((const char*)str,character);
	}
	inline const char* strrchr(const char *str,int character)
	{ //这里的character 可能包括null-character
		assert(str != 0);
		// 语言标准规定character 为int,这里转换一下
		const char chr = *(char*)&character;
		// const char chr = (char)character; // 也可以这样
		size_type len = mystd::strlen(str);
		const char *ptr = str + len;
		if(chr == 0)
			return ptr;
		--ptr;
		while(len--)
			if(*ptr == chr)
				return ptr;
			else
				--ptr;
		return 0;  //无匹配的字符
	}
    inline char* strrchr(char *str,int character)
	{
		assert(str != 0);
		return (char*)mystd::strrchr((const char*)str,character); // 转调
	}
	//c语言版本char* strstr(const char *,const char*);
	inline const char* strstr(const char* str1,const char* str2)
	{
		assert(str1 != 0 && str2 != 0);
		size_type len_1 = mystd::strlen(str1);
		size_type len_2 = mystd::strlen(str2);
		if(len_1 < len_2) 
			return 0;
		const char *search_last = str1 + (len_1 - len_2);
		while(str1 <= search_last)
		{
			if(mystd::strncmp(str1,str2,len_2) == 0)
				return str1;
			else
				++str1;
		}
		return 0;
	}
    inline char* strstr(char *str1,const char *str2) //c++重载
	{
		assert(str1 != 0 && str2 != 0);
		return (char*)mystd::strstr((const char*)str1,str2);
	}
	inline bool is_inside(const char *str,char chr) // 辅助函数,内部使用
	{
		assert(str != 0);
		while(*str)
		{
			if(*str == chr)
				return true;
			else 
				++str;
		}
		return false;
	}
	inline size_type strspn(const char* str1,const char *str2)
	{
		assert(str1 != 0 && str2 != 0);
		size_type count = 0;
		while(*str1 && is_inside(str2,*str1))
			  ++count, ++str1;
		return count;
	}
	inline size_type strcspn(const char* str1,const char *str2)
	{
		assert(str1 != 0 && str2 != 0);
		size_type count = 0;
		while(*str1 && !is_inside(str2,*str1))
			++count, ++str1;
		return count;
	}
	// c语言版本char* strpbrk(const char *,const char *); 
	inline const char* strpbrk(const char *str1,const char *str2)
	{
		assert(str1 != 0 && str2 != 0);
		while(*str1 && !is_inside(str2,*str1))
			++str1;
		if(*str1 == 0)
			return 0;
		else
			return str1;
	}
    inline char* strpbrk(char *str1,const char *str2) //c++重载
	{
		assert(str1 != 0 && str2 != 0);
		return (char*)strpbrk((const char*)str1,str2);  //转调
	}

MYSTD_END  // end of namespace mystd 
#endif // __cplusplus 
#endif // MYSTD_CSTRING_H
           

下面是一个简单的测试程序:

#include<iostream>
#include"cstring.h" //自写版本
#include<cstring> // 标准库
#define STD mystd  // 更改为std可以调用标准库版本

using std::cout;
using std::endl;

int main()
{
	char buf[100];
	const char* str = "hello world";
	STD::memset(buf,0,sizeof(buf));
	STD::memcpy(buf,str,STD::strlen(str));
	cout<<buf<<endl;

	if(STD::strncmp(str,buf,STD::strlen(str)) == 0)
		cout<<"str 等于 buf"<<endl;
	system("pause");
	return 0;
}
           

本人在vs2012中测试通过。

继续阅读