函数,删除所有代码注释
Function, which deletes all code comments
我需要做一个从文本(代码)中删除所有评论的功能。我的代码快完成了,但如果注释从文件的第一行开始,它就不起作用。它说索引超出范围,我尝试将 for
循环更改为从 1 开始然后 if
到 (text[i] == '/' && text[i - 1] == '/')
但它不起作用。
任何建议我如何解决这个问题或改进我的代码,因为它看起来很奇怪。
public void RemoveComments(string text)
{
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '/' && text[i + 1] == '/')
{
text = text.Remove(i, 2);
for (int j = i; j < text.Length; j++)
{
if (text[j] != '\n')
{
text = text.Remove(j, 1);
j--;
}
else if (text[j] == '\n')
{
text = text.Remove(j, 1);
j--;
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
}
i = j;
break;
}
}
}
else if (text[i] == '/' && text[i + 1] == '*')
{
text = text.Remove(i, 2);
for (int j = i; j < text.Length; j++)
{
if (text[j] != '*' && text[j + 1] != '/')
{
text = text.Remove(j, 1);
j--;
}
else if (text[j] == '*' && text[j + 1] == '/')
{
text = text.Remove(j, 2);
j = j - 2;
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
if (text[j] == '\n')
{
text = text.Remove(j, 1);
j--;
}
}
i = j;
break;
}
}
}
}
Console.WriteLine(text);
}
编辑:现在我做了很多实验,我发现问题出在(在//循环中)我需要这个循环来解决一些小的对齐问题:
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
}
Test.txt 文件。
//int a;
int c; //int d;
Console.Write/*Line*/("Hhehehe");
if(1>0)
/*ConsoleWriteLine("Yes")*/
//Nooo
你有一个基于 text.Length
的循环
for (int i = 0; i < text.Length; i++)
但是在循环内部,您正在缩短文本。在某一点上它比原点小 text.Length 而你 运行 超出索引我猜
您似乎有 C# 代码文件。因此,您可以使用 Roslyn 的强大功能。只需将代码文件解析为语法树,然后使用跳过注释的访问者访问该树:
var code = File.ReadAllText("Code.cs");
SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();
var codeWithoutComments = new CommentsRemover().Visit(root).ToString();
Console.WriteLine(codeWithoutComments);
访客:
class CommentsRemover : CSharpSyntaxRewriter
{
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
switch(trivia.Kind())
{
case SyntaxKind.SingleLineCommentTrivia:
case SyntaxKind.MultiLineCommentTrivia:
return default; // new SyntaxTrivia() // if C# <= 7.0
default:
return trivia;
}
}
}
示例代码文件:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
/* Sample
Multiline Comment */
class Program
{
static void Main(string[] args)
{
// Comment
Console.Write/*Line*/("Hello, World!"); // Print greeting
/*ConsoleWriteLine("Yes")*/
}
}
}
输出:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello, World!");
}
}
}
注意:如您所见,从除评论外什么都没有的行中删除评论后,您会得到空行。您可以再创建一位访问者以删除空行。同时考虑删除 XML 条评论。
我需要做一个从文本(代码)中删除所有评论的功能。我的代码快完成了,但如果注释从文件的第一行开始,它就不起作用。它说索引超出范围,我尝试将 for
循环更改为从 1 开始然后 if
到 (text[i] == '/' && text[i - 1] == '/')
但它不起作用。
任何建议我如何解决这个问题或改进我的代码,因为它看起来很奇怪。
public void RemoveComments(string text)
{
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '/' && text[i + 1] == '/')
{
text = text.Remove(i, 2);
for (int j = i; j < text.Length; j++)
{
if (text[j] != '\n')
{
text = text.Remove(j, 1);
j--;
}
else if (text[j] == '\n')
{
text = text.Remove(j, 1);
j--;
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
}
i = j;
break;
}
}
}
else if (text[i] == '/' && text[i + 1] == '*')
{
text = text.Remove(i, 2);
for (int j = i; j < text.Length; j++)
{
if (text[j] != '*' && text[j + 1] != '/')
{
text = text.Remove(j, 1);
j--;
}
else if (text[j] == '*' && text[j + 1] == '/')
{
text = text.Remove(j, 2);
j = j - 2;
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
if (text[j] == '\n')
{
text = text.Remove(j, 1);
j--;
}
}
i = j;
break;
}
}
}
}
Console.WriteLine(text);
}
编辑:现在我做了很多实验,我发现问题出在(在//循环中)我需要这个循环来解决一些小的对齐问题:
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
}
Test.txt 文件。
//int a;
int c; //int d;
Console.Write/*Line*/("Hhehehe");
if(1>0)
/*ConsoleWriteLine("Yes")*/
//Nooo
你有一个基于 text.Length
的循环for (int i = 0; i < text.Length; i++)
但是在循环内部,您正在缩短文本。在某一点上它比原点小 text.Length 而你 运行 超出索引我猜
您似乎有 C# 代码文件。因此,您可以使用 Roslyn 的强大功能。只需将代码文件解析为语法树,然后使用跳过注释的访问者访问该树:
var code = File.ReadAllText("Code.cs");
SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();
var codeWithoutComments = new CommentsRemover().Visit(root).ToString();
Console.WriteLine(codeWithoutComments);
访客:
class CommentsRemover : CSharpSyntaxRewriter
{
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
switch(trivia.Kind())
{
case SyntaxKind.SingleLineCommentTrivia:
case SyntaxKind.MultiLineCommentTrivia:
return default; // new SyntaxTrivia() // if C# <= 7.0
default:
return trivia;
}
}
}
示例代码文件:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
/* Sample
Multiline Comment */
class Program
{
static void Main(string[] args)
{
// Comment
Console.Write/*Line*/("Hello, World!"); // Print greeting
/*ConsoleWriteLine("Yes")*/
}
}
}
输出:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello, World!");
}
}
}
注意:如您所见,从除评论外什么都没有的行中删除评论后,您会得到空行。您可以再创建一位访问者以删除空行。同时考虑删除 XML 条评论。