使你的字符串类和 C# String一样兼容 ANSI 和 Unicode

文章介绍了如何在C++代码中处理Unicode字符串的繁琐过程,提出使用自定义类_StrW和_StrA以及转换函数来模拟CLR的System::String^,允许更简洁地创建和操作Unicode字符串,避免每次手动添加_L前缀。
摘要由CSDN通过智能技术生成

 有时候,假设你写了一个 string,默认是Unicode,

 每次测试时都要: wstring s = L"字符串" ;

 烦死人了。 例如:

  

_StringList _Syntax::Java_Keyword =
{ 
       _t("abstract"), _t("assert"), _t("boolean"), _t("break"), _t("byte"), _t("case"), _t("catch"), _t("char"), _t("class"),
       _t("const"),_t("continue"), _t("default"), _t("do"), _t("double"), _t("else"), _t("enum"), _t("extends"), _t("final"), _t("finally"),
       _t("float"), _t("for"), _t("goto"), _t("if"), _t("implements"), _t("import"), _t("instanceof"), _t("int"), _t("interface"),
       _t("long"), _t("native"), _t("new"), _t("package"), _t("private"), _t("protected"), _t("public"), _t("return"), _t("strictfp"),
       _t("short"), _t("static"), _t("super"), _t("switch"), _t("synchronized"), _t("this"), _t("throw"), _t("throws"), _t("transient"),
       _t("try"), _t("void"), _t("volatile"), _t("while") ,_t("operator") 
};
  
 


_StringList _Syntax::Java_Basicclass =
{ 
    _t("Integer"),  _t("Double"),  _t("_Number"),  _t("MathCharacter"),  _t("BooleanString"),  _t("BufferString"),
    _t("BuilderArrays"),  _t("Scanner"),  _t("Math"), _t("String"), _t("Arrays") 
    
 };



_StringList _Syntax::Python_Keyword =
{
    //blue
    _t("True"),
    _t("False"),
    _t("await"),
    _t("else"),
    _t("import"),
    _t("pass"),
    _t("None"),
    _t("break"),
    _t("except"),
    _t("in"),
    _t("raise"),
    _t("and"),
    _t("or"),
    _t("finally"),
    _t("is"),
    _t("return"),
    _t("as"),
    _t("for"),
    _t("lambda"),
    _t("try"),

    //red
    _t("assert"),
    _t("def"),
    _t("from"),
    _t("nonlocal"),
    _t("while"),
    _t("async"),
    _t("del"),
    _t("global"),
    _t("not"),
    _t("with"),

    //yellow
    _t("class"),
    _t("if"),
    _t("yield"),

    //green
    _t("continue"),
    _t("exec"),
    _t("print"),
    _t("imp"),
    _t("pkgutil"),
    _t("ctypes") /* 更多关键字可以自行添加 */
};

你可以用一个自动转换函数,模似CLR的 System::String^;

例子:

using namespace System;
using namespace lf;

int main(array<System::String ^> ^args)
{


    System::String^  clrs = gcnew  String("China");  //Ansi字行串

    clrs += L"(中国)"; //Unicode 字行串

    Console::Write("CLR字符串:");
    Console::WriteLine(clrs);


    _string s("China");    //Ansi字行串,_string 是 _StrW
     s += L"(中国)";  //Unicode 字行串
     _cout << _t("自定义类:") << s <<_t("\n");


    return 0;
}

输出:

 在_StrW类中定义:

#ifdef _STR_COMPATIBILITY_
     

    /// <summary>
    /// 允许 _StrW  str("abc");  而不是每次都要写 _StrW str(_t("abc"))
    /// </summary>
    /// <param name="pStr">数据指针</param>
    /// <param name="nBuffer">缓冲大小</param>
    /// <param name="bZeroBuffer">是否把缓冲初始化为零</param>
    /// 创建时间: 2023-05-08    最后一次修改时间:2023-05-08 
    _StrW(const char* pStr, const int& nBuffer = 0, bool bZeroBuffer = false);

    operator _StrA() const;
 

#endif
 

#ifdef _STR_COMPATIBILITY_  //兼容 _StrA 与 _StrW 互相兼容
   
/// <summary>
/// 允许 _StrW  str("abc");  而不是每次都要写 _StrW str(_t("abc"))
/// </summary>
/// <param name="pStr">数据指针</param>
/// <param name="nBuffer">缓冲大小</param>
/// <param name="bZeroBuffer">是否把缓冲初始化为零</param>
/// 创建时间: 2023-05-08    最后一次修改时间:2023-05-08 
_StrW::_StrW(const char* pStr, const int& nBuffer, bool bZeroBuffer) :
    _Str<wchar_t>(gs.StringToWString(pStr).Pointer, nBuffer, bZeroBuffer)
{

}

_StrW::operator _StrA() const
{
    return gs.WStringToString(_pData,_nLength).Pointer;     
}
 
#endif

在_StrA类中定义:

#ifdef _STR_COMPATIBILITY_
 
    /// <summary>
    /// 允许 _StrW  str("abc");  而不是每次都要写 _StrW str(_t("abc"))
    /// </summary>
    /// <param name="pStr">数据指针</param>
    /// <param name="nBuffer">缓冲大小</param>
    /// <param name="bZeroBuffer">是否把缓冲初始化为零</param>
    /// 创建时间: 2023-05-08    最后一次修改时间:2023-05-08 
    _StrA(const wchar_t* pStr, const int& nBuffer = 0, bool bZeroBuffer = false);

    operator _StrW() const;
 

#endif
 

#ifdef _STR_COMPATIBILITY_

 _StrA::_StrA(const wchar_t* pStr, const int& nBuffer, bool bZeroBuffer) 
	 : _Str<char>(gs.WStringToString(pStr).Pointer, nBuffer, bZeroBuffer)
 {

 }

 _StrA::operator _StrW() const
 {
	 return  gs.StringToWString(_pData, _nLength).Pointer;
 }


#endif

转换函数:


/// <summary>
/// 把 char* 转为 wchar_t*
/// 返回的Mem.Pointer是指向字符串的指针
/// 返回的Mem.DataLength是字符串的长度
/// </summary>
/// <param name="pstr"></param>
/// <param name="nStrLength">转换字符串的长度,如果没有给出,则自动计算字符串长度。</param>
/// <returns></returns>
/// 创建时间: 2023-05-08    最后一次修改时间:2023-05-09 
_Mem<wchar_t> global_c_str::StringToWString(const char* pstr, const int nStrLength)
{
	int nConverLen = nStrLength > 0 ? nStrLength : _Math::StrLen_t<char>(pstr);

	_Mem<wchar_t> m(nConverLen + 1);  //最起码长度加1
		 
	::mbstowcs_s(& m.DataLength, m.Pointer, m.AllLength, pstr, m.AllLength);
    
	//m.ZeroEnd();	 //wcstombs_s会把最后一位字节设为0
	return m;
}



/// <summary>
/// 把 wchar_t* 转为 char*
/// 返回的Mem.Pointer是指向字符串的指针
/// 返回的Mem.DataLength是字符串的长度
/// </summary>
/// <param name="pstr"></param>
/// <param name="nStrLength">转换字符串的长度,如果没有给出,则自动计算字符串长度。</param>
/// <returns></returns>
/// 创建时间: 2023-05-08    最后一次修改时间:2023-05-09 
_Mem<char> global_c_str::WStringToString(const wchar_t* pstr, const int nStrLength)
{
	/*
	pReturnValue: The number of characters converted. ----- 转换后的多字节字符串的字符数;
	mbstr: The address of a buffer for the resulting converted multibyte character string. ----- char类型的字符或字符串的地址 (接收转换后的字符串的缓冲区);
	sizeInBytes: The size in bytes of the mbstr buffer. ----- 用来接收转换后字符char类型缓冲区的大小(以字节记);
	wcstr: Points to the wide character string to be converted. ----- wchar_t类型的字符串的地址(需要转换的字符串的缓冲区的地址);
	count: The maximum number of bytes to be stored in the mbstr buffer ----- 最多可以存入mbstr的字节数。

	 // Conversion
	wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, pWCBuffer, (size_t)BUFFER_SIZE );
	*/

	int nConverLen = nStrLength > 0 ? nStrLength : _Math::StrLen_t<wchar_t>(pstr);

	_Mem<char> m(nConverLen * 2 + 1);
	 
	::wcstombs_s(&m.DataLength, m.Pointer, m.AllLength, pstr, m.AllLength);
		 
	//m.ZeroEnd();  //wcstombs_s会把最后一位字节设为0

	return m;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值