Clean up inreplace regexps

- use literal syntax
 - escape interpolated variables
 - remove intermediate variables
 - remove unnecessary capture
This commit is contained in:
Jack Nagel 2014-09-26 22:18:08 -05:00
parent 701691c261
commit e409aa3084

View File

@ -69,22 +69,23 @@ module StringInreplaceExtension
# value with "new_value", or removes the definition entirely.
def change_make_var! flag, new_value
new_value = "#{flag}=#{new_value}"
sub = gsub! Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$"), new_value, false
opoo "inreplace: changing '#{flag}' to '#{new_value}' failed" if sub.nil?
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, new_value, false)
opoo "inreplace: changing '#{flag}' to '#{new_value}' failed"
end
end
# Removes variable assignments completely.
def remove_make_var! flags
Array(flags).each do |flag|
# Also remove trailing \n, if present.
sub = gsub! Regexp.new("^#{flag}[ \\t]*=(.*)$\n?"), "", false
opoo "inreplace: removing '#{flag}' failed" if sub.nil?
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*=.*$\n?/, "", false)
opoo "inreplace: removing '#{flag}' failed"
end
end
end
# Finds the specified variable
def get_make_var flag
m = match Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$")
return m[1] if m
self[/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, 1]
end
end