Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytorrent/bencoding.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bencoded?(c) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rubytorrent/bencoding.rb', line 92

def self.bencoded?(c)
  c == ?i
end

.parse_bencoding(c, s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubytorrent/bencoding.rb', line 96

def self.parse_bencoding(c, s)
  ints = ""
  while ((x = s.getc.chr) != 'e')
    raise RubyTorrent::BEncodingError, "invalid bencoded integer #{x.inspect}" unless x =~ /\d|-/
    ints += x
  end
  raise RubyTorrent::BEncodingError, "invalid integer #{ints} (too long)" unless ints.length <= 20
  int = ints.to_i
  raise RubyTorrent::BEncodingError, %{can't parse bencoded integer "#{ints}"} if (int == 0) && (ints !~ /^0$/) #'
  int
end

Instance Method Details

#to_bencodingObject



88
89
90
# File 'lib/rubytorrent/bencoding.rb', line 88

def to_bencoding
  "i" + self.to_s + "e"
end