Skip to content

Commit

Permalink
FIX: find/last not respecting series index
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Apr 21, 2024
1 parent e10e98d commit e28e41e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/core/t-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ static void No_Nones_Or_Logic(REBVAL *arg) {

if (flags & (AM_FIND_REVERSE | AM_FIND_LAST)) {
skip = -1;
start = 0;
if (flags & AM_FIND_LAST) index = end - len;
else index--;
if (flags & AM_FIND_LAST) {
start = index;
index = end - len;
}
else {
start = 0;
index--;
}
}

// Optimized find word in block:
Expand Down
11 changes: 8 additions & 3 deletions src/core/t-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ static REBCNT find_string(REBSER *series, REBCNT index, REBCNT end, REBVAL *targ

if (flags & (AM_FIND_REVERSE | AM_FIND_LAST)) {
skip = -1;
start = 0;
if (flags & AM_FIND_LAST) index = end - len;
else index--;
if (flags & AM_FIND_LAST) {
start = index;
index = end - len;
}
else {
start = 0;
index--;
}
}

if (flags & AM_FIND_SAME) flags |= AM_FIND_CASE; // /SAME has same functionality as /CASE for any-string!
Expand Down
29 changes: 29 additions & 0 deletions src/tests/units/series-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,35 @@ Rebol [
--assert "AbcdAe" = find/same "aAbcdAe" "A"
--assert "Ae" = find/same/last "aAbcdAe" "A"


;@@ https://github.com/Oldes/Rebol-issues/issues/1802
--test-- "FIND/LAST string! in string!"
--assert "ab" = find/last "ab" "a"
--assert "ab" = find/last "aab" "a"
--assert none? find/last next "ab" "a"
--assert "ab" = find/last next "aab" "a"
--test-- "FIND/LAST char! in string!"
--assert "ab" = find/last "ab" #"a"
--assert "ab" = find/last "aab" #"a"
--assert none? find/last next "ab" #"a"
--assert "ab" = find/last next "aab" #"a"
--test-- "FIND/LAST tag! in string!"
--assert "<a>b" = find/last "<a>b" <a>
--assert "<a>b" = find/last "<a><a>b" <a>
--assert none? find/last next "<a>b" <a>
--assert none? find/last find "<a>b" #"b" <a>
--assert "<a>" = find/last find "<a>b<a>" #"b" <a>
--test-- "FIND/LAST integer! in string!"
--assert "1b" = find/last "1b" 1
--assert "1b" = find/last "11b" 1
--assert none? find/last next "1b" 1
--assert "1b" = find/last next "11b" 1
--test-- "FIND/LAST integer! in block!"
--assert [1 b] = find/last [1 b] 1
--assert [1 b] = find/last [1 1 b] 1
--assert none? find/last next [1 b] 1
--assert [1 b] = find/last next [1 1 b] 1

--test-- "FIND/LAST/CASE in string!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1495
--assert none? find/case "Abc" "a"
Expand Down

0 comments on commit e28e41e

Please sign in to comment.