Last active
August 29, 2015 14:20
-
-
Save up1/21d3119039eb6d2606a2 to your computer and use it in GitHub Desktop.
Counting Line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- // This file contains 3 lines of code | |
1 public interface Dave { | |
- /** | |
- * count the number of lines in a file | |
- */ | |
2 int countLines(File inFile); // not the real signature! | |
3 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LineCounter { | |
public int count(String code) { | |
code = code.replaceAll("\r\n", "\n"); | |
code = code.replaceAll("(?s)/\\*.*?\n.*?\\*/", "\n"); | |
code = code.replaceAll("(?s)/\\*.*?\\*/", ""); | |
code = code.replaceAll("(?s)\n+", "\n"); | |
code = code.trim(); | |
return code.equals("") ? 0 : code.split("\n").length; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isCodeLine(String line) { | |
line = line.trim(); | |
if (line.startsWith("//")) | |
return false; | |
else if (line.startsWith("/*")) | |
return false; | |
else if (line.endsWith("*/")) | |
return false; | |
else if (line.equals("")) | |
return false; | |
else | |
return true; | |
} | |
public boolean isStartOfBlockComment(String line) { | |
if (line.indexOf("/*") != -1) | |
return true; | |
else | |
return false; | |
} | |
public boolean isEndOfBlockComment(String line) { | |
if (line.indexOf("*/") != -1) | |
return true; | |
else | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment