当CSharp代码我转为C++ CLI 发现无效了,代码如下:
正确应该是:
/// <summary>
/// 记录C,C++代码中的预处理命令的位置
/// </summary>
/// <param name="sTrueText">注释用空格代替的文本</param>
/// <returns></returns>
/// 创建时间: 2022-10-01 最后一次修改时间:2022-10-01
static csharp_DList<csharp_Pair<int, int>^>^ s_Syntax_C_Find_Preprocessor(String^ sTrueText)
{
csharp_DList<csharp_Pair<int, int>^>^ dResult = gcnew csharp_DList<csharp_Pair<int, int>^>();
int j, k;
for (int i = 0; i < sTrueText->Length; i++)
{
char c1 = sTrueText[i];
//lg.D(c1.ToString(),"_Syntax_C_Find_Preprocessor");
//#ifndef _MS_WINDOWS_ 正确
//# ifndef _MS_WINDOWS_ 正确
//#include<test.h">
//# include <test.h> 正确
// # + 预处理命令 + ?
if (c1 == L'#') //开始记录
{
j = i + 1;
while (j < sTrueText->Length)
{
char c2 = sTrueText[j];
if (c_IsEnglishLetters(c2))
{
StringBuilder^ sbWord = gcnew StringBuilder(50);
sbWord->Append(c2);
//log::d(c2.ToString(), "global_clr::Syntax_C_Find_Preprocessor");
k = j + 1;
while (k < sTrueText->Length)
{
c2 = sTrueText[k];
if (!c_IsEnglishLetters(c2)) //遇到非英文字符停止
{
//log::d(sbWord->ToString(), "global_clr::Syntax_C_Find_Preprocessor");
if (csharp_Syntax::C_Preprocessor->IndexOf(sbWord->ToString()) != -1) //判断是否预处理命令
{
dResult->Add(gcnew csharp_Pair<int, int>(i, k - i + 1));
}
j = k;
break;
}
else
{
sbWord->Append(c2);
}
++k;
}
i = j;
break;
}
++j;
}
}
}
return dResult;
}
查看一下Log窗口,发现:
是 StringBuilder 的问题,只把 char 改为 char_ 即是 wchar_t就行了。
现在好了,执行结果:
最终版本:
/// <summary>
/// 记录C,C++代码中的预处理命令的位置
/// </summary>
/// <param name="sTrueText">注释用空格代替的文本</param>
/// <returns></returns>
/// 创建时间: 2022-10-01 最后一次修改时间:2022-10-28
static auto s_Syntax_C_Find_Preprocessor(String^ sTrueText)
{
auto dResult = gcnew csharp_DList<csharp_Pair<int, int>^>();
int j, k;
for (int i = 0; i < sTrueText->Length; i++)
{
char_ c1 = sTrueText[i];
//lg.D(c1.ToString(),"_Syntax_C_Find_Preprocessor");
//#ifndef _MS_WINDOWS_ 正确
//# ifndef _MS_WINDOWS_ 正确
//#include<test.h">
//# include <test.h> 正确
// # + 预处理命令 + ?
if (c1 == L'#') //开始记录
{
j = i + 1;
while (j < sTrueText->Length)
{
char_ c2 = sTrueText[j];
if (c_IsEnglishLetters(c2))
{
StringBuilder^ sbWord = gcnew StringBuilder(50);
sbWord->Append(c2);
//log::d(c2.ToString(), "global_clr::Syntax_C_Find_Preprocessor");
k = j + 1;
while (k < sTrueText->Length)
{
c2 = sTrueText[k];
if (!c_IsEnglishLetters(c2)) //遇到非英文字符停止
{
//log::d(sbWord->ToString(), "global_clr::Syntax_C_Find_Preprocessor");
if (csharp_Syntax::C_Preprocessor->IndexOf(sbWord->ToString()) != -1) //判断是否预处理命令
{
dResult->Add(gcnew csharp_Pair<int, int>(i, k - i + 1));
}
j = k;
break;
}
else
{
sbWord->Append(c2);
}
++k;
}
i = j;
break;
}
++j;
}
}
}
return dResult;
}
/// <summary>
/// 记录C,C++代码中的预处理命令的位置
/// </summary>
/// <param name="sTrueText">注释用空格代替的文本</param>
/// <returns></returns>
/// 创建时间: 2022-10-01 最后一次修改时间:2022-10-28
static auto s_Syntax_C_Find_Preprocessor(String^ sTrueText)
{
auto dResult = gcnew csharp_DList<csharp_Pair<int, int>^>();
int j, k;
for (int i = 0; i < sTrueText->Length; i++)
{
char_ c1 = sTrueText[i];
//lg.D(c1.ToString(),"_Syntax_C_Find_Preprocessor");
//#ifndef _MS_WINDOWS_ 正确
//# ifndef _MS_WINDOWS_ 正确
//#include<test.h">
//# include <test.h> 正确
// # + 预处理命令 + ?
if (c1 == L'#') //开始记录
{
j = i + 1;
while (j < sTrueText->Length)
{
char_ c2 = sTrueText[j];
if (c_IsEnglishLetters(c2))
{
StringBuilder^ sbWord = gcnew StringBuilder(50);
sbWord->Append(c2);
//log::d(c2.ToString(), "global_clr::Syntax_C_Find_Preprocessor");
k = j + 1;
while (k < sTrueText->Length)
{
c2 = sTrueText[k];
if (!c_IsEnglishLetters(c2)) //遇到非英文字符停止
{
//log::d(sbWord->ToString(), "global_clr::Syntax_C_Find_Preprocessor");
if (csharp_Syntax::C_Preprocessor->IndexOf(sbWord->ToString()) != -1) //判断是否预处理命令
{
dResult->Add(gcnew csharp_Pair<int, int>(i, k - i + 1));
}
j = k;
break;
}
else
{
sbWord->Append(c2);
}
++k;
}
i = j;
break;
}
++j;
}
}
}
return dResult;
}
下一次准备探索代码转换: