Skip to content

Commit

Permalink
FEAT: new indexz? action returning 0-based position (index) of the …
Browse files Browse the repository at this point in the history
…series

related to: Oldes/Rebol-issues#613
  • Loading branch information
Oldes committed Oct 6, 2021
1 parent 7d3a040 commit 101284b
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/boot/actions.reb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ index?: action [
/xy {Returns index as an XY pair offset}
]

indexz?: action [
{Returns the current 0-based position (index) of the series.}
series [series! gob! port! none!]
/xy {Returns index as an XY pair offset}
]

length?: action [
{Returns the length (from the current position for series.)}
series [series! port! map! tuple! bitset! object! gob! struct! any-word! none!]
Expand Down
4 changes: 4 additions & 0 deletions src/core/f-series.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
SET_INTEGER(DS_RETURN, ((REBI64)index) + 1);
return R_RET;

case A_INDEXZQ:
SET_INTEGER(DS_RETURN, ((REBI64)index));
return R_RET;

case A_LENGTHQ:
SET_INTEGER(DS_RETURN, tail > index ? tail - index : 0);
return R_RET;
Expand Down
4 changes: 4 additions & 0 deletions src/core/t-gob.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,10 @@ const REBCNT Gob_Flag_Words[] = {
SET_INTEGER(val, index+1);
break;

case A_INDEXZQ:
SET_INTEGER(val, index);
break;

case A_LENGTHQ:
index = (tail > index) ? tail - index : 0;
SET_INTEGER(val, index);
Expand Down
11 changes: 11 additions & 0 deletions src/core/t-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,17 @@ INLINE REBCNT ARGB_To_BGR(REBCNT i)
DS_RET_INT(index + 1);
return R_RET;
}
case A_INDEXZQ:
if (D_REF(2)) {
VAL_SET(D_RET, REB_PAIR);
VAL_PAIR_X(D_RET) = (REBD32)(index % VAL_IMAGE_WIDE(value));
VAL_PAIR_Y(D_RET) = (REBD32)(index / VAL_IMAGE_WIDE(value));
return R_RET;
}
else {
DS_RET_INT(index);
return R_RET;
}
case A_LENGTHQ:
DS_RET_INT(tail > index ? tail - index : 0);
return R_RET;
Expand Down
16 changes: 16 additions & 0 deletions src/tests/units/gob-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ Rebol [

~~~start-file~~~ "gob!"



===start-group=== "INDEX? / INDEXZ?"
g: make gob! [] loop 2 [append g make gob! []]
--test-- "index? gob!"
--assert 1 = index? g
--assert 1 = index? back g
--assert 2 = index? next g
--assert 3 = index? tail g
--test-- "indexz? gob!"
--assert 0 = indexz? g
--assert 0 = indexz? back g
--assert 1 = indexz? next g
--assert 2 = indexz? tail g
===end-group===

===start-group=== "gob issues"
--test-- "issue-185"
;@@ https://github.com/Oldes/Rebol-issues/issues/185
Expand Down
18 changes: 18 additions & 0 deletions src/tests/units/image-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ Rebol [

===end-group===

===start-group=== "INDEX? / INDEXZ?"
img: make image! 2x2
--test-- "index? image!"
--assert 1 = index? img
--assert 2 = index? next img
--assert 5 = index? tail img
--assert 1x1 = index?/xy img
--assert 2x1 = index?/xy next img
--assert 1x3 = index?/xy tail img
--test-- "indexz? image!"
--assert 0 = indexz? img
--assert 1 = indexz? next img
--assert 4 = indexz? tail img
--assert 0x0 = indexz?/xy img
--assert 1x0 = indexz?/xy next img
--assert 0x2 = indexz?/xy tail img
===end-group===

===start-group=== "FOREACH"
--test-- "issue-1479"
;@@ https://github.com/Oldes/Rebol-issues/issues/1479
Expand Down
32 changes: 32 additions & 0 deletions src/tests/units/series-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,38 @@ Rebol [
===end-group===


===start-group=== "INDEXZ?"
--test-- "indexz? on string"
s: "abc"
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
]
--test-- "indexz? on binary"
s: to binary! "abc"
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
]
--test-- "indexz? on block"
s: [1 2 3]
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
]
--test-- "indexz? on vector"
s: #[u16! 3]
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
]
===end-group===


===start-group=== "PICK"
;@@ https://github.com/Oldes/Rebol-issues/issues/608
;@@ https://github.com/Oldes/Rebol-issues/issues/857
Expand Down

0 comments on commit 101284b

Please sign in to comment.