这是我计算java文件中不同行数的代码的一部分。在大多数情况下,这个代码可以得到正确的结果号码。但是如果注释行类似于:
/*。。。。。。。在
*。。。。。。。在
*/
它将把这一块看作只有两条线
for eachLine in allLines:
if eachLine != " " :
eachLine = eachLine.replace(" ",""); #remove space
eachLine = self.trim(eachLine); #remove tabIndent
if (iscomment==False):
if(eachLine.strip().startswith("//")): #LINECOMMENT
commentCount += 1;
if eachLine == "":
blankCount += 1;
if(eachLine.strip().startswith("/*")):
commentCount += 1;
if(not eachLine.strip().endswith("*/")):
iscomment=True
else :
commentCount += 1;
if(eachLine.find("*/")):
iscomment=False
lineCount = lineCount + 1;
codeCount=lineCount-commentCount-blankCount
您应该考虑regexp:
我在一个非常简单的项目上试过,仅仅是这几行就行了,似乎能找到合适的线数。在
失败时返回-1,而不是false。在
http://docs.python.org/2/library/string.html
相关问题 更多 >
编程相关推荐