Skip to content

Commit e28e41e

Browse files
committed
FIX: find/last not respecting series index
resolves: Oldes/Rebol-issues#1802
1 parent e10e98d commit e28e41e

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/core/t-block.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ static void No_Nones_Or_Logic(REBVAL *arg) {
109109

110110
if (flags & (AM_FIND_REVERSE | AM_FIND_LAST)) {
111111
skip = -1;
112-
start = 0;
113-
if (flags & AM_FIND_LAST) index = end - len;
114-
else index--;
112+
if (flags & AM_FIND_LAST) {
113+
start = index;
114+
index = end - len;
115+
}
116+
else {
117+
start = 0;
118+
index--;
119+
}
115120
}
116121

117122
// Optimized find word in block:

src/core/t-string.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ static REBCNT find_string(REBSER *series, REBCNT index, REBCNT end, REBVAL *targ
112112

113113
if (flags & (AM_FIND_REVERSE | AM_FIND_LAST)) {
114114
skip = -1;
115-
start = 0;
116-
if (flags & AM_FIND_LAST) index = end - len;
117-
else index--;
115+
if (flags & AM_FIND_LAST) {
116+
start = index;
117+
index = end - len;
118+
}
119+
else {
120+
start = 0;
121+
index--;
122+
}
118123
}
119124

120125
if (flags & AM_FIND_SAME) flags |= AM_FIND_CASE; // /SAME has same functionality as /CASE for any-string!

src/tests/units/series-test.r3

+29
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,35 @@ Rebol [
296296
--assert "AbcdAe" = find/same "aAbcdAe" "A"
297297
--assert "Ae" = find/same/last "aAbcdAe" "A"
298298

299+
300+
;@@ https://github.com/Oldes/Rebol-issues/issues/1802
301+
--test-- "FIND/LAST string! in string!"
302+
--assert "ab" = find/last "ab" "a"
303+
--assert "ab" = find/last "aab" "a"
304+
--assert none? find/last next "ab" "a"
305+
--assert "ab" = find/last next "aab" "a"
306+
--test-- "FIND/LAST char! in string!"
307+
--assert "ab" = find/last "ab" #"a"
308+
--assert "ab" = find/last "aab" #"a"
309+
--assert none? find/last next "ab" #"a"
310+
--assert "ab" = find/last next "aab" #"a"
311+
--test-- "FIND/LAST tag! in string!"
312+
--assert "<a>b" = find/last "<a>b" <a>
313+
--assert "<a>b" = find/last "<a><a>b" <a>
314+
--assert none? find/last next "<a>b" <a>
315+
--assert none? find/last find "<a>b" #"b" <a>
316+
--assert "<a>" = find/last find "<a>b<a>" #"b" <a>
317+
--test-- "FIND/LAST integer! in string!"
318+
--assert "1b" = find/last "1b" 1
319+
--assert "1b" = find/last "11b" 1
320+
--assert none? find/last next "1b" 1
321+
--assert "1b" = find/last next "11b" 1
322+
--test-- "FIND/LAST integer! in block!"
323+
--assert [1 b] = find/last [1 b] 1
324+
--assert [1 b] = find/last [1 1 b] 1
325+
--assert none? find/last next [1 b] 1
326+
--assert [1 b] = find/last next [1 1 b] 1
327+
299328
--test-- "FIND/LAST/CASE in string!"
300329
;@@ https://github.com/Oldes/Rebol-issues/issues/1495
301330
--assert none? find/case "Abc" "a"

0 commit comments

Comments
 (0)