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

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

[ZILF-198]
parent 26c0b4e85c46
......@@ -303,6 +303,20 @@ namespace Zilf.Interpreter
[Subr]
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)));
}
// https://vaporware.atlassian.net/browse/ZILF-198
// MEMBER should do substring searches:
// <MEMBER "PART" "SUM OF PARTS">$
// "PARTS"
[TestMethod]
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
TestHelpers.EvalAndAssert(ctx,
"<MEMBER #SUBR \"MEMBER\" \"I REMEMBER YOU\">",
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);
TestHelpers.EvalAndAssert(ctx,
"<MEMBER \"EM\" #FOO \"MEMQ\">",
ZilString.FromString("EMQ"));
}
[TestMethod]
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