Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:20
Show Gist options
  • Save up1/21d3119039eb6d2606a2 to your computer and use it in GitHub Desktop.
Save up1/21d3119039eb6d2606a2 to your computer and use it in GitHub Desktop.
Counting Line
- // 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 }
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;
}
}
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