Sha256: 9281821fd3f5ed941a84cd9f40da3024ef157132ee4f5386b0c457e556685eaf

Contents?: true

Size: 1.37 KB

Versions: 22

Compression:

Stored size: 1.37 KB

Contents

class String
  # Standard in Ruby 1.9.3 See official documentation[http://ruby-doc.org/core-1.9.3/String.html#byteslice]
  def byteslice(start, len = Backports::Undefined)
    # Argument parsing & checking
    if Backports::Undefined == len
      if start.is_a?(Range)
        range = start
        start = Backports.coerce_to_int(range.begin)
        start += bytesize if start < 0
        last = Backports.coerce_to_int(range.end)
        last += bytesize if last < 0
        last += 1 unless range.exclude_end?
        len = last - start
      else
        start = Backports.coerce_to_int(start)
        start += bytesize if start < 0
        len = 1
        return if start >= bytesize
      end
    else
      start = Backports.coerce_to_int(start)
      start += bytesize if start < 0
      len = Backports.coerce_to_int(len)
      return if len < 0
    end
    return if start < 0 || start > bytesize
    len = 0 if len < 0
    # Actual implementation:
    str = unpack("@#{start}a#{len}").first
    str = dup.replace(str) unless self.instance_of?(String) # Must return subclass
    str.force_encoding(encoding)
  end unless method_defined? :byteslice

  # Standard in Ruby 1.9.3 See official documentation[http://ruby-doc.org/core-1.9.3/String.html#method-i-prepend]
  def prepend(other_str)
    replace Backports.coerce_to_str(other_str) + self
    self
  end unless method_defined? :prepend
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
backports-2.5.1 lib/backports/1.9.3/string.rb
backports-2.5.0 lib/backports/1.9.3/string.rb