1.后台有需求。需要提供源代码,交给第三方进行安全审查。为了减少代码泄露的影响,需要在其源代码中对外提供代码注释。代码中,去掉所有的代码注释,让别人更难阅读代码2.艰难的爬升过程1.java中组织注释多行注释:/**多行注释*/单行注释://单行注释2.初步分析上一步的情况简单总结,以/*开始,以*/结束以//开始,以换行或文件结束结束3.核心代码获取代码多行注释内容re.findall("\/\*.*?\*\/",content,re.S)逐行遍历文件,检查是否包含"//",替换"//"和下面的内容如果包含Fornewlinesre.findall("\/\/.*",line,re.S)4.入大坑我以为这是一个简单粗暴的方法,但是运行之后,我发现有一个很大的问题就是字符串中包含多行和单行注释会导致字符串中的内容被修改而单行被误删。"(?m)^//.*(?=\\n)"单行注释去除异常//后跟单个For",检查引号的优先级高,导致引号被reserved,导致整体去除异常字符串识别有问题。'"'"select*fromuserwherename='root'""\""中间的引号不能作为字符串的开头和结尾,最后一个双引号需要算作字符串的结尾,不能算作双引号,即不能作为字符串的开头和结尾。折腾了几天,发现按照穷举法找出所有的异常情况太难了,因为我们的精力有限,一时半会想不出来所有的情况。请问有什么办法吗?这时,内存深处的一些内容开始冒泡,程序员三大浪漫之一的编译原理开始了n出现。之前懵懵懂懂看的课文开始有点印象了。重新翻开经典的内容,看看他是如何处理词汇和语法的。1.回顾一下编译原理词法分析,程序中的词大致可以分为五类:语法分析,比如对于赋值语句position=initial+2*60,经过树的语义分析语法分析后生成,如position=initial+2*60语义分析后2.开始处理按照编译原理中提到的流程,首先要将词拆分成词,然后将词串成语句,然后逐个语句处理。整体思路:判断是否已经在不需要关注的范围内,比如是否在双引号中间,多行注释中,单行注释后,如果有已经开始了,只需要注意是双引号还是多行注释即可,如果单行注释的结尾是结尾,则单独处理,删除多行注释,删除单行注释-行注释,删除中间的双引号,保留开头的双引号、多行注释、单行注释,将之前的保存到新文件中Middle3.Code#coding=utf-8foler_path="./java/test/"defrewriteContent(dirpath,filename,content):writefile=open(dirpath+"/"+filename,"w+")#打印内容writefile.write(content)writefile.close()defclean_all_note():fordirpath,dirnanes,filenamsinos.walk(foler_path):forfilenameinfilenams:printdirpath+"/"+filenameclean_note(dirpath,filename)#判断是否是双引号,需要排除'"'和\",defis_available_quotes(ch,pre_ch,next_ch):returnch=="\""andpre_ch!="\\"andnot(pre_ch=="'"andnext_ch=="'")#判断是否是多行注释的开头,即/*defis_prefix_multiline_comment(ch,pre_ch):returnch=="*"andpre_ch=="/"#判断是否是多行注释的结尾,即*/defis_suffix_multiline_comment(ch,pre_ch):returnch=="/"andpre_ch=="*"#判断是否是单行注释//defis_single_line_comment(ch,pre_ch):returnch=="/"andpre_ch=="/"#判断是不是换行defis_line_feed(ch,pre_ch):returnch=="\n"defclean_note(dirpath,filename):file=open(dirpath+"/"+filename,"r+")content=file.read()multiline_ing=Falsesingle_line_ing=Falsequotes_ing=Falsepre_ch=""index=0lastPoi=0newContent=""forchincontent:如果multiline_ing:ifis_suffix_multiline_comment(ch,pre_ch):#print"mle:"+pre_ch+chlastPoi=index+1multiline_ing=Falseelifsingle_line_ing:ifis_line_feed(ch,pre_ch)orindex==len(content)-1:#print"sle:"+content[lastPoi:index-1]lastPoi=indexsingle_line_ing=Falseelifquotes_ing:#解决"\\"ifch=="\\"andpre_ch=="\\":ch=''ifis_available_quotes(ch,pre_ch,content[index+1]):#print"yinhaoe:"+pre_ch+chnewContent=newContent+content[lastPoi:index]lastPoi=indexquotes_ing=Falseelse:ifindex==len(content)-1:#print"es:"+pre_ch+chnewContent=newContent+content[lastPoi:index+1]elifis_available_quotes(ch,pre_ch,content[index+1]):#print"yinhaos:"+pre_ch+ch#newContent=newContent+content[lastPoi:index]+"----"quotes_ing=Trueelifis_prefix_multiline_comment(ch,pre_ch):#print"mls:"+pre_ch+chnewContent=newContent+content[lastPoi:index-1]multiline_ing=Trueelifis_single_line_comment(ch,pre_ch):#打印"sls:"+pre_ch+chnewContent=newContent+content[lastPoi:index-1]single_line_ing=Trueindex=index+1pre_ch=chrewriteContent(dirpath,filename,newContent)if__name__=='__main__':fordirpath,dirnanes,filenamsinos.walk(foler_path):forfilenameinfilenams:clean_note(dirpath,文件名)
