⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astring_h.h

📁 MCS-51单片机宏汇编器源程序,单片机初学者必看
💻 H
字号:
//---------------------------------------------------------------------------
//  Astring_H.h
//---------------------------------------------------------------------------
#ifndef	Astring_H.h  // 防止被重复引用
#define Astring_H.h
//---------------------------------------------------------------------------

#include "Bstring_H.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//                          ASCII字符串类
//
// 该字符串类对于程序中存在大量的空串时能有效地节省空间。
// 但它不适合于字串内容的改动非常频繁的字符串。
// 也不适合经常需要求字串长度的字符串。
//---------------------------------------------------------------------------
class Astring : public Bstring
{ protected:
    static char End_of_Str;        // 该字节必须恒为零,不得改写!

   // ------- protected functions --------

     char* NULLstr()const        { return &End_of_Str; } //

     void  Initial()             { p = NULLstr(); } //

     void  FreeBuf()             { if(p!=NULLstr()) { delete[] p; } } //

     char* RenewBuf(size_t n)    { FreeBuf(); return p = new char[n]; } //

  public:

   // --------- constructors ------------

    // 构造器0 Example: Astring obj_a;
    Astring()                    { Initial(); } // end constructor

    // 构造器1 Example: Astring obj_a(str);
    Astring(const char* str);

    // 构造器2 Example: Astring obj_a(obj_b);
    Astring(const Astring& obj);

    // 构造器3 Example: Zstring obj_a(str,len);
    Astring(const char* str, size_t len);

    // 构造器4 Example: Astring obj_a(obj_b,len);
    Astring(const Astring& obj, size_t len);

    // 构造器5 用字符ch填满整个字串。Example: obj_a('.',10);
    Astring(char ch, size_t len);

    // 销毁器 destructor
   ~Astring()                    { FreeBuf(); } // end destructor


   // --------- public functions --------

    // 取字串对象中的子串。x为子串开始的下标(缺省为零), len为子串的长度。
    // 该函数产生一个新Astring对象作为返回值。字串自身不改变。
    // Example:  obj2 = obj1(4,2); [obj1="abcdefghi", obj2="cdef"]
    // Example:  obj2 = obj1(6);   [obj1="abcdefghi", obj2="abcedf"]
    Astring operator()(size_t len, size_t x = 0)const;


   // ----------------------------------

    // 把原字串对象清除,释放内存,并初始化为:p = ""(NULLstr);
    void Removal();


    // 把字串数组str中前len个字节装载到p中。字串自身改变。
    // Example: obj.Loadstr(s, 11);
    void Loadstr(const char* str, size_t len);

    // 把字串对象str中前len个字节装载到p中。字串自身改变。
    // Example: obj.Loadstr(obj_a, 11);
    void Loadstr(const Astring& obj, size_t len);

    // 赋值运算符。字串自身改变。
    // Example: obj_a = s;
    const Astring& operator=(const char* s);

    // 赋值运算符。字串自身改变。
    // Example: obj_a = obj_b;
    const Astring& operator=(const Astring& obj);


    // 字串加法运算符。字串自身不改变。
    // Example: obj_b = obj_a + s;
    Astring operator+(const char* s)const;

    // 字串加法运算符。字串自身不改变。
    // Example: obj_c = obj_a + obj_b;
    Astring operator+(const Astring& obj)const;


    // 字串自加运算符。字串自身改变。
    // Example: obj_a += s;
    const Astring& operator+=(const char* s)
     { return *this = *this + s; }

    // 字串自加运算符。字串自身改变。
    // Example: obj_a += obj_b;
    const Astring& operator+=(const Astring& obj)
     { return *this = *this + obj; }


    // 把对象截断成长度为len的字串, 并返回字串指针。
    // 字串自身改变,取前len个字符。若len超出源字串长度,什么都不做。
    // 它只是在截得的那个字串的末尾填'\0'。比Bstring的truncStr安全!
    // Example: const char* p = obj.FixTruncStr(5);
    const char* FixTruncStr(size_t len)
     { if(len<=getLen()) { p[len] = '\0'; } // endif
        return p;
     } // end FixTruncStr


   // -------------------------------
    void show()const;

   // -------- friend functions ---------

    // 字串加法运算符。
    // Example: obj_b = s + obj_a;
    friend Astring operator+(const char* s, const Astring& obj);

    friend Astring operator+(const Bstring& obj, const char* s);

}; // end class Astring
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//
//                        下面是实现部分。
//
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#pragma warn -8058
char Astring::End_of_Str = '\0';
//---------------------------------------------------------------------------

//------ 构造器1 ------------------------------------------------------------
// Astring obj_a(str);    (str!=NULL)
//---------------------------------------------------------------------------
inline Astring::Astring(const char *str)
{ Jassert(str!=NULL);
  if(*str == '\0')                // 如果str为空,构造对象为空串。
   { Initial(); } // endif
  else                            // 如果str不空,构造出它的克隆。
   { p = new char[strlen(str)+1]; strcpy(p, str); } // end else
} // end constructor
//---------------------------------------------------------------------------

//------ 构造器2 ------------------------------------------------------------
// Astring obj_a(obj_b);
//---------------------------------------------------------------------------
inline Astring::Astring(const Astring& obj)
{ if(obj.IsEmpty())               // 如果obj为空,构造对象为空串。
   { Initial(); } // endif
  else                            // 如果obj不空,构造出它的克隆。
   { p = new char[obj.getLen()+1]; strcpy(p, obj.p); } // end else
} // end constructor
//---------------------------------------------------------------------------

//------ 构造器3 ------------------------------------------------------------
// Astring obj_a(str,len);   (str!=NULL)
//---------------------------------------------------------------------------
inline Astring::Astring(const char* str, size_t len)
{ Jassert(str!=NULL);
  Jassert(len<=strlen(str));
  if(len == 0)
   { Initial(); } // endif
  else
   { p = new char[len+1]; memcpy(p, str, len); p[len]='\0'; } // end else
} // end constructor
//---------------------------------------------------------------------------

//------ 构造器4 ------------------------------------------------------------
// Astring obj_a(obj_b,len);
//---------------------------------------------------------------------------
inline Astring::Astring(const Astring& obj, size_t len)
{ Jassert(len<=obj.getLen());
  if(obj.IsEmpty() || len == 0)
   { Initial(); } // endif
  else
   { p = new char[len+1]; memcpy(p, obj.p, len); p[len]='\0'; } // end else
} // end constructor
//---------------------------------------------------------------------------

//------ 构造器5 ------------------------------------------------------------
// 用字符ch填满整个字串。Example: obj_a('.',10);
//---------------------------------------------------------------------------
inline Astring::Astring(char ch, size_t len)
{ if(len == 0)
   { Initial(); } // endif
  else
   { p = new char[len+1]; setmem(p, len, ch); p[len]='\0'; } // end else
} // end constructor
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 把原字串对象清除,释放内存,并初始化为:p = ""(NULLstr);
//---------------------------------------------------------------------------
inline void Astring::Removal()
{ if(p != NULLstr())
   { delete[] p; Initial(); } // endif
} // end Initial
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 把字串数组str中前len个字节装载到p中。
// Example: obj.Loadstr(s, 11);
//---------------------------------------------------------------------------
inline void Astring::Loadstr(const char *str, size_t len)
{ Jassert(str!=NULL);
  Jassert(len<=strlen(str));

  if(*str == '\0' || len == 0)
   { Removal(); } // endif
  else
   { RenewBuf(len+1); memcpy(p, str, len); p[len] = '\0'; } // end else
} // end Loadstr --for char[]
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 把字串对象str中前len个字节装载到p中。
// Example: obj.Loadstr(obj_a, 11);
//---------------------------------------------------------------------------
inline void Astring::Loadstr(const Astring& obj, size_t len)
{ Jassert(len<=obj.getLen());

  if(obj.IsEmpty() || len == 0)
   { Removal(); } // endif
  else
   { RenewBuf(len+1); memcpy(p, obj.p, len); p[len] = '\0'; } // end else
} // end Loadstr --for obj
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// 取字串对象中的子串。x为子串开始的下标(缺省为零), len为子串的长度。
// Example:  obj2 = obj1(4,2); [obj1="abcdefghi", obj2="cdef"]
// Example:  obj2 = obj1(6);   [obj1="abcdefghi", obj2="abcedf"]
//---------------------------------------------------------------------------
inline Astring Astring::operator()(size_t len, size_t x)const
{ Jassert(x<=getLen());

  Astring temp(p + x, len);
  return temp;
} // end operator()
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// 赋值运算符。字串自身改变。
// Example: obj_a = s;
//---------------------------------------------------------------------------
inline const Astring& Astring::operator=(const char* s)
{ Jassert(s!=NULL);
  //if(this == s) { return *this; } // endif // 该行要不要随您。
  if(*s == '\0')
   { Removal(); } // endif
  else
   { RenewBuf(strlen(s)+1); strcpy(p, s); } // end else
  return *this;
} //end operator=  -- for str
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 赋值运算符。字串自身改变。
// Example: obj_a = obj_b;
//---------------------------------------------------------------------------
const Astring& Astring::operator=(const Astring& obj)
{ //if(this == &obj) { return *this; } // endif // 该行要不要随您。
  if(obj.IsEmpty())
   { Removal(); } // endif
  else
   { RenewBuf(obj.getLen()+1); strcpy(p, obj.p); } // end else
  return *this;
} //end operator=  -- for obj
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 字串加法运算符。字串自身不改变。
// Example: obj_b = obj_a + s;
//---------------------------------------------------------------------------
inline Astring Astring::operator+(const char* s)const
{ Jassert(s!=NULL);

  if(*s == '\0')     // 如果s为空串
   { return *this; } // endif
  else
   { Astring temp;                                // temp.p 还没被new。
     register size_t alen = this->getLen();

     temp.p = new char[alen + strlen(s) + 1];
     strcpy(temp.p, p);
     strcpy(temp.p + alen, s);
     return temp;
   } // end else
} // end operator+ -- for str
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 字串加法运算符。字串自身不改变。
// Example: obj_c = obj_a + obj_b;
//---------------------------------------------------------------------------
inline Astring Astring::operator+(const Astring& obj_b)const
{ if(obj_b.IsEmpty())     // 如果obj_b空
   { return *this; } // endif
  else                    // obj_b不空
   { Astring temp;                                // temp.p 还没被new。
     register size_t alen = this->getLen();

     temp.p = new char[alen + obj_b.getLen() + 1];
     strcpy(temp.p, p);
     strcpy(temp.p + alen, obj_b.p);
     return temp;
   } // end else
} // end operator+ -- for obj
//---------------------------------------------------------------------------


//------ Friend Function operator+ ------------------------------------------
//    obj_c = str + obj_b
//---------------------------------------------------------------------------
inline Astring operator+(const char *s, const Astring& obj)
{ Jassert(s!=NULL);

  if(*s == '\0')
   { Astring temp(obj); return temp; } // endif

  if(obj.IsEmpty())
   { Astring temp(s); return temp; } // endif

  Astring temp;
  register size_t slen = strlen(s);

  temp.p = new char[slen + obj.getLen() + 1];
  strcpy(temp.p, s);
  strcpy(temp.p + slen, obj.p);
  return temp;
} // end friend operator+
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//    str1 + str2 = ?
//    obj_c = Bstring::To(str1) + str2
//---------------------------------------------------------------------------
inline Astring operator+(const Bstring& obj, const char* s)
{ Jassert((const char*)obj!=NULL && s!=NULL);

  if(obj.IsEmpty())
   { Astring temp(s); return temp; } // endif

  if(*s == '\0')
   { Astring temp(obj); return temp; } // endif

  Astring temp;
  register size_t slen = obj.getLen();

  temp.p = new char[slen + strlen(s) + 1];
  strcpy(temp.p, (const char*)obj);
  strcpy(temp.p + slen, s);
  return temp;
} // end friend operator+
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
inline void Astring::show()const
{ printf(" [Astring][%s]",p);
  if(p==NULLstr()) { printf("\t(p=NULLstr)"); } // endif
  printf("\tlen=%u",getLen());
} // end show
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//               Written by JamesyFront.    ZLGmcu Dev.Co.Ltd.  2002.
//---------------------------------------------------------------------------


⌨️ 快捷键说明

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