Skip to content

Commit 3761b7d

Browse files
committed
FEAT: allow use of series index in parse's remove command
resolves: Oldes/Rebol-issues#2541
1 parent 1737424 commit 3761b7d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/core/u-parse.c

+18
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,23 @@ void Print_Parse_Index(REBCNT type, REBVAL *rules, REBSER *series, REBCNT index)
966966

967967
case SYM_REMOVE:
968968
SET_FLAG(flags, PF_REMOVE);
969+
if (IS_WORD(rules)) {
970+
// optionally using stored index as a counter
971+
// https://github.com/Oldes/Rebol-issues/issues/2541
972+
item = Get_Var(rules);
973+
if (VAL_SERIES(item) == parse->series) {
974+
rules++;
975+
if (VAL_INDEX(item) < index) {
976+
begin = VAL_INDEX(item);
977+
count = index - VAL_INDEX(item);
978+
}
979+
else {
980+
begin = index;
981+
count = VAL_INDEX(item) - index;
982+
}
983+
goto do_remove;
984+
}
985+
}
969986
continue;
970987

971988
case SYM_INSERT:
@@ -1407,6 +1424,7 @@ void Print_Parse_Index(REBCNT type, REBVAL *rules, REBSER *series, REBCNT index)
14071424
Throw_Return_Series(parse->type, ser);
14081425
}
14091426
if (GET_FLAG(flags, PF_REMOVE)) {
1427+
do_remove:
14101428
if (count) {
14111429
if (IS_PROTECT_SERIES(series)) Trap0(RE_PROTECTED);
14121430
Remove_Series(series, begin, count);

src/tests/units/parse-test.r3

+26
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,32 @@ Rebol [
551551
--assert parse v: [#L 1 "a" #L 2 "b"][some [string! | lr]]
552552
--assert v = ["a" "b"]
553553

554+
--test-- "remove using series' index"
555+
;@@ https://github.com/Oldes/Rebol-issues/issues/2541
556+
--assert all [
557+
parse s: "abcd" [skip p: 2 skip remove p skip]
558+
s == "ad"
559+
]
560+
--assert all [
561+
parse s: "abcd" [skip (p: tail s) remove p]
562+
s == "a"
563+
]
564+
--assert all [
565+
parse s: [a b c d] [skip p: 2 skip remove p skip]
566+
s == [a d]
567+
]
568+
--assert all [
569+
parse s: [a b c d] [skip (p: tail s) remove p]
570+
s == [a]
571+
]
572+
;; here the word `p` is used, but not as an index, but as a value
573+
--assert all [
574+
p: "bc" parse s: "abcd" [skip remove p skip]
575+
s == "ad"
576+
]
577+
p: "xx"
578+
--assert not parse s: "abcd" [skip remove p skip]
579+
554580
--test-- "while .. remove"
555581
remove-any-y: [while [remove #"y" | #"x"]]
556582
--assert parse v: "" remove-any-y

0 commit comments

Comments
 (0)