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

📄 z_shstr.h

📁 一个跨平台的共享字符串类实现
💻 H
字号:
#ifndef Z_SHSTR_H_
#define Z_SHSTR_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <z_dbg.h>
#include <string.h>
#include <malloc.h>
/////////////////////////////////////////////////////////////////////////////////////
//	NAME:	share_str
//	DESC:	具有共享能力的字符串类
/////////////////////////////////////////////////////////////////////////////////////
const int MAGIC_LOCK_CNT = -1;
#include <z_push8.h>
class ZBCDLL share_str  
{
public:
	share_str() : m_pBuf(ref_null_string())					//	构造一个空字符串
	{}
	share_str(const char* str) : m_pBuf(allocate(str))	//	根据C风格字符串构造
	{}
	explicit share_str(char c) : m_pBuf(allocate(1, &c, 1))//	构造单个字符的字符串
	{}
	share_str(const char* pb, const char* pe) : m_pBuf(allocate(pe-pb, pb, pe-pb))	//	字符串[pb, pe)
	{}
	share_str(const share_str& rhs) : m_pBuf(rhs.addRef())
	{}
	~share_str(){
		releaseRef();
	} 
public:
	static share_str print_str(const char* fmt, ...);		//	打印文字到一个新生成的字符串
	share_str substr(size_t nBegin, size_t nEnd) const;	//	取子串[nBegin, nEnd)
	share_str& print(const char* fmt, ...);//	打印文字到该字符串对象

	const char* replace(const char* pszFind, const char* pszRep);
	const char* replace(const char* pszBegin, const char* pszFind, const char* pszRep);

	char get(size_t idx) const;			//	取某位置处的字符
	void set(size_t idx, char c);		//	设置某位置处的字符,如果该字符为'\0',将导致字符串被截断,长度自动调整

	bool insert(size_t nPos, char c);	//	在字符串的某位置插入一个字符
	bool insert(size_t nPos, const char* pszIns);		//	在字符串的某位置插入一个字符串
	bool insert(size_t nPos, const char* pszIns, size_t nInsLen);	//	在字符串的某位置插入一个字符串的一部分
	bool remove(size_t nPos, size_t nRemoveLen);		//	在字符串的某位置开始擦除若干字符

	char* lock(size_t rq_len);			//	锁定字符串的缓冲区,请求保留长度为rq_len的缓存区
	char* lock();						//	锁定字符串的缓冲区
	void unlock();						//	解锁字符串的缓冲区,并将重新测试字符串的长度

	size_t measure();					//	测量字符串的实际长度,并更新到长度字段
	size_t length() const;				//	取字符串的长度
	bool islock() const;				//	判断该字符串是否已经锁定
	bool isempty() const;				//	判断该字符串是否空字串
	const char* c_str() const;			//	取实际存储的C风格字符串
	
	bool reserved(size_t bufSize);		//	为字符串保留一定长度的缓存空间
	void swap(share_str& rhs);			//	交换两个字符串的内部缓存
	void clear();						//	清除字符串的内容
public:
	share_str& operator=(const share_str& rhs);
	share_str& operator=(const char* str);
	share_str& operator=(char c);

	share_str& operator+=(const share_str& rhs);
	share_str& operator+=(const char* str);
	share_str& operator+=(char c);

	char operator[](size_t pos) const{
		return get(pos);
	}
public:
	bool zbcCompare_(const share_str& rhs) const{
		return m_pBuf == rhs.m_pBuf 
			|| (length() == rhs.length() && strcmp(c_str(), rhs.c_str()) == 0);	
	}
private:
	struct ZbcStringBuf{
		int		m_refcnt;			//使用计数
		size_t	m_len;				//字符串长度
		size_t	m_bufsize;			//缓冲可容纳的字符串的最大长度
		char	m_szBuf[4];			//字符串缓冲区
	};
	ZbcStringBuf* addRef() const;
	void releaseRef();
	char* lock(size_t minlen, ZbcStringBuf** pNewbuf);//指定长度的锁定
	void unlock(size_t cur_len);

	static ZbcStringBuf* allocate(size_t bufSize);
	static ZbcStringBuf* allocate(const char* szSrc);
	static ZbcStringBuf* allocate(size_t bufSize, const char* szSrc, size_t strLen);
	static ZbcStringBuf* ref_null_string();

	static ZbcStringBuf m_nullString;
private:
	ZbcStringBuf* m_pBuf;
};
//////////////////////////////////////////////////////////////////////////////
inline char share_str::get(size_t idx) const{			//	取某位置处的字符
	return m_pBuf->m_szBuf[idx];
}
inline bool share_str::insert(size_t nPos, char c){		//	在字符串的某位置插入一个字符
	return insert(nPos, &c, 1);
}
inline bool share_str::insert(size_t nPos, const char* pszIns){	//	在字符串的某位置插入一个字符串
	return insert(nPos, pszIns, strlen(pszIns));
}
inline char* share_str::lock(){							//	锁定字符串的缓冲区
	return lock(length());
}
inline void share_str::unlock(){						//	解锁字符串的缓冲区,并将重新测试字符串的长度
	unlock(strlen(c_str()));
}
inline void share_str::clear(){							//	清除字符串的内容
	releaseRef();
	m_pBuf = ref_null_string();
}
inline size_t share_str::measure(){						//	测量字符串的实际长度,并更新到长度字段
	return m_pBuf->m_len = strlen(m_pBuf->m_szBuf);
}
inline size_t share_str::length() const{				//	取字符串的长度
	return m_pBuf->m_len;
}
inline bool share_str::islock() const{					//	判断该字符串是否已经锁定
	return m_pBuf->m_refcnt == MAGIC_LOCK_CNT;
}
inline bool share_str::isempty() const{					//	判断该字符串是否空字串
	return m_pBuf->m_len == 0;
}
inline const char* share_str::c_str() const{			//	取实际存储的C风格字符串
	return m_pBuf->m_szBuf;
}
inline void share_str::unlock(size_t cur_len){				//	解锁缓冲区,重新设定长度
	ASSERT(cur_len == strlen(m_pBuf->m_szBuf));
	m_pBuf->m_refcnt = 1;
	m_pBuf->m_len = cur_len;
}
inline share_str::ZbcStringBuf* share_str::ref_null_string(){
	++m_nullString.m_refcnt;
	return &m_nullString;
}
//分配一个缓冲,并将szSrc拷贝到缓冲里
inline share_str::ZbcStringBuf* share_str::allocate(const char* szSrc){
	if (!szSrc)
		return ref_null_string();
	size_t len = strlen(szSrc);
	return allocate(len, szSrc, len);
}
inline void share_str::releaseRef(){
	if (m_pBuf->m_refcnt == MAGIC_LOCK_CNT || --m_pBuf->m_refcnt == 0){		//锁定状态和独占状态
		free(m_pBuf);
	}
}
inline share_str::ZbcStringBuf* share_str::addRef() const{
	if (islock())						//锁定的资源,重新分配
		return allocate(c_str());
	else{
		++m_pBuf->m_refcnt;
		return m_pBuf;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////
//	重载操作符
//////////////////////////////////////////////////////////////////////////////////////////
inline share_str operator+(const share_str& lhs, const share_str& rhs){
	share_str tmp(lhs);
	tmp += rhs;
	return tmp;
}
inline share_str operator+(const share_str& lhs, const char* str){
	share_str tmp(lhs);
	tmp += str;
	return tmp;
}
inline share_str operator+(const char* str, const share_str& rhs){
	share_str tmp(str);
	tmp += rhs;
	return tmp;
}
inline share_str operator+(const share_str& lhs, char c){
	share_str tmp(lhs);
	tmp += c;
	return tmp;
}
inline share_str operator+(char c, const share_str& rhs){
	share_str tmp(c);
	tmp += rhs;
	return tmp;
}
inline bool operator==(const share_str& lhs, const share_str& rhs){
	return lhs.zbcCompare_(rhs);
}
inline bool operator==(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) == 0;
}
inline bool operator==(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) == 0;
}
inline bool operator!=(const share_str& lhs, const share_str& rhs){
	return !operator==(lhs, rhs);
}
inline bool operator!=(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) != 0;
}
inline bool operator!=(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) != 0;
}
inline bool operator>(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) > 0;
}
inline bool operator>(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) > 0;
}
inline bool operator>(const share_str& lhs, const share_str& rhs){
	return strcmp(lhs.c_str(), rhs.c_str()) > 0;
}
inline bool operator<(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) < 0;
}
inline bool operator<(const share_str& lhs, const share_str& rhs){
	return strcmp(lhs.c_str(), rhs.c_str()) < 0;
}
inline bool operator<(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) < 0;
}
inline bool operator>=(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) >= 0;
}
inline bool operator>=(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) >= 0;
}
inline bool operator>=(const share_str& lhs, const share_str& rhs){
	return strcmp(lhs.c_str(), rhs.c_str()) >= 0;
}
inline bool operator<=(const share_str& lhs, const char* str){
	return strcmp(lhs.c_str(), str) <= 0;
}
inline bool operator<=(const char* str, const share_str& rhs){
	return strcmp(str, rhs.c_str()) <= 0;
}
inline bool operator<=(const share_str& lhs, const share_str& rhs){
	return strcmp(lhs.c_str(), rhs.c_str()) <= 0;
}
#include <z_pop.h>

#endif //Z_SHSTR_H_
/*
 *	Copyright (c) 2003-2005 Zbchun.
 *	这是一个开源软件,允许任意使用,但必须保留该版权声明。
 */

⌨️ 快捷键说明

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