Implement inreplace natively in Ruby
I found yet another instance where the escaping wasn't perfect, so got fed up and just did it in Ruby. I hope this works for all existing usage. It should. The bonus here is that you can use RegExps now.
This commit is contained in:
		
							parent
							
								
									7375758c9a
								
							
						
					
					
						commit
						e289164adc
					
				@ -149,13 +149,8 @@ end
 | 
			
		||||
 | 
			
		||||
# replaces before with after for the file path
 | 
			
		||||
def inreplace path, before, after
 | 
			
		||||
  before=Regexp.escape before.to_s
 | 
			
		||||
  before.gsub! "/", "\\/" # I guess not escaped as delimiter varies
 | 
			
		||||
  after=after.to_s
 | 
			
		||||
  after.gsub! "\\", "\\\\"
 | 
			
		||||
  after.gsub! "/", "\\/"
 | 
			
		||||
  after.gsub! "$", "\\$"
 | 
			
		||||
 | 
			
		||||
  # FIXME use proper Ruby for teh exceptions!
 | 
			
		||||
  safe_system "/usr/bin/perl", "-pi", "-e", "s/#{before}/#{after}/g", path
 | 
			
		||||
  f = File.open(path, 'r')
 | 
			
		||||
  o = f.read.gsub(before, after)
 | 
			
		||||
  f.reopen(path, 'w').write(o)
 | 
			
		||||
  f.close
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user