Class: String

Inherits:
Object show all
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

Instance Method Details

#absolute(root) ⇒ Object

returns the path as an absolute path with root ROOT MUST BE ABSOLUTE

Parameters:

  • root (String)

    absolute path to the root



828
829
830
831
# File 'lib/amp/support/support.rb', line 828

def absolute(root)
  return self if self[0] == ?/
  "#{root}/#{self}"
end

#add_slashesString

Adds minimal slashes to escape the string

Returns:

  • (String)

    the string slightly escaped.



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.

Returns:

  • (Boolean)

    does the string have anything in it?



41
42
43
# File 'lib/amp/support/ruby_19_compatibility.rb', line 41

def any?
  size > 0
end

#blueObject



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.

Parameters:

  • color_code (String)

    a TTY color code

Returns:

  • (String)

    the string wrapped in non-printing characters to make the text appear in a given color



649
650
651
# File 'lib/amp/support/support.rb', line 649

def colorize(color_code)
  "#{color_code}#{self}\e[0m"
end

#cyanObject



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?

Parameters:

  • suffix (String)

    the suffix to test

Returns:

  • (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

#greenObject



655
# File 'lib/amp/support/support.rb', line 655

def green; colorize("\e[32m"); end

#hexlifyObject

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_passwordString

removes the password from a url. else, just returns self

Returns:

  • (String)

    the URL with passwords censored.



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.

Returns:

  • (Boolean)

    is the string (most likely) binary data?



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

#lchopObject



48
49
50
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 48

def lchop
  slice(1, length)
end

#linesObject

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

#magentaObject



658
# File 'lib/amp/support/support.rb', line 658

def magenta; colorize("\e[35m"); end

#md5Digest::MD5

easy md5!

Returns:

  • (Digest::MD5)

    the MD5 digest of the string in hex form



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?

Returns:



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?

Returns:



677
678
679
# File 'lib/amp/support/support.rb', line 677

def null?
  self == Amp::RevlogSupport::Node::NULL_ID
end

#ordFixnum

Returns the numeric, ascii value of the first character in the string.

Returns:

  • (Fixnum)

    the 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

#redObject

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.

Parameters:

  • root (String)

    the root from which we want the relative path

Returns:

  • (String)

    the relative path from root to the string itself



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_slashesString

Removes minimal slashes to unescape the string

Returns:

  • (String)

    the string slightly unescaped.



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.

Parameters:

  • options (Hash) (defaults to: {})

    hash of the options for the command

  • args (Array) (defaults to: [])

    array of extra args

Returns:



770
771
772
773
774
775
776
# File 'lib/amp/support/support.rb', line 770

def run(options={}, args=[])
  if cmd = Amp::Command[self]
    cmd.run options, args
  else
    raise "No such command #{self}"
  end
end

#sha1Digest::SHA1

easy sha1! This is unsafe, as SHA1 kinda sucks.

Returns:

  • (Digest::SHA1)

    the SHA1 digest of the string in hex form



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.

Parameters:

  • char (String)

    the character to remove from the front of the string

Returns:

  • (String)

    the string with the leading char removed (if it is there).



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_hexObject

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_betterArray<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.

Returns:



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?

Parameters:

  • prefix (String)

    the prefix to test

Returns:

  • (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

#unhexlifyObject

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.

Examples:

“414243”.unhexlify == “ABC”

Parameters:

  • self (in)

    the string object to unhexlify

Returns:

  • A decoded, binary string



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

#whiteObject



660
# File 'lib/amp/support/support.rb', line 660

def white; colorize("\e[37m"); end

#yellowObject



656
# File 'lib/amp/support/support.rb', line 656

def yellow; colorize("\e[33m"); end