Read about our upcoming Code of Conduct on this issue

Commit e2709285 authored by Jesse McGrew's avatar Jesse McGrew
Browse files

Implemented substring search functionality for MEMBER. (Thanks to Roman Bartke.)

parent 26c0b4e85c46
......@@ -303,6 +303,20 @@ namespace Zilf.Interpreter
public static ZilObject MEMBER(Context ctx, [NotNull] ZilObject needle, [NotNull] IStructure haystack)
if (needle.PrimType == PrimType.STRING && ((ZilObject)haystack).PrimType == PrimType.STRING)
string n = ((ZilString)needle.GetPrimitive(ctx)).Text;
if (n != "")
string h = ((ZilString)((ZilObject)haystack).GetPrimitive(ctx)).Text;
int pos = h.IndexOf(n, StringComparison.Ordinal);
if (pos >= 0)
return (ZilObject)haystack.GetRest(pos) ?? ctx.FALSE;
return PerformMember(ctx, needle, haystack, (a, b) => a.StructurallyEquals(b));
......@@ -69,6 +69,33 @@ namespace Zilf.Tests.Interpreter
new ZilVector(new ZilList(new ZilObject[] { new ZilFix(5) }), new ZilFix(6), new ZilFix(7)));
// MEMBER should do substring searches:
// "PARTS"
public void TestMEMBER_Substring()
var ctx = new Context();
TestHelpers.EvalAndAssert(ctx, "<MEMBER \"PART\" \"SUM OF PARTS\">", ZilString.FromString("PARTS"));
TestHelpers.EvalAndAssert(ctx, "<MEMBER \"Ham\" \"I am Hamster\">", ZilString.FromString("Hamster"));
TestHelpers.EvalAndAssert(ctx, "<MEMBER \"\" \"Hamster\">", ctx.FALSE);
TestHelpers.EvalAndAssert(ctx, "<SET X \"a|bc\"> <==? <MEMBER \"|\" .X> <REST .X>>", ctx.TRUE);
// should work for newtypes with primtype STRING also
ZilString.FromString("MEMBER YOU"));
// we can't pass a string as the second argument...
// TODO: sort out the inconsistency with [F]SUBR being primtype STRING but non-structured
ctx.RegisterType(ZilAtom.Parse("FOO", ctx), PrimType.STRING);
"<MEMBER \"EM\" #FOO \"MEMQ\">",
public void TestILIST()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment