Skip to content

Commit

Permalink
FEAT/FIX: Picking the right PICK
Browse files Browse the repository at this point in the history
In Rebol2, picking value with index 0 was always returning null. In R3-alpha it was changed that zero index was working like index -1... more at this blog http://www.rebol.net/cgi-bin/r3blog.r?view=0173

I decided to revert this change and let it working as before (and so be compatible with Red too)

So now it works for example like:

>> pick tail "123" -1
== #"3"
>> pick tail "123" 0
== none

and it is also consistent with AT positioning:

>> at tail "123" -1
== "3"

Related issues:
metaeducation/rebol-issues#608
metaeducation/rebol-issues#609
metaeducation/rebol-issues#748
metaeducation/rebol-issues#857
metaeducation/rebol-issues#2117

and also:
metaeducation/rebol-issues#613
  • Loading branch information
Oldes committed Jun 13, 2018
1 parent 59f9cc3 commit 582e43d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/core/t-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,10 @@ static struct {
*/

if (IS_INTEGER(pvs->select)) {
n = Int32(pvs->select) + VAL_INDEX(pvs->value) - 1;
REBINT i = Int32(pvs->select);
if (i == 0) return PE_NONE; // like in case: path/0
if (i < 0) i++;
n = i + VAL_INDEX(pvs->value) - 1;
}
else if (IS_WORD(pvs->select)) {
n = Find_Word(VAL_SERIES(pvs->value), VAL_INDEX(pvs->value), VAL_WORD_CANON(pvs->select));
Expand Down Expand Up @@ -573,6 +576,8 @@ static struct {
REBINT n = 0;

n = Get_Num_Arg(selector);
if (n == 0) return 0;
if (n < 0) n++;
n += VAL_INDEX(block) - 1;
if (n < 0 || (REBCNT)n >= VAL_TAIL(block)) return 0;
return VAL_BLK_SKIP(block, n);
Expand Down
10 changes: 7 additions & 3 deletions src/core/t-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ static REBSER *make_binary(REBVAL *arg, REBOOL make)
REBSER *ser = VAL_SERIES(data);

if (IS_INTEGER(pvs->select)) {
n = Int32(pvs->select) + VAL_INDEX(data) - 1;
i = Int32(pvs->select);
if (i == 0) return PE_NONE; // like in case: path/0
if (i < 0) i++;
n = i + VAL_INDEX(data) - 1;
}
else return PE_BAD_SELECT;

Expand Down Expand Up @@ -542,8 +545,9 @@ static REBSER *make_binary(REBVAL *arg, REBOOL make)
case A_PICK:
case A_POKE:
len = Get_Num_Arg(arg); // Position
//if (len > 0) index--;
if (REB_I32_SUB_OF(len, 1, &len)
if (len < 0) REB_I32_ADD_OF(index, 1, &index);
if (len == 0
|| REB_I32_SUB_OF(len, 1, &len)
|| REB_I32_ADD_OF(index, len, &index)
|| index < 0 || index >= tail) {
if (action == A_PICK) goto is_none;
Expand Down
7 changes: 5 additions & 2 deletions src/core/t-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,13 @@ void Set_Vector_Row(REBSER *ser, REBVAL *blk)
REBINT bits;
REBYTE *vp;
REBI64 i;
REBDEC f;
REBDEC f = 0.0;

if (IS_INTEGER(pvs->select) || IS_DECIMAL(pvs->select))
if (IS_INTEGER(pvs->select) || IS_DECIMAL(pvs->select)) {
n = Int32(pvs->select);
if (n == 0) return PE_NONE;
if (n < 0) n++;
}
else return PE_BAD_SELECT;

n += VAL_INDEX(pvs->value);
Expand Down

0 comments on commit 582e43d

Please sign in to comment.