Class: String
- Defined in:
- lib/amp/dependencies/zip/stdrubyext.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/dependencies/amp_support/ruby_amp_support.rb,
lib/amp/support/support.rb
Instance Method Summary collapse
-
#absolute(root) ⇒ Object
returns the path as an absolute path with
root
ROOT MUST BE ABSOLUTE. -
#add_slashes ⇒ String
Adds minimal slashes to escape the string.
-
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?.
- #blue ⇒ Object
-
#colorize(color_code) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
- #cyan ⇒ Object
-
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?.
- #ends_with(aString) ⇒ Object
- #ensure_end(aString) ⇒ Object
- #green ⇒ Object
-
#hexlify ⇒ Object
Converts this text into hex.
-
#hide_password ⇒ String
removes the password from a url.
-
#is_binary_data? ⇒ Boolean
(also: #binary?)
Attempts to discern if the string represents binary data or not.
- #lchop ⇒ Object
-
#lines ⇒ Object
DON’T USE String#each.
- #magenta ⇒ Object
-
#md5 ⇒ Digest::MD5
easy md5!.
-
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?.
-
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?.
-
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
-
#red ⇒ Object
Returns the string, colored red.
-
#relative_path(root) ⇒ String
Returns the path from
root
to the path represented by the string. -
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string.
-
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it.
-
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
-
#shift(char) ⇒ String
(also: #lchomp)
Pops the given character off the front of the string, but only if the string starts with the given character.
-
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
-
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better.
-
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one.
-
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?.
- #starts_with(aString) ⇒ Object
-
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation.
- #white ⇒ Object
- #yellow ⇒ Object
Instance Method Details
#absolute(root) ⇒ Object
returns the path as an absolute path with root
ROOT MUST BE ABSOLUTE
828 829 830 831 |
# File 'lib/amp/support/support.rb', line 828 def absolute(root) return self if self[0] == ?/ "#{root}/#{self}" end |
#add_slashes ⇒ String
Adds minimal slashes to escape the string
812 813 814 |
# File 'lib/amp/support/support.rb', line 812 def add_slashes self.gsub(/\\/,"\\\\").gsub(/\n/,"\\n").gsub(/\r/,"\\r").gsub("\0","\\0") end |
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?. Luckily it’s quite easy to implement.
41 42 43 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 41 def any? size > 0 end |
#blue ⇒ Object
657 |
# File 'lib/amp/support/support.rb', line 657 def blue; colorize("\e[34m"); end |
#colorize(color_code) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
649 650 651 |
# File 'lib/amp/support/support.rb', line 649 def colorize(color_code) "#{color_code}#{self}\e[0m" end |
#cyan ⇒ Object
659 |
# File 'lib/amp/support/support.rb', line 659 def cyan; colorize("\e[36m"); end |
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?
700 701 702 |
# File 'lib/amp/support/support.rb', line 700 def end_with?(suffix) self[-suffix.size, suffix.size] == suffix # self =~ /#{str}$/ end |
#ends_with(aString) ⇒ Object
40 41 42 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 40 def ends_with(aString) index(aString, -aString.size) end |
#ensure_end(aString) ⇒ Object
44 45 46 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 44 def ensure_end(aString) ends_with(aString) ? self : self + aString end |
#green ⇒ Object
655 |
# File 'lib/amp/support/support.rb', line 655 def green; colorize("\e[32m"); end |
#hexlify ⇒ Object
Converts this text into hex. each letter is replaced with it’s hex counterpart
780 781 782 783 784 785 786 |
# File 'lib/amp/support/support.rb', line 780 def hexlify str = "" self.each_byte do |i| str << i.to_s(16).rjust(2, "0") end str end |
#hide_password ⇒ String
removes the password from a url. else, just returns self
797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/amp/support/support.rb', line 797 def hide_password if s = self.match(/^http(?:s)?:\/\/[^:]+(?::([^:]+))?(@)/) string = '' string << self[0..s.begin(1)-1] # get from beginning to the pass string << '***' string << self[s.begin(2)..-1] string else self end end |
#is_binary_data? ⇒ Boolean Also known as: binary?
Attempts to discern if the string represents binary data or not. Not 100% accurate. Is part of the YAML code that comes with ruby, but since we don’t load rubygems, we don’t get this method for free.
840 841 842 |
# File 'lib/amp/support/support.rb', line 840 def is_binary_data? ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty? end |
#lchop ⇒ Object
48 49 50 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 48 def lchop slice(1, length) end |
#lines ⇒ Object
DON’T USE String#each. Use String#each_line
8 9 10 11 12 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 8 def lines self.split("\n").each do |l| yield l end end |
#magenta ⇒ Object
658 |
# File 'lib/amp/support/support.rb', line 658 def magenta; colorize("\e[35m"); end |
#md5 ⇒ Digest::MD5
easy md5!
750 751 752 |
# File 'lib/amp/support/support.rb', line 750 def md5 Digest::MD5.new.update(self) end |
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?
682 683 684 |
# File 'lib/amp/support/support.rb', line 682 def not_null? !(null?) end |
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?
677 678 679 |
# File 'lib/amp/support/support.rb', line 677 def null? self == Amp::RevlogSupport::Node::NULL_ID end |
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
19 20 21 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 19 def ord self[0] end |
#red ⇒ Object
Returns the string, colored red.
654 |
# File 'lib/amp/support/support.rb', line 654 def red; colorize("\e[31m"); end |
#relative_path(root) ⇒ String
Returns the path from root
to the path represented by the string. Will fail if the string is not inside root
.
668 669 670 671 672 673 674 |
# File 'lib/amp/support/support.rb', line 668 def relative_path(root) return '' if self == root # return a more local path if possible... return self[root.length..-1] if start_with? root self # else we're outside the repo end |
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string
819 820 821 |
# File 'lib/amp/support/support.rb', line 819 def remove_slashes self.gsub(/\\0/,"\0").gsub(/\\r/,"\r").gsub(/\\n/,"\n").gsub(/\\\\/,"\\") end |
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it. Else, raise hell.
770 771 772 773 774 775 776 |
# File 'lib/amp/support/support.rb', line 770 def run(={}, args=[]) if cmd = Amp::Command[self] cmd.run , args else raise "No such command #{self}" end end |
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
759 760 761 |
# File 'lib/amp/support/support.rb', line 759 def sha1 Digest::SHA1.new.update(self) end |
#shift(char) ⇒ String Also known as: lchomp
Pops the given character off the front of the string, but only if the string starts with the given character. Otherwise, nothing happens. Often used to remove troublesome leading slashes. Much like an “lchomp” method.
711 712 713 714 715 |
# File 'lib/amp/support/support.rb', line 711 def shift(char) return '' if self.empty? return self[1..-1] if self.start_with? char self end |
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
790 791 792 |
# File 'lib/amp/support/support.rb', line 790 def short_hex hexlify[0..9] end |
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better. This splits on newlines, but includes the newline in each entry in the resultant string array.
740 741 742 743 744 |
# File 'lib/amp/support/support.rb', line 740 def split_lines_better result = [] each_line {|l| result << l} result end |
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one. This is how mercurial does it and i’m sticking to it. This method is evil. DON’T USE IT.
723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/amp/support/support.rb', line 723 def split_newlines(add_newlines=true) return [] if self.empty? lines = self.split("\n").map {|l| l + (add_newlines ? "\n" : "") } return lines if lines.size == 1 if (add_newlines && lines.last == "\n") || (!add_newlines && lines.last.empty?) lines.pop else lines[-1] = lines[-1][0..-2] if lines[-1][-1,1] == "\n" end lines end |
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?
691 692 693 |
# File 'lib/amp/support/support.rb', line 691 def start_with?(prefix) self[0,prefix.size] == prefix # self =~ /^#{str}/ end |
#starts_with(aString) ⇒ Object
36 37 38 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 36 def starts_with(aString) rindex(aString, 0) == 0 end |
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation. Method on strings. When the data in the string is hexademical (such as “DEADBEEF”), this method decodes the hex and converts every 2 bytes into its 1-byte binary representation.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 79 def unhexlify str = "\000" * (size/2) c = 0 (0..size-2).step(2) do |i| hex = self[i,2].to_i(16) str[c] = hex c += 1 end str end |
#white ⇒ Object
660 |
# File 'lib/amp/support/support.rb', line 660 def white; colorize("\e[37m"); end |
#yellow ⇒ Object
656 |
# File 'lib/amp/support/support.rb', line 656 def yellow; colorize("\e[33m"); end |