您的位置:首页 > 编程语言 > C语言/C++

【RLIB】String 的简易C++实现

2012-02-02 15:40 1121 查看
相关源码参见RLIB开源项目

下面是RLIB1.x的头文件:



namespace System
{
	/// <summary>
	/// 表示空值
	/// </summary>
	typedef enum Nullable
	{
		Nothing,
		null,
	};
	/// <summary>
	/// 前向声明
	/// </summary>
	class export StringArray;
	/// <summary>
	/// 表示非线程安全的文本类
	/// </summary>
	class export String
	{
	public:
		/// <summary>
		/// String核心结构
		/// </summary>
		typedef struct StringTag
		{
			//内部字符串指针
			TCHAR  *m_pstr; 
			//引用计数
			UINT   m_ref;  
			//字符串长度
			UINT   m_len;  
			//字符串缓冲区大小
			UINT   m_size;    
			//内部使用
			LPSTR  m_pchar;  
			//内部使用
			LPWSTR m_pwchar;    
			//指示字符串是否可写,如常量引用该值为false
			bool   m_write;   
			//指示字符串长度是否需要检查
			bool   m_check;   
			//对齐内存
			bool   m_unused[2]; 
		private:                //内部结构,禁止实例化
			StringTag();
			~StringTag();
		}*pTag;
	private:
		pTag m_ptag;                  //Tag指针
		void UpdateLen(UINT Len);     //更新为具有指定长度的string,方法可用于撤销常量引用
		void UpdatePtr(LPTSTR, UINT); //更新string的内部字符串指针为指定大小的新指针,注意Object不能为常量引用
	public:
		//初始化类,此时IsNull()返回true
		String();   
		//初始化指定长度的字符串,注意是长度不是大小(size),并填充指定字符
		String(UINT, TCHAR c = 0);
		//初始化类并拷贝指定长度的字符串到本地
		String(TCHAR *);      
		 //初始化类并引用指定长度的字符串,'写时拷贝'
		String(const CHAR *);  
		//初始化类并引用指定长度的字符串,'写时拷贝'
		String(const WCHAR *);  
		//初始化类
		String(const String &);   
		~String();
		RLIB_ClassNewDel;

		operator TCHAR *();       //详见 GetData()
		operator const TCHAR *(); //详见 GetConstData()
		//方法返回内部字符串指针并保证可读写,对其修改将影响该string
		LPTSTR   GetData();     
		//不应修改该方法返回的字符串,这将引发string的错误行为
		LPCTSTR  GetConstData() const;  

		const String& operator=(Nullable);       //String = null;可将string置空并释放内存
		const String& operator=(TCHAR *);        //将字符串拷贝到string中
		const String& operator=(const CHAR *);   //将字符串引用到string中,实现了自动转换
		const String& operator=(const WCHAR *);  //将字符串引用到string中,实现了自动转换 
		const String& operator=(const String &); //两个string类可能互相引用或者相互独立(比如源string本身引用了字符串常量) 

		void  ReferenceFrom(const String &); //引用另一string实例,除构造函数外不应调用该方法

		__declspec(property(get = GetSize)) const UINT Size;
		__declspec(property(get = GetLength)) const UINT Length;
		__declspec(property(get = GetLength)) const UINT CanReadSize;
		//返回缓冲区大小
		UINT GetSize() const;  
		//返回字符串长度
		UINT GetLength() const;   
		//返回字符串可读取大小
		UINT GetCanReadSize() const; 

		//创建一个与指定的 string 具有相同值的 TCHAR* 的新副本,
		//该副本的size仅保证容纳 string 值,且必须手动调用Collect(Object)释放.
		TCHAR *c_str();              
		//如果编译环境非Unicode,该方法直接调用c_str(),反之,
		//执行转换且NotCollect == true必须手动调用Collect(Object)释放
		LPSTR  ToMultiByte(bool NotCollect = false); 
		//如果编译环境为Unicode,该方法直接调用c_str(),反之,
		//执行转换且NotCollect == true必须手动调用Collect(Object)释放
		LPWSTR ToWideChar(bool NotCollect = false);  

		//获取当前 string 对象中位于指定字符位置(从0开始)的字符
		TCHAR  GetAt(int) const; 
		//设置当前 string 对象中位于指定字符位置(从0开始)的字符
		void   SetAt(int, TCHAR);

		//将指定数目的字符复制到string实例,len为零将自动获取长度
		bool  Copy(const CHAR *, UINT len = 0);   
		//将指定数目的字符复制到string实例,len为零将自动获取长度
		bool  Copy(const WCHAR *, UINT len = 0); 
		//尝试将指定数目的字符复制到string实例,该方法不会分配新的内存,空间不足返回false
		bool  TryCopy(LPCTSTR, UINT len = 0);   
		//追加指定长度字符串到此string实例,size为零将自动获取长度
		void  Append(const TCHAR *, UINT len = 0);  
		//追加指定长度string到此string实例,size为零将自动获取长度
		void  Append(const String &, UINT len = 0); 
		//将指定的 string 中的每个格式项替换为相应对象的值的文本等效项
		bool  __cdecl Format(LPCTSTR pstrFormat, ...); 

		int Compare(const TCHAR *) const;
		int CompareNoCase(const TCHAR *) const;

		void Empty();
		//获取字符串是否为引用类型(方法GetConstData()返回的指针确实不可写,否则将可能使程序崩溃) 
        bool IsConst() const; 
		bool IsEmpty() const;
		bool IsNull() const;
		bool IsNullOrEmpty() const;

		//确定 string 实例的开头是否与指定的字符串匹配
		bool StartsWith(TCHAR); 
		//确定 string 的实例的末尾是否与指定的字符串匹配
		bool EndsWith(TCHAR);       

		//报告指定字符在此字符串中的第一个匹配项的索引,如果未找到该字符串,则返回 -1
		int IndexOf(TCHAR);     
		//报告指定字符在在此实例中的最后一个匹配项的索引位置,如果未找到该字符串,则返回 -1
		int LastIndexOf(TCHAR); 
		//报告 string 或一个或多个字符在此字符串中的第一个匹配项的索引,如果未找到该字符串,则返回 -1
		int IndexOf(const TCHAR *);   
		//报告 string 或一个或多个字符在在此实例中的最后一个匹配项的索引位置,如果未找到该字符串,则返回 -1
		int LastIndexOf(const TCHAR *);
		//报告 string 或一个或多个字符在此字符串中的第一个匹配项的索引,
		//并返回末位索引(加上指定字符串长度),如果未找到该字符串,则返回 -1
		int IndexOfR(const TCHAR *);   
		//报告 string 或一个或多个字符在在此实例中的最后一个匹配项的索引位置,
		//并返回末位索引,如果未找到该字符串,则返回 -1
		int LastIndexOfR(const TCHAR *);

		//返回此 string 颠倒字符次序后的副本
		String Reverse(); 
		//返回此 string 转换为小写形式的副本
		String ToLower(); 
		//返回此 string 转换为大写形式的副本
		String ToUpper(); 
		//创建当前 String 的浅表副本
		String ToString();

		//将指定长度字符串(size为零将自动获取)拼到当前string末尾并返回新实例
		String Concat(const TCHAR *, UINT len = 0);  
		//将指定长度String(size为零将自动获取)拼到当前string末尾并返回新实例
		String Concat(const String &, UINT len = 0); 
		//从当前 string 对象移除所有前导空白字符和尾部空白字符
		String Trim();    
		//从当前 string 对象移除数组中指定的一组字符的所有前导空白字符
		String TrimStart();  
		//从当前 string 对象移除数组中指定的一组字符的所有尾部空白字符
		String TrimEnd();    
		//从此实例检索子字符串,子字符串从指定的字符位置(从0开始)开始且具有指定的长度
		String Substring(UINT, UINT len = 0);         

		//返回一个新字符串,其中当前实例中出现的 n 个指定字符串都替换为另一个指定的字符串,若n为0则替换全部
		String Replace(const TCHAR *, const TCHAR *, UINT n = 0); 

		bool operator == (LPCTSTR) const;
		bool operator != (LPCTSTR) const;
		bool operator <= (LPCTSTR) const;
		bool operator <  (LPCTSTR) const;
		bool operator >= (LPCTSTR) const;
		bool operator >  (LPCTSTR) const;

		//获取当前 string 对象中位于指定字符位置的字符
		TCHAR  operator[] (int) const; 
		String operator+(const TCHAR *);
		String operator+(const String &);
		const  String& operator+=(const TCHAR *);
		const  String& operator+=(const String &);

		//unused 返回的字符串数组包含此实例中的子字符串(由指定字符串的元素分隔)
		StringArray *Split(const TCHAR *); 

	public:
		/// <summary>
		/// 转换为多字符格式(ANSI), 必须手动调用Collect方法以释放内存
		/// </summary>
		static LPSTR  ConvertToMultiByte(LPCWSTR, int length = 0);
		/// <summary>
		/// 转换为宽字符格式(Unicode), 必须手动调用Collect方法以释放内存
		/// </summary>
		static LPWSTR ConvertToWideChar(LPCSTR, int length = 0); 
		/// <summary>
		/// 释放所有 string 占用的内存资源,此方法应确保在所有string不再被使用或析构后才被调用
		/// </summary>
		static void Dispose();
		/// <summary>
		/// string 内存统一申请接口
		/// </summary>
		static TCHAR *Allocate(ULONG);
		/// <summary>
		/// string 内存统一回收接口
		/// </summary>
		static void Collect(Object);
		/// <summary>
		/// 返回 string 使用的内存池(请勿手动销毁), 该方法可能返回NULL
		/// </summary>
		static System::IO::Memory *GetUsingPool();
	};
	/// <summary>
	/// String 数组
	/// </summary>
    class export StringArray:public Array<String>
	{
	public:
		StringArray();
		StringArray(UINT Length);
		StringArray(String[], INT Length);
		~StringArray();
		Object operator new(size_t);
		void   operator delete(Object);
	private:
		//确定某元素是否在数组中
		bool  Contains(const String &); 
		//如果找到,则返回值的索引;否则为 -1
		INDEX IndexOf(const String &);  
		//移除特定元素的第一个匹配项
		bool  Remove(const String &);   
		//分配指定数量元素内存
		bool _Init(UINT);     
		//增加分配数量
		bool _More(UINT);         
    public:
		//返回新元素所插入到的位置,或为 -1 表示失败
		INDEX  Add(const String &);    
		//设置指定索引处的值
		bool   SetValue(INDEX, const String &); 
		//移除所有数组元素
		void   Clear();      
		//插入新元素到指定位置
		bool   Insert(INDEX, const String &);   
		//移除指定索引处的元素
		bool   RemoveAt(INDEX);   
		//串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符
		String Join(const String &); 	        
	public:
		//释放所有 Array 占用的内存资源
		static void Dispose();              
		//返回所有 Array 公用的内存池 Memory类 指针,该方法可能返回NULL
		static System::IO::Memory *GetUsingPool(); 
	};
};
typedef System::String string;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: