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

📄 bstring_h.h

📁 MCS51单片机的宏汇编器源程序。有需要的朋友请下载!
💻 H
字号:
//---------------------------------------------------------------------------
//  Bstring_H.h
//---------------------------------------------------------------------------
#ifndef	Bstring_H.h  // 防止被重复引用
#define Bstring_H.h
//---------------------------------------------------------------------------
#include <stdio.h>

#include "Jtypes_H.h"

#include "string.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//                  类成员只有一个指针的字符串基类
//---------------------------------------------------------------------------
class Bstring : public JObject
{ protected:
    char* p;

    // 把字串对象转换成 char* 指针。注意,该操作极危险!不是高手请勿用!
    // Example:  char* p = (char*)obj;
    //operator char*()const
    // { puts("\nuse char*()"); return p; }

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

    // 一个返回const char*的运算符。p==NULL时,也返回空串""。
    // Example:  (const char*)obj;
    operator const char*()const     { return (const char*)p; }

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

    // 获取字串的长度。
    size_t getLen()const
     { Jassert(p!=NULL); return strlen(p); }

    // 比较运算符。Example: (obj == s)
    bool operator==(const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)==0; }

    // 比较运算符。Example: (obj != s)
    bool operator!=(const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)!=0; }

    // 比较运算符。Example: (obj < s)
    bool operator< (const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)< 0; }

    // 比较运算符。Example: (obj > s)
    bool operator> (const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)> 0; }

    // 比较运算符。Example: (obj <= s)
    bool operator<=(const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)<=0; }

    // 比较运算符。Example: (obj >= s)
    bool operator>=(const char* s)const
     { Jassert(p!=NULL && s!=NULL); return  strcmp(p, s)>=0; }

    // 比较运算符。Example: (obj1 == obj2)
    bool operator==(const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)==0; }

    // 比较运算符。Example: (obj1 != obj2)
    bool operator!=(const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)!=0; }

    // 比较运算符。Example: (obj1 < obj2)
    bool operator< (const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)< 0; }

    // 比较运算符。Example: (obj1 > obj2)
    bool operator> (const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)> 0; }

    // 比较运算符。Example: (obj1 <= obj2)
    bool operator<=(const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)<=0; }

    // 比较运算符。Example: (obj1 >= obj2)
    bool operator>=(const Bstring& obj)const
     { Jassert(p!=NULL && obj.p!=NULL); return strcmp(p, obj.p)>=0; }

    // 是否不为空串  (p!=NULL)
    bool IsNotEmpty()const
     { Jassert(p!=NULL); return (*p != '\0'); }

    // 是否空串      (p!=NULL)
    bool IsEmpty()const
     { Jassert(p!=NULL); return (*p == '\0'); }

    // 返回字串的第一个字符。  (p!=NULL)
    // 如果字串为空串,返回空字符(0x00)。
    char FirstChar()const
     { Jassert(p!=NULL); return *p; }

    // 返回字串的最后一个字符。(p!=NULL)
    // 如果字串为空串,返回空字符(0x00)。
    char LastChar()const
     { Jassert(p!=NULL); return *p ? p[getLen()-1] : '\0'; }

    // 把p字串复制到s中。s空间要足够。返回s。(p!=NULL)
    // Example: char* str2 = obj.makestr(str1);  ( str2地址与str1相同。)
    char* makeStr(char* s)const
     { Jassert(s!=NULL && p!=NULL); return strcpy(s, p); }

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


    // 一个直接把字串转换成字串对象的函数。
    static const Bstring& To(const char* s) { return (const Bstring&)s; } //

   // -------------------------
    void show()const        { printf("\n[Bstring][%s]",p?p:"null"); } //

}; // end class Bstring
//---------------------------------------------------------------------------


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




⌨️ 快捷键说明

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