Skip to content

Commit fb0daad

Browse files
committed
FEAT: new ATz action returning the seriest at 0-based position (index)
CHANGE: `at` action on image with pair position was 0-based, now is 1-based related to: Oldes/Rebol-issues#613
1 parent 101284b commit fb0daad

File tree

7 files changed

+77
-30
lines changed

7 files changed

+77
-30
lines changed

src/boot/actions.reb

+6
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ at: action [
170170
index [number! logic! pair!]
171171
]
172172

173+
atz: action [
174+
{Returns the series at the specified 0-based index.}
175+
series [series! gob! port!]
176+
index [number! logic! pair!]
177+
]
178+
173179
index?: action [
174180
{Returns the current position (index) of the series.}
175181
series [series! gob! port! none!]

src/core/f-series.c

+3-11
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,21 @@
8080

8181
case A_SKIP:
8282
case A_AT:
83+
case A_ATZ:
8384
len = Get_Num_Arg(arg);
8485
{
8586
REBI64 i = (REBI64)index + (REBI64)len;
8687
if (action == A_SKIP) {
8788
if (IS_LOGIC(arg)) i--;
88-
} else { // A_AT
89+
} else if (action == A_AT) {
8990
if (len > 0) i--;
9091
}
9192
if (i > (REBI64)tail) i = (REBI64)tail;
9293
else if (i < 0) i = 0;
9394
VAL_INDEX(value) = (REBCNT)i;
9495
}
9596
break;
96-
/*
97-
case A_ATZ:
98-
len = Get_Num_Arg(arg);
99-
{
100-
REBI64 idx = Add_Max(0, index, len, MAX_I32);
101-
if (idx < 0) idx = 0;
102-
VAL_INDEX(value) = (REBCNT)idx;
103-
}
104-
break;
105-
*/
97+
10698
case A_INDEXQ:
10799
SET_INTEGER(DS_RETURN, ((REBI64)index) + 1);
108100
return R_RET;

src/core/t-gob.c

+1
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ const REBCNT Gob_Flag_Words[] = {
910910
case A_AT:
911911
index--;
912912
case A_SKIP:
913+
case A_ATZ:
913914
index += VAL_INT32(arg);
914915
goto set_index;
915916

src/core/t-image.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -1070,21 +1070,18 @@ INLINE REBCNT ARGB_To_BGR(REBCNT i)
10701070
Pick_Path(value, arg, D_ARG(3));
10711071
return R_ARG3;
10721072

1073-
case A_SKIP:
1074-
case A_AT:
1075-
// This logic is somewhat complicated by the fact that INTEGER args use
1076-
// base-1 indexing, but PAIR args use base-0.
1073+
case A_AT: // base-1
1074+
case A_ATZ: // base-0
1075+
case A_SKIP: // base-0
10771076
if (IS_PAIR(arg)) {
1078-
if (action == A_AT) action = A_SKIP;
1079-
diff = (VAL_PAIR_Y_INT(arg) * VAL_IMAGE_WIDE(value) + VAL_PAIR_X_INT(arg)) +
1080-
((action == A_SKIP) ? 0 : 1);
1077+
diff = ((VAL_PAIR_Y_INT(arg) - ((action == A_AT) ? 1 : 0)) * VAL_IMAGE_WIDE(value) + VAL_PAIR_X_INT(arg));
10811078
} else
10821079
diff = Get_Num_Arg(arg);
10831080

10841081
index += diff;
10851082
if (action == A_SKIP) {
10861083
if (IS_LOGIC(arg)) index--;
1087-
} else {
1084+
} else if (action == A_AT) {
10881085
if (diff > 0) index--; // For at, pick, poke.
10891086
}
10901087

src/tests/units/gob-test.r3

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ Rebol [
1717
--assert 1 = index? back g
1818
--assert 2 = index? next g
1919
--assert 3 = index? tail g
20+
--assert 2 = index? at g 2
2021
--test-- "indexz? gob!"
2122
--assert 0 = indexz? g
2223
--assert 0 = indexz? back g
2324
--assert 1 = indexz? next g
2425
--assert 2 = indexz? tail g
26+
--assert 1 = indexz? atz g 1
2527
===end-group===
2628

2729
===start-group=== "gob issues"

src/tests/units/image-test.r3

+44-11
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,55 @@ Rebol [
6969

7070
===end-group===
7171

72-
===start-group=== "INDEX? / INDEXZ?"
72+
===start-group=== "INDEX? / INDEXZ? / AT / ATZ"
7373
img: make image! 2x2
7474
--test-- "index? image!"
75-
--assert 1 = index? img
76-
--assert 2 = index? next img
77-
--assert 5 = index? tail img
75+
--assert 1 = index? img
76+
--assert 2 = index? next img
77+
--assert 5 = index? tail img
78+
--assert 4 = index? skip tail img -1
79+
--test-- "index? at image!"
80+
--assert 1 = index? at img -1
81+
--assert 1 = index? at img 0
82+
--assert 1 = index? at img 1
83+
--assert 2 = index? at img 2
84+
--assert 5 = index? at img 6
85+
--assert 1 = index? skip at img 2 -1
86+
--test-- "index?/xy image!"
7887
--assert 1x1 = index?/xy img
7988
--assert 2x1 = index?/xy next img
8089
--assert 1x3 = index?/xy tail img
90+
--assert 2x2 = index?/xy skip tail img -1
91+
--test-- "index?/xy at image!"
92+
--assert 1x1 = index?/xy at img 1x1
93+
--assert 1x2 = index?/xy at img 1x2
94+
--assert 2x2 = index?/xy at img 2x2
95+
--assert 1x3 = index?/xy at img 20x2
96+
--assert 1x2 = index?/xy skip at img 2x2 -1x0
8197
--test-- "indexz? image!"
82-
--assert 0 = indexz? img
83-
--assert 1 = indexz? next img
84-
--assert 4 = indexz? tail img
98+
--assert 0 = indexz? img
99+
--assert 1 = indexz? next img
100+
--assert 4 = indexz? tail img
101+
--assert 3 = indexz? skip tail img -1
102+
--test-- "indexz? atz image!"
103+
--assert 0 = indexz? atz img -1
104+
--assert 0 = indexz? atz img 0
105+
--assert 2 = indexz? atz img 2
106+
--assert 4 = indexz? atz img 6
107+
--assert 3 = indexz? skip atz img 6 -1
108+
--assert 1 = indexz? skip atz img 2 -1
109+
--test-- "indexz?/xy image!"
85110
--assert 0x0 = indexz?/xy img
86111
--assert 1x0 = indexz?/xy next img
87112
--assert 0x2 = indexz?/xy tail img
113+
--assert 1x1 = indexz?/xy skip tail img -1x0
114+
--test-- "indexz?/xy atz image!"
115+
--assert 0x0 = indexz?/xy atz img 0x0
116+
--assert 0x1 = indexz?/xy atz img 0x1
117+
--assert 1x1 = indexz?/xy atz img 1x1
118+
--assert 0x2 = indexz?/xy atz img 2x2
119+
--assert 0x2 = indexz?/xy atz img 20x2
120+
--assert 0x1 = indexz?/xy skip atz img 1x1 -1x0
88121
===end-group===
89122

90123
===start-group=== "FOREACH"
@@ -250,26 +283,26 @@ Rebol [
250283
000000000000FFFFFFFFFFFF
251284
FFFFFFFFFFFFFFFFFFFFFFFF
252285
FFFFFFFFFFFFFFFFFFFFFFFF}
253-
change at img 1x1 make image! [2x2 220.22.22]
286+
change at img 2x2 make image! [2x2 220.22.22]
254287
--assert img/rgb = #{
255288
000000000000FFFFFFFFFFFF
256289
000000DC1616DC1616FFFFFF
257290
FFFFFFDC1616DC1616FFFFFF
258291
FFFFFFFFFFFFFFFFFFFFFFFF}
259-
change at img 2x2 make image! [3x3 33.33.33]
292+
change at img 3x3 make image! [3x3 33.33.33]
260293
--assert img/rgb = #{
261294
000000000000FFFFFFFFFFFF
262295
000000DC1616DC1616FFFFFF
263296
FFFFFFDC1616212121212121
264297
FFFFFFFFFFFF212121212121}
265-
change at img 0x3 make image! [4x4 66.166.66]
298+
change at img 1x4 make image! [4x4 66.166.66]
266299
--assert img/rgb = #{
267300
000000000000FFFFFFFFFFFF
268301
000000DC1616DC1616FFFFFF
269302
FFFFFFDC1616212121212121
270303
42A64242A64242A64242A642}
271304

272-
change at img 3x0 make image! [2x1 #{AAAAAABBBBBB}]
305+
change at img 4x1 make image! [2x1 #{AAAAAABBBBBB}]
273306
--assert img/rgb = #{
274307
000000000000FFFFFFAAAAAA
275308
000000DC1616DC1616FFFFFF

src/tests/units/series-test.r3

+16
Original file line numberDiff line numberDiff line change
@@ -627,27 +627,43 @@ Rebol [
627627
0 = indexz? s
628628
1 = indexz? next s
629629
3 = indexz? tail s
630+
0 = indexz? atz s -1
631+
0 = indexz? atz s 0
632+
2 = indexz? atz s 2
633+
3 = indexz? atz s 6
630634
]
631635
--test-- "indexz? on binary"
632636
s: to binary! "abc"
633637
--assert all [
634638
0 = indexz? s
635639
1 = indexz? next s
636640
3 = indexz? tail s
641+
0 = indexz? atz s -1
642+
0 = indexz? atz s 0
643+
2 = indexz? atz s 2
644+
3 = indexz? atz s 6
637645
]
638646
--test-- "indexz? on block"
639647
s: [1 2 3]
640648
--assert all [
641649
0 = indexz? s
642650
1 = indexz? next s
643651
3 = indexz? tail s
652+
0 = indexz? atz s -1
653+
0 = indexz? atz s 0
654+
2 = indexz? atz s 2
655+
3 = indexz? atz s 6
644656
]
645657
--test-- "indexz? on vector"
646658
s: #[u16! 3]
647659
--assert all [
648660
0 = indexz? s
649661
1 = indexz? next s
650662
3 = indexz? tail s
663+
0 = indexz? atz s -1
664+
0 = indexz? atz s 0
665+
2 = indexz? atz s 2
666+
3 = indexz? atz s 6
651667
]
652668
===end-group===
653669

0 commit comments

Comments
 (0)