Skip to content

Commit

Permalink
Fixes findtextlast() and adds test (#1451)
Browse files Browse the repository at this point in the history
* jesus christ

* remove debug statements from test

* fix defaults for findlasttextex

---------

Co-authored-by: amy <amylizzle@users.noreply.github.com>
  • Loading branch information
amylizzle and amylizzle authored Sep 17, 2023
1 parent 5143df2 commit 0fb43f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
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 @@ -691,8 +691,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 @@ -706,26 +706,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 @@ -741,17 +748,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

0 comments on commit 0fb43f6

Please sign in to comment.