| 
									
										
										
										
											2010-02-27 17:26:27 +00:00
										 |  |  | class String | 
					
						
							|  |  |  |   def undent | 
					
						
							| 
									
										
										
										
											2010-10-25 21:12:41 -07:00
										 |  |  |     gsub(/^.{#{slice(/^ +/).length}}/, '') | 
					
						
							| 
									
										
										
										
											2010-02-27 17:26:27 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-13 13:02:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-14 11:23:30 -04:00
										 |  |  |   # eg: | 
					
						
							|  |  |  |   #   if foo then <<-EOS.undent_________________________________________________________72 | 
					
						
							|  |  |  |   #               Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do | 
					
						
							|  |  |  |   #               eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad | 
					
						
							|  |  |  |   #               minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip | 
					
						
							|  |  |  |   #               ex ea commodo consequat. Duis aute irure dolor in reprehenderit in | 
					
						
							|  |  |  |   #               voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur | 
					
						
							|  |  |  |   #               sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt | 
					
						
							|  |  |  |   #               mollit anim id est laborum. | 
					
						
							|  |  |  |   #               EOS | 
					
						
							|  |  |  |   alias_method :undent_________________________________________________________72, :undent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 22:31:03 -05:00
										 |  |  |   def start_with?(*prefixes) | 
					
						
							|  |  |  |     prefixes.any? do |prefix| | 
					
						
							|  |  |  |       if prefix.respond_to?(:to_str) | 
					
						
							|  |  |  |         prefix = prefix.to_str | 
					
						
							|  |  |  |         self[0, prefix.length] == prefix | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-06-13 13:02:17 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-07-19 22:31:03 -05:00
										 |  |  |   end unless method_defined?(:start_with?) | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 22:32:56 -05:00
										 |  |  |   def end_with?(*suffixes) | 
					
						
							|  |  |  |     suffixes.any? do |suffix| | 
					
						
							|  |  |  |       if suffix.respond_to?(:to_str) | 
					
						
							|  |  |  |         suffix = suffix.to_str | 
					
						
							|  |  |  |         self[-suffix.length, suffix.length] == suffix | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end unless method_defined?(:end_with?) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |   # String.chomp, but if result is empty: returns nil instead. | 
					
						
							|  |  |  |   # Allows `chuzzle || foo` short-circuits. | 
					
						
							|  |  |  |   def chuzzle | 
					
						
							|  |  |  |     s = chomp | 
					
						
							|  |  |  |     s unless s.empty? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NilClass | 
					
						
							|  |  |  |   def chuzzle; end | 
					
						
							| 
									
										
										
										
											2010-02-27 17:26:27 +00:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # used by the inreplace function (in utils.rb) | 
					
						
							|  |  |  | module StringInreplaceExtension | 
					
						
							| 
									
										
										
										
											2012-02-25 16:57:03 -08:00
										 |  |  |   # Warn if nothing was replaced | 
					
						
							|  |  |  |   def gsub! before, after, audit_result=true | 
					
						
							|  |  |  |     sub = super(before, after) | 
					
						
							|  |  |  |     if audit_result and sub.nil? | 
					
						
							|  |  |  |       opoo "inreplace: replacement of '#{before}' with '#{after}' failed" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     return sub | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |   # Looks for Makefile style variable defintions and replaces the | 
					
						
							|  |  |  |   # value with "new_value", or removes the definition entirely. | 
					
						
							|  |  |  |   def change_make_var! flag, new_value | 
					
						
							|  |  |  |     new_value = "#{flag}=#{new_value}" | 
					
						
							| 
									
										
										
										
											2012-02-25 16:57:03 -08:00
										 |  |  |     sub = gsub! Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$"), new_value, false | 
					
						
							| 
									
										
										
										
											2012-02-23 21:05:49 -06:00
										 |  |  |     opoo "inreplace: changing '#{flag}' to '#{new_value}' failed" if sub.nil? | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Removes variable assignments completely. | 
					
						
							|  |  |  |   def remove_make_var! flags | 
					
						
							| 
									
										
										
										
											2013-02-15 00:35:52 -06:00
										 |  |  |     Array(flags).each do |flag| | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |       # Also remove trailing \n, if present. | 
					
						
							| 
									
										
										
										
											2012-02-25 16:57:03 -08:00
										 |  |  |       sub = gsub! Regexp.new("^#{flag}[ \\t]*=(.*)$\n?"), "", false | 
					
						
							| 
									
										
										
										
											2012-02-23 21:05:49 -06:00
										 |  |  |       opoo "inreplace: removing '#{flag}' failed" if sub.nil? | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Finds the specified variable | 
					
						
							|  |  |  |   def get_make_var flag | 
					
						
							|  |  |  |     m = match Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$") | 
					
						
							|  |  |  |     return m[1] if m | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |