📄 zstring_hh.h
字号:
//---------------------------------------------------------------------------
//-------- Zstring_H.h ------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Zstring_H.h // 防止被重复引用
#define Zstring_H.h
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "Jtypes_H.h"
//---------------------------------------------------------------------------
typedef int16u SizeType;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
class Zstring : public JObject
{ private:
static char NULLstr;
protected:
char* p; // 未初始化时p = NULL, size = 0.
SizeType size; // 一般情况下size = strlen(p)+1。
public:
// ------- constructors ----------------
// 构造器 Example: Zstring obj_a;
Zstring():size(0),p(NULL) {} // end constructor
// 构造器 Example: Zstring obj_a(str);
Zstring(const char* str);
// 构造器 Example: Zstring obj_a(obj_b);
Zstring(const Zstring& obj);
// 构造器 Example: Zstring obj_a(str,len);
Zstring(const char* str, const SizeType len);
// 构造器 用字符ch填满整个字串。Example: obj_a('.',10);
Zstring(const char ch, const SizeType len);
// 销毁器 destructor
~Zstring() { delete[] p; }
// ------- public functions ----------------
// 把字串对象初始化为: p = ""; size = 1;
// Example: obj.Initial();
void Initial();
// 把字串对象清除为:p = NULL; size = 0;
// Example: obj.Removal();
void Removal();
// 字串str部分装载,长度为len。
// Example: obj.Loadstr(s, 11);
void Loadstr(const char* str, const SizeType len);
// 字串对象部分装载,长度为len。
// Example: obj.Loadstr(obj_a, 11);
void Loadstr(const Zstring& str, const SizeType len);
// 得到字串必须占用的内存空间。(实际占用的内存可能还要更大。)
// Example: obj.GetSize();
SizeType GetSize()const { return size; } // 0 ~ 65535 对size只读。
// 得到字串的长度。返回字串的长度, 字串还没有初始化也返回零。
// Example: obj.Length(); p==NULL时,也返回零。
SizeType Length()const { return size ? size-1 : 0; } // 0 ~ 65534。
// 比较运算符。Example: (obj == s)
bool operator==(const char* s)const { return !strcmp(p, s); }
// 比较运算符。Example: (obj != s)
bool operator!=(const char* s)const { return strcmp(p, s); }
// 比较运算符。Example: (obj < s)
bool operator< (const char* s)const { return strcmp(p, s)< 0; }
// 比较运算符。Example: (obj > s)
bool operator> (const char* s)const { return strcmp(p, s)> 0; }
// 比较运算符。Example: (obj <= s)
bool operator<=(const char* s)const { return strcmp(p, s)<=0; }
// 比较运算符。Example: (obj >= s)
bool operator>=(const char* s)const { return strcmp(p, s)>=0; }
// 比较运算符。Example: (obj1 == obj2)
bool operator==(const Zstring& obj)const { return !strcmp(p, obj.p); }
// 比较运算符。Example: (obj1 != obj2)
bool operator!=(const Zstring& obj)const { return strcmp(p, obj.p); }
// 比较运算符。Example: (obj1 < obj2)
bool operator< (const Zstring& obj)const { return strcmp(p, obj.p)< 0; }
// 比较运算符。Example: (obj1 > obj2)
bool operator> (const Zstring& obj)const { return strcmp(p, obj.p)> 0; }
// 比较运算符。Example: (obj1 <= obj2)
bool operator<=(const Zstring& obj)const { return strcmp(p, obj.p)<=0; }
// 比较运算符。Example: (obj1 >= obj2)
bool operator>=(const Zstring& obj)const { return strcmp(p, obj.p)>=0; }
// 一个返回const char*的运算符。p==NULL时,也返回空串""。
// Example: (const char*)obj;
operator const char*()const
{ return p ? (const char*)p : (const char*)""; }
// 把字串对象转换成 const char* 指针。
// Example: const char* p = obj(); printf("%s",str());
const char* operator()(void)const { return (const char*)p; }
// 把字串对象转换成 char* 指针。注意,该操作极不安全!
// Example: char* p = (char*)obj;
operator char*()const { return p ? (char*)p : (char*)""; }
// 取子串对象中的子串。x为子串开始的下标(缺省为零), len为子串的长度。
// Example: obj2 = obj1(5,2); obj2 = obj1(6);
Zstring operator()(SizeType len, SizeType x = 0)const;
// 赋值运算符。字串自身改变,返回值只能用于右值。
// Example: obj_a = s;
const Zstring& operator=(const char* s);
// 赋值运算符。字串自身改变,返回值只能用于右值。
// Example: obj_a = obj_b;
const Zstring& operator=(const Zstring& obj);
// 字串加法运算符。字串自身不改变。
// Example: obj_b = obj_a + s;
Zstring operator+(const char* s)const;
// 字串加法运算符。字串自身不改变。
// Example: obj_c = obj_a + obj_b;
Zstring operator+(const Zstring& obj)const;
// 字串自加运算符。字串自身改变,返回值只能用于右值。
// Example: obj_a += s;
const Zstring& operator+=(const char* s)
{ return *this = *this + s; }
// 字串自加运算符。字串自身改变,返回值只能用于右值。
// Example: obj_a += obj_b;
const Zstring& operator+=(const Zstring& obj)
{ return *this = *this + obj; }
// 字串加法运算符。
// Example: obj_b = s + obj_a;
friend Zstring operator+(const char* s, const Zstring& obj);
// 把p字串复制到s中。s空间要足够。返回s。
// p如果为空,则立刻返回空。
// Example: char* str2 = obj.makestr(str1); ( str2地址与str1相同。)
char* makestr(char* s)const { return (p ? strcpy(s, p) : NULL); }
// 把对象截断成长度为len的字串, 并返回字串指针。
// 字串自身改变,取前len个字符。如果len超出源字串长度,什么都不做。
// Example: const char* p = obj.TruncStr(5);
const char* TruncStr(SizeType len);
// 返回字串的第一个字符。如果字串未初始化,返回空字符(0x00)。
// Example: char ch = obj.FirstChar();
char FirstChar()const { return p ? *p : NULL; }
// 返回字串的最后一个字符。如果字串未初始化或为空串,返回空字符(0x00)。
// Example: char ch = obj.LastChar();
char LastChar()const { return size>=2 ? p[size-2]: NULL; }
// 是否不为空串
bool IsNotEmpty()const { return (p && *p); }
// 是否空串(p="" or p=NULL)
bool IsEmpty()const { return !IsNotEmpty(); }
}; // end class Zstring
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// 下面是实现部分。
//
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#define Renew(var,typ); delete[] var; var = new typ;
#define NewChar(var,len); var = new char[len];
#define ReNewChar(var,len); Renew(var,char[len])
//---------------------------------------------------------------------------
//------ 构造器 -------------------------------------------------------------
// Zstring obj_a(str);
//---------------------------------------------------------------------------
inline Zstring::Zstring(const char *str)
{ if(str != NULL) // 如果str不空,构造出它的克隆。
{ size = strlen(str) + 1;
NewChar(p, size);
strcpy(p, str);
} // endif
else // 如果str为空,构造对象也为空。
{ p = NULL; size = 0; } // end else
} // end constructor
//---------------------------------------------------------------------------
//------ 构造器 -------------------------------------------------------------
// Zstring obj_a(obj_b);
//---------------------------------------------------------------------------
inline Zstring::Zstring(const Zstring& obj)
{ if(obj.p != NULL) // 如果obj不空,构造出它的克隆。
{ size = obj.size;
NewChar(p, size);
strcpy(p, obj.p);
} // endif
else // 如果obj为空,构造对象也为空。
{ p = NULL; size = 0; } // end else
} // end constructor
//---------------------------------------------------------------------------
//------ 构造器 -------------------------------------------------------------
// Zstring obj_a(str, len); 把长度为len的字串str创建为Zstring对象。
//---------------------------------------------------------------------------
inline Zstring::Zstring(const char *str, const SizeType len)
:p(NULL),size(0)
{ if(str != NULL) // 如果str不空,构造出它的克隆。
{ if( (size_t)len <= strlen(str) )
{ size = len + 1;
NewChar(p, size);
memcpy(p, str, len);
p[len]='\0';
} // endif
// else 所要的长度len比源字串还要长?什么都不做。
} // endif
} // end constructor
//---------------------------------------------------------------------------
//------ 构造器 -------------------------------------------------------------
// 把长度为len且全部初始化为ch的字串创建为Zstring对象。
//---------------------------------------------------------------------------
inline Zstring::Zstring(const char ch, const SizeType len)
{ size = len + 1;
NewChar(p, size);
setmem(p,len,ch); p[len] = '\0';
} // end constructor
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 该函数适用于 p==NULL 时的情况。置对象为空串("")。
//---------------------------------------------------------------------------
inline void Zstring::Initial()
{ size = 1; // this size maybe less than the old size
NewChar(p, 1);
*p = '\0';
} // end Initial
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 该函数适用于收回占用的资源。置对象为空(word = NULL)。
//---------------------------------------------------------------------------
inline void Zstring::Removal()
{ size = 0; // this size maybe less than the old size
delete[] p;
p = NULL;
} // end Removal
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 把字串数组str中前len个字节装载到p中。
//---------------------------------------------------------------------------
inline void Zstring::Loadstr(const char *str, const SizeType len)
{ if(str != NULL) // 如果str不空,构造出它的克隆。
{ if(size < len + 1) { ReNewChar(p, len + 1); } // endif
memcpy(p, str, len);
p[len] = '\0';
size = len + 1; // this size maybe less than the old size
} // endif
else { Removal(); } // end else
} // end Loadstr --for char[]
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 把字串对象str中前len个字节装载到p中。
//---------------------------------------------------------------------------
inline void Zstring::Loadstr(const Zstring&str, const SizeType len)
{ if(str.p != NULL) // 如果str不空,构造出它的克隆。
{ if(size < str.size) { ReNewChar(p, len + 1); } // endif
memcpy(p, str.p, len);
p[len] = '\0';
size = len + 1; // this size maybe less than the old size
} // endif
else { Removal(); } // end else
} // end Loadstr --for Zstring
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 字串自身改变,取前len个字符。如果len超出源字串长度,什么都不做。
//---------------------------------------------------------------------------
inline const char* Zstring::TruncStr(SizeType len)
{ if( len + 1 < size )
{ p[len] = '\0';
size = len + 1; // this size maybe less than the old size
} // endif
return p;
} // end TruncStr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// obj_a = str
//---------------------------------------------------------------------------
inline const Zstring& Zstring::operator=(const char *s)
{ if(s != NULL) // 如果str不空,构造出它的克隆。
{ register SizeType len = strlen(s) + 1 ;
if(size < len) { ReNewChar(p, len); } // endif
strcpy(p, s);
size = len; // this size maybe less than the old size
} // endif
else { Removal(); } // end else
return *this;
} //end operator=
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// obj_a = obj_b
//---------------------------------------------------------------------------
inline const Zstring& Zstring::operator=(const Zstring& obj)
{ if(obj.p != NULL) // 如果obj不空,构造出它的克隆。
{ if(size < obj.size) { ReNewChar(p, obj.size); } // endif
strcpy(p, obj.p);
size = obj.size; // this size maybe less than the old size
} // endif
else { Removal(); } // end else
return *this;
} // end operator=
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
// Written by JamesyFront. ZLGmcu Dev.Co.Ltd. 2002.
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -