依据第二版本,可以写一个跳过注释的查找函数
C_IndexOfWord
Java_IndexOfWord
CSharp_IndexOfWord
还有一种方法,可以先把所有注释用空格代替,查出的字符位置也不变。
以前版本:
DList<TextColor> Syntax::GetTextColorPosForC_Keywords1() const
{
DList<TextColor> dtResult;
int nPos = -1;
if (_LanguageType == LanguageType::C)//-----------------------------------C&C++ Begin
{
//-----------------------------------------------------------------关键字
for (_string s : C_Keyword)
{
nPos = _code.IndexOfWord(s);
while (nPos != -1)
{
dtResult.Add(TextColor(nPos, s.length(), GetColor(SyntaxType::关键字)));
nPos = _nh_en.IndexOfWord(s, nPos + s.length());
}
}
}
return dtResult;
}
修改版本:
/// <summary>
/// 记录C,C++关键字的位置,跳过注释和字符串文本。
/// </summary>
/// <returns></returns>
/// 创建时间: 2022-12-19 最后一次修改时间:2022-12-19
DList<TextColor> Syntax::GetTextColorPosForC_Keywords2() const
{
DList<TextColor> dtResult;
if (_code.length() <= 0) return dtResult;
const _char *p = _code.c_str();
_string sWord(_t(""), 50); //50个_char缓冲
Color_ cKeyWordColor = GetColor(SyntaxType::关键字);
for(int i = 0; i < _code.length(); ++ i)
{
if ( p[i] == _t('/'))
{
if (i + 1 < _code.length())
{
if (p[i + 1] == _t('/') ) //是"//"注释,查找注释结束位置 \n ,跳过注释
{
i += 2;
while (p[i] != 0 && p[i] != '\n')
{
++i;
}
}
else if (p[i + 1] == _t('*')) //是"/*"注释,查找注释结束位置 */ ,跳过注释
{
i += 2;
while (p[i] != 0 && p[i] != _t('*'))
{
if (p[i + 1] != 0 && p[i + 1] != '/') //结束位置
{
++i;
}
else
{
++i;
break;
}
}
}
}
else
{
return dtResult;
}
}
else if (p[i] == _t('\"')) //字符开始
{
++i;
while(p[i] != 0 && p[i] != '\"')
{
++i;
}
}
if (gs.s_Syntax_IsWordSeparator(p[i]))
{
//关键字最小长度大于2,且全是小写字母
if (sWord.length() >= 2 && sWord.IsAllLowerCaseEnglishLetter())
{
if (C_Keyword.findNoteItem(sWord) != null) //是关键字,记录位置
{
dtResult.Add(TextColor(i - sWord.length(), sWord.length(), cKeyWordColor));
}
//log::d(_getc(sWord));
}
sWord.Clear(); //清空
}
else
{
sWord.add(p[i]);
}
}
return dtResult;
}
/// <summary>
/// 记录C,C++关键字的位置,跳过注释和字符串文本。
/// </summary>
/// <returns></returns>
/// 创建时间: 2022-12-19 最后一次修改时间:2022-12-19
DList<TextColor> Syntax::GetTextColorPosForC_Keywords2() const
{
DList<TextColor> dtResult;
if (_code.length() <= 0) return dtResult;
const _char *p = _code.c_str();
_string sWord(_t(""), 50); //50个_char缓冲
Color_ cKeyWordColor = GetColor(SyntaxType::关键字);
for(int i = 0; i < _code.length(); ++ i)
{
if ( p[i] == _t('/'))
{
if (i + 1 < _code.length())
{
if (p[i + 1] == _t('/') ) //是"//"注释,查找注释结束位置 \n ,跳过注释
{
i += 2;
while (p[i] != 0 && p[i] != '\n')
{
++i;
}
}
else if (p[i + 1] == _t('*')) //是"/*"注释,查找注释结束位置 */ ,跳过注释
{
i += 2;
while (p[i] != 0 && p[i] != _t('*'))
{
if (p[i + 1] != 0 && p[i + 1] != '/') //结束位置
{
++i;
}
else
{
++i;
break;
}
}
}
}
else
{
return dtResult;
}
}
else if (p[i] == _t('\"')) //字符开始
{
++i;
while(p[i] != 0 && p[i] != '\"')
{
++i;
}
}
if (gs.s_Syntax_IsWordSeparator(p[i]))
{
//关键字最小长度大于2,且全是小写字母
if (sWord.length() >= 2 && sWord.IsAllLowerCaseEnglishLetter())
{
if (C_Keyword.findNoteItem(sWord) != null) //是关键字,记录位置
{
dtResult.Add(TextColor(i - sWord.length(), sWord.length(), cKeyWordColor));
}
//log::d(_getc(sWord));
}
sWord.Clear(); //清空
}
else
{
sWord.add(p[i]);
}
}
return dtResult;
}
其中:
/// <summary>
/// 判断字符是否单词的有效分隔符
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
inline static bool s_Syntax_IsWordSeparator(_char c) {
return (gs.c_IsPunctuation(c) || gs.c_IsControl(c) || Math_::strchr(IdentifierSpecial, c) != -1) && c != _t('_');
}
const _char* global_c_str::IdentifierSpecial = _t("~!@#$%^&*()+-/.{}[]|\\ `=");