Sha256: 1179d89a4ffd33266fd052cbae22ca16dfbe43db49bb3c7348e6620b8c0fd036

Contents?: true

Size: 1.13 KB

Versions: 26

Compression:

Stored size: 1.13 KB

Contents

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

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

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
scout_realtime-1.0.5 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.4 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.3 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.3.pre lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.2 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.1 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-1.0.0 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.5 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.5.pre lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.4 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
backports-3.6.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.5.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.4.1 lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.3 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.2 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
scout_realtime-0.5.1 lib/vendor/backports-3.3.5/lib/backports/1.9.3/string/byteslice.rb
backports-3.4.0 lib/backports/1.9.3/string/byteslice.rb
backports-3.3.5 lib/backports/1.9.3/string/byteslice.rb
backports-3.3.4 lib/backports/1.9.3/string/byteslice.rb
backports-3.3.3 lib/backports/1.9.3/string/byteslice.rb