2010-02-27 17:26:27 +00:00
|
|
|
class String
|
|
|
|
def undent
|
2015-08-26 16:50:36 -04:00
|
|
|
gsub(/^[ \t]{#{(slice(/^[ \t]+/) || '').length}}/, "")
|
2010-02-27 17:26:27 +00:00
|
|
|
end
|
2017-05-28 16:59:53 +01:00
|
|
|
alias unindent undent
|
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
|
2016-09-23 18:13:48 +02:00
|
|
|
alias undent_________________________________________________________72 undent
|
2012-08-14 11:23:30 -04:00
|
|
|
|
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
|
2015-12-02 14:23:36 +08:00
|
|
|
|
|
|
|
def strip_prefix(prefix)
|
|
|
|
start_with?(prefix) ? self[prefix.length..-1] : self
|
|
|
|
end
|
2012-08-10 16:06:51 -04:00
|
|
|
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
|
2014-09-28 01:08:31 -05:00
|
|
|
attr_accessor :errors
|
|
|
|
|
|
|
|
def self.extended(str)
|
|
|
|
str.errors = []
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def sub!(before, after)
|
2014-09-27 19:10:12 -05:00
|
|
|
result = super
|
|
|
|
unless result
|
2014-09-28 01:08:31 -05:00
|
|
|
errors << "expected replacement of #{before.inspect} with #{after.inspect}"
|
2014-09-27 19:10:12 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2012-02-25 16:57:03 -08:00
|
|
|
# Warn if nothing was replaced
|
2015-08-03 13:09:07 +01:00
|
|
|
def gsub!(before, after, audit_result = true)
|
2014-09-27 19:10:12 -05:00
|
|
|
result = super(before, after)
|
|
|
|
if audit_result && result.nil?
|
2014-09-28 01:08:31 -05:00
|
|
|
errors << "expected replacement of #{before.inspect} with #{after.inspect}"
|
2012-02-25 16:57:03 -08:00
|
|
|
end
|
2014-09-27 19:10:12 -05:00
|
|
|
result
|
2012-02-25 16:57:03 -08:00
|
|
|
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.
|
2015-08-03 13:09:07 +01:00
|
|
|
def change_make_var!(flag, new_value)
|
2016-09-23 22:02:23 +02:00
|
|
|
return if gsub!(/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, "#{flag}=#{new_value}", false)
|
|
|
|
errors << "expected to change #{flag.inspect} to #{new_value.inspect}"
|
2010-11-12 21:05:35 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Removes variable assignments completely.
|
2015-08-03 13:09:07 +01:00
|
|
|
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.
|
2014-09-26 22:18:08 -05:00
|
|
|
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*=.*$\n?/, "", false)
|
2014-09-28 01:08:31 -05:00
|
|
|
errors << "expected to remove #{flag.inspect}"
|
2014-09-26 22:18:08 -05:00
|
|
|
end
|
2010-11-12 21:05:35 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Finds the specified variable
|
2015-08-03 13:09:07 +01:00
|
|
|
def get_make_var(flag)
|
2014-09-26 22:18:08 -05:00
|
|
|
self[/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, 1]
|
2010-11-12 21:05:35 -08:00
|
|
|
end
|
|
|
|
end
|