Skip to content

Commit

Permalink
Fix exceptions in strings
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyang-wen committed Apr 24, 2014
1 parent 3e43181 commit 54b1a88
Showing 1 changed file with 70 additions and 6 deletions.
76 changes: 70 additions & 6 deletions Ruby/remove-comments-in-java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
multi_line = false
line_no = 0

lines = 0

MUL_REPLACE_LEFT = "__MUL_REPLACE_LEFT__"
MUL_REPLACE_RIGHT = "__MUL_REPLACE_RIGHT__"
SIG_REPLACE = "__SIG_REPLACE__"

File::open(ARGV[0])do |file|

# handle each line
Expand All @@ -25,15 +31,48 @@
else
# line.lstrip!
line.rstrip!
line.gsub!(/\/\/.*/,'') # remove one line comment
line.gsub!(/\/\*.*\*\//, '') # remove multi-line comment on one line

p "Cur LINE #{line_no}: #{line}"

line.gsub!(/\".*?\"/) do |match|

# replace the coment symbols in double or single quotes
match.gsub!(/\/\*/) do |match1|
match1.gsub!("/*", MUL_REPLACE_LEFT)
end

match.gsub!(/\*\//) do |match1|
match1.gsub!("*/", MUL_REPLACE_RIGHT)
end

match.gsub!(/\/\//) do |match1|
match1.gsub!("//", SIG_REPLACE)
end

match

end

=begin
# replace the coment symbols in double or single quotes
line.gsub!(/\".*(\/\*).*\"/) do |match|
match.gsub!("/*", MUL_REPLACE_LEFT)
end
line.gsub!(/\".*(\*\/).*\"/) do |match|
match.gsub!("*/", MUL_REPLACE_RIGHT)
end
line.gsub!(/\".*(\/\/).*\"/) do |match|
match.gsub!("//", SIG_REPLACE)
end
=end

p "Cur LINE #{line_no}: #{line}"
if multi_line then
# we alread in mult-line mode
# try to find "*/"
# p "In True Loop"
# p line.include?("*/")
if !line.include?("*/") then
# p "Next"
next
Expand All @@ -42,20 +81,43 @@
start = line.index("*/")
line = line.slice(start+2, line.length)
multi_line = false
end
end
end


line.gsub!(/\/\*.*?\*\//, '') # remove multi-line comment on one line
line.gsub!(/\/\/.*/,'') # remove one line comment

p "Cur LINE #{line_no}: #{line}"
# after gsub, there still multi-line comment
multi_line = line.include?("/*")

# p multi_line
p multi_line
# p double_quotes

if multi_line then
stop = line.index("/*")
line.slice!(stop, line.length)
end

# replace back all the special symbols

=begin
i = 0
line.gsub!(DOUBLE_QUOTES) do |match|
temp = double_quotes[0]
double_quotes.pop
match.gsub!(/.+/, temp)
end
=end
# =begin
line.gsub!(MUL_REPLACE_LEFT, "/*")
line.gsub!(MUL_REPLACE_RIGHT, "*/")
line.gsub!(SIG_REPLACE, "//")
# =end

result<<line<<"\n" if line.length > 0
lines += 1 if line.lstrip.length > 0
end
Expand All @@ -67,3 +129,5 @@
file.puts(result)
end
puts "There are #{lines} in the program"

0 comments on commit 54b1a88

Please sign in to comment.