-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathrange.ahk
59 lines (51 loc) · 1.41 KB
/
range.ahk
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
range(start, stop:="", step:=1) {
static range := { _NewEnum: Func("_RangeNewEnum") }
if !step {
throw "range(): Parameter 'step' must not be 0 or blank"
}
if (stop == "") {
stop := start, start := 0
}
; Formula: r[i] := start + step*i ; r = range object, i = 0-based index
; For a postive 'step', the constraints are i >= 0 and r[i] < stop
; For a negative 'step', the constraints are i >= 0 and r[i] > stop
; No result is returned if r[0] does not meet the value constraint
if (step > 0 ? start < stop : start > stop) {
;// start == start + step*0
return { base: range, start: start, stop: stop, step: step }
}
}
_RangeNewEnum(r) {
static enum := { "Next": Func("_RangeEnumNext") }
return { base: enum, r: r, i: 0 }
}
_RangeEnumNext(enum, ByRef k, ByRef v:="") {
stop := enum.r.stop+1, step := enum.r.step
, k := enum.r.start + step*enum.i
if (ret := step > 0 ? k < stop : k > stop) {
enum.i += 1
}
return ret
}
; ;// Simple usage
; len := (10-0)//1 ; (stop-start)//step
; out := "["
; for i in range(10) {
; out .= i . (A_Index < len ? ", " : "]")
; }
; MsgBox %out%
; ;// step = 5
; len := (50-0)//5 ; (stop-start)//step
; out := "["
; for i in range(0, 50, 5) {
; out .= i . (A_Index < len ? ", " : "]")
; }
; MsgBox %out%
; ;// Negative numbers
; len := (-10-0)//-1 ; (stop-start)//step
; out := "["
; for i in range(0, -10, -1) {
; out .= i . (A_Index < len ? ", " : "]")
; }
; MsgBox %out%