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