|
|
发表于 2008-2-13 10:39:00
|
显示全部楼层
Re:请问两个LPCWSTR字符串怎么才能连接在一起?
//instStr.h
#ifndef INST_STR_H
#define INST_STR_H
#include <instTypes.h>
#include <instDefines.h>
namespace inst
{
typedef UInt32 CODEPAGE;
//win32获取某Locale对应的CP的方法:::GetLocaleInfo(locale, 11 ,...)
class CStr
{
private:
WChar *m_pBuf;
UInt32 m_Len; //当len==0时,m_pBuf[0]=='0',size==len+1
public:
static CODEPAGE GetSystemCodePage();
private:
static CODEPAGE CP(CODEPAGE cp){ return cp?cp:GetSystemCodePage(); }
private:
CStr(UInt32 len,Int32 unused); //不为数组每个元素初始化
public:
//ADT运算操作
CStr();
CStr(const CStr &str);
CStr(const Char *chars); //非常慢!建议不要用这个来初始化!
CStr(const Char *chars,CODEPAGE cp); //非常慢!建议不要用这个来初始化!
CStr(const WChar *wchars);
CStr(Int32 n);
CStr(UInt32 n);
CStr(Float f);
CStr(Double d);
CStr(Bool b);
~CStr();
Int32 GetLen()const{ return m_Len; }
CStr &operator =(const CStr &str);
// CStr &operator =(const Char *chars); //非常慢!建议不要用这个!
// CStr &operator =(const WChar *wchars);
Bool operator ==(const CStr &str)const;
// Bool operator ==(const Char *chars)const; //非常慢!建议不要用这个!
// Bool operator ==(const WChar *wchars)const;
Bool operator !=(const CStr &str)const;
// Bool operator !=(const Char *chars)const; //非常慢!建议不要用这个!
// Bool operator !=(const WChar *wchars)const;
CStr &operator +=(const CStr &str);
// CStr &operator +=(const Char *chars); //非常慢!建议不要用这个!
// CStr &operator +=(const WChar *wchars);
CStr operator +(const CStr &str)const;
// CStr operator +(const Char *chars)const; //非常慢!建议不要用这个!
// CStr operator +(const WChar *wchars)const;
//缓冲区操作
static CStr &CreateUninitializedStr(Int32 len){ return *(new CStr(len,0)); }
//必须要分开搞两个函数,否则会出现const错误
const WChar *ReadW()const{ return m_pBuf; } //这个很常用
WChar *ReadWriteW(){ return m_pBuf; }
//用户需要自己delete[],否则内存泄漏!
//大多情况下,ansi字符串尾的多余的\0是不要紧的
Char *ToCharArrayA()const{ return ToCharArrayA(0); } //很慢!建议不要用!
Char *ToCharArrayA(CODEPAGE cp)const; //很慢!建议不要用!
WChar *ToCharArrayW()const; //建议尽量不要用!为了更快,大多场合用ReadW()即可。
#ifdef TmpCharArrayA
#undef TmpCharArrayA
#endif
#define TmpCharArrayA(str) (AutoArrayPtr<Char>((str).ToCharArrayA()).GetPtr())
#ifdef TmpCharArrayAEx
#undef TmpCharArrayAEx
#endif
#define TmpCharArrayAEx(str,cp) (AutoArrayPtr<Char>((str).ToCharArrayA(cp)).GetPtr())
#ifdef TmpCharArrayW
#undef TmpCharArrayW
#endif
#define TmpCharArrayW(str) (AutoArrayPtr<WChar>((str).ToCharArrayW()).GetPtr())
//pos均从0开始!(改了~
//这里为了速度!!不可以检查边界!!
Char GetCharA(UInt32 pos);
WChar GetCharW(UInt32 pos){ return m_pBuf[pos]; }
void SetCharA(UInt32 pos,Char ch);
void SetCharW(UInt32 pos,WChar wch){ m_pBuf[pos]=wch; }
static void BltA(CStr &dest,UInt32 posdest,UInt32 lendest,const Char *src,UInt32 possrc,UInt32 lensrc);
static void BltA(Char *dest,UInt32 posdest,UInt32 lendest,const CStr &src,UInt32 possrc,UInt32 lensrc);
static void BltA(Char *dest,UInt32 posdest,UInt32 lendest,const Char *src,UInt32 possrc,UInt32 lensrc);
static void BltExA(CStr &dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const Char *src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);
static void BltExA(Char *dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const CStr &src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);
static void BltExA(Char *dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const Char *src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);
static void BltFastW(CStr &dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len);
static void BltFastW(CStr &dest,UInt32 posdest,const WChar *src,UInt32 possrc,UInt32 len);
static void BltFastW(WChar *dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len);
static void BltFastW(WChar *dest,UInt32 posdest,const WChar *src,UInt32 possrc,UInt32 len);
static Int32 GetCharArrayBinaryLenA(Char *chars);
static Int32 GetCharArrayBinaryLenW(WChar *wchars);
static Int32 GetCharArrayLenA(Char *chars);
static Int32 GetCharArrayLenW(WChar *wchars);
//高级操作
CStr &ToLowerCase();
CStr &ToUpperCase();
CStr &ToHiragana();
CStr &ToKatakana();
};
}// end of namespace inst
#endif
//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=str.m_pBuf;
m_pBuf[m_Len]=0;
}
CStr::CStr(const Char *chars) //非常慢!建议不要用这个来初始化!
{
UInt32 i=0;
while(chars!=0)
++i;
UInt32 len=i;
WChar *tmp=new WChar[len+1];
for(i=0;i<len;i++) //不 包括字符串尾 //其实只需要填充后半部分!
tmp=0;
tmp[len]=0;
::MultiByteToWideChar(GetSystemCodePage(),0,chars,len,tmp,len);
i=0;
while(tmp!=0) //其实如果倒过来搜索可以加速
++i;
m_Len=i;
//或者把下列代码改为m_pBuf=tmp;
m_pBuf=new WChar[m_Len+1];
for(i=0;i<m_Len;i++) //不 包括字符串尾
m_pBuf=tmp;
m_pBuf[m_Len]=0;
}
CStr::CStr(const Char *chars,CODEPAGE cp) //非常慢!建议不要用这个来初始化!
{
UInt32 i=0;
while(chars!=0)
++i;
UInt32 len=i;
WChar *tmp=new WChar[len+1];
for(i=0;i<len;i++) //不 包括字符串尾 //其实只需要填充后半部分!
tmp=0;
tmp[len]=0;
::MultiByteToWideChar(CP(cp),0,chars,len,tmp,len);
i=0;
while(tmp!=0) //其实如果倒过来搜索可以加速
++i;
m_Len=i;
//或者把下列代码改为m_pBuf=tmp;
m_pBuf=new WChar[m_Len+1];
for(i=0;i<m_Len;i++) //不 包括字符串尾
m_pBuf=tmp;
m_pBuf[m_Len]=0;
}
CStr::CStr(const WChar *wchars)
{
UInt32 i=0; //UInt32 pos=1;
while(wchars!=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=wchars;
m_pBuf[m_Len]=0;
}
CStr::~CStr()
{
SafeDelArray(m_pBuf);
}
//建议使用CStr来相互运算,否则慢
CStr &CStr: perator =(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=str.m_pBuf;
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!=str.m_pBuf)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!=str.m_pBuf)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=m_pBuf;
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=m_pBuf;
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=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=m_pBuf;
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 |
|