mstring.h

来自「可能是能找到的处理速度最快」· C头文件 代码 · 共 130 行

H
130
字号
#ifndef MSTRING_H_
#define MSTRING_H_
#include"misc.h"
#include<assert.h>
class CStr
{
protected:
	char *str;
	int Own;
public:
	CStr(int O,char *s=NULL)	// point-to version
	{
		Own=O;
		str=s;
	}
	CStr (int O,CStr *s)
	{
		Own=O;
		str=s->str;
	}
	CStr(char *s)		// copy version, copy and own it
	{
		Own=TRUE;
		str=StringCopy(s);		// if s=NULL ,StringCopy return NULL
	}

	CStr()
	{
		Own=TRUE;
		str=NULL;
	}
	CStr(CStr &s)		// copy version
	{
		Own=TRUE;
		str=StringCopy(s.str);
	}
	CStr &operator=(CStr &s)	// copy  version
	{
		if (Own&&str)
			delete str;
		Own=TRUE;
		str=StringCopy(s.str);
		return *this;
	}
	CStr &operator=(CStr *s)	// Point-to version
	{
		if (Own&&str)
			delete str;
		Own=FALSE;
		str=s->str;
		return *this;
	}
	CStr &operator=(char &s)	// copy version
	{
		if (Own&&str)
			delete str;
		Own=TRUE;
		str=StringCopy(&s);
		return *this;
	}

	CStr &operator=(char *s)	// point-to version
	{
		if (Own&&str)
			delete str;
		Own=TRUE;
		str=s;
		return *this;
	}
	~CStr()
	{
		if (Own&&str)
			delete str;
	}
#define Compare_Char(A)	bool operator##A(char*s){return (strcmp(str,s) ##A 0);}
	Compare_Char(==);
	Compare_Char(>);
	Compare_Char(<);
	Compare_Char(>=);
	Compare_Char(<=);
#define Compare_String(A)	bool operator##A(CStr &s){return (strcmp(str,s.str) ##A 0);}
	Compare_String(==);
	Compare_String(>);
	Compare_String(<);
	Compare_String(>=);
	Compare_String(<=);
	int operator-(CStr &s)
	{
		return strcmp(str,s.str);
	}
	operator char *() const 
	{
		if (this)
			return str;
		else
			return NULL;
	}
	char *string()
	{
		if (this)
			return str;
		else
			return NULL;
	}
	CStr &operator+=(char *s)
	{
		int len1=0,len2=0;
		if (s)
			len1=strlen(s);
		else
			return *this;
		if (str)
			len2=strlen(str);
		char *p=new char[len1+len2+1];
		assert(p);
		if (str)
		{
			p=StringCopy(str);
			delete str;
		}
		else
			*p='\0';
		if (s)
			strcat(p,s);
		str=p;
		return *this;
	}
};

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?