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



830
831
832
833
# File 'lib/amp/support/support.rb', line 830

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.



814
815
816
# File 'lib/amp/support/support.rb', line 814

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?



44
45
46
# File 'lib/amp/support/ruby_19_compatibility.rb', line 44

def any?
  size > 0
end

#blueObject



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

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



651
652
653
# File 'lib/amp/support/support.rb', line 651

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

#cyanObject



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

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?



702
703
704
# File 'lib/amp/support/support.rb', line 702

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



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

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

#hexlifyObject

Converts this text into hex. each letter is replaced with it’s hex counterpart



782
783
784
785
786
787
788
# File 'lib/amp/support/support.rb', line 782

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.



799
800
801
802
803
804
805
806
807
808
809
# File 'lib/amp/support/support.rb', line 799

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?



842
843
844
# File 'lib/amp/support/support.rb', line 842

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



10
11
12
13
14
15
# File 'lib/amp/support/ruby_19_compatibility.rb', line 10

def lines
  return to_enum(:lines) if !block_given?
  self.split(/^/).each do |l|
    yield l
  end
end

#magentaObject



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

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

#md5Digest::MD5

easy md5!

Returns:

  • (Digest::MD5)

    the MD5 digest of the string in hex form



752
753
754
# File 'lib/amp/support/support.rb', line 752

def md5
  Digest::MD5.new.update(self)
end

#not_null?Boolean

Am I not equal to the NULL_ID used in revision logs?

Returns:



684
685
686
# File 'lib/amp/support/support.rb', line 684

def not_null?
  !(null?)
end

#null?Boolean

Am I equal to the NULL_ID used in revision logs?

Returns:



679
680
681
# File 'lib/amp/support/support.rb', line 679

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



22
23
24
# File 'lib/amp/support/ruby_19_compatibility.rb', line 22

def ord
  self[0]
end

#redObject

Returns the string, colored red.



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

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



670
671
672
673
674
675
676
# File 'lib/amp/support/support.rb', line 670

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.



821
822
823
# File 'lib/amp/support/support.rb', line 821

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:



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

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



761
762
763
# File 'lib/amp/support/support.rb', line 761

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



713
714
715
716
717
# File 'lib/amp/support/support.rb', line 713

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.



792
793
794
# File 'lib/amp/support/support.rb', line 792

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:



742
743
744
745
746
# File 'lib/amp/support/support.rb', line 742

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.



725
726
727
728
729
730
731
732
733
734
735
# File 'lib/amp/support/support.rb', line 725

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?



693
694
695
# File 'lib/amp/support/support.rb', line 693

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



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

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

#yellowObject



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

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