Last active
December 16, 2015 03:19
-
-
Save jamescway/5369205 to your computer and use it in GitHub Desktop.
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
require 'ruby-debug' | |
class LineCounter | |
def initialize | |
@count = 0 | |
@commenting = false | |
end | |
def lines_of_code(file) | |
@count = 0 | |
File.open(file).each_with_index do |line| | |
if starts_with?(line,"//") || starts_and_ends_with_comment?(line) || line.strip.empty? | |
check_commenting(line) # check anyway in case theres a /* after the // | |
else | |
check_commenting(line) | |
if @commenting == false && !ends_with?(line, "*/") | |
increment | |
end | |
end | |
puts sprintf("count: %4s -- %6s | %s ", @count, @commenting, line[0..-2]) | |
end | |
@count | |
end | |
def increment | |
@count += 1 | |
end | |
def check_commenting(line) | |
@commenting = start_comment?(line) | |
@commenting = end_comment?(line) | |
end | |
def starts_and_ends_with_comment?(line) | |
line.strip =~ /^(.*)\/\*(.*)/ && | |
line.strip =~ /(.*)\*\/(.*)$/ && | |
@commenting == false | |
end | |
def start_comment?(line) | |
if (line.strip =~ /(.*)\/\*(.*)/) && (@commenting == false) | |
increment unless starts_with?(line, "/*") || starts_with?(line, "//") | |
true | |
else | |
@commenting | |
end | |
end | |
def end_comment?(line) | |
if (line.strip =~ /(.*)\*\/(.*)/) && (@commenting == true) | |
increment unless ends_with?(line, "*/") || starts_with?(line, "//") || start_comment?(line) | |
false | |
else | |
@commenting | |
end | |
end | |
def starts_with?(line, str) | |
line.strip[0..1] == str[0..1] rescue false | |
end | |
def ends_with?(line, str) | |
line.strip[-2..-1] == str[0..1] rescue false | |
end | |
end | |
line_counter = LineCounter.new | |
puts "C O U N T-1: #{line_counter.lines_of_code("test1").to_s}" | |
puts "\n" | |
puts "C O U N T-2: #{line_counter.lines_of_code("test2").to_s}" | |
# require 'pry' | |
# LOC | |
# - inside of strings are ignored " to " or ' to ' | |
# - // in the front after strip | |
# - /* to */ | |
# use cases | |
# 1. line.strip starts with // | |
# 2. /* */ | |
# a. covers entire line | |
# -may start on a line (not counted) and finish on a not counted line | |
# in between is counted | |
# b. if it not terminated, the lines end up getting counted | |
# /* asdf | |
# adsfsdfa | |
# asfdasdfasfd | |
# // some comment | |
# ==> 2 lines of code | |
count: 0 -- false | // This file contains 3 lines of code | |
count: 1 -- false | public interface Dave { | |
count: 1 -- true | /** | |
count: 1 -- true | * count the number of lines in a file | |
count: 1 -- false | */ | |
count: 2 -- false | int countLines(File inFile); // not the real signature! | |
count: 3 -- false | } | |
count: 3 -- false | | |
C O U N T-1: 3 | |
count: 0 -- true | /***** | |
count: 0 -- true | * This is a test program with 5 lines of code | |
count: 0 -- true | * \/* no nesting allowed! | |
count: 0 -- false | //*****//***/// Slightly pathological comment ending... | |
count: 0 -- false | | |
count: 1 -- false | public class Hello { | |
count: 2 -- false | public static final void main(String [] args) { // gotta love Java | |
count: 2 -- false | // Say hello | |
count: 3 -- false | System./*wait*/out./*for*/println/*it*/("Hello/*"); | |
count: 4 -- false | } | |
count: 4 -- false | | |
count: 5 -- false | } | |
C O U N T-2: 5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment