Write string processing class by yourself

Source: Internet
Author: User

[Cpp] # ifndef _ young1_buffer # define _ young1_buffer # include <stdio h = ""> class YoungBuffer {protected: char * m_pData; public: YoungBuffer ();}; # endif # include "YoungBuffer. h "YoungBuffer: YoungBuffer (): m_pData (NULL) {}</stdio> [cpp] # ifndef _ young1_string # define _ young1_string # include <stdarg. h> # include <string> # define MAX_LOCAL_STRING_LEN 256 using namespace std; class YoungString: pub Lic YoungBuffer {public: virtual void clear () = 0; virtual int length () = 0; virtual void assign (const char * p_data, int iLen =-1) = 0; virtual void append (const char * p_data) = 0; virtual void upper () = 0; virtual void lower () = 0; virtual int _ cdecl Format (char * pstrFormat ,...) = 0 ;}; class YoungStringA: public YoungString {protected: bool m_bWideChar; char m_Buffer [MAX_LOCAL_STRING_LEN + 1]; // Because the end of \ 0 is required, the length is + 1 protected: char * GetPtr (const char * p_data, bool bAppend); public: int _ cdecl Format (char * pstrFormat ,...); void upper (); void lower (); void clear (); int length (); void assign (const char * p_data, int iLen =-1 ); void append (const char * p_data); YoungStringA (const char * p_data = NULL); YoungStringA (const char * p_data, int iLen); YoungStringA (YoungStringA & src ); operator char *( ); Operator const char * (); YoungStringA & operator = (const char * p_data); YoungStringA & operator = (string str); // YoungString operator = (string str ); difference whether to call the copy constructor}; # endif [cpp] // YoungString. cpp: Defines the entry point for the console application. // # include <tchar. h> # include "YoungBuffer. h "# include" YoungString. h "int YoungStringA: Format (char * pstrFormat ,...) {YoungStringA sFormat = ps TrFormat; // Do ordinary printf replacements // NOTE: incluented max-length of wvsprintf () is 1024 char szBuffer [1025] = {0}; va_list argList; va_start (argList, pstrFormat); int iRet = vsprintf (szBuffer, sFormat, argList); va_end (argList); assign (szBuffer); return iRet;} void YoungStringA: clear () {m_pData = NULL; memset (m_Buffer, 0, MAX_LOCAL_STRING_LEN + 1);} void YoungStringA: append (const c Har * p_data) {int iNewLength = length () + (int) strlen (p_data); if (iNewLength> = MAX_LOCAL_STRING_LEN) {if (m_pData = m_Buffer) {m_pData = static_cast <char *> (malloc (iNewLength + 1) * sizeof (char); strcpy (m_pData, m_Buffer); strcat (m_pData, p_data );} else {m_pData = static_cast <char *> (realloc (m_pData, (iNewLength + 1) * sizeof (char); strcat (m_pData, p_data );}} else {if (m_pData! = M_Buffer) {free (m_pData); m_pData = m_Buffer;} strcat (m_Buffer, p_data) ;}void YoungStringA: upper () {strupr (m_pData);} void YoungStringA:: lower () {strlwr (m_pData);} int YoungStringA: length () {if (m_bWideChar) {return wcslen (const wchar_t *) m_pData );} else {return strlen (const char *) m_pData);} // return strlen ()} void YoungStringA: assign (const char * p_data, int iLen) {if (p_dat A = NULL) p_data = ""; iLen = (iLen <0? (Int) strlen (p_data): iLen); if (iLen <MAX_LOCAL_STRING_LEN) {if (m_pData! = M_Buffer) {free (m_pData); m_pData = m_Buffer;} else if (iLen> length () | m_pData = m_Buffer) // {if (m_pData = m_Buffer) m_pData = NULL; m_pData = (char *) realloc (m_pData, (iLen + 1) * sizeof (char);} memset (m_pData, 0, iLen); strncpy (m_pData, p_data, iLen);} YoungStringA: YoungStringA (const char * sz) {clear (); assign (sz);} YoungStringA: YoungStringA (const char * sz, int iLen) {clear (); assign (sz, iLen);} YoungStringA: YoungStringA (YoungStringA & src) // copy the constructor {assign (src. m_pData);} YoungStringA & YoungStringA: operator = (const char * p_data) {if (p_data = NULL) {clear ();} else {assign (p_data );} return * this;} YoungStringA & YoungStringA: operator = (string str) {assign (str. c_str (); return * this;} YoungStringA: operator char * () {return m_pData;} YoungStringA: operator const char * () {return m_pData ;}

Related Article

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.