2019-03-28 15:14:16 +01:00
|
|
|
#!/usr/bin/env python
|
2013-01-14 16:21:40 +01:00
|
|
|
|
|
|
|
|
"""
|
2023-01-02 23:24:59 +01:00
|
|
|
Copyright (c) 2006-2023 sqlmap developers (https://sqlmap.org/)
|
2017-10-11 14:50:46 +02:00
|
|
|
See the file 'LICENSE' for copying permission
|
2013-01-14 16:21:40 +01:00
|
|
|
"""
|
|
|
|
|
|
2019-03-28 15:14:16 +01:00
|
|
|
import numbers
|
|
|
|
|
|
2013-01-14 16:18:03 +01:00
|
|
|
class xrange(object):
|
|
|
|
|
"""
|
2013-01-14 16:24:55 +01:00
|
|
|
Advanced (re)implementation of xrange (supports slice/copy/etc.)
|
2013-01-14 16:18:03 +01:00
|
|
|
Reference: http://code.activestate.com/recipes/521885-a-pythonic-implementation-of-xrange/
|
2013-03-13 19:42:22 +01:00
|
|
|
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(1, 9)) == list(range(1, 9))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(8, 0, -16)) == list(range(8, 0, -16))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(0, 8, 16)) == list(range(0, 8, 16))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(0, 4, 5)) == list(range(0, 4, 5))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(4, 0, 3)) == list(range(4, 0, 3))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(0, -3)) == list(range(0, -3))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2019-04-30 13:20:31 +02:00
|
|
|
>>> list(xrange(0, 7, 2)) == list(range(0, 7, 2))
|
2019-03-28 16:32:46 +01:00
|
|
|
True
|
2013-03-13 19:42:22 +01:00
|
|
|
>>> foobar = xrange(1, 10)
|
|
|
|
|
>>> 7 in foobar
|
|
|
|
|
True
|
|
|
|
|
>>> 11 in foobar
|
|
|
|
|
False
|
|
|
|
|
>>> foobar[0]
|
|
|
|
|
1
|
2013-01-14 16:18:03 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ['_slice']
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
2013-01-14 16:24:28 +01:00
|
|
|
if args and isinstance(args[0], type(self)):
|
2013-01-14 16:18:03 +01:00
|
|
|
self._slice = slice(args[0].start, args[0].stop, args[0].step)
|
|
|
|
|
else:
|
|
|
|
|
self._slice = slice(*args)
|
|
|
|
|
if self._slice.stop is None:
|
|
|
|
|
raise TypeError("xrange stop must not be None")
|
2014-08-20 21:07:19 +02:00
|
|
|
|
2013-01-14 16:18:03 +01:00
|
|
|
@property
|
|
|
|
|
def start(self):
|
|
|
|
|
if self._slice.start is not None:
|
|
|
|
|
return self._slice.start
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def stop(self):
|
|
|
|
|
return self._slice.stop
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def step(self):
|
|
|
|
|
if self._slice.step is not None:
|
|
|
|
|
return self._slice.step
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
|
return hash(self._slice)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
2018-06-09 23:38:00 +02:00
|
|
|
return '%s(%r, %r, %r)' % (type(self).__name__, self.start, self.stop, self.step)
|
2013-01-14 16:18:03 +01:00
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
|
return self._len()
|
|
|
|
|
|
|
|
|
|
def _len(self):
|
2019-03-28 16:32:46 +01:00
|
|
|
return max(0, 1 + int((self.stop - 1 - self.start) // self.step))
|
2013-01-14 16:18:03 +01:00
|
|
|
|
2013-02-05 10:27:31 +01:00
|
|
|
def __contains__(self, value):
|
|
|
|
|
return (self.start <= value < self.stop) and (value - self.start) % self.step == 0
|
|
|
|
|
|
2013-01-14 16:18:03 +01:00
|
|
|
def __getitem__(self, index):
|
|
|
|
|
if isinstance(index, slice):
|
|
|
|
|
start, stop, step = index.indices(self._len())
|
|
|
|
|
return xrange(self._index(start),
|
2018-03-13 13:45:42 +01:00
|
|
|
self._index(stop), step * self.step)
|
2019-03-28 15:14:16 +01:00
|
|
|
elif isinstance(index, numbers.Integral):
|
2013-01-14 16:18:03 +01:00
|
|
|
if index < 0:
|
|
|
|
|
fixed_index = index + self._len()
|
|
|
|
|
else:
|
|
|
|
|
fixed_index = index
|
2014-08-20 21:07:19 +02:00
|
|
|
|
2013-01-14 16:18:03 +01:00
|
|
|
if not 0 <= fixed_index < self._len():
|
|
|
|
|
raise IndexError("Index %d out of %r" % (index, self))
|
2014-08-20 21:07:19 +02:00
|
|
|
|
2013-01-14 16:18:03 +01:00
|
|
|
return self._index(fixed_index)
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError("xrange indices must be slices or integers")
|
|
|
|
|
|
|
|
|
|
def _index(self, i):
|
|
|
|
|
return self.start + self.step * i
|
2016-09-23 12:45:06 +02:00
|
|
|
|
|
|
|
|
def index(self, i):
|
|
|
|
|
if self.start <= i < self.stop:
|
|
|
|
|
return i - self.start
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError("%d is not in list" % i)
|