Skip to content

Instantly share code, notes, and snippets.

@tiennou
Created March 17, 2017 01:11
Show Gist options
  • Save tiennou/f1844f89077ee3d68459198b8d7045f8 to your computer and use it in GitHub Desktop.
Save tiennou/f1844f89077ee3d68459198b8d7045f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -W
require 'pp'
require 'ostruct'
$global_stats = {
file_count: 0,
line_count: 0,
space_indent: 0,
tab_indent: 0,
brace_prototype_inline: 0,
brace_prototype_wrap: 0,
brace_control_inline: 0,
brace_control_wrap: 0,
method_prototype_space_after_return_type: 0,
method_prototype_no_space_after_return_type: 0,
}
$in_comment_block = false
def check_indent(line)
case line
when /^ /
return if $in_comment_block
$global_stats[:space_indent] += 1
when /^\t/
return if $in_comment_block
$global_stats[:tab_indent] += 1
when /^\s*?\/\*\*/
$in_comment_block = true
when /^\s*?\*\*\// then
$in_comment_block = false
end
end
def check_prototype(line)
return if $in_comment_block
if line =~ /^\s*[:alnum:]*\s+[:alnum:]*\(.*\)\s*({)?/
# puts "function #{line}: #{$~[1].nil?}"
$global_stats[:brace_prototype_inline] += 1 if $~[1]
$global_stats[:brace_prototype_wrap] += 1 unless $~[1]
elsif line =~ /^\s*[+-]\s*\([a-zA-Z\s*]*\)(\s*)[a-zA-Z]*(?:[a-zA-Z]*(?::\(.*\)[a-zA-Z]*))?\s*({)?/
$global_stats[:brace_prototype_inline] += 1 if $~[2]
$global_stats[:brace_prototype_wrap] += 1 unless $~[2]
$global_stats[:method_prototype_space_after_return_type] += 1 unless $~[1].empty?
$global_stats[:method_prototype_no_space_after_return_type] += 1 if $~[1].empty?
end
end
def check_control_statement(line)
return if $in_comment_block
if $check_brace_control_wrap
$global_stats[:brace_control_wrap] += 1 if line =~ /^\s*{/
$check_brace_control_wrap = false
end
if line =~ /^\s*(?:for|if|while|switch)\s*\(.*\)\s*({)?/
puts "control: #{line} \"#{$~[1]}\""
$global_stats[:brace_control_inline] += 1 if $~[1]
$check_brace_control_wrap = true
end
end
# main
glob = ARGV[0] || "**/*.[chm]"
Dir.glob(glob) do |path|
puts " • Checking \"#{path}\""
$global_stats[:file_count] += 1
$in_comment_block = false
File.open(path, 'r').each do |line|
$global_stats[:line_count] += 1
check_indent(line)
check_prototype(line)
check_control_statement(line)
end
end
pp $global_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment