Sha256: a9f9e7a11e54880fb7fe6d252e0258625faa4368bb78639f3a117a2bfeffc476

Contents?: true

Size: 1.18 KB

Versions: 45

Compression:

Stored size: 1.18 KB

Contents

unless String.method_defined? :byteslice
  require 'backports/tools/arguments'

  class String
    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) if respond_to?(:encoding)
      str
    end
  end
end

Version data entries

45 entries across 45 versions & 4 rubygems

Version Path
backports-3.25.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.24.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.24.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.23.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.22.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.22.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.21.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.20.2 lib/backports/1.9.3/string/byteslice.rb
backports-3.20.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.20.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.19.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.18.2 lib/backports/1.9.3/string/byteslice.rb
backports-3.18.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.18.0 lib/backports/1.9.3/string/byteslice.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/backports-3.12.0/lib/backports/1.9.3/string/byteslice.rb
backports-3.17.2 lib/backports/1.9.3/string/byteslice.rb
backports-3.17.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.17.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.16.1 lib/backports/1.9.3/string/byteslice.rb
backports-3.16.0 lib/backports/1.9.3/string/byteslice.rb