-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslice.js
More file actions
executable file
·70 lines (65 loc) · 1.8 KB
/
slice.js
File metadata and controls
executable file
·70 lines (65 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @constructor
* @param {number} start
* @param {number=} stop
* @param {null|number=} step
*/
Sk.builtin.slice = function slice(start, stop, step)
{
if (!(this instanceof Sk.builtin.slice)) return new Sk.builtin.slice(start, stop, step);
if (stop === undefined && step === undefined)
{
stop = start;
start = null;
}
if (!start) start = null;
if (stop === undefined) stop = null;
if (step === undefined) step = null;
this.start = start;
this.stop = stop;
this.step = step;
return this;
};
Sk.builtin.slice.prototype.tp$str = function()
{
var a = Sk.builtin.repr(this.start).v;
var b = Sk.builtin.repr(this.stop).v;
var c = Sk.builtin.repr(this.step).v;
return new Sk.builtin.str("slice(" + a + ", " + b + ", " + c + ")");
};
Sk.builtin.slice.prototype.indices = function(length)
{
// this seems ugly, better way?
var start = this.start, stop = this.stop, step = this.step, i;
if (step === null) step = 1;
if (step > 0)
{
if (start === null) start = 0;
if (stop === null) stop = length;
if (start < 0) start = length + start;
if (stop < 0) stop = length + stop;
}
else
{
if (start === null) start = length - 1;
else if (start < 0) start = length + start;
if (stop === null) stop = -1;
else if (stop < 0) stop = length + stop;
}
return [start, stop, step];
};
Sk.builtin.slice.prototype.sssiter$ = function(wrt, f)
{
var sss = this.indices(typeof wrt === "number" ? wrt : wrt.v.length);
if (sss[2] > 0)
{
var i;
for (i = sss[0]; i < sss[1]; i += sss[2])
if (f(i, wrt) === false) return;
}
else
{
for (i = sss[0]; i > sss[1]; i += sss[2])
if (f(i, wrt) === false) return;
}
};