📄 filter.cs
字号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Scanner
{
public class Filter
{
public void Seperator(string path, ref string number, ref string identifier, ref string keyword, ref string comment,ref string importan)
{
StringBuilder bimportan = new StringBuilder();
bimportan.Append("程序存在的全局标识:");
bimportan.Append("\r\n");
StringBuilder bnumber = new StringBuilder();
bnumber.Append("源文件中存在的操作数字:");
bnumber.Append("\r\n");
StringBuilder bidentifier = new StringBuilder();
bidentifier.Append("源文件中存在的标识符:");
bidentifier.Append("\r\n");
StringBuilder bkeyword = new StringBuilder();
bkeyword.Append("源文件中存在的关键字:");
bkeyword.Append("\r\n");
StringBuilder bcomment = new StringBuilder();
bcomment.Append("源文件中存在的注释:");
bcomment.Append("\r\n");
using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
{
try
{
while (sr.Peek() != -1)
{
string realcontent;
string temp = sr.ReadLine().Trim();
if (temp.IndexOf("#") != -1)
{
bimportan.Append(temp);
bimportan.Append("\r\n");
continue;
}
if (temp.IndexOf("//") != -1)
{
if (temp.IndexOf("//{{") != -1 || temp.IndexOf("//}}") != -1)
{
bimportan.Append(temp);
bimportan.Append("\r\n");
realcontent = "";
}
else if (temp.IndexOf("////") != -1)
{
realcontent = "";
}
else
{
realcontent = temp.Substring(0, temp.IndexOf("//")).Trim();
bcomment.Append(temp.Remove(0, temp.IndexOf("//") + 2));
bcomment.Append("\r\n");
}
}
else
{
realcontent = temp;
}
if (realcontent != "")
{
string[] array = Split(realcontent);
foreach (string s in array)
{
if (CheckNumber(s.Trim()))
{
if (bnumber.ToString().IndexOf(s) == -1)
{
bnumber.Append(s);
bnumber.Append(',');
}
}
if (CheckKeyword(s.Trim()))
{
if (bkeyword.ToString().IndexOf(s) == -1)
{
bkeyword.Append(s);
bkeyword.Append(',');
}
}
if (CheckIndentifer(s.Trim()))
{
if (bidentifier.ToString().IndexOf(s) == -1)
{
bidentifier.Append(s);
bidentifier.Append(',');
}
}
}
}
}
bnumber.Append("\r\n");
bidentifier.Append("\r\n");
bkeyword.Append("\r\n");
number = bnumber.ToString();
identifier = bidentifier.ToString();
keyword = bkeyword.ToString();
comment = bcomment.ToString();
importan = bimportan.ToString();
}
catch (IOException ex)
{
throw ex;
}
}
}
private string[] Split(string input)
{
input.Replace(';', ' ');
input.Replace('(', ' ');
input.Replace(')', ' ');
input.Replace("->", " ");
input.Replace('*', ' ');
input.Replace(',', ' ');
input.Replace('{', ' ');
input.Replace('}', ' ');
input.Replace('<', ' ');
input.Replace('>', ' ');
input.Replace('=', ' ');
input.Replace('-', ' ');
input.Replace('+', ' ');
input.Replace("\\"," ");
input.Replace('%', ' ');
input.Replace("&&", " ");
input.Replace("||", " ");
input.Replace('&', ' ');
input.Replace('|', ' ');
input.Replace('~', ' ');
input.Replace('^', ' ');
char[] space = new char[] { ' ', ' ' };
string[] content = input.Split(space,StringSplitOptions.RemoveEmptyEntries);
return content;
}
private bool CheckNumber(string input)
{
string numregex = @"^(-?[0-9]*[.]*[0-9]{0,4})$";
return IsRegEx(numregex, input);
}
private bool CheckKeyword(string input)
{
bool flag = false;
switch (input)
{
case "virtual":
case "auto":
case "asm":
case "break":
case "bool":
case "case":
case "char":
case "class":
case "const":
case "continue":
case "default":
case "delete":
case "do":
case "double":
case "else":
case "enum":
case "extern":
case "explicit":
case "float":
case "friend":
case "for":
case "goto":
case "if":
case "int":
case "inline":
case "mutable":
case "operator":
case "long":
case "new":
case "namespace":
case "private":
case "protected":
case "public":
case "register":
case "return":
case "short":
case "signed":
case "sizeof":
case "static":
case "struct":
case "switch":
case "template":
case "this":
case "throw":
case "typedef":
case "typeid":
case "typename":
case "union":
case "unsigned":
case "void":
case "volatile":
case "while":
case "wchar_t":
case "include":
case "try":
case "catch":
case "finally":
flag = true;
break;
default:
flag = false;
break;
}
return flag;
}
private bool CheckIndentifer(string input)
{
bool flag = false;
char[] array = input.ToCharArray();
if ((array[0] >= 'a' && array[0] <= 'z') || (array[0] >= 'A' && array[0] <= 'Z') || array[0] == '_')
{
flag = true;
}
if (flag&&array.Length>1)
{
for (int i = 1; i < array.Length; i++)
{
if ((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z') || array[i] == '_' || (array[i] >= 0 && array[i] <= 9))
{
flag = true;
}
else
{
flag = false;
break;
}
}
}
if (flag)
flag = !CheckKeyword(input);
return flag;
}
private bool IsRegEx(string regExValue, string itemValue)
{
bool flag = false;
try
{
Regex regex = new Regex(regExValue);
if (regex.IsMatch(itemValue))
flag = true;
}
catch (Exception e)
{
throw e;
}
return flag;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -