Skip to content

Commit b53caf8

Browse files
committed
FEAT: find/same and select/same
implements: Oldes/Rebol-issues#1720
1 parent b05316a commit b53caf8

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

src/boot/actions.reb

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ find: action [
199199
length [number! series! pair!]
200200
/only {Treats a series value as only a single value}
201201
/case {Characters are case-sensitive}
202+
/same {Use "same?" as comparator}
202203
/any {Enables the * and ? wildcards}
203204
/with {Allows custom wildcards}
204205
wild [string!] "Specifies alternates for * and ?"
@@ -218,6 +219,7 @@ select: action [
218219
length [number! series! pair!]
219220
/only {Treats a series value as only a single value}
220221
/case {Characters are case-sensitive}
222+
/same {Use "same?" as comparator}
221223
/any {Enables the * and ? wildcards}
222224
/with {Allows custom wildcards}
223225
wild [string!] "Specifies alternates for * and ?"

src/core/f-series.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
case REB_OBJECT:
283283
case REB_MODULE:
284284
case REB_PORT:
285-
return VAL_OBJ_FRAME(s) - VAL_OBJ_FRAME(t);
285+
//return VAL_OBJ_FRAME(s) - VAL_OBJ_FRAME(t); // strict variant
286+
return !CT_Object(s, t, 1); // equality used
286287

287288
case REB_NATIVE:
288289
return &VAL_FUNC_CODE(s) - &VAL_FUNC_CODE(t);

src/core/t-block.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,18 @@ static void No_Nones_Or_Logic(REBVAL *arg) {
167167
}
168168
// All other cases:
169169
else {
170-
for (; index >= start && index < end; index += skip) {
171-
value = BLK_SKIP(series, index);
172-
if (0 == Cmp_Value(value, target, (REBOOL)(flags & AM_FIND_CASE))) return index;
173-
if (flags & AM_FIND_MATCH) break;
170+
if (flags & AM_FIND_SAME) {
171+
for (; index >= start && index < end; index += skip) {
172+
value = BLK_SKIP(series, index);
173+
if (Compare_Values(target, value, 3)) return index;
174+
if (flags & AM_FIND_MATCH) break;
175+
}
176+
} else {
177+
for (; index >= start && index < end; index += skip) {
178+
value = BLK_SKIP(series, index);
179+
if (0 == Cmp_Value(value, target, (REBOOL)(flags & AM_FIND_CASE))) return index;
180+
if (flags & AM_FIND_MATCH) break;
181+
}
174182
}
175183
return NOT_FOUND;
176184
}

src/core/t-string.c

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static REBCNT find_string(REBSER *series, REBCNT index, REBCNT end, REBVAL *targ
116116
else index--;
117117
}
118118

119+
if (flags & AM_FIND_SAME) flags |= AM_FIND_CASE; // /SAME has same functionality as /CASE for any-string!
120+
119121
if (ANY_BINSTR(target)) {
120122
// Do the optimal search or the general search?
121123
if (BYTE_SIZE(series) && VAL_BYTE_SIZE(target) && !(flags & ~(AM_FIND_CASE|AM_FIND_MATCH))) {

src/tests/units/series-test.r3

+19
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ Rebol [
6565
--assert "d" = find/any/reverse/tail tail "ažcdažcd" "?c"
6666
--assert "d" = find/any/reverse/tail tail "ažcdažcd" "žc"
6767

68+
--test-- "FIND/SAME in block!"
69+
a: "a"
70+
obj1: context [a: 1 b: 2]
71+
obj2: context [a: 1 b: 2]
72+
b: reduce ["a" obj1 a obj2]
73+
--assert 1 = index? find b :a
74+
--assert 3 = index? find/same b :a
75+
--assert 2 = index? find/same b :obj1
76+
--assert 4 = index? find/same b :obj2
77+
--assert 2 = index? find b :obj1
78+
--assert 2 = index? find b :obj2
79+
--assert 2 = index? find/case b :obj2 ;/case is not /same in this case
80+
81+
--test-- "FIND/SAME in string!"
82+
--assert "AbcdAe" = find/same "aAbcdAe" "A"
83+
--assert "Ae" = find/same/last "aAbcdAe" "A"
84+
6885
--test-- "FIND/PART"
6986
;@@ https://github.com/Oldes/Rebol-issues/issues/2329
7087
;@@ need to decide, which result is correct
@@ -129,6 +146,8 @@ Rebol [
129146
--assert #"2" = select/last "ab1ab2" "ab"
130147
--assert #"2" = select/last/any "ab1ab2" "?b"
131148
--assert #"2" = select/last/any "ab1ab2" "ab"
149+
--assert #"b" = select/same "aAbcdAe" "A"
150+
--assert #"e" = select/same/last "aAbcdAe" "A"
132151

133152
===end-group===
134153

0 commit comments

Comments
 (0)