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

📄 jstring_h.h

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

#include "Astring_H.h"

#pragma warn -8027
//---------------------------------------------------------------------------
// 一个增强的字串类。
//---------------------------------------------------------------------------
class Jstring : public Astring
{ public:
   // -------- constructor ---------------

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

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

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

    // 构造器3 Example: JStrings obj_a(str,len);
     Jstring(const char* str, size_t len):Astring(str,len) {}

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

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

    // 析构器 destructor
    ~Jstring()  {} // end destructor

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

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

     // 慎用。 Example:   const char* str = obj();
     const char* operator()(void)const    { return (const char*)p; } //

     // 把Astring对象转换成Jstring对象。
     // 用处在于把基类升级为高级类, 来得到高级类的特殊服务。请慎用。
     // Example: const Jstring& s2 = s2(s1);
     const Jstring& operator()(const Astring& obj)const
      { return (const Jstring&) obj; }

     // 赋值运算符。Example: obj_a = s;
     const Jstring& operator=(const char* s)
      { return (const Jstring&)Astring::operator=(s); }

     // 赋值运算符。Example: obj_a = obj_b;
     const Jstring& operator=(const Astring& obj)
      { return (const Jstring&)Astring::operator=(obj); }


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

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


     // 字串自加运算符。字串自身改变。其实字串自加并不会提高效率。
     // Example: obj_a += s;
     const Jstring& operator+=(const char* s)
      { return *this = *this + s; }

     // 字串自加运算符。字串自身改变。其实字串自加并不会提高效率。
     // Example: obj_a += obj_b;
     const Jstring& operator+=(const Astring& obj)
      { return *this = *this + obj; }

     // 把对象截断成长度为len的字串, 并返回字串对象引用。注意,是对象引用!
     // 字串自身改变,取前len个字符。
     // 它只是在截得的那个字串的末尾填'\0'。
     // 注意!若len超出源字串长度,~! :(
     // Example: const char* p = obj.Trunc(5);
     const Jstring& Trunc(size_t len)
      { Jassert(p!=NULLstr());
        Jassert(len<=getLen());
        p[len] = '\0';
        return *this;
      } // end Trunc


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

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

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

     void show()const   { puts("J show."); }


   // -------- something special ---------------

     // 返回字串的第二个字符。注意下标不要越界!
     // 当字串只含有一个字符时,如("A"),返回值是'\0'。
     char SecondChar()const
      { Jassert(getLen()>=1); return p[1]; }

     // p字串自己改变为全大写。
     void ToUp()                   { strupr(p); }

     // p字串自己改变为全小写。
     void ToDown()                 { strlwr(p); }

     // 返回一个除去首字符的字符串。p字串自己不改变。
     const char* maskHead()const
      { Jassert(getLen()>=1); return p+1; }

     // 返回一个除去前x个字符的字符串。p字串自己不改变。
     const char* maskHead(size_t x)const
      { Jassert(getLen()>=x); return p+x; }


     // 删除字串的第一个字符,p字串自己改变。注意p不应该是空串("")。
     void DelHead()
      { Jassert(p!=NULL && *p!='\0');
        while(*p) { *(p-1) = *(++p); } // end while   It is the old flavour.
      }

     // 删除字串的最后一个字符,p字串自己改变。注意p不应该是空串("")。
     void DelTail()
      { Jassert(p!=NULL && *p!='\0'); p[getLen()-1] = '\0'; }


     // 判断第一个字符是否为数字。在Jstring里,p不会为NULL。
     bool IsFirstCharDigit()const
      { Jassert(p!=NULL); return (*p >= '0' && *p <= '9'); }


     // 判断字符串是否为数值串, 是则返回X进制值。
     // 否则返回零。( 0, 2, 8, 10, 16 )
     int8u IsValueStr()const;


     // 把字符串转换为32位的整数。
     // 注意,p字串必须是数串!!!不包含'H','O','B'等。
     int32u TransToint32u(int8u radix)const
      { register char* endptr;
        return strtoul(p, &endptr, radix);
      } // end TransHexToint32u


     // 如果是数串,输出它的值到value。
     // 如果不是数字串,返回0; 如果是数串,返回非零值
     // 如果是数串中含有'$'符号,它会被剔除。(dollarCat())
     int8u ValueStrToValue(int32u &value);  // value 带值返回。

     // 把其中的'$'号去掉。
     void dollarCat();       // 只用于ValueStrToValue()内部。

     // 判断字串是否数字串,是则显示出相应的信息。否则什么都不做。
     void TellmeStrtype()const;   // ( for Debug )

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

     // 把字串中的某个字符删除,注意,n指向的字符就是所删字符,它必须在字串里!
     void DelCharInStr(size_t n); // 此函数要慎用!

     // 用字串中的字串替换本身。注意,n指向的就是新串,它必须在字串里!
     void ReplaceBySubstr(size_t n);    // 此函数要慎用!


     // 将字串首尾的空格去除。
     void NoBlankInStr();              // 只供特别用途。

     // 将字串首尾的字符ch去除(如果ch存在的话)。
     void Trim(char ch);               // 只供特别用途。

}; // end class Jstring
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// 取子串。
//---------------------------------------------------------------------------
inline Jstring Jstring::operator()(size_t len, size_t x)const
{ Jassert(x<=getLen());
                            puts("\nuse Jstring::()(len,x)");
  Jstring temp(p + x, len);
  return temp;
} // end operator()
//---------------------------------------------------------------------------

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

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

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

⌨️ 快捷键说明

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