【RLIB】String 的簡易C++實現

來源:互聯網
上載者:User

相關源碼參見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;    //指示字串是否可寫,如常量引用該值為falsebool   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()返回trueString();   //初始化指定長度的字串,注意是長度不是大小(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()//方法返回內部字串指標並保證可讀寫,對其修改將影響該stringLPTSTR   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執行個體,該方法不會分配新的記憶體,空間不足返回falsebool  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);       //報告指定字元在此字串中的第一個匹配項的索引,如果未找到該字串,則返回 -1int IndexOf(TCHAR);     //報告指定字元在在此執行個體中的最後一個匹配項的索引位置,如果未找到該字串,則返回 -1int LastIndexOf(TCHAR); //報告 string 或一個或多個字元在此字串中的第一個匹配項的索引,如果未找到該字串,則返回 -1int IndexOf(const TCHAR *);   //報告 string 或一個或多個字元在在此執行個體中的最後一個匹配項的索引位置,如果未找到該字串,則返回 -1int LastIndexOf(const TCHAR *);//報告 string 或一個或多個字元在此字串中的第一個匹配項的索引,//並返回末位索引(加上指定字串長度),如果未找到該字串,則返回 -1int IndexOfR(const TCHAR *);   //報告 string 或一個或多個字元在在此執行個體中的最後一個匹配項的索引位置,//並返回末位索引,如果未找到該字串,則返回 -1int 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 &); //如果找到,則傳回值的索引;否則為 -1INDEX 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類 指標,該方法可能返回NULLstatic System::IO::Memory *GetUsingPool(); };};typedef System::String string;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.