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

📄 inststr.cpp

📁 跨平台2D引擎
💻 CPP
字号:
//instStr.cpp
#ifndef INST_STR_CPP
#define INST_STR_CPP


#include <instDefines.h>
#include <instStr.h>

#include <windows.h>


namespace inst
{


__forceinline CODEPAGE CStr::GetSystemCodePage()
{
	return ::GetACP(); 
}

CStr::CStr()
{
 	m_pBuf=new WChar[1+(m_Len=0)];
}

CStr::CStr(UInt32 len,Int32 unused)
{
	m_pBuf=new WChar[1+(m_Len=len)];
}

CStr::CStr(const CStr &str)
{
	m_pBuf=new WChar[1+(m_Len=str.m_Len)];
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		m_pBuf[i]=str.m_pBuf[i];
	m_pBuf[m_Len]=0;
}

CStr::CStr(const Char *chars) //非常慢!建议不要用这个来初始化!
{
	UInt32 i=0;
	while(chars[i]!=0)
		++i;
	UInt32 len=i;
	WChar *tmp=new WChar[len+1];
	for(i=0;i<len;i++)  //不 包括字符串尾 //其实只需要填充后半部分!
		tmp[i]=0;
	tmp[len]=0;
	::MultiByteToWideChar(GetSystemCodePage(),0,chars,len,tmp,len);
	i=0;
	while(tmp[i]!=0) //其实如果倒过来搜索可以加速
		++i;
	m_Len=i;
	//或者把下列代码改为m_pBuf=tmp;
	m_pBuf=new WChar[m_Len+1];
	for(i=0;i<m_Len;i++) //不 包括字符串尾
		m_pBuf[i]=tmp[i];
	m_pBuf[m_Len]=0;
}

CStr::CStr(const Char *chars,CODEPAGE cp) //非常慢!建议不要用这个来初始化!
{
	UInt32 i=0;
	while(chars[i]!=0)
		++i;
	UInt32 len=i;
	WChar *tmp=new WChar[len+1];
	for(i=0;i<len;i++)  //不 包括字符串尾 //其实只需要填充后半部分!
		tmp[i]=0;
	tmp[len]=0;
	::MultiByteToWideChar(CP(cp),0,chars,len,tmp,len);
	i=0;
	while(tmp[i]!=0) //其实如果倒过来搜索可以加速
		++i;
	m_Len=i;
	//或者把下列代码改为m_pBuf=tmp;
	m_pBuf=new WChar[m_Len+1];
	for(i=0;i<m_Len;i++) //不 包括字符串尾
		m_pBuf[i]=tmp[i];
	m_pBuf[m_Len]=0;
}

CStr::CStr(const WChar *wchars)
{
	UInt32 i=0;			//UInt32 pos=1;
	while(wchars[i]!=0) //while(wchars[pos-1]!=0)
		++i;			//	++pos;
	m_Len=i;			//m_Len=pos-1;
	m_pBuf=new WChar[m_Len+1];
	for(i=0;i<m_Len;i++) //不 包括字符串尾
		m_pBuf[i]=wchars[i];
	m_pBuf[m_Len]=0;
}

CStr::~CStr()
{
	SafeDelArray(m_pBuf);
}

//建议使用CStr来相互运算,否则慢

CStr &CStr::operator =(const CStr &str)
{
	if(&str==this)return *this;
	SafeDelArray(m_pBuf);
	m_pBuf=new WChar[1+(m_Len=str.m_Len)];
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		m_pBuf[i]=str.m_pBuf[i];
	m_pBuf[m_Len]=0;
	return *this;
}

Bool CStr::operator ==(const CStr &str)const
{
	if(m_Len!=str.m_Len)return False;
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		if(m_pBuf[i]!=str.m_pBuf[i])return False;
	return True;
}

Bool CStr::operator !=(const CStr &str)const
{
	if(m_Len!=str.m_Len)return False;
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		if(m_pBuf[i]!=str.m_pBuf[i])return True;
	return False;
}

CStr &CStr::operator +=(const CStr &str)
{
	WChar *tmp=new WChar[1+m_Len+str.m_Len];
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		tmp[i]=m_pBuf[i];
	for(UInt32 j=0;j<str.m_Len;j++) //不 包括字符串尾
		tmp[m_Len+j]=str.m_pBuf[j];
	tmp[m_Len+str.m_Len]=0;
	SafeDelArray(m_pBuf);
	m_pBuf=tmp;
	m_Len=m_Len+str.m_Len;
	return *this;
}

CStr CStr::operator +(const CStr &str)const
{
	CStr ret=CStr(m_Len+str.m_Len,0); //不为数组每个元素初始化
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		ret.m_pBuf[i]=m_pBuf[i];
	for(UInt32 j=0;j<str.m_Len;j++) //不 包括字符串尾
		ret.m_pBuf[m_Len+j]=str.m_pBuf[j];
	ret.m_pBuf[m_Len+str.m_Len]=0;
	return ret; //拷贝过去!不能传递引用!否则内存泄漏!看看MFC的STRCORE.CPP
}

//大多情况下,ansi字符串尾的多余的\0是不要紧的
Char *CStr::ToCharArrayA(CODEPAGE cp)const 
{
	Char *tmp=new Char[m_Len*2+1];
	for(UInt32 i=0;i<m_Len*2;i++) //不 包括字符串尾 //其实只需要填充后半部分!
		tmp[i]=0;
	tmp[m_Len*2]=0;
	::WideCharToMultiByte(CP(cp),0,m_pBuf,m_Len,tmp,m_Len*2,0,False);
	return tmp;
}

WChar *CStr::ToCharArrayW()const
{
	WChar *ret=new WChar[m_Len+1];
	for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
		ret[i]=m_pBuf[i];
	ret[m_Len]=0;
	return ret;
}

void CStr::BltFastW(WChar *dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len)
{
	if(!dest)return;
	if(src.m_Len<=0)return;
	
	if(possrc<0)possrc=0;
	if(possrc+len > src.m_Len)len=src.m_Len-possrc;
	if(posdest<0)posdest=0;

	for(UInt32 i=0;i<len;i++)
	{
		dest[posdest+i]=src.m_pBuf[possrc+i];
	}	
}


}// end of ns inst


#endif

⌨️ 快捷键说明

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