Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes findtextlast() and adds test #1451

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Content.Tests/DMProject/Tests/Text/findlasttext.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/proc/RunTest()
ASSERT(findlasttext("abcdefg", "f", 0, -1) == 0)
ASSERT(findlasttext("abcdefg", "f", 0, -2) == 6)
ASSERT(findlasttext("abcdefg", "f", 6) == 6)
ASSERT(findlasttext("abcdefg", "f", 5) == 0)
ASSERT(findlasttext("abcdefg", "bc", 2) == 2)
ASSERT(findlasttext("abcdefg", "ab", 10) == 1)
ASSERT(findlasttext("abcdefg", "f", 5) == 0)
ASSERT(findlasttext("Banana", "na", -3) == 3)
ASSERT(findlasttext("Banana", "na", -5) == 0)
ASSERT(findlasttext("Banana", "na", 0) == 5)
ASSERT(findlasttext("Banana", "na", 3) == 3)
ASSERT(findlasttext("Banana", "na", 2) == 0)
ASSERT(findlasttext("Banana", "na", 5) == 5)
ASSERT(findlasttext("Banana", "na", 50) == 5)
ASSERT(findlasttext("Banana", "na", -50) == 0)
ASSERT(findlasttext("abcdefg", "f", 6, -50) == 6)
ASSERT(findlasttext("abcdefg", "f", 6, 50) == 0)


66 changes: 40 additions & 26 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ public static DreamValue NativeProc_findtextEx(NativeProc.Bundle bundle, DreamOb
[DreamProc("findlasttext")]
[DreamProcParameter("Haystack", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Needle", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
public static DreamValue NativeProc_findlasttext(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
// TODO This is for handling nulls, check if it works right for other bad types
int failCount = 0;
Expand All @@ -700,26 +700,33 @@ public static DreamValue NativeProc_findlasttext(NativeProc.Bundle bundle, Dream
return new DreamValue(failCount == 2 ? 1 : 0);
}

int start = bundle.GetArgument(2, "Start").MustGetValueAsInteger(); //1-indexed
int end = bundle.GetArgument(3, "End").MustGetValueAsInteger(); //1-indexed
int start = bundle.GetArgument(2, "Start").MustGetValueAsInteger(); //chars from the end
int end = bundle.GetArgument(3, "End").MustGetValueAsInteger(); //1-indexed from the beginning
int actualstart;
int actualcount;

if (end == 0) {
end = text.Length + 1;
}
if(start > 0)
actualstart = start-1;
else
actualstart = (text.Length-1) + start;
actualstart += needle.Length-1;
actualstart = Math.Max(Math.Min(text.Length, actualstart),0);

int needleIndex = text.LastIndexOf(needle, end - 1, end - start, StringComparison.OrdinalIgnoreCase);
if (needleIndex != -1) {
return new DreamValue(needleIndex + 1); //1-indexed
} else {
return new DreamValue(0);
}
if(end > 0)
actualcount = actualstart - (end-1);
else
actualcount = actualstart - ((text.Length-1) + (end));
actualcount += needle.Length-1;
actualcount = Math.Max(Math.Min(actualstart+1, actualcount),0);
int needleIndex = text.LastIndexOf(needle, actualstart, actualcount, StringComparison.OrdinalIgnoreCase);
return new DreamValue(needleIndex + 1); //1-indexed, or 0 if not found (LastIndexOf returns -1 if not found)
}

[DreamProc("findlasttextEx")]
[DreamProcParameter("Haystack", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Needle", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
public static DreamValue NativeProc_findlasttextEx(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
// TODO This is for handling nulls, check if it works right for other bad types
int failCount = 0;
Expand All @@ -735,17 +742,24 @@ public static DreamValue NativeProc_findlasttextEx(NativeProc.Bundle bundle, Dre

int start = bundle.GetArgument(2, "Start").GetValueAsInteger(); //1-indexed
int end = bundle.GetArgument(3, "End").GetValueAsInteger(); //1-indexed

if (end == 0) {
end = text.Length + 1;
}

int needleIndex = text.LastIndexOf(needle, end - 1, end - start, StringComparison.InvariantCulture);
if (needleIndex != -1) {
return new DreamValue(needleIndex + 1); //1-indexed
} else {
return new DreamValue(0);
}
int actualstart;
int actualcount;

if(start > 0)
actualstart = start-1;
else
actualstart = (text.Length-1) + start;
actualstart += needle.Length-1;
actualstart = Math.Max(Math.Min(text.Length, actualstart),0);

if(end > 0)
actualcount = actualstart - (end-1);
else
actualcount = actualstart - ((text.Length-1) + (end));
actualcount += needle.Length-1;
actualcount = Math.Max(Math.Min(actualstart+1, actualcount),0);
int needleIndex = text.LastIndexOf(needle, actualstart, actualcount, StringComparison.InvariantCulture);
return new DreamValue(needleIndex + 1); //1-indexed, or 0 if not found (LastIndexOf returns -1 if not found)
}

[DreamProc("flick")]
Expand Down