Custom string type

Source: Internet
Author: User

Custom string type

Custom String object, inherited from the dynamic array template class (dynamic array template class implements memory management, can reduce memory allocation and memory copy, optimize the append array, etc., refer to the http://blog.csdn.net/chenjiayi_yun/article/details/44752051 ), the append string, formatted string, and string comparison operations that are commonly used in strings are reloaded.

Class header file, as follows:

 

Class AnsiString: public Array
 
  
{Public: typedef Array
  
   
Super; public: AnsiString (): super () // default constructor {} AnsiString (const AnsiString & another) // copy constructor: super (another) {} AnsiString (const char * str) // string initialization constructor: super (str, str? Strlen (str): 0) {} AnsiString (const char * str, size_t len) // string initialization constructor: super (str, len) {} AnsiString (const size_t len): super (len) // Constructor (Reserved memory size) {} AnsiString (size_t max, const char * fmt ,...): super () // string formatting {va_list args; va_start (args, fmt); if (max! = 0) formatArgs (max, fmt, args); else formatArgs (fmt, args); va_end (args);} inline AnsiString & operator = (const char * str) {size_t len = str? Strlen (str): 0; setLength (0); if (len> 0) {setLength (len); memcpy (m_ptr, str, len * sizeof (* m_ptr ));} return * this;} inline AnsiString & operator = (const AnsiString & str) {super: operator = (str); return * this ;} inline AnsiString & operator + = (const char * str) {if (! Str |! Str [0]) return * this; cat (str, strlen (str); return * this;} inline AnsiString & operator + = (const AnsiString & str) {super :: operator + = (str); return * this;} AnsiString operator + (const char * str) const; inline AnsiString operator + (const AnsiString & str) const {AnsiString as = * this; as + = str; return as;} inline bool operator = (const AnsiString & another) const {const char * aptr = another. ptr (); if (m_ptr = Aptr) return true; if (! M_ptr |! Aptr) return false; if (length ()! = Another. length () return false; return compare (another) = 0;} inline bool operator = (const char * str) const {if (m_ptr = str) return true; if (! M_ptr |! Str) return false; return compare (str) = 0;} // compare whether the string is equal to the inline bool operator! = (Const AnsiString & another) const {return! (Operator = (another);} // compare whether the string is equal to inline bool operator! = (Const char * str) const {return! (Operator = (str);} // The overload is smaller than the operator inline bool operator <(const AnsiString & another) const {return compare (another) <0 ;} // overload less than operator inline bool operator <(const char * another) const {return compare (another) <0 ;} // overload is greater than the operator inline bool operator> (const AnsiString & another) const {return compare (another)> 0 ;} // overload greater than operator inline bool operator> (const char * another) const {return compare (another)> 0 ;}// compare string size int compare (const AnsiString & another) const; // compare string size inline int compare (const char * str) const {return strcmp (m_ptr, str );} // string formatting AnsiString & format (const AnsiString fmt ,...); ansiString & format (const char * fmt ,...); ansiString & format (size_t max, const AnsiString fmt ,...); ansiString & format (size_t max, const char * fmt ,...); ansiString & formatArgs (const char * fmt, va_list args); AnsiString & formatArgs (size_t max, const char * fmt, va_list args); AnsiString & catWith (size_t max, const char * fmt ,...); ansiString & catWithArgs (size_t max, const char * fmt, va_list args); inline static size_t formatStringArgs (char * buffer, size_t max, const char * fmt, va_list args ); double toNumber () const; inline float toSingle () const {return (float) toNumber ();} inline int toInt () const {return (int) toNumber ();} long toInt64 () const;
  
 

The source file code is as follows:
AnsiString: operator + (const char * str) const // append String Length {if (! Str |! Str [0]) return * this; size_t myCount = length (); // the effective length of this object size_t srcLen = strlen (str ); // append the string length AnsiString result (myCount + srcLen); // The result object char * destPtr = result. own_ptr (); // obtain the writable memory pointer of the result object (new memory object requires a new writable object; otherwise, the result memory object will be reclaimed at the end of this function) if (myCount> 0) {memcpy (destPtr, m_ptr, myCount * sizeof (* m_ptr )); // copy the array object data of this object to the result object's memory: destPtr + = myCount; // move the Data Pointer} memcpy (destPtr, str, srcLen * sizeof (* m_ptr )); // copy the string data to the end If the append position of the object is destPtr + = srcLen; return result;} // compare the size of the two string objects: int AnsiString: compare (const AnsiString & another) const {if (another. ptr () = m_ptr) return 0; int cmp = (int) (length ()-another. length (); // String length comparison if (cmp! = 0) return cmp; return strcmp (m_ptr, another. m_ptr); // compares the string data pointed to by the String Data Pointer} // format the string to the string object data memory AnsiString & AnsiString: format (const AnsiString fmt ,...) {va_list args; va_start (args, fmt); formatArgs (fmt. ptr (), args); va_end (args); return * this;} // string format to string object data memory AnsiString & AnsiString :: format (const char * fmt ,...) // string formatting {va_list args; va_start (args, fmt); formatArgs (fmt, args); va_end (args); return * this;} // word String format to string object data memory (specify the maximum length) AnsiString & AnsiString: format (size_t max, const AnsiString fmt ,...) {va_list args; va_start (args, fmt); formatArgs (max, fmt. ptr (), args); va_end (args); return * this;} // string formatting (specify the maximum length) AnsiString & AnsiString: format (size_t max, const char * fmt ,...) {va_list args; va_start (args, fmt); formatArgs (max, fmt, args); va_end (args); return * this ;} // format the string to the data memory of the character object (maximum length limited) AnsiString & AnsiStri Ng: formatArgs (const char * fmt, va_list args) {char buffer [4059]; size_t len = formatStringArgs (buffer, sizeof (buffer)/sizeof (buffer [0]), fmt, args); setLength (len); memcpy (m_ptr, buffer, len * sizeof (* m_ptr); return * this ;} // format the string to the data memory (maximum length) of the character object AnsiString & AnsiString: formatArgs (size_t max, const char * fmt, va_list args) {setLength (max ); // warning C4996: 'vsnprintf': This function or variable may Be unsafe. # pragma warning (disable: 4996) size_t len = formatStringArgs (m_ptr, max, fmt, args); setLength (len); return * this ;} // append the string (Maximum length of formatting limited) AnsiString & AnsiString: catWith (size_t max, const char * fmt ,...) {va_list args; va_start (args, fmt); // obtain the parameter list catWithArgs (max, fmt, args); va_end (args); return * this ;} // append string (Maximum length of formatting limited) AnsiString & AnsiString: catWithArgs (size_t max, const char * fmt, va_list args ){ Char buffer [4059]; char * bufferPtr; size_t myLen = length (); if (max! = 0) {setLength (myLen + max); // reserved space bufferPtr = m_ptr + myLen;} else {bufferPtr = buffer; max = sizeof (buffer) /sizeof (buffer [0]); // use the character array cache without a limit on the maximum length} size_t len = formatStringArgs (bufferPtr, max, fmt, args ); setLength (myLen + len); if (bufferPtr = buffer) memcpy (m_ptr + myLen, buffer, len * sizeof (* m_ptr )); // copy the character array and cache it to the object's memory return * this;} // format it to the buffer size_t AnsiString: formatStringArgs (char * buffer, size_t max, Const char * fmt, va_list args) {// warning C4996: 'vsnprintf': This function or variable may be unsafe. # pragma warning (disable: 4996) size_t len = vsnprintf (buffer, max, fmt, args); if (len =-1) //-1 indicates that the buffer is filled with len = max; return len;} // converts it to a float number double AnsiString: toNumber () const {if (! M_ptr) return 0; char * e = NULL; double d = strtodd (m_ptr, & e); if (e & e [0]) return 0; return d ;} // convert to the long integer number long AnsiString: toInt64 () const {if (! M_ptr) return 0; const char * ptr = m_ptr; long lu = 0; while (* ptr) {lu * = 10; lu + = * ptr-'0 '; ptr ++;} return lu ;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.