/usr/lib/ruby/1.8/extlib/string.rb is in libextlib-ruby1.8 0.9.13-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | require "pathname"
class String
##
# Escape all regexp special characters.
#
# "*?{}.".escape_regexp #=> "\\*\\?\\{\\}\\."
#
# @return [String] Receiver with all regexp special characters escaped.
#
# @api public
def escape_regexp
Regexp.escape self
end
##
# Unescape all regexp special characters.
#
# "\\*\\?\\{\\}\\.".unescape_regexp #=> "*?{}."
#
# @return [String] Receiver with all regexp special characters unescaped.
#
# @api public
def unescape_regexp
self.gsub(/\\([\.\?\|\(\)\[\]\{\}\^\$\*\+\-])/, '\1')
end
##
# Convert to snake case.
#
# "FooBar".snake_case #=> "foo_bar"
# "HeadlineCNNNews".snake_case #=> "headline_cnn_news"
# "CNN".snake_case #=> "cnn"
#
# @return [String] Receiver converted to snake case.
#
# @api public
def snake_case
return self.downcase if self =~ /^[A-Z]+$/
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
return $+.downcase
end
##
# Convert to camel case.
#
# "foo_bar".camel_case #=> "FooBar"
#
# @return [String] Receiver converted to camel case.
#
# @api public
def camel_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map{|e| e.capitalize}.join
end
##
# Convert a path string to a constant name.
#
# "merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"
#
# @return [String] Receiver converted to a constant name.
#
# @api public
def to_const_string
gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
##
# Convert a constant name to a path, assuming a conventional structure.
#
# "FooBar::Baz".to_const_path # => "foo_bar/baz"
#
# @return [String] Path to the file containing the constant named by receiver
# (constantized string), assuming a conventional structure.
#
# @api public
def to_const_path
snake_case.gsub(/::/, "/")
end
##
# Join with _o_ as a file path.
#
# "merb"/"core_ext" #=> "merb/core_ext"
#
# @param [String] o Path component to join with receiver.
#
# @return [String] Receiver joined with o as a file path.
#
# @api public
def /(o)
File.join(self, o.to_s)
end
##
# Calculate a relative path *from* _other_.
#
# "/opt/local/lib".relative_path_from("/opt/local/lib/ruby/site_ruby") # => "../.."
#
# @param [String] other Base path to calculate *from*.
#
# @return [String] Relative path from _other_ to receiver.
#
# @api public
def relative_path_from(other)
Pathname.new(self).relative_path_from(Pathname.new(other)).to_s
end
# Overwrite this method to provide your own translations.
def self.translate(value)
translations[value] || value
end
def self.translations
@translations ||= {}
end
##
# Replace sequences of whitespace (including newlines) with either
# a single space or remove them entirely (according to param _spaced_)
#
# <<QUERY.compress_lines
# SELECT name
# FROM users
# QUERY => "SELECT name FROM users"
#
# @param [TrueClass, FalseClass] spaced (default=true)
# Determines whether returned string has whitespace collapsed or removed
#
# @return [String] Receiver with whitespace (including newlines) replaced
#
# @api public
def compress_lines(spaced = true)
split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
end
##
# Remove whitespace margin.
#
# @param [Object] indicator ???
#
# @return [String] receiver with whitespace margin removed
#
# @api public
def margin(indicator = nil)
lines = self.dup.split($/)
min_margin = 0
lines.each do |line|
if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin)
min_margin = $1.size
end
end
lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/)
end
##
# Formats String for easy translation. Replaces an arbitrary number of
# values using numeric identifier replacement.
#
# "%s %s %s" % %w(one two three) #=> "one two three"
# "%3$s %2$s %1$s" % %w(one two three) #=> "three two one"
#
# @param [#to_s] values
# A list of values to translate and interpolate into receiver
#
# @return [String]
# Receiver translated with values translated and interpolated positionally
#
# @api public
def t(*values)
self.class::translate(self) % values.collect! { |value| value.frozen? ? value : self.class::translate(value.to_s) }
end
end # class String
|